-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathgtp.cpp
More file actions
406 lines (344 loc) · 11.8 KB
/
Copy pathgtp.cpp
File metadata and controls
406 lines (344 loc) · 11.8 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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include "core/global.h"
#include "core/config_parser.h"
#include "core/timer.h"
#include "search/asyncbot.h"
#include "program/setup.h"
#include "main.h"
using namespace std;
#define TCLAP_NAMESTARTSTRING "-" //Use single dashes for all flags
#include <tclap/CmdLine.h>
static bool tryParsePlayer(const string& s, Player& pla) {
string str = Global::toLower(s);
if(str == "black" || str == "b") {
pla = P_BLACK;
return true;
}
else if(str == "white" || str == "w") {
pla = P_WHITE;
return true;
}
return false;
}
static bool tryParseLoc(const string& s, const Board& b, Loc& loc) {
return Location::tryOfString(s,b,loc);
}
int MainCmds::gtp(int argc, const char* const* argv) {
Board::initHash();
Rand seedRand;
string configFile;
string nnModelFile;
try {
TCLAP::CmdLine cmd("Run GTP engine", ' ', "1.0",true);
TCLAP::ValueArg<string> configFileArg("","config-file","Config file to use (see configs/gtp_example.cfg)",true,string(),"FILE");
TCLAP::ValueArg<string> nnModelFileArg("","nn-model-file","Neural net model .pb graph file to use",true,string(),"FILE");
cmd.add(configFileArg);
cmd.add(nnModelFileArg);
cmd.parse(argc,argv);
configFile = configFileArg.getValue();
nnModelFile = nnModelFileArg.getValue();
}
catch (TCLAP::ArgException &e) {
cerr << "Error: " << e.error() << " for argument " << e.argId() << endl;
return 1;
}
ConfigParser cfg(configFile);
Logger logger;
logger.addFile(cfg.getString("logFile"));
bool logAllGTPCommunication = cfg.getBool("logAllGTPCommunication");
bool logSearchInfo = cfg.getBool("logSearchInfo");
logger.write("GTP Engine starting...");
NNEvaluator* nnEval;
{
Setup::initializeSession(cfg);
vector<NNEvaluator*> nnEvals = Setup::initializeNNEvaluators({nnModelFile},cfg,logger,seedRand);
assert(nnEvals.size() == 1);
nnEval = nnEvals[0];
}
logger.write("Loaded neural net");
Rules initialRules;
{
string koRule = cfg.getString("koRule", Rules::koRuleStrings());
string scoringRule = cfg.getString("scoringRule", Rules::scoringRuleStrings());
bool multiStoneSuicideLegal = cfg.getBool("multiStoneSuicideLegal");
float komi = 7.5f; //Default komi, gtp will generally override this
initialRules.koRule = Rules::parseKoRule(koRule);
initialRules.scoringRule = Rules::parseScoringRule(scoringRule);
initialRules.multiStoneSuicideLegal = multiStoneSuicideLegal;
initialRules.komi = komi;
}
SearchParams params;
{
vector<SearchParams> paramss = Setup::loadParams(cfg);
if(paramss.size() != 1)
throw StringError("Can only specify examply one search bot in gtp mode");
params = paramss[0];
}
string searchRandSeed;
if(cfg.contains("searchRandSeed"))
searchRandSeed = cfg.getString("searchRandSeed");
else
searchRandSeed = Global::uint64ToString(seedRand.nextUInt64());
bool ponderingEnabled = cfg.getBool("ponderingEnabled");
AsyncBot* bot = new AsyncBot(params, nnEval, &logger, searchRandSeed);
{
Board board(19,19);
Player pla = P_BLACK;
BoardHistory hist(board,pla,initialRules);
bot->setPosition(pla,board,hist);
}
{
vector<string> unusedKeys = cfg.unusedKeys();
for(size_t i = 0; i<unusedKeys.size(); i++) {
string msg = "WARNING: Unused key '" + unusedKeys[i] + "' in " + configFile;
logger.write(msg);
cerr << msg << endl;
}
}
vector<string> knownCommands = {
"protocol_version",
"name",
"version",
"known_command",
"list_commands",
"quit",
"boardsize",
"clear_board",
"komi",
"play",
"genmove",
};
logger.write("Beginning main protocol loop");
string line;
while(cin) {
getline(cin,line);
//Filter down to only "normal" ascii characters. Also excludes carrage returns.
//Newlines are already handled by getline
size_t newLen = 0;
for(size_t i = 0; i < line.length(); i++)
if(((int)line[i] >= 32 && (int)line[i] <= 126) || line[i] == '\t')
line[newLen++] = line[i];
line.erase(line.begin()+newLen, line.end());
//Remove comments
size_t commentPos = line.find("#");
if(commentPos != string::npos)
line = line.substr(0, commentPos);
//Convert tabs to spaces
for(size_t i = 0; i < line.length(); i++)
if(line[i] == '\t')
line[i] = ' ';
line = Global::trim(line);
if(line.length() == 0)
continue;
assert(line.length() > 0);
string strippedLine = line;
if(logAllGTPCommunication)
logger.write("Controller: " + strippedLine);
//Parse id number of command, if present
bool hasId = false;
int id = 0;
{
size_t digitPrefixLen = 0;
while(digitPrefixLen < line.length() && Global::isDigit(line[digitPrefixLen]))
digitPrefixLen++;
if(digitPrefixLen > 0) {
hasId = true;
try {
id = Global::parseDigits(line,0,digitPrefixLen);
}
catch(const IOError& e) {
cout << "? GTP id '" << id << "' could not be parsed: " << e.what() << endl;
continue;
}
line = line.substr(digitPrefixLen);
}
}
line = Global::trim(line);
if(line.length() <= 0) {
cout << "? empty command" << endl;
continue;
}
vector<string> pieces = Global::split(line,' ');
for(size_t i = 0; i<pieces.size(); i++)
pieces[i] = Global::trim(pieces[i]);
assert(pieces.size() > 0);
string command = pieces[0];
pieces.erase(pieces.begin());
bool responseIsError = false;
bool shouldQuitAfterResponse = false;
bool maybeStartPondering = false;
string response;
if(command == "protocol_version") {
response = "2";
}
else if(command == "name") {
response = "lightvector/GoNN GTP Bot";
}
else if(command == "version") {
response = nnModelFile;
}
else if(command == "known_command") {
if(pieces.size() != 1) {
responseIsError = true;
response = "Expected single argument for known_command but got '" + Global::concat(pieces," ") + "'";
}
else {
if(std::find(knownCommands.begin(), knownCommands.end(), pieces[0]) != knownCommands.end())
response = "true";
else
response = "false";
}
}
else if(command == "list_commands") {
for(size_t i = 0; i<knownCommands.size(); i++)
response += knownCommands[i] + "\n";
}
else if(command == "quit") {
shouldQuitAfterResponse = true;
logger.write("Quit requested by controller");
}
else if(command == "boardsize") {
int newBSize = 0;
if(pieces.size() != 1 || !Global::tryStringToInt(pieces[0],newBSize)) {
responseIsError = true;
response = "Expected single int argument for boardsize but got '" + Global::concat(pieces," ") + "'";
}
else if(newBSize < 9 || newBSize > 19) {
responseIsError = true;
response = "unacceptable size";
}
else {
Board board(newBSize,newBSize);
Player pla = P_BLACK;
BoardHistory hist(board,pla,bot->getRootHist().rules);
bot->setPosition(pla,board,hist);
}
}
else if(command == "clear_board") {
assert(bot->getRootBoard().x_size == bot->getRootBoard().y_size);
int newBSize = bot->getRootBoard().x_size;
Board board(newBSize,newBSize);
Player pla = P_BLACK;
BoardHistory hist(board,pla,bot->getRootHist().rules);
bot->setPosition(pla,board,hist);
}
else if(command == "komi") {
float newKomi = 0;
if(pieces.size() != 1 || !Global::tryStringToFloat(pieces[0],newKomi)) {
responseIsError = true;
response = "Expected single float argument for komi but got '" + Global::concat(pieces," ") + "'";
}
//GTP spec says that we should accept any komi, but we're going to ignore that.
else if(isnan(newKomi) || newKomi < -100.0 || newKomi > 100.0) {
responseIsError = true;
response = "unacceptable komi";
}
else if(newKomi * 2 != (int)(newKomi * 2)) {
responseIsError = true;
response = "komi must be an integer or half-integer";
}
else {
bot->setKomi(newKomi);
//In case the controller tells us komi every move, restart pondering afterward.
maybeStartPondering = bot->getRootHist().moveHistory.size() > 0;
}
}
else if(command == "play") {
Player pla;
Loc loc;
if(pieces.size() != 2) {
responseIsError = true;
response = "Expected two arguments for play but got '" + Global::concat(pieces," ") + "'";
}
else if(!tryParsePlayer(pieces[0],pla)) {
responseIsError = true;
response = "Could not parse color: '" + pieces[0] + "'";
}
else if(!tryParseLoc(pieces[1],bot->getRootBoard(),loc)) {
responseIsError = true;
response = "Could not parse vertex: '" + pieces[1] + "'";
}
else {
bool suc = bot->makeMove(loc,pla);
if(!suc) {
responseIsError = true;
response = "illegal move";
}
maybeStartPondering = true;
}
}
else if(command == "genmove") {
Player pla;
if(pieces.size() != 1) {
responseIsError = true;
response = "Expected one argument for genmove but got '" + Global::concat(pieces," ") + "'";
}
else if(!tryParsePlayer(pieces[0],pla)) {
responseIsError = true;
response = "Could not parse color: '" + pieces[0] + "'";
}
else {
ClockTimer timer;
nnEval->clearStats();
Loc loc = bot->genMoveSynchronous(pla);
bool isLegal = bot->isLegal(loc,pla);
if(loc == Board::NULL_LOC || !isLegal) {
responseIsError = true;
response = "genmove returned null location or illegal move";
ostringstream sout;
sout << "genmove null location or illegal move!?!" << "\n";
sout << bot->getRootBoard() << "\n";
sout << "Pla: " << playerToString(pla) << "\n";
sout << "Loc: " << Location::toString(loc,bot->getRootBoard()) << "\n";
logger.write(sout.str());
}
response = Location::toString(loc,bot->getRootBoard());
if(logSearchInfo) {
Search* search = bot->getSearch();
ostringstream sout;
Board::printBoard(sout, bot->getRootBoard(), loc, &(bot->getRootHist().moveHistory));
sout << "\n";
sout << "Time taken: " << timer.getSeconds() << "\n";
sout << "Root visits: " << search->numRootVisits() << "\n";
sout << "NN rows: " << nnEval->numRowsProcessed() << endl;
sout << "NN batches: " << nnEval->numBatchesProcessed() << endl;
sout << "NN avg batch size: " << nnEval->averageProcessedBatchSize() << endl;
sout << "PV: ";
search->printPV(sout, search->rootNode, 25);
sout << "\n";
sout << "Tree:\n";
search->printTree(sout, search->rootNode, PrintTreeOptions().maxDepth(1).maxChildrenToShow(10));
logger.write(sout.str());
}
bool suc = bot->makeMove(loc,pla);
assert(suc);
maybeStartPondering = true;
}
}
else {
responseIsError = true;
response = "unknown command";
}
//Postprocessing of response
if(hasId)
response = Global::intToString(id) + " " + response;
else
response = " " + response;
if(responseIsError)
response = "?" + response;
else
response = "=" + response;
cout << response << endl;
cout << endl; //GTP needs extra newline
if(logAllGTPCommunication)
logger.write(response);
if(shouldQuitAfterResponse)
break;
if(maybeStartPondering && ponderingEnabled)
bot->ponder();
} //Close read loop
delete bot;
delete nnEval;
NeuralNet::globalCleanup();
logger.write("All cleaned up, quitting");
return 0;
}