-
Notifications
You must be signed in to change notification settings - Fork 16
/
tests.py
1136 lines (991 loc) · 40.6 KB
/
tests.py
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
# -*- coding: utf-8 -*-
from conf import conf
conf['SIZE'] = 9 # Override settings for tests
conf['KOMI'] = 5.5 # Override settings for tests
import unittest
import numpy as np
import random
import os
from play import (
color_board, _get_points, capture_group, make_play, legal_moves,
index2coord, game_init,
)
from self_play import (
play_game
)
from engine import simulate, ModelEngine
from symmetry import (
_id,
left_diagonal, reverse_left_diagonal,
right_diagonal, reverse_right_diagonal,
vertical_axis, reverse_vertical_axis,
horizontal_axis, reverse_horizontal_axis,
rotation_90, reverse_rotation_90,
rotation_180, reverse_rotation_180,
rotation_270, reverse_rotation_270,
)
import itertools
from sgfsave import save_game_sgf
from gtp import Engine
class DummyModel(object):
name = "dummy_model"
def predict(self, X):
policies, values = self.predict_on_batch(X)
return policies[0], values[0]
def predict_on_batch(self, X):
size = conf['SIZE']
batch_size = X.shape[0]
policy = np.zeros((batch_size, size * size + 1), dtype=np.float32)
value = np.zeros((batch_size, 1), dtype=np.float32)
for i in range(batch_size):
policy[i,:] = list(reversed(range(1, size*size + 2)))
policy[:,:] /= np.sum(policy, axis=1)[:,np.newaxis]
value[:, :] = 1
return policy, value
class TestGoMethods(unittest.TestCase):
def assertEqualList(self, arr1, arr2):
self.assertEqual(arr1.tolist(), arr2.tolist())
def test_coloring_player_1(self):
board = np.array(
[[1, 1, 1],
[1, 0, 1],
[1, 1, 1]])
target = np.array(
[[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
self.assertEqualList(color_board(board, 1), target)
board = np.array(
[[1, 1, 1, -1, -1, -1],
[1, 0, 1, -1, 0, -1],
[1, 1, 1, -1, -1, -1]])
target = np.array(
[[1, 1, 1, -1, -1, -1],
[1, 1, 1, -1, 0, -1],
[1, 1, 1, -1, -1, -1]])
self.assertEqualList(color_board(board, 1), target)
def test_player_1_big(self):
board = np.array([
[0, 0, 0, 1, 0, -1, 0, 0, 0,],
[0, 0, 0, 1, 0, -1, 0, 0, 0,],
[0, 0, 0, 1, 0, -1, 0, 0, 0,],
[0, 0, 0, 1, -1, 0, 0, -1, 0,],
[1, 1, 1, -1, 0, -1, -1, 0, 0,],
[0, 0, 0, 1, -1, 0, 0, -1, -1,],
[0, 0, 0, 1, 0, -1, 0, 0, 0,],
[0, 0, 0, 1, 0, -1, 0, 1, 0,],
[0, 0, 0, 0, 0, -1, 0, 0, 0,],
])
target = np.array([
[1, 1, 1, 2, 0, -2, -1, -1, -1,],
[1, 1, 1, 2, 0, -2, -1, -1, -1,],
[1, 1, 1, 2, 0, -2, -1, -1, -1,],
[1, 1, 1, 2, -2, -1, -1, -2, -1,],
[2, 2, 2, -2, -1, -2, -2, -1, -1,],
[0, 0, 0, 2, -2, 0, 0, -2, -2,],
[0, 0, 0, 2, 0, -2, 0, 0, 0,],
[0, 0, 0, 2, 0, -2, 0, 2, 0,],
[0, 0, 0, 0, 0, -2, 0, 0, 0,],
])
colored1 = color_board(board, 1)
colored2 = color_board(board, -1)
total = colored1 + colored2
self.assertEqualList(total, target)
def test_coloring_player_2(self):
board = np.array(
[[1, 1, 1],
[1, 0, 1],
[1, 1, 1]])
target = np.array(
[[1, 1, 1],
[1, 0, 1],
[1, 1, 1]])
self.assertEqualList(color_board(board, -1), target)
board = np.array(
[[1, 1, 1, -1, -1, -1],
[1, 0, 1, -1, 0, -1],
[1, 1, 1, -1, -1, -1]])
target = np.array(
[[1, 1, 1, -1, -1, -1],
[1, 0, 1, -1, -1, -1],
[1, 1, 1, -1, -1, -1]])
self.assertEqualList(color_board(board, -1), target)
def test_get_winner(self):
board = np.array([
[0, 0, 0, 1, 0, -1, 0, 0, 0,],
[0, 0, 0, 1, 0, -1, 0, 0, 0,],
[0, 0, 0, 1, 0, -1, 0, 0, 0,],
[0, 0, 0, 1, -1, 0, 0, -1, 0,],
[1, 1, 1, -1, 0, -1, -1, 0, 0,],
[0, 0, 0, 1, -1, 0, 0, -1, -1,],
[0, 0, 0, 1, 0, -1, 0, 0, 0,],
[0, 0, 0, 1, 0, -1, 0, 1, 0,],
[0, 0, 0, 0, 0, -1, 0, 0, 0,],
])
self.assertEqual(_get_points(board), {0: 29, 1: 12, 2: 11, -1: 15, -2: 14})
def test_taking_stones(self):
board = np.array(
[[0, 1, 0],
[1,-1, 1],
[0, 1, 0]])
target_group = [(1, 1)]
group = capture_group(1, 1, board)
self.assertEqual(group, target_group)
def test_taking_group_stones(self):
board = np.array(
[[0, 1, 0],
[1,-1, 1],
[1,-1, 1],
[0, 1, 0]])
target_group = [(1, 1), (1, 2)]
group = capture_group(1, 1, board)
self.assertEqual(group, target_group)
target_group = [(1, 2), (1, 1)]
group = capture_group(1, 2, board)
self.assertEqual(group, target_group)
def test_taking_group_stones_sides(self):
board = np.array(
[[-1, 1, 0],
[ 1, 0, 0],
[ 0, 0, 0]])
target_group = [(0, 0)]
group = capture_group(0, 0, board)
self.assertEqual(group, target_group)
board = np.array(
[[-1,-1, 1],
[ 1, 1, 0],
[ 0, 0, 0]])
target_group = [(0, 0), (1, 0)]
group = capture_group(0, 0, board)
self.assertEqual(group, target_group)
target_group = [(1, 0), (0, 0)]
group = capture_group(1, 0, board)
self.assertEqual(group, target_group)
def test_taking_group_sucide(self):
board = np.array(
[[-1, 1, 0],
[ 1, 0, 0],
[ 0, 0, 0]])
target_group = [(0, 0)]
group = capture_group(0, 0, board)
self.assertEqual(group, target_group)
board = np.array(
[[-1,-1, 1],
[ 1, 1, 0],
[ 0, 0, 0]])
target_group = [(0, 0), (1, 0)]
group = capture_group(0, 0, board)
self.assertEqual(group, target_group)
target_group = [(1, 0), (0, 0)]
group = capture_group(1, 0, board)
self.assertEqual(group, target_group)
def test_circle_group(self):
board = np.array(
[[ 0, 1, 1, 1, 0],
[ 1,-1,-1,-1, 1],
[ 1,-1, 1,-1, 1],
[ 1,-1,-1,-1, 1],
[ 0, 1, 1, 1, 0]])
target_group = [(1, 1), (2, 1), (3, 1), (3, 2), (3, 3), (2, 3), (1, 3), (1, 2)]
self.assertEqual(len(target_group), 8)
for x, y in target_group:
group = capture_group(x, y, board)
self.assertEqual(sorted(group), sorted(target_group))
class TestBoardMethods(unittest.TestCase):
def test_self_sucide(self):
board, player = game_init()
make_play(0, 0, board) # black
make_play(1, 0, board) # white
make_play(8, 9, board) # black random
make_play(2, 1, board) # white
make_play(8, 8, board) # black random pos
make_play(3, 0, board) # white
# ○ ● . ● . .
# . . ● . . .
# . . . . . .
make_play(2, 0, board) # black sucides
self.assertEqual(board[0][0][1][0], 1) # white stone
self.assertEqual(board[0][0][1][1], 0) # was not taken
self.assertEqual(board[0][0][2][0], 0) # black stone
self.assertEqual(board[0][0][2][1], 0) # was taken
def test_legal_moves_ko(self):
board, player = game_init()
make_play(0, 0, board) # black
make_play(1, 0, board) # white
make_play(1, 1, board) # black
make_play(2, 1, board) # white
make_play(8, 8, board) # black random pos
make_play(3, 0, board) # white
# ○ ● . ● . .
# . ○ ● . . .
# . . . . . .
make_play(2, 0, board) # black captures_first
# ○ . ○ ● . .
# . ○ ● . . .
# . . . . . .
mask = legal_moves(board)
self.assertEqual(board[0][0][1][0], 0) # white stone
self.assertEqual(board[0][0][1][1], 0) # was taken
self.assertEqual(board[0][0][1][2], 1) # white stone was here
self.assertEqual(board[0][0][1][3], 0) # black stone was not here
self.assertEqual(mask[1], True)
def test_legal_moves_not_ko(self):
board, player = game_init()
make_play(0, 0, board) # black
make_play(1, 0, board) # white
make_play(1, 1, board) # black
make_play(2, 0, board) # white
make_play(2, 1, board) # black
make_play(8, 8, board) # white random pos
# ○ ● ● . . .
# . ○ ○ . . .
# . . . . . .
make_play(3, 0, board) # black captures_first
# ○ . . ○ . .
# . ○ ○ . . .
# . . . . . .
mask = legal_moves(board)
self.assertEqual(board[0][0][1][0], 0) # white stone 1
self.assertEqual(board[0][0][1][1], 0) # was taken
self.assertEqual(board[0][0][2][0], 0) # white stone 2
self.assertEqual(board[0][0][2][1], 0) # was taken
self.assertEqual(board[0][0][1][2], 1) # white stone 1 was here
self.assertEqual(board[0][0][1][3], 0) # black stone was not here
self.assertEqual(board[0][0][2][2], 1) # white stone 2 was here
self.assertEqual(board[0][0][2][3], 0) # black stone was not here
self.assertEqual(mask[1], False)
self.assertEqual(mask[2], False)
def test_full_board_capture(self):
size = conf['SIZE']
board, player = game_init()
for i in range(size*size - 2):
x, y = index2coord(i)
make_play(x, y, board) # black
make_play(0, size, board) # white pass
# ○ ○ ○ ○ ○ ○
# ○ ○ ○ ○ ○ ○
# ○ ○ ○ ○ ○ ○
# ○ ○ ○ ○ . .
make_play(0, size, board) # black pass
make_play(size -1, size - 1, board) # white corner
# ○ ○ ○ ○ ○ ○
# ○ ○ ○ ○ ○ ○
# ○ ○ ○ ○ ○ ○
# ○ ○ ○ ○ . ●
for i in range(size*size - 2):
x, y = index2coord(i)
self.assertEqual(board[0][y][x][0], 1) # black stone i
self.assertEqual(board[0][y][x][1], 0) # black stone i
self.assertEqual(board[0][size - 1][size - 1][0], 0) # white stone
self.assertEqual(board[0][size - 1][size - 1][1], 1) # white stone
self.assertEqual(board[0][size - 1][size - 2][0], 0) # empty
self.assertEqual(board[0][size - 1][size - 2][1], 0) # empty
make_play(size - 2, size - 1, board) # black
# ○ ○ ○ ○ ○ ○
# ○ ○ ○ ○ ○ ○
# ○ ○ ○ ○ ○ ○
# ○ ○ ○ ○ ○ .
for i in range(size*size - 1):
x, y = index2coord(i)
self.assertEqual(board[0][y][x][0], 0) # black stone i
self.assertEqual(board[0][y][x][1], 1) # black stone i (it's white's turn)
self.assertEqual(board[0][size - 1][size - 1][0], 0) # empty
self.assertEqual(board[0][size - 1][size - 1][1], 0) # empty
make_play(size - 1, size - 1, board) # white
# . . . . . .
# . . . . . .
# . . . . . .
# . . . . . ●
for i in range(size*size - 1):
x, y = index2coord(i)
self.assertEqual(board[0][y][x][0], 0) # empty
self.assertEqual(board[0][y][x][1], 0) # empty
self.assertEqual(board[0][size - 1][size - 1][0], 0) # white
self.assertEqual(board[0][size - 1][size - 1][1], 1) # white
def test_bug(self):
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ○ ○ ● ●
# ● ● ● ● ● ● . ● ●
# ● ● ● ● ● ● ○ ○ ○
size = conf['SIZE']
board, player = game_init()
for i in range(size*size):
x, y = index2coord(i)
if (x, y) in [(5, 6), (6, 6), (6, 8), (7, 8), (8, 8)]:
make_play(x, y, board) # black
make_play(0, size, board) # white pass
elif (x, y) in [(6, 7)]:
make_play(0, size, board) # black pass
make_play(0, size, board) # white pass
else:
make_play(0, size, board) # black pass
make_play(x, y, board) # white
make_play(0, size, board) # black pass
make_play(6, 7, board) # white
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● . . ● ●
# ● ● ● ● ● ● ● ● ●
# ● ● ● ● ● ● . . .
for i in range(size*size - 1):
x, y = index2coord(i)
if (x, y) in [(5, 6), (6, 6), (6, 8), (7, 8), (8, 8)]:
self.assertEqual(board[0][y][x][0], 0) # empty
self.assertEqual(board[0][y][x][1], 0) # emtpy
else:
self.assertEqual(board[0][y][x][0], 0) # white
self.assertEqual(board[0][y][x][1], 1) # white
class TestSymmetrydTestCase(unittest.TestCase):
def setUp(self):
size = conf['SIZE']
board, player = game_init()
policy = np.zeros((1, size * size + 1), dtype=np.float32)
self.board = board
self.size = size
self.policy = policy
board = self.board
for x, y in [(1, 1), (1, 2), (1, 3), (2, 3)]:
make_play(x, y, board) # black
make_play(0, size, board) # white pass
policy[0, x + y * size] = 1
policy[0, size * size] = -1 # Pass move
def test_id(self):
board = self.board
size = self.size
old_board = np.copy(board)
board = _id(board)
for i, j in zip(old_board.reshape(-1), board.reshape(-1)):
self.assertEqual(i, j)
policy = np.arange(size*size + 1)
old_policy = np.copy(policy)
policy = _id(policy)
for i, j in zip(old_policy.reshape(-1), policy.reshape(-1)):
self.assertEqual(i, j)
def test_left_diagonal(self):
board = self.board
size = self.size
should_be_ones = [(1, 1), (2, 1), (3, 1), (3, 2)] # Transposed
board = left_diagonal(board)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(board[0,y,x,0], 1)
self.assertEqual(board[0,y,x,1], 0)
else:
self.assertEqual(board[0,y,x,0], 0)
self.assertEqual(board[0,y,x,1], 0)
policy = self.policy
policy = reverse_left_diagonal(policy)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(policy[0, x + size * y], 1)
else:
self.assertEqual(policy[0, x + size * y], 0)
self.assertEqual(policy[0, size * size], -1)
def test_vertical_axis(self):
board = self.board
size = self.size
should_be_ones = [(7, 1), (7, 2), (7, 3), (6, 3)] # vertical_axis
board = vertical_axis(board)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(board[0,y,x,0], 1)
self.assertEqual(board[0,y,x,1], 0)
else:
self.assertEqual(board[0,y,x,0], 0)
self.assertEqual(board[0,y,x,1], 0)
policy = self.policy
policy = reverse_vertical_axis(policy)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(policy[0, x + size * y], 1)
else:
self.assertEqual(policy[0, x + size * y], 0)
self.assertEqual(policy[0, size * size], -1)
def test_right_diagonal(self):
board = self.board
size = self.size
should_be_ones = [(7, 7), (6, 7), (5, 7), (5, 6)] # right diagonal
board = right_diagonal(board)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(board[0,y,x,0], 1)
self.assertEqual(board[0,y,x,1], 0)
else:
self.assertEqual(board[0,y,x,0], 0)
self.assertEqual(board[0,y,x,1], 0)
policy = self.policy
policy = reverse_right_diagonal(policy)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(policy[0, x + size * y], 1)
else:
self.assertEqual(policy[0, x + size * y], 0)
self.assertEqual(policy[0, size * size], -1)
def test_horizontal_axis(self):
board = self.board
size = self.size
should_be_ones = [(1, 7), (1, 6), (1, 5), (2, 5)] # horizontal_axis
board = horizontal_axis(board)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(board[0,y,x,0], 1)
self.assertEqual(board[0,y,x,1], 0)
else:
self.assertEqual(board[0,y,x,0], 0)
self.assertEqual(board[0,y,x,1], 0)
policy = self.policy
policy = reverse_horizontal_axis(policy)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(policy[0, x + size * y], 1)
else:
self.assertEqual(policy[0, x + size * y], 0)
self.assertEqual(policy[0, size * size], -1)
def test_rotation_90(self):
board = self.board
size = self.size
should_be_ones = [(1, 7), (2, 7), (3, 7), (3, 6)] # Rotation 90deg anticlockwise
board = rotation_90(board)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(board[0,y,x,0], 1)
self.assertEqual(board[0,y,x,1], 0)
else:
self.assertEqual(board[0,y,x,0], 0)
self.assertEqual(board[0,y,x,1], 0)
policy = self.policy
policy = reverse_rotation_90(policy)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(policy[0, x + size * y], 1)
else:
self.assertEqual(policy[0, x + size * y], 0)
self.assertEqual(policy[0, size * size], -1)
def test_rotation_180(self):
board = self.board
size = self.size
should_be_ones = [(7, 7), (7, 6), (7, 5), (6, 5)] # Rotation 180deg anticlockwise
board = rotation_180(board)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(board[0,y,x,0], 1)
self.assertEqual(board[0,y,x,1], 0)
else:
self.assertEqual(board[0,y,x,0], 0)
self.assertEqual(board[0,y,x,1], 0)
policy = self.policy
policy = reverse_rotation_180(policy)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(policy[0, x + size * y], 1)
else:
self.assertEqual(policy[0, x + size * y], 0)
self.assertEqual(policy[0, size * size], -1)
def test_rotation_270(self):
board = self.board
size = self.size
should_be_ones = [(7, 1), (6, 1), (5, 1), (5, 2)] # Rotation 270deg anticlockwise
board = rotation_270(board)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(board[0,y,x,0], 1)
self.assertEqual(board[0,y,x,1], 0)
else:
self.assertEqual(board[0,y,x,0], 0)
self.assertEqual(board[0,y,x,1], 0)
policy = self.policy
policy = reverse_rotation_270(policy)
for x, y in itertools.product(range(size), repeat=2):
if (x, y) in should_be_ones:
self.assertEqual(policy[0, x + size * y], 1)
else:
self.assertEqual(policy[0, x + size * y], 0)
self.assertEqual(policy[0, size * size], -1)
class MCTSTestCase(unittest.TestCase):
def setUp(self):
# Remove the symmetries for reproductibility
import symmetry
symmetry.SYMMETRIES = symmetry.SYMMETRIES[0:1]
tree = {
'count': 0,
'mean_value': 0,
'value': 0,
'parent': None,
'move': 1,
'subtree': {
0:{
'count': 0,
'p': 1,
'value': 0,
'mean_value': 0,
'move': 2,
'subtree': {}
},
1: {
'count': 0,
'p': 0,
'mean_value': 0,
'value': 0,
'move': 2,
'subtree': {}
}
}
}
tree['subtree'][0]['parent'] = tree
tree['subtree'][1]['parent'] = tree
board, player = game_init()
model = DummyModel()
self.model = model
self.board = board
self.tree = tree
def test_leaf(self):
tree = self.tree
board = self.board
model = self.model
simulate(tree, board, model, mcts_batch_size=2, original_player=1)
self.assertEqual(tree['subtree'][0]['count'], 1)
self.assertEqual(tree['subtree'][1]['count'], 1)
self.assertEqual(tree['subtree'][0]['value'], -1)
self.assertEqual(tree['subtree'][1]['value'], -1)
self.assertEqual(tree['count'], 2)
self.assertEqual(tree['value'], -2)
self.assertEqual(tree['mean_value'], -1)
def test_model_evaluation(self):
tree = self.tree
board = self.board
size = conf['SIZE']
test_board1, player = game_init()
make_play(0, 0, test_board1)
test_board2, player = game_init()
make_play(1, 0, test_board2)
class DummyModel(object):
def predict_on_batch(_, X):
size = conf['SIZE']
board1 = X[0].reshape(1, size, size, 17)
board2 = X[1].reshape(1, size, size, 17)
self.assertTrue(np.array_equal(board1, test_board1))
self.assertTrue(np.array_equal(board2, test_board2))
batch_size = X.shape[0]
policy = np.zeros((batch_size, size * size + 1), dtype=np.float32)
policy[:,0] = 1
value = np.zeros((batch_size, 1), dtype=np.float32)
value[:] = 1
return policy, value
model = DummyModel()
simulate(tree, board, model, mcts_batch_size=2, original_player=1)
def test_model_evaluation_nested(self):
tree = {
'count': 0,
'mean_value': 0,
'value': 0,
'parent': None,
'move': 1,
'subtree':{
0:{
'count': 0,
'p': 1,
'value': 0,
'mean_value': 0,
'move': 2,
'subtree': {
1: { # <----- This will be checked first
'count': 0,
'p': 1,
'mean_value': 0,
'value': 0,
'move': 3,
'subtree': {},
},
2: { # <----- This will be checked second
'count': 0,
'p': 0,
'mean_value': 0,
'value': 0,
'move': 3,
'subtree': {},
}
}
},
1: {
'count': 0,
'p': 0,
'mean_value': 0,
'value': 0,
'move': 2,
'subtree': {},
}
}
}
tree['subtree'][0]['parent'] = tree
tree['subtree'][0]['subtree'][1]['parent'] = tree['subtree'][0]
tree['subtree'][0]['subtree'][2]['parent'] = tree['subtree'][0]
tree['subtree'][1]['parent'] = tree
board = self.board
test_board1, player = game_init()
make_play(0, 0, test_board1)
make_play(1, 0, test_board1)
test_board2, player = game_init()
make_play(0, 0, test_board2)
make_play(2, 0, test_board2)
class DummyModel(object):
def predict_on_batch(_, X):
size = conf['SIZE']
board1 = X[0].reshape(1, size, size, 17)
board2 = X[1].reshape(1, size, size, 17)
self.assertTrue(np.array_equal(board1, test_board1))
self.assertTrue(np.array_equal(board2, test_board2))
batch_size = X.shape[0]
policy = np.zeros((batch_size, size * size + 1), dtype=np.float32)
policy[:,0] = 1
value = np.zeros((batch_size, 1), dtype=np.float32)
value[:] = 1
return policy, value
model = DummyModel()
# Remove the symmetries for reproductibility
simulate(tree, board, model, mcts_batch_size=2, original_player =1)
def test_model_evaluation_other_nested(self):
tree = {
'count': 0,
'mean_value': 0,
'value': 0,
'parent': None,
'move': 1,
'subtree':{
0:{
'count': 0,
'p': 1,
'value': 0,
'mean_value': 0,
'move': 2,
'subtree': {},
},
1: {
'count': 0,
'p': 0,
'mean_value': 0,
'value': 0,
'move': 2,
'subtree': {
0: {
'count': 0,
'p': 0,
'mean_value': 0,
'value': 0,
'move': 3,
'subtree': {},
},
2: {
'count': 0,
'p': 1,
'mean_value': 0,
'value': 0,
'move': 3,
'subtree': {},
}
}
}
}
}
tree['subtree'][0]['parent'] = tree
tree['subtree'][1]['parent'] = tree
tree['subtree'][1]['subtree'][0]['parent'] = tree['subtree'][1]
tree['subtree'][1]['subtree'][2]['parent'] = tree['subtree'][1]
board = self.board
test_board1, player = game_init()
make_play(0, 0, test_board1)
test_board2, player = game_init()
make_play(1, 0, test_board2)
make_play(2, 0, test_board2)
class DummyModel(object):
def predict_on_batch(_, X):
size = conf['SIZE']
board1 = X[0].reshape(1, size, size, 17)
board2 = X[1].reshape(1, size, size, 17)
self.assertTrue(np.array_equal(board1, test_board1))
self.assertTrue(np.array_equal(board2, test_board2))
batch_size = X.shape[0]
policy = np.zeros((batch_size, size * size + 1), dtype=np.float32)
policy[:,0] = 1
value = np.zeros((batch_size, 1), dtype=np.float32)
value[:] = 1
return policy, value
model = DummyModel()
simulate(tree, board, model, mcts_batch_size=2, original_player=1)
def test_small_batch_size(self):
tree = self.tree
model = self.model
board = self.board
simulate(tree, board, model, mcts_batch_size=1, original_player=1)
self.assertEqual(tree['subtree'][0]['count'], 1)
self.assertEqual(tree['subtree'][0]['value'], -1)
self.assertNotEqual(tree['subtree'][0]['subtree'], {})
self.assertEqual(tree['subtree'][1]['count'], 0)
self.assertEqual(tree['subtree'][1]['value'], 0)
self.assertEqual(tree['subtree'][1]['subtree'], {})
def test_nested_selected(self):
model = self.model
board = self.board
tree = {
'count': 0,
'mean_value': 0,
'value': 0,
'parent': None,
'move': 1,
'subtree':{
0:{
'count': 0,
'p': 1,
'value': 0,
'mean_value': 0,
'move': 2,
'subtree': {
1: {
'count': 0,
'p': 0,
'mean_value': 0,
'value': 0,
'move': 3,
'subtree': {},
},
2: {
'count': 0,
'p': 1,
'mean_value': 0,
'value': 0,
'move': 3,
'subtree': {},
}
}
},
1: {
'count': 0,
'p': 0,
'mean_value': 0,
'value': 0,
'move': 2,
'subtree': {},
}
}
}
tree['subtree'][0]['parent'] = tree
tree['subtree'][0]['subtree'][1]['parent'] = tree['subtree'][0]
tree['subtree'][0]['subtree'][2]['parent'] = tree['subtree'][0]
tree['subtree'][1]['parent'] = tree
simulate(tree, board, model, mcts_batch_size=2, original_player=1)
self.assertEqual(tree['subtree'][0]['count'], 2)
self.assertEqual(tree['subtree'][0]['subtree'][1]['count'], 1)
self.assertEqual(tree['subtree'][0]['subtree'][2]['count'], 1)
self.assertEqual(tree['subtree'][1]['count'], 0)
self.assertEqual(tree['subtree'][0]['value'], 2)
self.assertEqual(tree['subtree'][0]['mean_value'], 1)
self.assertEqual(tree['subtree'][1]['value'], 0)
def test_nested_other_leaves(self):
model = self.model
board = self.board
tree = {
'count': 0,
'mean_value': 0,
'value': 0,
'parent': None,
'move': 1,
'subtree': {
0:{
'count': 0,
'p': .75,
'value': 0,
'mean_value': 0,
'move': 2,
'subtree': {}
},
1: {
'count': 0,
'p': .25,
'mean_value': 0,
'value': 0,
'move': 2,
'subtree': {
0: {
'count': 0,
'p': 1,
'mean_value': 0,
'value': 0,
'move': 3,
'subtree': {},
},
2: {
'count': 0,
'p': 0,
'mean_value': 0,
'value': 0,
'move': 3,
'subtree': {},
}
}
},
2:{
'count': 0,
'p': 0,
'value': 0,
'mean_value': 0,
'move': 2,
'subtree': {}
},
}
}
tree['subtree'][0]['parent'] = tree
tree['subtree'][1]['parent'] = tree
tree['subtree'][1]['subtree'][0]['parent'] = tree['subtree'][1]
tree['subtree'][1]['subtree'][2]['parent'] = tree['subtree'][1]
simulate(tree, board, model, mcts_batch_size=2, original_player=1)
self.assertEqual(tree['subtree'][0]['count'], 1)
self.assertEqual(tree['subtree'][0]['value'], -1)
self.assertEqual(tree['subtree'][1]['value'], 1)
self.assertEqual(tree['subtree'][1]['count'], 1)
self.assertEqual(tree['subtree'][1]['subtree'][0]['count'], 1)
self.assertEqual(tree['subtree'][1]['subtree'][0]['value'], 1)
self.assertEqual(tree['subtree'][1]['subtree'][2]['count'], 0)
self.assertEqual(tree['count'], 2)
self.assertEqual(tree['mean_value'], 0)
self.assertEqual(tree['subtree'][2]['count'], 0)
self.assertEqual(tree['subtree'][2]['subtree'], {})
class PlayTestCase(unittest.TestCase):
def setUp(self):
# Remove the symmetries for reproductibility
import symmetry
symmetry.SYMMETRIES = symmetry.SYMMETRIES[0:1]
from random import seed
seed(0)
def test_play(self):
model = DummyModel()
mcts_simulations = 8 # mcts batch size is 8 and we need at least one batch
game_data = play_game(model, model, mcts_simulations, conf['STOP_EXPLORATION'], self_play=True, num_moves=2)
winner = game_data['winner']
test_board1, player = game_init()
board = game_data['moves'][0]['board']
self.assertTrue(np.array_equal(board, test_board1)) # First board is empty
self.assertEqual(winner, -1) # White should win with 5.5 komi after 2 moves
for move, move_data in enumerate(game_data['moves'][::2]): # Black player lost
value_target = 1 if winner == move_data['player'] else -1
self.assertEqual(move_data['player'], 1)
self.assertEqual(value_target, -1)
for move, move_data in enumerate(game_data['moves'][1::2]): # White player won
value_target = 1 if winner == move_data['player'] else -1
self.assertEqual(move_data['player'], -1)
self.assertEqual(value_target, 1)