-
Notifications
You must be signed in to change notification settings - Fork 0
/
go_board_tests.py
199 lines (174 loc) · 5.92 KB
/
go_board_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
import unittest
from hyper_params import *
from go_board import *
class TestGoBoard(unittest.TestCase):
def test_make_pos_from_matrix(self):
mat_pos = np.array([
[0, 2, 0, 0, 1],
[2, 1, 2, 0, 1],
[0, 1, 2, 0, 0],
[0, 2, 0, 1, 0],
[0, 0, 0, 0, 0]
])
pos = make_pos_from_matrix(mat_pos)
self.assertEqual(pos.shape, (2, BOARD_SIZE, BOARD_SIZE))
def test_make_liberties_in_place(self):
mat_pos = np.array([
[0, 2, 0, 0, 1],
[2, 1, 2, 0, 1],
[2, 1, 2, 0, 0],
[0, 2, 0, 1, 0],
[0, 0, 0, 0, 0]
])
pos = make_pos_from_matrix(mat_pos)
prev_pos = pos.copy()
make_liberties_in_place(pos, 2-1)
self.assertTrue(np.sum(prev_pos[0] != pos[0]) == 2)
def test_check_suicide(self):
mat_pos = self.mat_positions_from_string("""
-O--x
OxO--
O-O-O
-O-x-
-----
xxxxx
Oxxxx
--OO-
OxOOx
-O-OO
""")
moves = [(PLAYER_1, encode_action(2, 1)), (PLAYER_1, encode_action(4, 2))]
for pos, (player, action) in zip(mat_pos, moves):
new_pos = next_position(make_pos_from_matrix(pos), player, action)
self.assertTrue(new_pos is None)
def test_next_state(self):
mat_positions = [np.array([
[0, 2, 1, 0, 0],
[2, 1, 0, 1, 0],
[0, 2, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]), np.array([
[0, 2, 1, 0, 0],
[2, 0, 2, 1, 0],
[0, 2, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
])]
state = create_zero_state(0)
# set the last 2 states
state.pos[-2:] = np.array([make_pos_from_matrix(pos) for pos in mat_positions])
new_state, outcome = next_state(state, encode_action(3, 0))
self.assertTrue(new_state.player == 1)
self.assertTrue(outcome is None)
def test_next_state_take_all(self):
pos = self.mat_positions_from_string("""
OO-OO
OOOOO
OOOOO
OOOOO
OOOOO
""")[0]
new_pos = next_position(make_pos_from_matrix(pos), PLAYER_1, encode_action(0, 2))
print(to_pretty_print(new_pos))
def test_next_state_end_game(self):
mat_positions = [np.array([
[0, 2, 1, 1, 1],
[2, 1, 0, 0, 0],
[0, 2, 1, 1, 1],
[0, 0, 2, 2, 2],
[0, 0, 0, 2, 0]
]), np.array([
[0, 2, 1, 1, 1],
[2, 1, 0, 0, 0],
[0, 2, 1, 1, 1],
[0, 0, 2, 2, 2],
[0, 0, 0, 2, 0]
])]
state = create_zero_state(0)
# set the last 2 states
state.pos[-2:] = np.array([make_pos_from_matrix(pos) for pos in mat_positions])
new_state, outcome = next_state(state, encode_action(None))
self.assertTrue(new_state.player == 1)
self.assertTrue(outcome == OUTCOME_WIN_PLAYER_2)
def mat_positions_from_string(self, positions):
pos_numeric = positions.replace("O", "2") \
.replace("x", "1") \
.replace("-", "0")
mat_positions = np.array([[list(ln.strip()) for ln in p.strip().split("\n")]
for p in pos_numeric.split("\n\n")], dtype=int)
return mat_positions
def test_calc_game_outcome(self):
positons = """
OOOOO
OOOOO
OOxOO
OOOOO
OOOO-
OOOxO
-OOOO
OOxOO
OOOOO
OOOOx
OOOOx
OOOOO
xxOOO
xO-OO
OOOxO
x--O-
--O-x
Ox-OO
-xOxx
OOO-O
-xxx-
-OxOO
x-OO-
x-x-O
O--x-
"""
mat_positions = self.mat_positions_from_string(positons)
outcomes = [calc_game_outcome(make_pos_from_matrix(m)) for m in mat_positions]
self.assertTrue(outcomes == [OUTCOME_WIN_PLAYER_2, OUTCOME_WIN_PLAYER_2, OUTCOME_WIN_PLAYER_2, OUTCOME_WIN_PLAYER_2, OUTCOME_DRAW])
def test_dihedral_transformations(self):
mat_pos = self.mat_positions_from_string("""
-O--x
OxO--
O-O-O
-O-x-
-----
""")[0]
state = create_zero_state(0)
state.pos[-1:] = make_pos_from_matrix(mat_pos)
for i in range(8):
new_state, inverse_transform_func = sample_dihedral_transformation(state)
inv_state = apply_transform(new_state, inverse_transform_func)
self.assertTrue(np.equal(inv_state.pos, state.pos).all())
def test_valid_actions(self):
mat_positions = [np.array([
[0, 2, 1, 0, 0],
[2, 1, 0, 1, 0],
[0, 2, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]), np.array([
[0, 2, 1, 0, 0],
[2, 0, 2, 1, 0],
[0, 2, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
])]
state = create_zero_state(PLAYER_1)
# set the last 2 states
state.pos[-2:] = np.array([make_pos_from_matrix(pos) for pos in mat_positions])
self.assertTrue(state.pos.shape == (STATE_HIST_SIZE, 2, BOARD_SIZE, BOARD_SIZE))
valid_acts = maybe_valid_actions(state)
#print(valid_acts)
brd = np.zeros((BOARD_SIZE, BOARD_SIZE))
for act in valid_acts:
new_state, outome = next_state(state, act)
if new_state is not None and not is_pass_action(act):
brd[get_action_coords(act)] = 1
self.assertTrue(brd[1, 1] == 0)
#print(brd)
if __name__ == '__main__':
unittest.main()