Repository navigation
Expand file tree
/
Copy pathgtp_player.cc
More file actions
Latest commit
549 lines (468 loc) · 15.5 KB
/
Copy pathgtp_player.cc
File metadata and controls
549 lines (468 loc) · 15.5 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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
// Copyright 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "cc/gtp_player.h"
#include <algorithm>
#include <cctype>
#include <cmath>
#include <fstream>
#include <iostream>
#include <sstream>
#include <utility>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_split.h"
#include "absl/time/clock.h"
#include "cc/constants.h"
#include "cc/sgf.h"
#include "nlohmann/json.hpp"
namespace minigo {
GtpPlayer::GtpPlayer(std::unique_ptr<DualNet> network, const Options& options)
: MctsPlayer(std::move(network), options),
ponder_limit_(options.ponder_limit),
courtesy_pass_(options.courtesy_pass) {
RegisterCmd("benchmark", &GtpPlayer::HandleBenchmark);
RegisterCmd("boardsize", &GtpPlayer::HandleBoardsize);
RegisterCmd("clear_board", &GtpPlayer::HandleClearBoard);
RegisterCmd("echo", &GtpPlayer::HandleEcho);
RegisterCmd("final_score", &GtpPlayer::HandleFinalScore);
RegisterCmd("gamestate", &GtpPlayer::HandleGamestate);
RegisterCmd("genmove", &GtpPlayer::HandleGenmove);
RegisterCmd("info", &GtpPlayer::HandleInfo);
RegisterCmd("known_command", &GtpPlayer::HandleKnownCommand);
RegisterCmd("komi", &GtpPlayer::HandleKomi);
RegisterCmd("list_commands", &GtpPlayer::HandleListCommands);
RegisterCmd("loadsgf", &GtpPlayer::HandleLoadsgf);
RegisterCmd("name", &GtpPlayer::HandleName);
RegisterCmd("play", &GtpPlayer::HandlePlay);
RegisterCmd("ponder_limit", &GtpPlayer::HandlePonderLimit);
RegisterCmd("readouts", &GtpPlayer::HandleReadouts);
RegisterCmd("report_search_interval", &GtpPlayer::HandleReportSearchInterval);
}
void GtpPlayer::Run() {
std::cerr << "GTP engine ready" << std::endl;
std::string line;
std::ios::sync_with_stdio(false);
while (!std::cin.eof()) {
if (MaybePonder()) {
continue;
}
std::getline(std::cin, line);
if (!HandleCmd(line)) {
break;
}
ponder_count_ = 0;
}
}
Coord GtpPlayer::SuggestMove() {
if (courtesy_pass_ && root()->move == Coord::kPass) {
return Coord::kPass;
}
return MctsPlayer::SuggestMove();
}
void GtpPlayer::RegisterCmd(const std::string& cmd, CmdHandler handler) {
cmd_handlers_[cmd] = handler;
}
bool GtpPlayer::MaybePonder() {
// We ponder if all the following conditions are true:
// 1) Pondering is enabled.
// 2) We haven't pondered too much.
// 3) There's no GTP command pending on stdin.
// 4) It's the opponent's turn.
bool should_ponder =
(ponder_limit_ > 0 && ponder_count_ < ponder_limit_ &&
std::cin.rdbuf()->in_avail() == 0 && last_genmove_ != Color::kEmpty &&
last_genmove_ != root()->position.to_play());
if (!should_ponder) {
ponder_count_ = 0;
return false;
}
if (ponder_count_ == 0) {
std::cerr << "pondering..." << std::endl;
}
TreeSearch();
ponder_count_ += options().batch_size;
if (ponder_count_ >= ponder_limit_) {
std::cerr << root()->Describe() << "\n";
std::cerr << "finished pondering" << std::endl;
}
return true;
}
bool GtpPlayer::HandleCmd(const std::string& line) {
std::vector<absl::string_view> args =
absl::StrSplit(line, absl::ByAnyChar(" \t\r\n"), absl::SkipWhitespace());
if (args.empty()) {
std::cout << "=" << std::endl;
return true;
}
// Split the GTP command and its arguments.
auto cmd = std::string(args[0]);
args.erase(args.begin());
if (cmd == "quit") {
std::cout << "=\n\n" << std::flush;
return false;
}
auto response = DispatchCmd(cmd, args);
std::cout << (response.ok ? "=" : "?");
if (!response.str.empty()) {
std::cout << " " << response.str;
}
std::cout << "\n\n" << std::flush;
return true;
}
absl::Span<MctsNode* const> GtpPlayer::TreeSearch() {
auto leaves = MctsPlayer::TreeSearch();
if (!leaves.empty() && report_search_interval_ != absl::ZeroDuration()) {
auto now = absl::Now();
if (now - last_report_time_ > report_search_interval_) {
last_report_time_ = now;
ReportSearchStatus(leaves.back());
}
}
return leaves;
}
GtpPlayer::Response GtpPlayer::CheckArgsExact(absl::string_view cmd,
size_t expected_num_args,
CmdArgs args) {
if (args.size() != expected_num_args) {
return Response::Error("expected ", expected_num_args,
" args for GTP command ", cmd, ", got ", args.size(),
" args: ", absl::StrJoin(args, " "));
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::CheckArgsRange(absl::string_view cmd,
size_t expected_min_args,
size_t expected_max_args,
CmdArgs args) {
if (args.size() < expected_min_args || args.size() > expected_max_args) {
return Response::Error("expected between ", expected_min_args, " and ",
expected_max_args, " args for GTP command ", cmd,
", got ", args.size(),
" args: ", absl::StrJoin(args, " "));
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::DispatchCmd(const std::string& cmd,
CmdArgs args) {
auto it = cmd_handlers_.find(cmd);
if (it == cmd_handlers_.end()) {
return Response::Error("unknown command");
}
auto handler = it->second;
return (this->*handler)(cmd, args);
}
GtpPlayer::Response GtpPlayer::HandleBenchmark(absl::string_view cmd,
CmdArgs args) {
// benchmark [readouts] [batch_size]
// Note: By default use current time_control (readouts or time).
auto response = CheckArgsRange(cmd, 0, 2, args);
if (!response.ok) {
return response;
}
auto saved_options = options();
MctsPlayer::Options temp_options = options();
if (args.size() > 0) {
temp_options.seconds_per_move = 0;
if (!absl::SimpleAtoi(args[0], &temp_options.num_readouts)) {
return Response::Error("bad num_readouts");
}
}
if (args.size() == 2) {
if (!absl::SimpleAtoi(args[1], &temp_options.batch_size)) {
return Response::Error("bad batch_size");
}
}
// Set options.
*mutable_options() = temp_options;
// Run benchmark.
MctsPlayer::SuggestMove();
// Reset options.
*mutable_options() = saved_options;
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleBoardsize(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 1, args);
if (!response.ok) {
return response;
}
int x;
if (!absl::SimpleAtoi(args[0], &x) || x != kN) {
return Response::Error("unacceptable size");
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleClearBoard(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 0, args);
if (!response.ok) {
return response;
}
last_genmove_ = Color::kEmpty;
NewGame();
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleEcho(absl::string_view cmd, CmdArgs args) {
return Response::Ok(absl::StrJoin(args, " "));
}
GtpPlayer::Response GtpPlayer::HandleFinalScore(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 0, args);
if (!response.ok) {
return response;
}
if (!game_over()) {
// Game isn't over yet, calculate the current score using Tromp-Taylor
// scoring.
return Response::Ok(
FormatScore(root()->position.CalculateScore(options().komi)));
} else {
// Game is over, we have the result available.
return Response::Ok(result_string());
}
}
GtpPlayer::Response GtpPlayer::HandleGamestate(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 0, args);
if (!response.ok) {
return response;
}
const auto& position = root()->position;
std::ostringstream oss;
for (const auto& stone : position.stones()) {
char ch;
if (stone.color() == Color::kBlack) {
ch = 'X';
} else if (stone.color() == Color::kWhite) {
ch = 'O';
} else {
ch = '.';
}
oss << ch;
}
nlohmann::json j = {
{"toPlay", position.to_play() == Color::kBlack ? "B" : "W"},
{"moveNum", position.n()},
{"board", oss.str()},
{"q", root()->parent != nullptr ? root()->parent->Q() : 0},
{"gameOver", game_over()},
};
if (!history().empty()) {
j["lastMove"] = history().back().c.ToKgs();
}
std::cerr << "mg-gamestate: " << j.dump() << std::endl;
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleGenmove(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsRange(cmd, 0, 1, args);
if (!response.ok) {
return response;
}
auto c = SuggestMove();
std::cerr << root()->Describe() << std::endl;
last_genmove_ = root()->position.to_play();
PlayMove(c);
return Response::Ok(c.ToKgs());
}
GtpPlayer::Response GtpPlayer::HandleInfo(absl::string_view cmd, CmdArgs args) {
auto response = CheckArgsExact(cmd, 0, args);
if (!response.ok) {
return response;
}
std::ostringstream oss;
oss << options();
oss << " report_search_interval:" << report_search_interval_;
return Response::Ok(oss.str());
}
GtpPlayer::Response GtpPlayer::HandleKnownCommand(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 1, args);
if (!response.ok) {
return response;
}
std::string result;
if (cmd_handlers_.find(std::string(args[0])) != cmd_handlers_.end()) {
result = "true";
} else {
result = "false";
}
return Response::Ok(result);
}
GtpPlayer::Response GtpPlayer::HandleKomi(absl::string_view cmd, CmdArgs args) {
auto response = CheckArgsExact(cmd, 1, args);
if (!response.ok) {
return response;
}
double x;
if (!absl::SimpleAtod(args[0], &x) || x != options().komi) {
return Response::Error("unacceptable komi");
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleListCommands(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 0, args);
if (!response.ok) {
return response;
}
std::vector<absl::string_view> cmds;
for (const auto& kv : cmd_handlers_) {
cmds.push_back(kv.first);
}
std::sort(cmds.begin(), cmds.end());
response.str = absl::StrJoin(cmds, "\n");
return response;
}
GtpPlayer::Response GtpPlayer::HandleLoadsgf(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 1, args);
if (!response.ok) {
return response;
}
std::ifstream f;
f.open(std::string(args[0]));
if (!f.is_open()) {
std::cerr << "Couldn't read \"" << args[0] << "\"" << std::endl;
return Response::Error("cannot load file");
}
std::stringstream buffer;
buffer << f.rdbuf();
sgf::Ast ast;
if (!ast.Parse(buffer.str())) {
std::cerr << "Couldn't parse \"" << args[0] << std::endl;
return Response::Error("cannot load file");
}
// Clear the board before replaying sgf.
last_genmove_ = Color::kEmpty;
NewGame();
for (const auto& move : sgf::GetMainLineMoves(ast)) {
PlayMove(move.c);
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleName(absl::string_view cmd, CmdArgs args) {
auto response = CheckArgsExact(cmd, 0, args);
if (!response.ok) {
return response;
}
return Response::Ok(options().name);
}
GtpPlayer::Response GtpPlayer::HandlePlay(absl::string_view cmd, CmdArgs args) {
auto response = CheckArgsExact(cmd, 2, args);
if (!response.ok) {
return response;
}
Color color;
if (std::tolower(args[0][0]) == 'b') {
color = Color::kBlack;
} else if (std::tolower(args[0][0]) == 'w') {
color = Color::kWhite;
} else {
std::cerr << "ERRROR: expected b or w for player color, got " << args[0]
<< std::endl;
return Response::Error("illegal move");
}
if (color != root()->position.to_play()) {
// TODO(tommadams): Allow out of turn moves.
return Response::Error("out of turn moves are not yet supported");
}
Coord c = Coord::FromKgs(args[1], true);
if (c == Coord::kInvalid) {
std::cerr << "ERRROR: expected KGS coord for move, got " << args[1]
<< std::endl;
return Response::Error("illegal move");
}
if (!root()->position.IsMoveLegal(c)) {
return Response::Error("illegal move");
}
PlayMove(c);
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandlePonderLimit(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 1, args);
if (!response.ok) {
return response;
}
int x;
if (!absl::SimpleAtoi(args[0], &x) || x < 0) {
return Response::Error("couldn't parse ", args[0], " as an integer >= 0");
} else {
ponder_limit_ = x;
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleReadouts(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 1, args);
if (!response.ok) {
return response;
}
int x;
if (!absl::SimpleAtoi(args[0], &x) || x <= 0) {
return Response::Error("couldn't parse ", args[0], " as an integer > 0");
} else {
mutable_options()->num_readouts = x;
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleReportSearchInterval(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 1, args);
if (!response.ok) {
return response;
}
int x;
if (!absl::SimpleAtoi(args[0], &x) || x < 0) {
return Response::Error("couldn't parse ", args[0], " as an integer >= 0");
}
report_search_interval_ = absl::Milliseconds(x);
return Response::Ok();
}
void GtpPlayer::ReportSearchStatus(const MctsNode* last_read) {
const auto& pos = root()->position;
nlohmann::json j = {
{"moveNum", pos.n()},
{"toPlay", pos.to_play() == Color::kBlack ? "B" : "W"},
};
auto& search = j["search"];
std::vector<const MctsNode*> path;
for (const auto* node = last_read; node != root(); node = node->parent) {
path.push_back(node);
}
for (auto it = path.rbegin(); it != path.rend(); ++it) {
search.push_back((*it)->move.ToKgs());
}
auto& qs = j["dq"];
for (int i = 0; i < kNumMoves; ++i) {
float dq = root()->child_Q(i) - root()->Q();
qs.push_back(static_cast<int>(std::round(dq * 100)));
}
auto& ns = j["n"];
for (const auto& edge : root()->edges) {
ns.push_back(static_cast<int>(edge.N));
}
// Only report the principal variation when it changes.
auto principal_variation = root()->MostVisitedPath();
if (principal_variation != last_principal_variation_sent_) {
auto& pv = j["pv"];
for (Coord c : principal_variation) {
pv.push_back(c.ToKgs());
}
last_principal_variation_sent_ = std::move(principal_variation);
}
std::cerr << "mg-search:" << j.dump() << std::endl;
}
} // namespace minigo