Repository navigation
Expand file tree
/
Copy pathLeela.cpp
More file actions
320 lines (288 loc) · 9.52 KB
/
Copy pathLeela.cpp
File metadata and controls
320 lines (288 loc) · 9.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include "config.h"
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <memory>
#ifdef USE_OPTIONS
#include <boost/program_options.hpp>
#endif
#include <boost/format.hpp>
#ifdef USE_CAFFE
#include <glog/logging.h>
#endif
#include "Network.h"
#include "Zobrist.h"
#include "GTP.h"
#include "SMP.h"
#include "Random.h"
#include "Utils.h"
#include "Matcher.h"
#include "AttribScores.h"
#include "ThreadPool.h"
#include "MCPolicy.h"
using namespace Utils;
#ifdef USE_OPTIONS
void parse_commandline(int argc, char *argv[], bool & gtp_mode) {
namespace po = boost::program_options;
// Declare the supported options.
po::options_description v_desc("Allowed options");
v_desc.add_options()
("help,h", "Show commandline options.")
("gtp,g", "Enable GTP mode.")
("threads,t", po::value<int>()->default_value(cfg_num_threads),
"Number of threads to use.")
("playouts,p", po::value<int>(),
"Weaken engine by limiting the number of playouts. "
"Requires --noponder.")
("lagbuffer,b", po::value<int>()->default_value(cfg_lagbuffer_cs),
"Safety margin for time usage in centiseconds.")
("logfile,l", po::value<std::string>(), "File to log input/output to.")
("quiet,q", "Disable all diagnostic output.")
("komiadjust,k", "Adjust komi one point in my disadvantage "
"(for territory scoring).")
("noponder", "Disable thinking on opponent's time.")
("nonets", "Disable use of neural networks.")
("nobook", "Disable use of the fuseki library.")
#ifdef USE_OPENCL
("gpu", po::value<std::vector<int> >(),
"ID of the OpenCL device(s) to use (disables autodetection).")
("rowtiles", po::value<int>()->default_value(cfg_rowtiles),
"Split up the board in # tiles.")
#endif
#ifdef USE_TUNER
("mature_threshold", po::value<int>())
("expand_threshold", po::value<int>())
("beta", po::value<float>())
("patternbonus", po::value<float>())
("bound", po::value<float>())
("fpu", po::value<float>())
("puct", po::value<float>())
("uct", po::value<float>())
("psa", po::value<float>())
("cutoff_offset", po::value<float>())
("cutoff_ratio", po::value<float>())
("mix_opening", po::value<float>())
("mix_ending", po::value<float>())
("softmax_temp", po::value<float>())
("mc_softmax", po::value<float>())
("eval_thresh", po::value<int>())
("rave_moves", po::value<int>())
("extra_symmetry", po::value<int>())
("random_loops", po::value<int>())
#endif
;
// These won't be shown, we use them to catch incorrect usage of the
// command line.
po::options_description h_desc("Hidden options");
h_desc.add_options()
("arguments", po::value<std::vector<std::string>>());
// Parse both the above, we will check if any of the latter are present.
po::options_description all("All options");
all.add(v_desc).add(h_desc);
po::positional_options_description p_desc;
p_desc.add("arguments", -1);
po::variables_map vm;
try {
po::store(po::command_line_parser(argc, argv)
.options(all).positional(p_desc).run(), vm);
po::notify(vm);
} catch(const boost::program_options::error& e) {
myprintf("ERROR: %s\n", e.what());
std::cout << v_desc << std::endl;
exit(EXIT_FAILURE);
}
// Handle commandline options
if (vm.count("help") || vm.count("arguments")) {
auto ev = EXIT_SUCCESS;
// The user specified an argument. We don't accept any, so explain
// our usage.
if (vm.count("arguments")) {
for (auto& arg : vm["arguments"].as<std::vector<std::string>>()) {
std::cout << "Unrecognized argument: " << arg << std::endl;
}
ev = EXIT_FAILURE;
}
std::cout << v_desc << std::endl;
exit(ev);
}
if (vm.count("quiet")) {
cfg_quiet = true;
}
#ifdef USE_TUNER
if (vm.count("mature_threshold")) {
cfg_mature_threshold = vm["mature_threshold"].as<int>();
}
if (vm.count("expand_threshold")) {
cfg_expand_threshold = vm["expand_threshold"].as<int>();
}
if (vm.count("bound")) {
cfg_bound = vm["bound"].as<float>();
}
if (vm.count("fpu")) {
cfg_fpu = vm["fpu"].as<float>();
}
if (vm.count("puct")) {
cfg_puct = vm["puct"].as<float>();
}
if (vm.count("uct")) {
cfg_uct = vm["uct"].as<float>();
}
if (vm.count("psa")) {
cfg_psa = vm["psa"].as<float>();
}
if (vm.count("softmax_temp")) {
cfg_softmax_temp = vm["softmax_temp"].as<float>();
}
if (vm.count("mc_softmax")) {
cfg_mc_softmax = vm["mc_softmax"].as<float>();
}
if (vm.count("beta")) {
cfg_beta = vm["beta"].as<float>();
}
if (vm.count("patternbonus")) {
cfg_patternbonus = vm["patternbonus"].as<float>();
}
if (vm.count("cutoff_offset")) {
cfg_cutoff_offset = vm["cutoff_offset"].as<float>();
}
if (vm.count("cutoff_ratio")) {
cfg_cutoff_ratio = vm["cutoff_ratio"].as<float>();
}
if (vm.count("mix_opening")) {
cfg_mix_opening = vm["mix_opening"].as<float>();
}
if (vm.count("mix_ending")) {
cfg_mix_ending = vm["mix_ending"].as<float>();
}
if (vm.count("eval_thresh")) {
cfg_eval_thresh = vm["eval_thresh"].as<int>();
}
if (vm.count("rave_moves")) {
cfg_rave_moves = vm["rave_moves"].as<int>();
}
if (vm.count("extra_symmetry")) {
cfg_extra_symmetry = vm["extra_symmetry"].as<int>();
}
if (vm.count("random_loops")) {
cfg_random_loops = vm["random_loops"].as<int>();
}
#endif
if (vm.count("logfile")) {
cfg_logfile = vm["logfile"].as<std::string>();
myprintf("Logging to %s.\n", cfg_logfile.c_str());
cfg_logfile_handle = fopen(cfg_logfile.c_str(), "a");
}
if (vm.count("gtp")) {
gtp_mode = true;
}
if (vm.count("threads")) {
int num_threads = vm["threads"].as<int>();
if (num_threads > cfg_num_threads) {
myprintf("Clamping threads to maximum = %d\n", cfg_num_threads);
} else if (num_threads != cfg_num_threads) {
myprintf("Using %d thread(s).\n", num_threads);
cfg_num_threads = num_threads;
}
}
if (vm.count("noponder")) {
cfg_allow_pondering = false;
}
if (vm.count("playouts")) {
cfg_max_playouts = vm["playouts"].as<int>();
if (!vm.count("noponder")) {
myprintf("Nonsensical options: Playouts are restricted but "
"thinking on the opponent's time is still allowed. "
"Add --noponder if you want a weakened engine.\n");
exit(EXIT_FAILURE);
}
}
if (vm.count("nonets")) {
cfg_enable_nets = false;
}
if (vm.count("nobook")) {
cfg_allow_book = false;
}
if (vm.count("komiadjust")) {
myprintf("Adjusting komi for territory scoring rules.\n");
cfg_komi_adjust = true;
}
if (vm.count("lagbuffer")) {
int lagbuffer = vm["lagbuffer"].as<int>();
if (lagbuffer != cfg_lagbuffer_cs) {
myprintf("Using per-move time margin of %.2fs.\n", lagbuffer/100.0f);
cfg_lagbuffer_cs = lagbuffer;
}
}
#ifdef USE_OPENCL
if (vm.count("gpu")) {
cfg_gpus = vm["gpu"].as<std::vector<int> >();
}
if (vm.count("rowtiles")) {
int rowtiles = vm["rowtiles"].as<int>();
rowtiles = std::min(19, rowtiles);
rowtiles = std::max(1, rowtiles);
if (rowtiles != cfg_rowtiles) {
myprintf("Splitting the board in %d tiles.\n", rowtiles);
cfg_rowtiles = rowtiles;
}
}
#endif
}
#endif
#ifdef _CONSOLE
int main (int argc, char *argv[]) {
bool gtp_mode = false;
std::string input;
#ifdef USE_CAFFE
::google::InitGoogleLogging(argv[0]);
#endif
// Set up engine parameters
GTP::setup_default_parameters();
#ifdef USE_OPTIONS
parse_commandline(argc, argv, gtp_mode);
#endif
// Disable IO buffering as much as possible
std::cout.setf(std::ios::unitbuf);
std::cerr.setf(std::ios::unitbuf);
std::cin.setf(std::ios::unitbuf);
setbuf(stdout, NULL);
setbuf(stderr, NULL);
#ifndef WIN32
setbuf(stdin, NULL);
#endif
thread_pool.initialize(cfg_num_threads);
// Use deterministic random numbers for hashing
std::unique_ptr<Random> rng(new Random(5489));
Zobrist::init_zobrist(*rng);
// Initialize things
AttribScores::get_attribscores();
Matcher::get_Matcher();
Network::get_Network();
std::unique_ptr<GameState> maingame(new GameState);
/* set board limits */
float komi = 7.5;
maingame->init_game(19, komi);
if (!GTP::perform_self_test(*maingame)) {
exit(EXIT_FAILURE);
}
for(;;) {
if (!gtp_mode) {
maingame->display_state();
std::cout << "Leela: ";
}
if (std::getline(std::cin, input)) {
Utils::log_input(input);
GTP::execute(*maingame, input);
} else {
// eof or other error
break;
}
// Force a flush of the logfile
if (cfg_logfile_handle) {
fclose(cfg_logfile_handle);
cfg_logfile_handle = fopen(cfg_logfile.c_str(), "a");
}
}
return 0;
}
#endif