forked from machinaut/azero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_game.py
executable file
·197 lines (186 loc) · 9.02 KB
/
test_game.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
#!/usr/bin/env python
import random
import unittest
import numpy as np
from collections import defaultdict
from itertools import product
from util import sample_logits
from game import (games, Game,
Null, Binary, Flip, Count, Narrow, Matching, Roshambo,
Modulo, Connect3, MNOP)
N = 100
class TestGames(unittest.TestCase):
def test_random_play(self):
for game_cls in games:
game = game_cls()
self.assertIsInstance(game, Game)
for _ in range(N):
state, player, outcome = game.start()
while outcome is None:
# Verify incorrect player asserts valid()
with self.assertRaises(AssertionError):
game.valid(state, player + 1)
with self.assertRaises(AssertionError):
game.valid(state, player - 1)
# Verify incorrect player asserts view()
with self.assertRaises(AssertionError):
game.view(state, player + 1)
with self.assertRaises(AssertionError):
game.view(state, player - 1)
# Get a view for the current player
game.view(state, player)
# Get a valid mask for the current state
valid = game.valid(state, player)
# Check that at least one action is valid
self.assertGreater(sum(valid), 0)
# Draw a random action, maybe invalid
action = random.randrange(game.n_action)
# Verify that out-of-range action asserts step()
with self.assertRaises(AssertionError):
game.step(state, player, -1)
with self.assertRaises(AssertionError):
game.step(state, player, len(valid))
# Verify that improper player asserts step()
with self.assertRaises(AssertionError):
game.step(state, player + 1, action)
with self.assertRaises(AssertionError):
game.step(state, player - 1, action)
# Take the action, if invalid, raise assert
if valid[action]:
state, player, outcome = game.step(state, player, action)
else: # Verify that invalid action raises assert
with self.assertRaises(AssertionError):
game.step(state, player, action)
# End of game checks
self.assertIsNone(player)
def check_trajectory(self, game, traj, out):
state, player, outcome = game.start()
for action in traj:
self.assertIsNone(outcome)
state, player, outcome = game.step(state, player, action)
self.assertIsNotNone(outcome)
np.testing.assert_equal(outcome, out)
def test_trajectories(self):
self.check_trajectory(Null(), (), (0,))
self.check_trajectory(Binary(), (0,), (-1,))
self.check_trajectory(Binary(), (1,), (1,))
with self.assertRaises(AssertionError):
self.check_trajectory(Binary(), (2,), None)
self.check_trajectory(Flip(0), (0,), (-1,))
self.check_trajectory(Flip(0), (1,), (1,))
self.check_trajectory(Flip(1), (0,), (1,))
with self.assertRaises(AssertionError):
self.check_trajectory(Flip(0), (2,), None)
self.check_trajectory(Count(), (0, 1, 2), (1,))
self.check_trajectory(Count(), (0, 1, 1), (-1,))
self.check_trajectory(Count(), (0, 1, 0), (-1,))
self.check_trajectory(Count(), (1,), (-1,))
self.check_trajectory(Count(), (2,), (-1,))
with self.assertRaises(AssertionError):
self.check_trajectory(Count(), (3,), None)
self.check_trajectory(Narrow(), (0,), (-1,))
self.check_trajectory(Narrow(), (1, 0), (-1,))
self.check_trajectory(Narrow(), (2, 1, 0), (-1,))
self.check_trajectory(Narrow(), (2, 0), (-1,))
with self.assertRaises(AssertionError):
self.check_trajectory(Narrow(), (1, 1), None)
with self.assertRaises(AssertionError):
self.check_trajectory(Narrow(), (2, 2), None)
with self.assertRaises(AssertionError):
self.check_trajectory(Narrow(), (3,), None)
self.check_trajectory(Matching(), (0, 0), (-1, 1))
self.check_trajectory(Matching(), (0, 1), (1, -1))
self.check_trajectory(Matching(), (1, 0), (1, -1))
self.check_trajectory(Matching(), (1, 1), (-1, 1))
with self.assertRaises(AssertionError):
self.check_trajectory(Matching(), (2,), None)
self.check_trajectory(Roshambo(), (0, 0), (-1, -1))
self.check_trajectory(Roshambo(), (0, 1), (1, -1))
self.check_trajectory(Roshambo(), (0, 2), (-1, 1))
self.check_trajectory(Roshambo(), (1, 0), (-1, 1))
self.check_trajectory(Roshambo(), (1, 1), (-1, -1))
self.check_trajectory(Roshambo(), (1, 2), (1, -1))
self.check_trajectory(Roshambo(), (2, 0), (1, -1))
self.check_trajectory(Roshambo(), (2, 1), (-1, 1))
self.check_trajectory(Roshambo(), (2, 2), (-1, -1))
with self.assertRaises(AssertionError):
self.check_trajectory(Roshambo(), (3,), None)
self.check_trajectory(Modulo(), (0, 0, 0), (1, -1, -1))
self.check_trajectory(Modulo(), (0, 1, 0), (-1, 1, -1))
self.check_trajectory(Modulo(), (1, 0, 1), (-1, -1, 1))
self.check_trajectory(Modulo(), (2, 2, 2), (1, -1, -1))
self.check_trajectory(Modulo(), (2, 1, 1), (-1, 1, -1))
with self.assertRaises(AssertionError):
self.check_trajectory(Modulo(), (3,), None)
self.check_trajectory(Connect3(), (0, 1, 0, 1, 0), (1, -1))
self.check_trajectory(Connect3(), (0, 1, 0, 2, 4, 3), (-1, 1))
self.check_trajectory(Connect3(), (0, 1, 1, 2, 3, 2, 2), (1, -1))
self.check_trajectory(Connect3(), (1, 2, 0, 1, 0, 0), (-1, 1))
self.check_trajectory(MNOP(), (0, 3, 1, 4, 2), (1, -1))
self.check_trajectory(MNOP(), (0, 1, 4, 2, 8), (1, -1))
self.check_trajectory(MNOP(), (0, 1, 3, 2, 6), (1, -1))
self.check_trajectory(MNOP(), (2, 1, 4, 0, 6), (1, -1))
self.check_trajectory(MNOP(), (3, 0, 4, 1, 6, 2), (-1, 1))
self.check_trajectory(MNOP(), (3, 6, 1, 4, 5, 2), (-1, 1))
self.check_trajectory(MNOP(2, 2, 2), (0, 1, 2), (1, -1))
self.check_trajectory(MNOP(2, 2, 2), (0, 1, 3), (1, -1))
self.check_trajectory(MNOP(2, 2, 2), (0, 2, 1), (1, -1))
self.check_trajectory(MNOP(2, 2, 2), (1, 0, 2), (1, -1))
with self.assertRaises(AssertionError):
self.check_trajectory(MNOP(), (0, 0), None)
with self.assertRaises(AssertionError):
self.check_trajectory(MNOP(), (0, 1, 1), None)
def check_conditional_independence(self, data):
''' Ensure conditional independence of X and Y given Z '''
X = defaultdict(int)
Y = defaultdict(int)
Z = defaultdict(int)
XZ = defaultdict(int)
YZ = defaultdict(int)
XYZ = defaultdict(int)
for x, y, z in data:
X[x] += 1
Y[y] += 1
Z[z] += 1
XZ[(x, z)] += 1
YZ[(y, z)] += 1
XYZ[(x, y, z)] += 1
for x, y, z in XYZ.keys():
self.assertEqual(XYZ[(x, y, z)] * Z[z], XZ[(x, z)] * YZ[(y, z)])
for x, (y, z) in product(X.keys(), YZ.keys()):
if (x, y, z) not in XYZ:
assert (x, z) not in XZ or (y, z) not in YZ
for y, (x, z) in product(Y.keys(), XZ.keys()):
if (x, y, z) not in XYZ:
assert (x, z) not in XZ or (y, z) not in YZ
def check_dependence(self, data):
''' Ensure that Y depends entirely on X '''
X = dict()
for x, y in data:
if x not in X:
X[x] = y
self.assertEqual(X[x], y)
def test_statistics(self):
'''
Statistical Quantity tests
- Ensure valid doesn't give extra information about state
- Ensure that player wholly depends on state
'''
for game_cls in games:
game = game_cls()
state_valid_view = []
state_player = []
for _ in range(N):
state, player, outcome = game.start()
while outcome is None:
valid = game.valid(state, player)
view = game.view(state, player).tostring()
tstate = tuple(state)
state_valid_view.append((tstate, valid, view))
state_player.append((tstate, player))
action = sample_logits((0,) * len(valid), valid)
state, player, outcome = game.step(state, player, action)
self.check_conditional_independence(state_valid_view)
self.check_dependence(state_player)
if __name__ == '__main__':
unittest.main()