Repository navigation
Expand file tree
/
Copy pathGTP.cpp
More file actions
1399 lines (1235 loc) · 44.6 KB
/
Copy pathGTP.cpp
File metadata and controls
1399 lines (1235 loc) · 44.6 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
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of Leela Zero.
Copyright (C) 2017-2019 Gian-Carlo Pascutto and contributors
Leela Zero is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Leela Zero is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Leela Zero. If not, see <http://www.gnu.org/licenses/>.
Additional permission under GNU GPL version 3 section 7
If you modify this Program, or any covered work, by linking or
combining it with NVIDIA Corporation's libraries from the
NVIDIA CUDA Toolkit and/or the NVIDIA CUDA Deep Neural
Network library and/or the NVIDIA TensorRT inference library
(or a modified version of those libraries), containing parts covered
by the terms of the respective license agreement, the licensors of
this Program grant you additional permission to convey the resulting
work.
*/
#include "config.h"
#include <algorithm>
#include <cctype>
#include <chrono>
#include <cmath>
#include <cstdlib>
#include <exception>
#include <fstream>
#include <limits>
#include <memory>
#include <random>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include "GTP.h"
#include "FastBoard.h"
#include "FullBoard.h"
#include "GameState.h"
#include "Network.h"
#include "SGFTree.h"
#include "SMP.h"
#include "Training.h"
#include "UCTSearch.h"
#include "Utils.h"
using namespace Utils;
// Configuration flags
bool cfg_gtp_mode;
bool cfg_allow_pondering;
unsigned int cfg_num_threads;
unsigned int cfg_batch_size;
int cfg_max_playouts;
int cfg_max_visits;
size_t cfg_max_memory;
size_t cfg_max_tree_size;
int cfg_max_cache_ratio_percent;
TimeManagement::enabled_t cfg_timemanage;
int cfg_lagbuffer_cs;
int cfg_resignpct;
int cfg_noise;
int cfg_random_cnt;
int cfg_random_min_visits;
float cfg_random_temp;
std::uint64_t cfg_rng_seed;
bool cfg_dumbpass;
#ifdef USE_OPENCL
std::vector<int> cfg_gpus;
bool cfg_sgemm_exhaustive;
bool cfg_tune_only;
#ifdef USE_HALF
precision_t cfg_precision;
#endif
#endif
float cfg_puct;
float cfg_logpuct;
float cfg_logconst;
float cfg_softmax_temp;
float cfg_fpu_reduction;
float cfg_fpu_root_reduction;
std::string cfg_weightsfile;
std::string cfg_logfile;
FILE* cfg_logfile_handle;
bool cfg_quiet;
std::string cfg_options_str;
bool cfg_benchmark;
bool cfg_cpu_only;
AnalyzeTags cfg_analyze_tags;
/* Parses tags for the lz-analyze GTP command and friends */
AnalyzeTags::AnalyzeTags(std::istringstream& cmdstream, const GameState& game) {
std::string tag;
/* Default color is the current one */
m_who = game.board.get_to_move();
auto avoid_not_pass_resign_b = false, avoid_not_pass_resign_w = false;
auto allow_b = false, allow_w = false;
while (true) {
cmdstream >> std::ws;
if (isdigit(cmdstream.peek())) {
tag = "interval";
} else {
cmdstream >> tag;
if (cmdstream.fail() && cmdstream.eof()) {
/* Parsing complete */
m_invalid = false;
return;
}
}
if (tag == "avoid" || tag == "allow") {
std::string textcolor, textmoves;
size_t until_movenum;
cmdstream >> textcolor;
cmdstream >> textmoves;
cmdstream >> until_movenum;
if (cmdstream.fail()) {
return;
}
std::vector<int> moves;
std::istringstream movestream(textmoves);
while (!movestream.eof()) {
std::string textmove;
getline(movestream, textmove, ',');
auto sepidx = textmove.find_first_of(':');
if (sepidx != std::string::npos) {
if (!(sepidx == 2 || sepidx == 3)) {
moves.clear();
break;
}
auto move1_compressed = game.board.text_to_move(
textmove.substr(0, sepidx)
);
auto move2_compressed = game.board.text_to_move(
textmove.substr(sepidx + 1)
);
if (move1_compressed == FastBoard::NO_VERTEX ||
move1_compressed == FastBoard::PASS ||
move1_compressed == FastBoard::RESIGN ||
move2_compressed == FastBoard::NO_VERTEX ||
move2_compressed == FastBoard::PASS ||
move2_compressed == FastBoard::RESIGN)
{
moves.clear();
break;
}
auto move1_xy = game.board.get_xy(move1_compressed);
auto move2_xy = game.board.get_xy(move2_compressed);
auto xmin = std::min(move1_xy.first, move2_xy.first);
auto xmax = std::max(move1_xy.first, move2_xy.first);
auto ymin = std::min(move1_xy.second, move2_xy.second);
auto ymax = std::max(move1_xy.second, move2_xy.second);
for (auto move_x = xmin; move_x <= xmax; move_x++) {
for (auto move_y = ymin; move_y <= ymax; move_y++) {
moves.push_back(game.board.get_vertex(move_x,move_y));
}
}
} else {
auto move = game.board.text_to_move(textmove);
if (move == FastBoard::NO_VERTEX) {
moves.clear();
break;
}
moves.push_back(move);
}
}
if (moves.empty()) {
return;
}
int color;
if (textcolor == "w" || textcolor == "white") {
color = FastBoard::WHITE;
} else if (textcolor == "b" || textcolor == "black") {
color = FastBoard::BLACK;
} else {
return;
}
if (until_movenum < 1) {
return;
}
until_movenum += game.get_movenum() - 1;
for (const auto& move : moves) {
if (tag == "avoid") {
add_move_to_avoid(color, move, until_movenum);
if (move != FastBoard::PASS && move != FastBoard::RESIGN) {
if (color == FastBoard::BLACK) {
avoid_not_pass_resign_b = true;
} else {
avoid_not_pass_resign_w = true;
}
}
} else {
add_move_to_allow(color, move, until_movenum);
if (color == FastBoard::BLACK) {
allow_b = true;
} else {
allow_w = true;
}
}
}
if ((allow_b && avoid_not_pass_resign_b) ||
(allow_w && avoid_not_pass_resign_w)) {
/* If "allow" is in use, it is illegal to use "avoid" with any
* move that is not "pass" or "resign". */
return;
}
} else if (tag == "w" || tag == "white") {
m_who = FastBoard::WHITE;
} else if (tag == "b" || tag == "black") {
m_who = FastBoard::BLACK;
} else if (tag == "interval") {
cmdstream >> m_interval_centis;
if (cmdstream.fail()) {
return;
}
} else if (tag == "minmoves") {
cmdstream >> m_min_moves;
if (cmdstream.fail()) {
return;
}
} else {
return;
}
}
}
void AnalyzeTags::add_move_to_avoid(int color, int vertex, size_t until_move) {
m_moves_to_avoid.emplace_back(color, until_move, vertex);
}
void AnalyzeTags::add_move_to_allow(int color, int vertex, size_t until_move) {
m_moves_to_allow.emplace_back(color, until_move, vertex);
}
int AnalyzeTags::interval_centis() const {
return m_interval_centis;
}
int AnalyzeTags::invalid() const {
return m_invalid;
}
int AnalyzeTags::who() const {
return m_who;
}
size_t AnalyzeTags::post_move_count() const {
return m_min_moves;
}
bool AnalyzeTags::is_to_avoid(int color, int vertex, size_t movenum) const {
for (auto& move : m_moves_to_avoid) {
if (color == move.color && vertex == move.vertex && movenum <= move.until_move) {
return true;
}
}
if (vertex != FastBoard::PASS && vertex != FastBoard::RESIGN) {
auto active_allow = false;
for (auto& move : m_moves_to_allow) {
if (color == move.color && movenum <= move.until_move) {
active_allow = true;
if (vertex == move.vertex) {
return false;
}
}
}
if (active_allow) {
return true;
}
}
return false;
}
bool AnalyzeTags::has_move_restrictions() const {
return !m_moves_to_avoid.empty() || !m_moves_to_allow.empty();
}
std::unique_ptr<Network> GTP::s_network;
void GTP::initialize(std::unique_ptr<Network>&& net) {
s_network = std::move(net);
bool result;
std::string message;
std::tie(result, message) =
set_max_memory(cfg_max_memory, cfg_max_cache_ratio_percent);
if (!result) {
// This should only ever happen with 60 block networks on 32bit machine.
myprintf("LOW MEMORY SETTINGS! Couldn't set default memory limits.\n");
myprintf("The network you are using might be too big\n");
myprintf("for the default settings on your system.\n");
throw std::runtime_error("Error setting memory requirements.");
}
myprintf("%s\n", message.c_str());
}
void GTP::setup_default_parameters() {
cfg_gtp_mode = false;
cfg_allow_pondering = true;
// we will re-calculate this on Leela.cpp
cfg_num_threads = 1;
// we will re-calculate this on Leela.cpp
cfg_batch_size = 1;
cfg_max_memory = UCTSearch::DEFAULT_MAX_MEMORY;
cfg_max_playouts = UCTSearch::UNLIMITED_PLAYOUTS;
cfg_max_visits = UCTSearch::UNLIMITED_PLAYOUTS;
// This will be overwriiten in initialize() after network size is known.
cfg_max_tree_size = UCTSearch::DEFAULT_MAX_MEMORY;
cfg_max_cache_ratio_percent = 10;
cfg_timemanage = TimeManagement::AUTO;
cfg_lagbuffer_cs = 100;
cfg_weightsfile = leelaz_file("best-network");
#ifdef USE_OPENCL
cfg_gpus = { };
cfg_sgemm_exhaustive = false;
cfg_tune_only = false;
#ifdef USE_HALF
cfg_precision = precision_t::AUTO;
#endif
#endif
cfg_puct = 0.5f;
cfg_logpuct = 0.015f;
cfg_logconst = 1.7f;
cfg_softmax_temp = 1.0f;
cfg_fpu_reduction = 0.25f;
// see UCTSearch::should_resign
cfg_resignpct = -1;
cfg_noise = false;
cfg_fpu_root_reduction = cfg_fpu_reduction;
cfg_random_cnt = 0;
cfg_random_min_visits = 1;
cfg_random_temp = 1.0f;
cfg_dumbpass = false;
cfg_logfile_handle = nullptr;
cfg_quiet = false;
cfg_benchmark = false;
#ifdef USE_CPU_ONLY
cfg_cpu_only = true;
#else
cfg_cpu_only = false;
#endif
cfg_analyze_tags = AnalyzeTags{};
// C++11 doesn't guarantee *anything* about how random this is,
// and in MinGW it isn't random at all. But we can mix it in, which
// helps when it *is* high quality (Linux, MSVC).
std::random_device rd;
std::ranlux48 gen(rd());
std::uint64_t seed1 = (gen() << 16) ^ gen();
// If the above fails, this is one of our best, portable, bets.
std::uint64_t seed2 = std::chrono::high_resolution_clock::
now().time_since_epoch().count();
cfg_rng_seed = seed1 ^ seed2;
}
const std::string GTP::s_commands[] = {
"protocol_version",
"name",
"version",
"quit",
"known_command",
"list_commands",
"boardsize",
"clear_board",
"komi",
"play",
"genmove",
"showboard",
"undo",
"final_score",
"final_status_list",
"time_settings",
"time_left",
"fixed_handicap",
"last_move",
"move_history",
"place_free_handicap",
"set_free_handicap",
"loadsgf",
"printsgf",
"kgs-genmove_cleanup",
"kgs-time_settings",
"kgs-game_over",
"heatmap",
"lz-analyze",
"lz-genmove_analyze",
"lz-memory_report",
"lz-setoption",
"gomill-explain_last_move",
""
};
// Default/min/max could be moved into separate fields,
// but for now we assume that the GUI will not send us invalid info.
const std::string GTP::s_options[] = {
"option name Maximum Memory Use (MiB) type spin default 2048 min 128 max 131072",
"option name Percentage of memory for cache type spin default 10 min 1 max 99",
"option name Visits type spin default 0 min 0 max 1000000000",
"option name Playouts type spin default 0 min 0 max 1000000000",
"option name Lagbuffer type spin default 0 min 0 max 3000",
"option name Resign Percentage type spin default -1 min -1 max 30",
"option name Pondering type check default true",
""
};
std::string GTP::get_life_list(const GameState & game, bool live) {
std::vector<std::string> stringlist;
std::string result;
const auto& board = game.board;
if (live) {
for (int i = 0; i < board.get_boardsize(); i++) {
for (int j = 0; j < board.get_boardsize(); j++) {
int vertex = board.get_vertex(i, j);
if (board.get_state(vertex) != FastBoard::EMPTY) {
stringlist.push_back(board.get_string(vertex));
}
}
}
}
// remove multiple mentions of the same string
// unique reorders and returns new iterator, erase actually deletes
std::sort(begin(stringlist), end(stringlist));
stringlist.erase(std::unique(begin(stringlist), end(stringlist)),
end(stringlist));
for (size_t i = 0; i < stringlist.size(); i++) {
result += (i == 0 ? "" : "\n") + stringlist[i];
}
return result;
}
void GTP::execute(GameState & game, const std::string& xinput) {
std::string input;
static auto search = std::make_unique<UCTSearch>(game, *s_network);
bool transform_lowercase = true;
// Required on Unixy systems
if (xinput.find("loadsgf") != std::string::npos) {
transform_lowercase = false;
}
/* eat empty lines, simple preprocessing, lower case */
for (unsigned int tmp = 0; tmp < xinput.size(); tmp++) {
if (xinput[tmp] == 9) {
input += " ";
} else if ((xinput[tmp] > 0 && xinput[tmp] <= 9)
|| (xinput[tmp] >= 11 && xinput[tmp] <= 31)
|| xinput[tmp] == 127) {
continue;
} else {
if (transform_lowercase) {
input += std::tolower(xinput[tmp]);
} else {
input += xinput[tmp];
}
}
// eat multi whitespace
if (input.size() > 1) {
if (std::isspace(input[input.size() - 2]) &&
std::isspace(input[input.size() - 1])) {
input.resize(input.size() - 1);
}
}
}
std::string command;
int id = -1;
if (input == "") {
return;
} else if (input == "exit") {
exit(EXIT_SUCCESS);
} else if (input.find("#") == 0) {
return;
} else if (std::isdigit(input[0])) {
std::istringstream strm(input);
char spacer;
strm >> id;
strm >> std::noskipws >> spacer;
std::getline(strm, command);
} else {
command = input;
}
/* process commands */
if (command == "protocol_version") {
gtp_printf(id, "%d", GTP_VERSION);
return;
} else if (command == "name") {
gtp_printf(id, PROGRAM_NAME);
return;
} else if (command == "version") {
gtp_printf(id, PROGRAM_VERSION);
return;
} else if (command == "quit") {
gtp_printf(id, "");
exit(EXIT_SUCCESS);
} else if (command.find("known_command") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
cmdstream >> tmp; /* remove known_command */
cmdstream >> tmp;
for (int i = 0; s_commands[i].size() > 0; i++) {
if (tmp == s_commands[i]) {
gtp_printf(id, "true");
return;
}
}
gtp_printf(id, "false");
return;
} else if (command.find("list_commands") == 0) {
std::string outtmp(s_commands[0]);
for (int i = 1; s_commands[i].size() > 0; i++) {
outtmp = outtmp + "\n" + s_commands[i];
}
gtp_printf(id, outtmp.c_str());
return;
} else if (command.find("boardsize") == 0) {
std::istringstream cmdstream(command);
std::string stmp;
int tmp;
cmdstream >> stmp; // eat boardsize
cmdstream >> tmp;
if (!cmdstream.fail()) {
if (tmp != BOARD_SIZE) {
gtp_fail_printf(id, "unacceptable size");
} else {
float old_komi = game.get_komi();
Training::clear_training();
game.init_game(tmp, old_komi);
gtp_printf(id, "");
}
} else {
gtp_fail_printf(id, "syntax not understood");
}
return;
} else if (command.find("clear_board") == 0) {
Training::clear_training();
game.reset_game();
search = std::make_unique<UCTSearch>(game, *s_network);
assert(UCTNodePointer::get_tree_size() == 0);
gtp_printf(id, "");
return;
} else if (command.find("komi") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
float komi = 7.5f;
float old_komi = game.get_komi();
cmdstream >> tmp; // eat komi
cmdstream >> komi;
if (!cmdstream.fail()) {
if (komi != old_komi) {
game.set_komi(komi);
}
gtp_printf(id, "");
} else {
gtp_fail_printf(id, "syntax not understood");
}
return;
} else if (command.find("play") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
std::string color, vertex;
cmdstream >> tmp; //eat play
cmdstream >> color;
cmdstream >> vertex;
if (!cmdstream.fail()) {
if (!game.play_textmove(color, vertex)) {
gtp_fail_printf(id, "illegal move");
} else {
gtp_printf(id, "");
}
} else {
gtp_fail_printf(id, "syntax not understood");
}
return;
} else if (command.find("genmove") == 0
|| command.find("lz-genmove_analyze") == 0) {
auto analysis_output = command.find("lz-genmove_analyze") == 0;
std::istringstream cmdstream(command);
std::string tmp;
cmdstream >> tmp; // eat genmove
int who;
AnalyzeTags tags;
if (analysis_output) {
tags = AnalyzeTags{cmdstream, game};
if (tags.invalid()) {
gtp_fail_printf(id, "cannot parse analyze tags");
return;
}
who = tags.who();
} else {
/* genmove command */
cmdstream >> tmp;
if (tmp == "w" || tmp == "white") {
who = FastBoard::WHITE;
} else if (tmp == "b" || tmp == "black") {
who = FastBoard::BLACK;
} else {
gtp_fail_printf(id, "syntax error");
return;
}
}
if (analysis_output) {
// Start of multi-line response
cfg_analyze_tags = tags;
if (id != -1) gtp_printf_raw("=%d\n", id);
else gtp_printf_raw("=\n");
}
// start thinking
{
game.set_to_move(who);
// Outputs winrate and pvs for lz-genmove_analyze
int move = search->think(who);
game.play_move(move);
std::string vertex = game.move_to_text(move);
if (!analysis_output) {
gtp_printf(id, "%s", vertex.c_str());
} else {
gtp_printf_raw("play %s\n", vertex.c_str());
}
}
if (cfg_allow_pondering) {
// now start pondering
if (!game.has_resigned()) {
// Outputs winrate and pvs through gtp for lz-genmove_analyze
search->ponder();
}
}
if (analysis_output) {
// Terminate multi-line response
gtp_printf_raw("\n");
}
cfg_analyze_tags = {};
return;
} else if (command.find("lz-analyze") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
cmdstream >> tmp; // eat lz-analyze
AnalyzeTags tags{cmdstream, game};
if (tags.invalid()) {
gtp_fail_printf(id, "cannot parse analyze tags");
return;
}
// Start multi-line response.
if (id != -1) gtp_printf_raw("=%d\n", id);
else gtp_printf_raw("=\n");
// Now start pondering.
if (!game.has_resigned()) {
cfg_analyze_tags = tags;
// Outputs winrate and pvs through gtp
game.set_to_move(tags.who());
search->ponder();
}
cfg_analyze_tags = {};
// Terminate multi-line response
gtp_printf_raw("\n");
return;
} else if (command.find("kgs-genmove_cleanup") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
cmdstream >> tmp; // eat kgs-genmove
cmdstream >> tmp;
if (!cmdstream.fail()) {
int who;
if (tmp == "w" || tmp == "white") {
who = FastBoard::WHITE;
} else if (tmp == "b" || tmp == "black") {
who = FastBoard::BLACK;
} else {
gtp_fail_printf(id, "syntax error");
return;
}
game.set_passes(0);
{
game.set_to_move(who);
int move = search->think(who, UCTSearch::NOPASS);
game.play_move(move);
std::string vertex = game.move_to_text(move);
gtp_printf(id, "%s", vertex.c_str());
}
if (cfg_allow_pondering) {
// now start pondering
if (!game.has_resigned()) {
search->ponder();
}
}
} else {
gtp_fail_printf(id, "syntax not understood");
}
return;
} else if (command.find("undo") == 0) {
if (game.undo_move()) {
gtp_printf(id, "");
} else {
gtp_fail_printf(id, "cannot undo");
}
return;
} else if (command.find("showboard") == 0) {
gtp_printf(id, "");
game.display_state();
return;
} else if (command.find("final_score") == 0) {
float ftmp = game.final_score();
/* white wins */
if (ftmp < -0.1) {
gtp_printf(id, "W+%3.1f", float(fabs(ftmp)));
} else if (ftmp > 0.1) {
gtp_printf(id, "B+%3.1f", ftmp);
} else {
gtp_printf(id, "0");
}
return;
} else if (command.find("final_status_list") == 0) {
if (command.find("alive") != std::string::npos) {
std::string livelist = get_life_list(game, true);
gtp_printf(id, livelist.c_str());
} else if (command.find("dead") != std::string::npos) {
std::string deadlist = get_life_list(game, false);
gtp_printf(id, deadlist.c_str());
} else {
gtp_printf(id, "");
}
return;
} else if (command.find("time_settings") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
int maintime, byotime, byostones;
cmdstream >> tmp >> maintime >> byotime >> byostones;
if (!cmdstream.fail()) {
// convert to centiseconds and set
game.set_timecontrol(maintime * 100, byotime * 100, byostones, 0);
gtp_printf(id, "");
} else {
gtp_fail_printf(id, "syntax not understood");
}
return;
} else if (command.find("time_left") == 0) {
std::istringstream cmdstream(command);
std::string tmp, color;
int time, stones;
cmdstream >> tmp >> color >> time >> stones;
if (!cmdstream.fail()) {
int icolor;
if (color == "w" || color == "white") {
icolor = FastBoard::WHITE;
} else if (color == "b" || color == "black") {
icolor = FastBoard::BLACK;
} else {
gtp_fail_printf(id, "Color in time adjust not understood.\n");
return;
}
game.adjust_time(icolor, time * 100, stones);
gtp_printf(id, "");
if (cfg_allow_pondering) {
// KGS sends this after our move
// now start pondering
if (!game.has_resigned()) {
search->ponder();
}
}
} else {
gtp_fail_printf(id, "syntax not understood");
}
return;
} else if (command.find("auto") == 0) {
do {
int move = search->think(game.get_to_move(), UCTSearch::NORMAL);
game.play_move(move);
game.display_state();
} while (game.get_passes() < 2 && !game.has_resigned());
return;
} else if (command.find("go") == 0 && command.size() < 6) {
int move = search->think(game.get_to_move());
game.play_move(move);
std::string vertex = game.move_to_text(move);
myprintf("%s\n", vertex.c_str());
return;
} else if (command.find("heatmap") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
std::string symmetry;
cmdstream >> tmp; // eat heatmap
cmdstream >> symmetry;
Network::Netresult vec;
if (cmdstream.fail()) {
// Default = DIRECT with no symmetric change
vec = s_network->get_output(
&game, Network::Ensemble::DIRECT,
Network::IDENTITY_SYMMETRY, false);
} else if (symmetry == "all") {
for (auto s = 0; s < Network::NUM_SYMMETRIES; ++s) {
vec = s_network->get_output(
&game, Network::Ensemble::DIRECT, s, false);
Network::show_heatmap(&game, vec, false);
}
} else if (symmetry == "average" || symmetry == "avg") {
vec = s_network->get_output(
&game, Network::Ensemble::AVERAGE, -1, false);
} else {
vec = s_network->get_output(
&game, Network::Ensemble::DIRECT, std::stoi(symmetry), false);
}
if (symmetry != "all") {
Network::show_heatmap(&game, vec, false);
}
gtp_printf(id, "");
return;
} else if (command.find("fixed_handicap") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
int stones;
cmdstream >> tmp; // eat fixed_handicap
cmdstream >> stones;
if (!cmdstream.fail() && game.set_fixed_handicap(stones)) {
auto stonestring = game.board.get_stone_list();
gtp_printf(id, "%s", stonestring.c_str());
} else {
gtp_fail_printf(id, "Not a valid number of handicap stones");
}
return;
} else if (command.find("last_move") == 0) {
auto last_move = game.get_last_move();
if (last_move == FastBoard::NO_VERTEX) {
gtp_fail_printf(id, "no previous move known");
return;
}
auto coordinate = game.move_to_text(last_move);
auto color = game.get_to_move() == FastBoard::WHITE ? "black" : "white";
gtp_printf(id, "%s %s", color, coordinate.c_str());
return;
} else if (command.find("move_history") == 0) {
if (game.get_movenum() == 0) {
gtp_printf_raw("= \n");
} else {
gtp_printf_raw("= ");
}
auto game_history = game.get_game_history();
// undone moves may still be present, so reverse the portion of the
// array we need and resize to trim it down for iteration.
std::reverse(begin(game_history),
begin(game_history) + game.get_movenum() + 1);
game_history.resize(game.get_movenum());
for (const auto &state : game_history) {
auto coordinate = game.move_to_text(state->get_last_move());
auto color = state->get_to_move() == FastBoard::WHITE ? "black" : "white";
gtp_printf_raw("%s %s\n", color, coordinate.c_str());
}
gtp_printf_raw("\n");
return;
} else if (command.find("place_free_handicap") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
int stones;
cmdstream >> tmp; // eat place_free_handicap
cmdstream >> stones;
if (!cmdstream.fail()) {
game.place_free_handicap(stones, *s_network);
auto stonestring = game.board.get_stone_list();
gtp_printf(id, "%s", stonestring.c_str());
} else {
gtp_fail_printf(id, "Not a valid number of handicap stones");
}
return;
} else if (command.find("set_free_handicap") == 0) {
std::istringstream cmdstream(command);
std::string tmp;
cmdstream >> tmp; // eat set_free_handicap
do {
std::string vertex;
cmdstream >> vertex;
if (!cmdstream.fail()) {
if (!game.play_textmove("black", vertex)) {
gtp_fail_printf(id, "illegal move");
} else {
game.set_handicap(game.get_handicap() + 1);
}
}
} while (!cmdstream.fail());
std::string stonestring = game.board.get_stone_list();
gtp_printf(id, "%s", stonestring.c_str());
return;
} else if (command.find("loadsgf") == 0) {
std::istringstream cmdstream(command);
std::string tmp, filename;
int movenum;
cmdstream >> tmp; // eat loadsgf
cmdstream >> filename;
if (!cmdstream.fail()) {
cmdstream >> movenum;
if (cmdstream.fail()) {
movenum = 999;
}
} else {
gtp_fail_printf(id, "Missing filename.");
return;
}
auto sgftree = std::make_unique<SGFTree>();
try {
sgftree->load_from_file(filename);
game = sgftree->follow_mainline_state(movenum - 1);
gtp_printf(id, "");
} catch (const std::exception&) {
gtp_fail_printf(id, "cannot load file");
}
return;
} else if (command.find("kgs-chat") == 0) {
// kgs-chat (game|private) Name Message
std::istringstream cmdstream(command);
std::string tmp;
cmdstream >> tmp; // eat kgs-chat
cmdstream >> tmp; // eat game|private
cmdstream >> tmp; // eat player name
do {
cmdstream >> tmp; // eat message
} while (!cmdstream.fail());
gtp_fail_printf(id, "I'm a go bot, not a chat bot.");
return;
You can’t perform that action at this time.