-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmichi.c
1146 lines (1044 loc) · 41.5 KB
/
michi.c
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
// michi.c -- A minimalistic Go-playing engine
/*
This is a recoding in C (for speed) of the michi.py code by Petr Baudis
avalaible at https://github.com/pasky/michi .
(c) 2015 Petr Baudis <[email protected]> Denis Blumstein <[email protected]>
MIT licence (i.e. almost public domain)
The following comments are taken almost verbatim from the michi.py code
A minimalistic Go-playing engine attempting to strike a balance between
brevity, educational value and strength. It can beat GNUGo on 13x13 board
on a modest 4-thread laptop.
To start reading the code, begin either:
* Bottom up by looking at the goban implementation (in board.c) - starting with
the Position struct (in board.h), empty_position() and play_move() functions.
* in the middle of this file by looking at the Monte Carlo playout
implementation, starting with the mcplayout() function.
* Top down, by looking at the MCTS implementation, starting with the
tree_search() function. It is just a loop of tree_descend(),
mcplayout() and tree_update() round and round.
It may be better to jump around a bit instead of just reading straight
from start to end.
The source is composed in 7 independent parts
- Board routines (in board.c)
- Go heuristics (this file)
- Monte Carlo Playout policy (this file)
- Monte Carlo Tree search (this file)
- User Interface (in ui.c, params.c and debug.c)
- Pattern code : 3x3 and large patterns (in patterns.c)
- Overall management of the search (time, dynamic komi, etc.) (in control.c)
In C, functions prototypes must be declared before use.
In order to avoid these declarations, functions are defined before they are
used, which has the same effect.
This means that the higher level functions are found towards the bottom of
this file. This may not be a good idea in terms of readibility but at least
the order is the same as in the michi python code.
Short bibliography
------------------
[1] Martin Mueller, Computer Go, Artificial Intelligence, Vol.134, No 1-2,
pp 145-179, 2002
[2] Remi Coulom. Efficient Selectivity and Backup Operators in Monte-Carlo Tree
Search. Paolo Ciancarini and H. Jaap van den Herik. 5th International
Conference on Computer and Games, May 2006, Turin, Italy. 2006.
<inria-00116992>
[3] Sylvain Gelly, Yizao Wang, Remi Munos, Olivier Teytaud. Modification of UCT
with Patterns in Monte-Carlo Go. [Research Report] RR-6062, 2006.
<inria-00117266v3>
[4] David Stern, Ralf Herbrich, Thore Graepel, Bayesian Pattern Ranking for Move
Prediction in the Game of Go, In Proceedings of the 23rd international
conference on Machine learning, pages 873–880, Pittsburgh, Pennsylvania,
USA, 2006
[5] Rémi Coulom. Computing Elo Ratings of Move Patterns in the Game of Go.
In ICGA Journal (2007), pp 198-208.
[6] Sylvain Gelly, David Silver. Achieving Master Level Play in 9×9 Computer Go.
Proceedings of the Twenty-Third AAAI Conference on Artificial Intelligence
(2008)
[7] Albert L Zobrist. A New Hashing Method with Application for Game Playing.
Technical Report #88. April 1970
[8] Petr Baudis. MCTS with Information Sharing, PhD Thesis, 2011
[9] Robert Sedgewick, Algorithms in C, Addison-Wesley, 1990
+ many other PhD thesis accessible on the WEB
[1] can be consulted for the definition of Computer Go terms :
points, blocks, eyes, false eyes, liberties, etc.
and historical bibliography
*/
#include "michi.h"
//========================= Definition of Data Structures =====================
// Given a board of size NxN (N=9, 19, ...), we represent the position
// as an (N+1)*(N+2)+1 Byte array, with 0 (empty), 2 (white), 3 (black),
// and 1 (off-board border to make rules implementation easier).
// Coordinates are just indices in this array.
//
// -------------------------------- Global Data -------------------------------
Mark *mark1, *mark2, *already_suggested;
char buf[BUFLEN];
static int disp_ladder;
static char* colstr = "@ABCDEFGHJKLMNOPQRST";
// Stack of Positions for use in recursive calls fix_atari/read_ladder_attack
int avail_pos;
Position stack_pos[500];
//================================ Go heuristics ==============================
// The couple of functions read_ladder_attack / fix_atari is maybe the most
// complicated part of the whole program (sadly).
// Feel free to just TREAT IT AS A BLACK-BOX, it's not really that interesting!
//
int fix_atari_r(Position *pos, Point pt, Slist moves);
__INLINE__ Position *push_position(Position *pos)
{
Position *newpos = stack_pos + avail_pos++;
if (avail_pos > 500)
fatal_error("stack of Position overflow (> 500)");
memcpy(newpos, pos, sizeof(Position));
return newpos;
}
__INLINE__ void pop_position(void)
{
avail_pos--;
}
Point read_ladder_attack(Position *pos, Point pt, Slist libs)
// Check if a capturable ladder is being pulled out at pt and return a move
// that continues it in that case. Expects its two liberties in libs.
// Actually, this is a general 2-lib capture exhaustive solver.
{
char *ret;
Point moves[100];
Point l1, l2, move=PASS_MOVE;
// always play first on the liberty that has 3 neighbors EMPTY
if (point_nlibs(pos, libs[1]) == 3) {
l2 = libs[1];
goto process_l2;
}
if (point_nlibs(pos, libs[2]) == 3) {
l2 = libs[2];
goto process_l2;
}
if (line_height(libs[1], board_size(pos)) > 0) {
// for ladder try first the liberty that is not on the first line
l1 = libs[1];
l2 = libs[2];
}
else {
l1 = libs[2];
l2 = libs[1];
}
Position *pos_l = push_position(pos);
if (disp_ladder) fprintf(stderr,"read_ladder_attack:\n");
ret = play_move(pos_l, l1);
if (ret[0]==0) { // move is legal
if (disp_ladder) print_pos(pos_l, stderr, 0);
// fix_atari_r() will recursively call read_ladder_attack() back
slist_clear(moves);
int is_atari = fix_atari_r(pos_l, pt, moves);
// if block is in atari and cannot escape, it is caugth in a ladder
if (is_atari && slist_size(moves) == 0) {
move = l1;
pop_position();
goto finished;
}
}
pop_position();
process_l2: // exploration can be done in pos which is a workspace
if (disp_ladder) fprintf(stderr,"read_ladder_attack:\n");
ret = play_move(pos, l2);
if (ret[0]==0) { // move is legal
if (disp_ladder) print_pos(pos, stderr, 0);
// fix_atari_r() will recursively call read_ladder_attack() back
slist_clear(moves);
int is_atari = fix_atari_r(pos, pt, moves);
// if block is in atari and cannot escape, it is caugth in a ladder
if (is_atari && slist_size(moves) == 0)
move = l2;
}
finished:
return move;
}
int fix_atari_r(Position *pos, Point pt, Slist moves)
// An atari/capture analysis routine that checks the group at Point pt,
// determining whether (i) it is in atari (ii) if it can escape it,
// either by playing on its liberty or counter-capturing another group.
//
// Return 1 (true) if the group is in atari, 0 otherwise
// moves : a list of moves that capture or save blocks
// sizes : list of same lenght as moves (size of corresponding blocks)
// singlept_ok!=0 means that we will not try to save one-point groups
//
// Only called by read_ladder_attack()
{
Block b = point_block(pos, pt);
int in_atari=1;
Point l, libs[5], blocks[256], blibs[5];
slist_clear(moves);
if (block_nlibs(pos,b) >= 2) {
return 0;
}
Color other=color_other(board_color_to_play(pos));
block_compute_libs(pos, b, libs, 1);
if (point_color(pos, pt) == other) {
// - this is opponent's group, that's enough to capture it
//fprintf(stderr, "fix_atari_r: opponent block\n");
slist_insert(moves, libs[1]);
return in_atari;
}
// This is our group and it is in atari
// Before thinking about defense, what about counter-capturing a neighbor ?
make_list_neighbor_blocks_in_atari(pos, b, blocks, pt);
FORALL_IN_SLIST(blocks, b1) {
block_compute_libs(pos, b1, blibs, 1);
slist_insert(moves, blibs[1]);
}
slist_sort(moves); // used only for compatibility tests with michi.py
l = libs[1];
// We are escaping.
// Will playing our last liberty gain/ at least two liberties?
char *ret = play_move(pos, l);
if (ret[0]!=0)
return 1; // oops, suicidal move
b = point_block(pos, l);
if (block_nlibs(pos,b) >= 2) {
// Good, there is still some liberty remaining - but if it's just the
// two, check that we are not caught in a ladder... (Except that we
// don't care if we already have some alternative escape routes!)
if (slist_size(moves)>0 || block_nlibs(pos,b)>=3) {
slist_insert(moves, l);
}
else if (block_nlibs(pos,b)==2) {
block_compute_libs(pos,b,libs,2);
if (read_ladder_attack(pos,l,libs) == 0)
slist_insert(moves, l);
}
}
return in_atari;
}
int fix_atari(Position *pos, Point pt, int singlept_ok
, int twolib_test, int twolib_edgeonly, Slist moves, Slist sizes)
// An atari/capture analysis routine that checks the group at Point pt,
// determining whether (i) it is in atari (ii) if it can escape it,
// either by playing on its liberty or counter-capturing another group.
//
// Return 1 (true) if the group is in atari, 0 otherwise
// moves : a list of moves that capture or save blocks
// sizes : list of same lenght as moves (size of corresponding blocks)
// singlept_ok!=0 means that we will not try to save one-point groups
{
Block b = point_block(pos, pt);
int in_atari=1, maxlibs=3;
Point l, libs[5], blocks[256], blibs[5];
slist_clear(moves); slist_clear(sizes);
if (singlept_ok && block_size(pos, b) == 1) return 0;
if (block_nlibs(pos,b) >= 2) {
if (twolib_test && block_nlibs(pos,b) == 2 && block_size(pos,b) > 1) {
block_compute_libs(pos, b, libs, maxlibs);
if (twolib_edgeonly
&& ((line_height(libs[1], board_size(pos)))>0
|| (line_height(libs[2], board_size(pos)))>0)) {
// no expensive ladder check
return 0;
}
else {
// check that the block cannot be caught in a working ladder
// If it can, that's as good as in atari, a capture threat.
// (Almost - N/A for countercaptures.)
Position workpos = *pos;
Point ladder_attack = read_ladder_attack(&workpos, pt, libs);
if (ladder_attack) {
if(slist_insert(moves, ladder_attack))
slist_push(sizes, block_size(pos, b));
}
}
}
return 0;
}
block_compute_libs(pos, b, libs, maxlibs);
Color other=color_other(board_color_to_play(pos));
if (point_color(pos, pt) == other) {
// - this is opponent's group, that's enough to capture it
if (slist_insert(moves, libs[1]))
slist_push(sizes, block_size(pos, b));
return in_atari;
}
// This is our group and it is in atari
// Before thinking about defense, what about counter-capturing a neighbor ?
make_list_neighbor_blocks_in_atari(pos, b, blocks, pt);
FORALL_IN_SLIST(blocks, b1) {
block_compute_libs(pos, b1, blibs, 1);
slist_insert(moves, blibs[1]);
}
slist_sort(moves);
FORALL_IN_SLIST(moves, n)
slist_push(sizes,block_size(pos, point_block(pos, n)));
l = libs[1];
// We are escaping.
// Will playing our last liberty gain/ at least two liberties?
char *ret = play_move(pos, l);
if (ret[0]!=0)
return 1; // oops, suicidal move
b = point_block(pos, l);
if (block_nlibs(pos,b) >= 2) {
// Good, there is still some liberty remaining - but if it's just the
// two, check that we are not caught in a ladder... (Except that we
// don't care if we already have some alternative escape routes!)
if (slist_size(moves)>0 || block_nlibs(pos,b)>=3) {
if (slist_insert(moves, l))
slist_push(sizes, block_size(pos, b));
}
else if (block_nlibs(pos,b)==2) {
block_compute_libs(pos,b,libs,2);
Position workpos = *pos; // workspace for read_ladder_attack
if (read_ladder_attack(&workpos, l, libs) == 0)
if (slist_insert(moves, l))
slist_push(sizes, block_size(pos, b));
}
}
undo_move(pos);
return in_atari;
}
//========================= Montecarlo playout policy =========================
int gen_playout_moves_capture(Position *pos, Slist heuristic_set, float prob,
int expensive_ok, Slist moves, Slist sizes)
// Compute list of candidate next moves in the order of preference (capture)
// heuristic_set is the set of coordinates considered for applying heuristics;
// this is the immediate neighborhood of last two moves in the playout, but
// the whole board while prioring the tree.
{
int k, twolib_edgeonly = !expensive_ok;
Point move2[20], size2[20];
slist_clear(moves); slist_clear(sizes);
if (random_int(10000) <= prob*10000.0) {
mark_init(already_suggested);
FORALL_IN_SLIST(heuristic_set, pt)
if (point_is_stone(pos, pt)) {
Block b = point_block(pos, pt);
if (is_marked(already_suggested, b)) continue;
mark(already_suggested, b);
fix_atari(pos, pt, SINGLEPT_NOK, TWOLIBS_TEST,
twolib_edgeonly, move2, size2);
k=1;
FORALL_IN_SLIST(move2, move)
if (slist_insert(moves, move))
slist_push(sizes, size2[k++]);
}
mark_release(already_suggested);
}
return slist_size(moves);
}
int gen_playout_moves_pat3(Position *pos, Slist heuristic_set, float prob,
Slist moves)
// Compute list of candidate next moves in the order of preference (3x3 pattern)
// heuristic_set is the set of coordinates considered for applying heuristics;
// this is the immediate neighborhood of last two moves in the playout, but
// the whole board while prioring the tree.
{
slist_clear(moves);
if (random_int(10000) <= prob*10000.0) {
mark_init(already_suggested);
FORALL_IN_SLIST(heuristic_set, pt)
if (point_color(pos,pt) == EMPTY && pat3_match(pos, pt))
slist_push(moves, pt);
mark_release(already_suggested);
}
return slist_size(moves);
}
int gen_playout_moves_random(Position *pos, Point moves[BOARDSIZE], Point i0)
// Generate a list of moves (includes false positives - suicide moves;
// does not include true-eye-filling moves), starting from a given board index
// (that can be used for randomization)
{
Color c=board_color_to_play(pos);
slist_clear(moves);
for(Point i=i0 ; i<BOARD_IMAX ; i++) {
if (point_color(pos,i) != EMPTY) continue; // ignore NOT EMPTY Points
if (is_eye(pos,i) == c) continue; // ignore true eyes for player
slist_push(moves, i);
}
for(Point i=BOARD_IMIN-1 ; i<i0 ; i++) {
if (point_color(pos,i) != EMPTY) continue; // ignore NOT EMPTY Points
if (is_eye(pos,i) == c) continue; // ignore true eyes for player
slist_push(moves, i);
}
return slist_size(moves);
}
Point choose_random_move(Position *pos, Point i0, int disp)
// Replace the sequence gen_playout_moves_random(); choose_from()
{
char *ret;
Color c=board_color_to_play(pos);
Info sizes[20];
Point ds[20], i=i0, move=PASS_MOVE;
//Position saved_pos = *pos;
do {
if (point_color(pos,i) != EMPTY) goto not_found;
if (is_eye(pos,i) == c) goto not_found; // ignore true eyes for player
ret = play_move(pos, i);
if (ret[0] == 0) { // move OK
move = i;
// check if the suggested move did not turn out to be a self-atari
int r = random_int(10000), tstrej;
tstrej = r<=10000.0*PROB_RSAREJECT;
if (tstrej) {
slist_clear(ds); slist_clear(sizes);
fix_atari(pos, i, SINGLEPT_OK, TWOLIBS_TEST, 1, ds, sizes);
if (slist_size(ds) > 0) {
if(disp) fprintf(stderr, "rejecting self-atari move %s\n",
str_coord(i, buf));
undo_move(pos);
//*pos = saved_pos; // undo move;
move = PASS_MOVE;
goto not_found;
}
}
break;
}
not_found:
i++;
if (i >= BOARD_IMAX) i = BOARD_IMIN - 1;
} while (i!=i0);
return move;
}
Point choose_from(Position *pos, Slist moves, char *kind, int disp)
{
char *ret;
Info sizes[20];
Point move = PASS_MOVE, ds[20];
//Position saved_pos = *pos;
FORALL_IN_SLIST(moves, pt) {
if (is_marked(already_suggested, pt))
continue;
mark(already_suggested, pt);
if (disp && strcmp(kind, "random")!=0)
fprintf(stderr,"move suggestion (%s) %s\n", kind,str_coord(pt,buf));
ret = play_move(pos, pt);
if (ret[0] == 0) { // move OK
move = pt;
// check if the suggested move did not turn out to be a self-atari
int r = random_int(10000), tstrej;
if (strcmp(kind,"random") == 0) tstrej = r<=10000.0*PROB_RSAREJECT;
else tstrej = r<= 10000.0*PROB_SSAREJECT;
if (tstrej) {
slist_clear(ds); slist_clear(sizes);
fix_atari(pos, pt, SINGLEPT_OK, TWOLIBS_TEST, 1, ds, sizes);
if (slist_size(ds) > 0) {
if(disp) fprintf(stderr, "rejecting self-atari move %s\n",
str_coord(pt, buf));
undo_move(pos);
michi_assert(pos, blocks_OK(pos,pt));
//*pos = saved_pos; // undo move;
move = PASS_MOVE;
continue;
}
}
break;
}
}
return move;
}
Point choose_capture_move(Position *pos,Slist heuristic_set,float prob,int disp)
// Replace the sequence gen_playout_capture_moves(); choose_from()
{
int twolib_edgeonly = 1;
Point move=PASS_MOVE, moves[20], sizes[20];
if (random_int(10000) <= prob*10000.0) {
mark_init(already_suggested);
FORALL_IN_SLIST(heuristic_set, pt)
if (point_is_stone(pos, pt)) {
Block b = point_block(pos, pt);
if (is_marked(already_suggested, b)) continue;
mark(already_suggested, b);
fix_atari(pos, pt, SINGLEPT_NOK, TWOLIBS_TEST,
twolib_edgeonly, moves, sizes);
slist_shuffle(moves);
move = choose_from(pos, moves, "capture", disp);
if (move != PASS_MOVE) break;
}
mark_release(already_suggested);
}
return move;
}
double playout_score(Position *pos, int owner_map[], int score_count[2*N*N+1])
// compute score (>0 if BLACK wins); this assumes a final position with all
// dead stones captured and only single point eyes on the board ...
{
double s1;
int s=0;
FORALL_POINTS(pos,pt) {
Color c = point_color(pos, pt);
if (c == EMPTY) c = is_eyeish(pos,pt);
if (c == BLACK) {
s++;
owner_map[pt]++;
}
else if (c == WHITE) {
s--;
owner_map[pt]--;
}
}
s1 = s;
score_count[s + N*N]++;
return s1 - board_komi(pos) - board_delta_komi(pos);
}
double mcplayout(Position *pos, int amaf_map[], int owner_map[],
int score_count[2*N*N+1], int disp)
// Start a Monte Carlo playout from a given position, return score for to-play
// player at the starting position; amaf_map is board-sized scratchpad recording// who played at a given position first
{
double s=0.0;
int passes=0;
Point last_moves_neighbors[20], moves[BOARDSIZE], move;
if(disp) {
disp_ladder = 1;
fprintf(stderr, "** SIMULATION **\n");
}
if (board_nmoves(pos)>0 && board_last_move(pos)==0) passes=1;
while (passes < 2 && board_nmoves(pos) < MAX_GAME_LEN) {
michi_assert(pos, all_blocks_OK(pos));
move = 0;
if(disp) {
fprintf(stderr, "mcplayout: idum = %u\n", idum);
print_pos(pos, stderr, NULL);
}
// We simply try the moves our heuristics generate, in a particular
// order, but not with 100% probability; this is on the border between
// "rule-based playouts" and "probability distribution playouts".
make_list_last_moves_neighbors(pos, last_moves_neighbors);
// Capture heuristic suggestions
if((move=choose_capture_move(pos, last_moves_neighbors,
PROB_HEURISTIC_CAPTURE, disp)) != PASS_MOVE)
goto found;
// 3x3 patterns heuristic suggestions
if (gen_playout_moves_pat3(pos, last_moves_neighbors,
PROB_HEURISTIC_PAT3, moves)) {
mark_init(already_suggested);
if((move=choose_from(pos, moves, "pat3", disp)) != PASS_MOVE) {
mark_release(already_suggested);
goto found;
}
mark_release(already_suggested);
}
int x0 = random_int(N) + 1, y0 = random_int(N) + 1;
move = choose_random_move(pos, y0*(N+1) + x0 , disp);
found:
if (move == PASS_MOVE) { // No valid move : pass
pass_move(pos);
passes++;
}
else {
if (amaf_map[move] == 0) // mark the point with 1 for BLACK
// WHITE because in michi.py pos is updated after this line
amaf_map[move] = (board_color_to_play(pos) == WHITE ? 1 : -1);
passes=0;
}
//print_pos(pos, stderr, 0);
}
s = playout_score(pos, owner_map, score_count);
return s;
}
//========================== Montecarlo tree search ===========================
TreeNode* new_tree_node(void)
// Build a new tree node initialized with prior EVEN
{
TreeNode *node = michi_calloc(1,sizeof(TreeNode));
node->pv = PRIOR_EVEN; node->pw = PRIOR_EVEN/2;
return node;
}
void expand(Position *pos, TreeNode *tree)
// add and initialize children to a leaf node which represents the Position pos
{
char cfg_map[BOARDSIZE];
int nchildren = 0;
Info sizes[BOARDSIZE];
Point moves[BOARDSIZE];
Position pos2;
TreeNode *childset[BOARDSIZE], *node;
if (board_last_move(pos) != PASS_MOVE)
compute_cfg_distances(pos, board_last_move(pos), cfg_map);
// Use light random playout generator to get all the empty points (not eye)
gen_playout_moves_random(pos, moves, BOARD_IMIN-1);
tree->children = michi_calloc(slist_size(moves)+1, sizeof(TreeNode*));
FORALL_IN_SLIST(moves, pt) {
assert(point_color(pos, pt) == EMPTY);
char* ret = play_move(pos, pt);
if (ret[0] != 0) continue;
undo_move(pos);
// pt is a legal move : we build a new node for it
node = childset[pt]= tree->children[nchildren++] = new_tree_node();
node->move = pt;
}
tree->nchildren = nchildren;
// Update the prior for the 'capture' and 3x3 patterns suggestions
gen_playout_moves_capture(pos, allpoints, 1, 1, moves, sizes);
int k=1;
FORALL_IN_SLIST(moves, pt) {
char* ret = play_move(pos, pt);
if (ret[0] != 0) continue;
undo_move(pos);
node = childset[pt];
if (sizes[k] == 1) {
node->pv += PRIOR_CAPTURE_ONE;
node->pw += PRIOR_CAPTURE_ONE;
}
else {
node->pv += PRIOR_CAPTURE_MANY;
node->pw += PRIOR_CAPTURE_MANY;
}
k++;
}
gen_playout_moves_pat3(pos, allpoints, 1, moves);
FORALL_IN_SLIST(moves, pt) {
char* ret = play_move(pos, pt);
if (ret[0] != 0) continue;
undo_move(pos);
node = childset[pt];
node->pv += PRIOR_PAT3;
node->pw += PRIOR_PAT3;
}
// Second pass setting priors, considering each move just once now
copy_to_large_board(pos); // For large patterns
pos2 = *pos;
for (int k=0 ; k<tree->nchildren ; k++) {
node = tree->children[k];
Point pt = node->move;
play_move(&pos2, pt); // No need to check the move is legal
if (board_last_move(pos) != PASS_MOVE
&& cfg_map[pt]-1 < LEN_PRIOR_CFG) {
node->pv += PRIOR_CFG[cfg_map[pt]-1];
node->pw += PRIOR_CFG[cfg_map[pt]-1];
}
int height = line_height(pt, board_size(pos)); // 0-indexed
if (height <= 2 && empty_area(pos, pt, 3)) {
// No stones around; negative prior for 1st + 2nd line, positive
// for 3rd line; sanitizes opening and invasions
if (height <= 1) {
node->pv += PRIOR_EMPTYAREA;
node->pw += 0;
}
if (height == 2) {
node->pv += PRIOR_EMPTYAREA;
node->pw += PRIOR_EMPTYAREA;
}
}
fix_atari(&pos2, pt, SINGLEPT_OK, TWOLIBS_TEST, !TWOLIBS_EDGE_ONLY,
moves, sizes);
if (slist_size(moves) > 0) {
node->pv += PRIOR_SELFATARI;
node->pw += 0; // negative prior
}
double patternprob = large_pattern_probability(pt);
if (patternprob > 0.0) {
double pattern_prior = sqrt(patternprob); // tone up
node->pv += pattern_prior * PRIOR_LARGEPATTERN;
node->pw += pattern_prior * PRIOR_LARGEPATTERN;
}
undo_move(&pos2);
}
if (tree->nchildren <= 2) {
int nc = tree->nchildren;
// add a pass move. Useful, for example in case of seki
tree->children[nc] = new_tree_node();
tree->children[nc]->move = PASS_MOVE;
tree->nchildren = nc+1;
}
}
void free_tree(TreeNode *tree)
// Free memory allocated for the tree
{
if (tree->children != NULL) {
for (TreeNode **child = tree->children ; *child != NULL ; child++)
free_tree(*child);
free(tree->children);
}
free(tree);
}
double rave_urgency(TreeNode *node)
{
double v = node->v + node->pv;
double expectation = (node->w + node->pw)/v;
if (node->av==0) return expectation;
double rave_expectation = (double) node->aw / (double) node->av;
double beta = node->av / (node->av + v + (double)v*node->av/RAVE_EQUIV);
return beta * rave_expectation + (1-beta) * expectation;
}
double winrate(TreeNode *node)
// Return the winrate (number of wins divided by the number of visits)
{
double wr;
if (node->v>0) wr = (double) node->w / (double) node->v;
else wr = -0.1;
return wr;
}
void find_two_most_visited_children(TreeNode *tree, TreeNode **bests, int *v)
// find the two children that have been most visited
{
int vmax=-1, vmax2=-1;
TreeNode *c=NULL, *c2=NULL;
if (tree->children != NULL) {
for (TreeNode **child = tree->children ; *child != NULL ; child++) {
if ((*child)->v > vmax2) {
if ((*child)->v > vmax) {
vmax2 = vmax;
c2 = c;
vmax = (*child)->v;
c = *child;
}
else {
vmax2 = (*child)->v;
c2 = *child;
}
}
}
}
bests[0] = c;
bests[1] = c2;
v[0] = vmax;
v[1] = vmax2;
}
TreeNode* best_move(TreeNode *tree, TreeNode **except)
// best move is the most simulated one (avoiding nodes in except list)
{
int vmax=-1;
TreeNode *best=NULL;
if (tree->children == NULL) return NULL;
for (TreeNode **child = tree->children ; *child != NULL ; child++) {
if ((*child)->v > vmax) {
int update = 1;
if (except != NULL)
for (TreeNode **n=except ; *n!=NULL ; n++)
if (*child == *n) update=0;
if (update) {
vmax = (*child)->v;
best = (*child);
}
}
}
return best;
}
void dump_subtree(TreeNode *node, double thres, char *indent, FILE *f
, int recurse);
TreeNode* most_urgent(TreeNode **children, int nchildren, int disp)
// Return the most urgent child to play
{
int k=0;
double urgency, umax=0;
TreeNode *urgent = children[0];
// Randomize the order of the nodes
SHUFFLE(TreeNode *, children, nchildren);
for (TreeNode **child = children ; *child != NULL ; child++) {
if (disp)
dump_subtree(*child, N_SIMS/50, "", stderr, 0);
urgency = rave_urgency(*child);
if (urgency > umax) {
umax = urgency;
urgent = *child;
}
k++;
}
return urgent;
}
int tree_descend(Position *pos, TreeNode *tree, int amaf_map[], int disp
, TreeNode **nodes)
// Descend through the tree to a leaf
{
int last=0, passes = 0;
Point move;
//tree->v += 1;
nodes[last] = tree;
while (nodes[last]->children != NULL && passes <2) {
if (disp) print_pos(pos, stderr, NULL);
// Pick the most urgent child
TreeNode *node = most_urgent(nodes[last]->children,
nodes[last]->nchildren, disp);
nodes[++last] = node;
move = node->move;
if (disp) { fprintf(stderr, "chosen "); ppoint(move); }
if (move == PASS_MOVE) {
passes++;
pass_move(pos);
}
else {
passes = 0;
play_move(pos, move);
if (amaf_map[move] == 0) //Mark the point with 1 for black
amaf_map[move] = (board_color_to_play(pos) == BLACK ? -1 : 1);
}
if (node->children == NULL && node->v >= EXPAND_VISITS)
expand(pos, node);
}
return last;
}
void tree_update(Position *pos, TreeNode **nodes, int last
, int amaf_map[], double score, int disp)
// Store simulation result in the tree (nodes is the tree path)
{
int amaf_map_value = 1;
if (board_color_to_play(pos) == WHITE) {
amaf_map_value = -1;
score = -score; // score > 0 means the player in the first node wins
}
for (int k=0 ; k<=last ; k++) {
TreeNode *n= nodes[k];
if(disp) {
char str[8]; str_coord(n->move, str);
fprintf(stderr, "updating %s %d\n", str, score<0.0);
}
n->v += 1; // TODO put it in tree_descend when parallelize
n->w += score<0.0; // score is for to-play, node stats for just-played
// Update the node children AMAF stats with moves we made
// with their color
if (n->children != NULL) {
for (TreeNode **child = n->children ; *child != NULL ; child++) {
if ((*child)->move == 0) continue;
if (amaf_map[(*child)->move] == amaf_map_value) {
if (disp) {
char str[8];
str_coord((*child)->move, str);
fprintf(stderr, " AMAF updating %s %d\n", str,score>0);
}
(*child)->aw += score > 0; // reversed perspective
(*child)->av += 1;
}
}
}
score = -score;
amaf_map_value = -amaf_map_value;
}
}
float nplayouts, nplayouts_per_second=-1.0;
float start_playouts_sec, stop_playouts_sec;
int nplayouts_real;
void update_speed()
// Update the number of playouts per seconds
{
stop_playouts_sec=(float) clock() / (float) CLOCKS_PER_SEC;
nplayouts_per_second = nplayouts_real /
(stop_playouts_sec-start_playouts_sec);
}
float best2, bestr, bestwr;
void collect_infos(TreeNode *tree, int n, TreeNode *best,
TreeNode *workspace[], Position *pos)
{
char str[10], str2[10], str_r[10];
TreeNode *second, *reply;
nplayouts_real += n;
update_speed();
workspace[0] = best; workspace[1] = 0;
second = best_move(tree, workspace);
bestwr= winrate(best);
if (second != NULL) {
best2 = bestwr / winrate(second);
str_coord(second->move, str2);
}
else {
best2 = 0.0;
str2[0] = 0;
}
reply = best_move(best, NULL);
if (reply != NULL) {
bestr = bestwr - winrate(reply);
str_coord(reply->move, str_r);
}
else {
bestr = 0.0;
str_r[0] = 0;
}
str_coord(best->move, str);
sprintf(buf, "%12u %5.1f %3s (%3s) %3s %5.2f %6.3f %6.3f %7.1f"
, idum, board_delta_komi(pos), str, str2, str_r
, best2, bestr, bestwr, nplayouts_per_second);
log_fmt_s('S', buf, NULL);
}
void print_tree_summary(TreeNode *tree, int sims, FILE *f);
Point tree_search(Position *pos, TreeNode *tree, int n, int owner_map[],
int score_count[] , int disp)
// Perform MCTS search from a given position for a given number of iterations
{
double s;
int *amaf_map=michi_calloc(BOARDSIZE, sizeof(int)), i, last, visit[2];
Point bestmove;
Position *workpos=michi_malloc(sizeof(Position));
TreeNode *best, *bests[2], *nodes[500];
// Initialize the root node if necessary
if (tree->children == NULL) expand(pos, tree);
int live_gfx = strcmp(Live_gfx,"None") != 0;
for (i=0 ; i<n/2 ; i++) {
if (live_gfx && (i % Live_gfx_interval) == Live_gfx_interval-1)
display_live_gfx(pos, tree, owner_map);
*workpos = *pos;
memset(amaf_map, 0, BOARDSIZE*sizeof(int));
if (i>0 && i % REPORT_PERIOD == 0) print_tree_summary(tree, i, stderr);
last = tree_descend(workpos, tree, amaf_map, disp, nodes);
s = mcplayout(workpos, amaf_map, owner_map, score_count, disp);
tree_update(pos, nodes, last, amaf_map, s, disp);
// Early stop test
best = best_move(tree, NULL);
double best_wr = winrate(best);
if ( (best_wr > FASTPLAY5_THRES && i>n*0.05)
|| (best_wr > FASTPLAY20_THRES && i>n*0.2)) {
best = best_move(tree, NULL);
sprintf(buf,"%10u tree_search() breaks at %d (%.3f)",
idum, i, winrate(best));
log_fmt_s('I', buf, NULL);
nplayouts_real += i;
update_speed();
goto finished;
}
}
for ( ; i<n ; i++) {
if (live_gfx && (i % Live_gfx_interval) == Live_gfx_interval-1)
display_live_gfx(pos, tree, owner_map);
*workpos = *pos;
memset(amaf_map, 0, BOARDSIZE*sizeof(int));
if (i>0 && i % REPORT_PERIOD == 0)
print_tree_summary(tree, i, stderr);
last = tree_descend(workpos, tree, amaf_map, disp, nodes);
s = mcplayout(workpos, amaf_map, owner_map, score_count, disp);
tree_update(pos, nodes, last, amaf_map, s, disp);
find_two_most_visited_children(tree, bests, visit);
//printf("%d %d %d %d %.4lf %.4lf\n", i, n, visit[0], visit[1]
// , winrate(bests[0]), winrate(bests[1]));
if (visit[1] + n - i < visit[0]) {
sprintf(buf, "early abort save %d / %d simulations", n-i, n);
log_fmt_s('S', buf, NULL);
break; // 2nd most visited child will not be able to win
}
}
best = best_move(tree, NULL);
collect_infos(tree, i, best, nodes, pos);
finished:
if (verbosity > 0) {
dump_subtree(tree, N_SIMS/50, "", stderr, 1);
print_tree_summary(tree, i, stderr);
}
free(amaf_map); free(workpos);
if (best->move == PASS_MOVE && board_last_move(pos) == PASS_MOVE)
bestmove = PASS_MOVE;
else if (((double) best->w / (double) best->v) < RESIGN_THRES)