Repository navigation
Expand file tree
/
Copy pathgtp_player.cc
More file actions
965 lines (834 loc) · 27.7 KB
/
Copy pathgtp_player.cc
File metadata and controls
965 lines (834 loc) · 27.7 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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
// 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 <functional>
#include <iostream>
#include <sstream>
#include <thread>
#include <utility>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h"
#include "absl/time/clock.h"
#include "cc/constants.h"
#include "cc/logging.h"
#include "cc/sgf.h"
#include "nlohmann/json.hpp"
namespace minigo {
namespace {
// String written to stderr to signify that handling a GTP command is done.
constexpr auto kGtpCmdDone = "__GTP_CMD_DONE__";
} // namespace
GtpPlayer::GtpPlayer(std::unique_ptr<DualNet> network, const Options& options)
: MctsPlayer(std::move(network), options),
courtesy_pass_(options.courtesy_pass),
ponder_read_limit_(options.ponder_limit),
num_eval_reads_(options.num_eval_reads) {
if (ponder_read_limit_ > 0) {
ponder_type_ = PonderType::kReadLimited;
}
RegisterCmd("benchmark", &GtpPlayer::HandleBenchmark);
RegisterCmd("boardsize", &GtpPlayer::HandleBoardsize);
RegisterCmd("clear_board", &GtpPlayer::HandleClearBoard);
RegisterCmd("echo", &GtpPlayer::HandleEcho);
RegisterCmd("final_score", &GtpPlayer::HandleFinalScore);
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("playsgf", &GtpPlayer::HandlePlaysgf);
RegisterCmd("ponder", &GtpPlayer::HandlePonder);
RegisterCmd("prune_nodes", &GtpPlayer::HandlePruneNodes);
RegisterCmd("readouts", &GtpPlayer::HandleReadouts);
RegisterCmd("report_search_interval", &GtpPlayer::HandleReportSearchInterval);
RegisterCmd("select_position", &GtpPlayer::HandleSelectPosition);
RegisterCmd("undo", &GtpPlayer::HandleUndo);
RegisterCmd("variation", &GtpPlayer::HandleVariation);
RegisterCmd("verbosity", &GtpPlayer::HandleVerbosity);
NewGame();
}
void GtpPlayer::Run() {
MG_LOG(INFO) << "GTP engine ready";
// Start a background thread that pushes lines read from stdin into the
// thread safe stdin_queue_. This allows us to ponder when there's nothing
// to read from stdin.
std::atomic<bool> running(true);
std::thread stdin_thread([this, &running]() {
std::string line;
while (std::cin) {
std::getline(std::cin, line);
stdin_queue_.Push(line);
}
running = false;
});
while (running) {
std::string line;
// If there's a command waiting on stdin, process it.
if (stdin_queue_.TryPop(&line)) {
if (!HandleCmd(line)) {
break;
}
continue;
}
// Otherwise, ponder if enabled.
if (!MaybePonder()) {
// If pondering isn't enabled, try and pop a command from stdin with a
// short timeout. The timeout gives us a chance to break out of the loop
// when stdin is closed with ctrl-C.
if (stdin_queue_.PopWithTimeout(&line, absl::Seconds(1))) {
if (!HandleCmd(line)) {
break;
}
}
}
}
stdin_thread.join();
}
void GtpPlayer::NewGame() {
node_to_info_.clear();
id_to_info_.clear();
to_eval_.clear();
MctsPlayer::NewGame();
RegisterNode(root());
}
Coord GtpPlayer::SuggestMove() {
if (courtesy_pass_ && root()->move == Coord::kPass) {
return Coord::kPass;
}
return MctsPlayer::SuggestMove();
}
bool GtpPlayer::PlayMove(Coord c) {
if (!MctsPlayer::PlayMove(c)) {
return false;
}
RefreshPendingWinRateEvals();
return true;
}
void GtpPlayer::RegisterCmd(const std::string& cmd, CmdHandler handler) {
cmd_handlers_[cmd] = handler;
}
bool GtpPlayer::MaybePonder() {
if (root()->game_over() || ponder_type_ == PonderType::kOff ||
ponder_limit_reached_) {
return false;
}
// Check if we're finished pondering.
if ((ponder_type_ == PonderType::kReadLimited &&
ponder_read_count_ >= ponder_read_limit_) ||
(ponder_type_ == PonderType::kTimeLimited &&
absl::Now() >= ponder_time_limit_)) {
if (!ponder_limit_reached_) {
MG_LOG(INFO) << "mg-ponder: done";
ponder_limit_reached_ = true;
}
return false;
}
// Remember the number of reads at the root.
int n = root()->N();
// First populate the batch with any nodes that require win rate evaluation.
std::vector<TreePath> paths;
while (!to_eval_.empty()) {
auto* info = to_eval_.front();
int eval_limit = options().batch_size;
// While there are still nodes in the win rate eval queue that haven't had
// any reads, use all the available reads in the batch to perform win rate
// evaluation. Otherwise use up to 50% of each batch for win rate
// evaluation.
if (info->num_eval_reads > 0) {
eval_limit /= 2;
}
if (static_cast<int>(paths.size()) >= eval_limit) {
break;
}
to_eval_.pop_front();
SelectLeaves(info->node, 1, &paths);
}
// While there is still space left in the batch, perform regular tree search.
int num_eval_reads = static_cast<int>(paths.size());
int num_search_reads = options().batch_size - num_eval_reads;
if (num_search_reads > 0) {
SelectLeaves(root(), num_search_reads, &paths);
}
ProcessLeaves(absl::MakeSpan(paths), options().random_symmetry);
// Send updated visit and Q data for all the nodes we performed win rate
// evaluation on. This updates Minigui's win rate graph.
for (int i = 0; i < num_eval_reads; ++i) {
auto* root = paths[i].root;
nlohmann::json j = {
{"id", GetAuxInfo(root)->id},
{"n", root->N()},
{"q", root->Q()},
};
MG_LOG(INFO) << "mg-update:" << j.dump();
}
// Increment the ponder count by difference new and old reads.
ponder_read_count_ += root()->N() - n;
// Increment the number of reads for all the nodes we performed win rate
// evaluation on, pushing nodes that require more reads onto the back of the
// queue.
for (int i = 0; i < num_eval_reads; ++i) {
auto* info = GetAuxInfo(paths[i].root);
if (++info->num_eval_reads < num_eval_reads_) {
to_eval_.push_back(info);
}
}
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()) {
MG_LOG(INFO) << kGtpCmdDone;
std::cout << "=\n\n" << std::flush;
return true;
}
// Split the GTP command and its arguments.
auto cmd = std::string(args[0]);
args.erase(args.begin());
if (cmd == "quit") {
MG_LOG(INFO) << kGtpCmdDone;
std::cout << "=\n\n" << std::flush;
return false;
}
auto response = DispatchCmd(cmd, args);
MG_LOG(INFO) << kGtpCmdDone;
std::cout << (response.ok ? "=" : "?");
if (!response.str.empty()) {
std::cout << " " << response.str;
}
std::cout << "\n\n" << std::flush;
return true;
}
void
8000
GtpPlayer::ProcessLeaves(absl::Span<TreePath> paths,
bool random_symmetry) {
MctsPlayer::ProcessLeaves(paths, random_symmetry);
if (!paths.empty() && report_search_interval_ != absl::ZeroDuration()) {
auto now = absl::Now();
if (now - last_report_time_ > report_search_interval_) {
last_report_time_ = now;
ReportSearchStatus(paths.back().root, paths.back().leaf);
}
}
}
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;
}
NewGame();
ReportPosition(root());
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 (!root()->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::HandleGenmove(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsRange(cmd, 0, 1, args);
if (!response.ok) {
return response;
}
auto c = SuggestMove();
MG_LOG(INFO) << root()->Describe();
MG_CHECK(PlayMove(c));
// Begin pondering again if requested.
if (ponder_type_ != PonderType::kOff) {
ponder_limit_reached_ = false;
ponder_read_count_ = 0;
if (ponder_type_ == PonderType::kTimeLimited) {
ponder_time_limit_ = absl::Now() + ponder_duration_;
}
}
ReportPosition(root());
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()) {
MG_LOG(ERROR) << "couldn't read \"" << args[0] << "\"";
return Response::Error("cannot load file");
}
std::stringstream buffer;
buffer << f.rdbuf();
return ParseSgf(buffer.str());
}
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 {
MG_LOG(ERROR) << "expected b or w for player color, got " << args[0];
return Response::Error("illegal move");
}
if (color != root()->position.to_play()) {
return Response::Error("out of turn moves are not yet supported");
}
Coord c = Coord::FromKgs(args[1], true);
if (c == Coord::kInvalid) {
MG_LOG(ERROR) << "expected GTP coord for move, got " << args[1];
return Response::Error("illegal move");
}
if (!PlayMove(c)) {
return Response::Error("illegal move");
}
ReportPosition(root());
return Response::Ok();
}
10000
GtpPlayer::Response GtpPlayer::HandlePlaysgf(absl::string_view cmd,
CmdArgs args) {
auto sgf_str = absl::StrReplaceAll(absl::StrJoin(args, " "), {{"\\n", "\n"}});
return ParseSgf(sgf_str);
}
GtpPlayer::Response GtpPlayer::HandlePonder(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsRange(cmd, 1, 2, args);
if (!response.ok) {
return response;
}
if (args[0] == "off") {
// Disable pondering.
ponder_type_ = PonderType::kOff;
ponder_read_count_ = 0;
ponder_read_limit_ = 0;
ponder_duration_ = {};
ponder_time_limit_ = absl::InfinitePast();
ponder_limit_reached_ = true;
return Response::Ok();
}
// Subsequent sub commands require exactly 2 arguments.
response = CheckArgsExact(cmd, 2, args);
if (!response.ok) {
return response;
}
if (args[0] == "winrate") {
// Set the number of reads for win rate evaluation.
int num_reads;
if (!absl::SimpleAtoi(args[1], &num_reads) || num_reads < 0) {
return Response::Error("invalid num_reads");
}
num_eval_reads_ = num_reads;
RefreshPendingWinRateEvals();
return Response::Ok();
}
if (args[0] == "reads") {
// Enable pondering limited by number of reads.
int read_limit;
if (!absl::SimpleAtoi(args[1], &read_limit) || read_limit <= 0) {
return Response::Error("couldn't parse read limit");
}
ponder_read_limit_ = read_limit;
ponder_type_ = PonderType::kReadLimited;
ponder_read_count_ = 0;
ponder_limit_reached_ = false;
return Response::Ok();
}
if (args[0] == "time") {
// Enable pondering limited by time.
float duration;
if (!absl::SimpleAtof(args[1], &duration) || duration <= 0) {
return Response::Error("couldn't parse time limit");
}
ponder_type_ = PonderType::kTimeLimited;
ponder_duration_ = absl::Seconds(duration);
ponder_time_limit_ = absl::Now() + ponder_duration_;
ponder_limit_reached_ = false;
return Response::Ok();
}
return Response::Error("unrecognized ponder mode");
}
GtpPlayer::Response GtpPlayer::HandlePruneNodes(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)) {
return Response::Error("couldn't parse ", args[0], " as an integer");
}
mutable_options()->prune_orphaned_nodes = x != 0;
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();
}
GtpPlayer::Response GtpPlayer::HandleSelectPosition(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsExact(cmd, 1, args);
if (!response.ok) {
return response;
}
if (args[0] == "root") {
ResetRoot();
return Response::Ok();
}
auto it = id_to_info_.find(args[0]);
if (it == id_to_info_.end()) {
return Response::Error("unknown position id");
}
auto* node = it->second->node;
// Build the sequence of moves the will end up at the requested position.
std::vector<Coord> moves;
while (node->parent != nullptr) {
moves.push_back(node->move);
node = node->parent;
}
std::reverse(moves.begin(), moves.end());
// Rewind to the start & play the sequence of moves.
ResetRoot();
for (const auto& move : moves) {
MG_CHECK(PlayMove(move));
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleUndo(absl::string_view cmd, CmdArgs args) {
auto response = CheckArgsExact(cmd, 0, args);
if (!response.ok) {
return response;
}
if (!UndoMove()) {
return Response::Error("cannot undo");
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleVariation(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsRange(cmd, 0, 1, args);
if (!response.ok) {
return response;
}
if (args.size() == 0) {
child_variation_ = Coord::kInvalid;
} else {
Coord c = Coord::FromKgs(args[0], true);
if (c == Coord::kInvalid) {
MG_LOG(ERROR) << "expected GTP coord for move, got " << args[0];
return Response::Error("illegal move");
}
if (c != child_variation_) {
child_variation_ = c;
ReportSearchStatus(root(), nullptr);
}
}
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::HandleVerbosity(absl::string_view cmd,
CmdArgs args) {
auto response = CheckArgsRange(cmd, 0, 1, args);
if (!response.ok) {
return response;
}
int x;
if (!absl::SimpleAtoi(args[0], &x)) {
return Response::Error("bad verbosity");
}
mutable_options()->verbose = x != 0;
return Response::Ok();
}
GtpPlayer::Response GtpPlayer::ParseSgf(const std::string& sgf_str) {
sgf::Ast ast;
if (!ast.Parse(sgf_str)) {
MG_LOG(ERROR) << "couldn't parse SGF";
return Response::Error("cannot parse file");
}
// Clear the board before replaying sgf.
NewGame();
// Traverse the SGF's game trees, loading them into the backend & running
// inference on the positions in batches.
std::function<Response(const sgf::Node&)> traverse =
[&](const sgf::Node& node) {
if (node.move.color != root()->position.to_play()) {
// The move color is different than expected. Play a pass move to flip
// the colors.
if (root()->move == Coord::kPass) {
auto expected = ColorToCode(root()->position.to_play());
auto actual = node.move.ToSgf();
return Response::Error(
"expected move by ", expected, ", got ", actual,
" but can't play an intermediate pass because the previous ",
"move was also a pass");
}
MG_LOG(WARNING) << "Inserting pass move";
MG_CHECK(PlayMove(Coord::kPass));
ReportPosition(root());
}
if (!PlayMove(node.move.c)) {
return Response::Error("error playing ", node.move.ToSgf());
}
if (!node.comment.empty()) {
auto* info = GetAuxInfo(root());
info->comment = node.comment;
}
ReportPosition(root());
for (const auto& child : node.children) {
auto response = traverse(*child);
if (!response.ok) {
return response;
}
}
UndoMove();
return Response::Ok();
};
auto trees = sgf::GetTrees(ast);
for (const auto& tree : trees) {
auto response = traverse(*tree);
if (!response.ok) {
return response;
}
}
// Play the main line.
ResetRoot();
if (!trees.empty()) {
for (const auto& move : trees[0]->ExtractMainLine()) {
// We already validated that all the moves could be played in traverse(),
// so if PlayMove fails here, something has gone seriously awry.
MG_CHECK(PlayMove(move.c));
}
ReportPosition(root());
}
return Response::Ok();
}
void GtpPlayer::ReportSearchStatus(MctsNode* root, MctsNode* leaf) {
nlohmann::json j = {
{"id", GetAuxInfo(root)->id},
{"n", root->N()},
{"q", root->Q()},
};
// Pricipal variation.
auto src_pv = root->MostVisitedPath();
if (!src_pv.empty()) {
auto& dst_pv = j["variations"]["pv"];
for (Coord c : src_pv) {
dst_pv.push_back(c.ToKgs());
}
}
// Current tree search variation.
if (leaf != nullptr) {
std::vector<const MctsNode*> src_search;
for (const auto* node = leaf; node != root; node = node->parent) {
src_search.push_back(node);
}
if (!src_search.empty()) {
std::reverse(src_search.begin(), src_search.end());
auto& dst_search = j["variations"]["search"];
for (const auto* node : src_search) {
dst_search.push_back(node->move.ToKgs());
}
}
}
// Requested child variation, if any.
if (child_variation_ != Coord::kInvalid) {
auto& child_v = j["variations"][child_variation_.ToKgs()];
child_v.push_back(child_variation_.ToKgs());
auto it = root->children.find(child_variation_);
if (it != root->children.end()) {
for (Coord c : it->second->MostVisitedPath()) {
child_v.push_back(c.ToKgs());
}
}
}
// Child N.
auto& childN = j["childN"];
for (const auto& edge : root->edges) {
childN.push_back(static_cast<int>(edge.N));
}
// Child Q.
auto& childQ = j["childQ"];
for (int i = 0; i < kNumMoves; ++i) {
childQ.push_back(static_cast<int>(std::round(root->child_Q(i) * 1000)));
}
MG_LOG(INFO) << "mg-update:" << j.dump();
}
void GtpPlayer::ReportPosition(MctsNode* node) {
const auto& position = node->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;
}
auto* info = GetAuxInfo(node);
nlohmann::json j = {
{"id", info->id},
{"toPlay", position.to_play() == Color::kBlack ? "B" : "W"},
{"moveNum", position.n()},
{"stones", oss.str()},
{"gameOver", node->game_over()},
};
const auto& captures = node->position.num_captures();
if (captures[0] != 0 || captures[1] != 0) {
j["caps"].push_back(captures[0]);
j["caps"].push_back(captures[1]);
}
if (node->parent != nullptr) {
j["parentId"] = GetAuxInfo(node->parent)->id;
if (node->N() > 0) {
// Only send Q if the node has been read at least once.
j["q"] = node->Q();
}
}
if (node->move != Coord::kInvalid) {
j["move"] = node->move.ToKgs();
}
if (!info->comment.empty()) {
j["comment"] = info->comment;
}
MG_LOG(INFO) << "mg-position: " << j.dump();
}
GtpPlayer::AuxInfo* GtpPlayer::RegisterNode(MctsNode* node) {
auto it = node_to_info_.find(node);
if (it != node_to_info_.end()) {
return it->second.get();
}
auto* parent = node->parent != nullptr ? GetAuxInfo(node->parent) : nullptr;
auto info = absl::make_unique<AuxInfo>(parent, node);
auto raw_info = info.get();
id_to_info_.emplace(info->id, raw_info);
node_to_info_.emplace(node, std::move(info));
return raw_info;
}
GtpPlayer::AuxInfo* GtpPlayer::GetAuxInfo(MctsNode* node) const {
auto it = node_to_info_.find(node);
MG_CHECK(it != node_to_info_.end());
return it->second.get();
}
GtpPlayer::AuxInfo::AuxInfo(AuxInfo* parent, MctsNode* node)
: parent(parent), node(node), id(absl::StrFormat("%p", node)) {
if (parent != nullptr) {
parent->children.push_back(this);
}
}
void GtpPlayer::RefreshPendingWinRateEvals() {
to_eval_.clear();
// Build a new list of nodes that require win rate evaluation.
// First, traverse to the leaf node of the current position's main line.
auto* info = RegisterNode(root());
while (!info->children.empty()) {
info = info->children[0];
}
// Walk back up the tree to the root, enqueing all nodes that have fewer than
// the num_eval_reads_ win rate evaluations.
while (info != nullptr) {
if (info->num_eval_reads < num_eval_reads_) {
to_eval_.push_back(info);
}
info = info->parent;
}
// Sort the nodes for eval by number of eval reads, breaking ties by the move
// number.
std::sort(to_eval_.begin(), to_eval_.end(), [](AuxInfo* a, AuxInfo* b) {
if (a->num_eval_reads != b->num_eval_reads) {
return a->num_eval_reads < b->num_eval_reads;
}
return a->node->position.n() < b->node->position.n();
});
}
} // namespace minigo