-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_birdcage.py
215 lines (184 loc) · 5.88 KB
/
test_birdcage.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
from birdcage import *
from birdcage import _to_alpha, _to_numeric
import pytest
from sympy import Rational
"""
● - ● - ● ● Q
5 ○ ○ ○ ○ / | \
4 | ● ● ● | ● - ● - ●
3 ○ ○ ○ ○ | | |
2 | ● ● ● | ● - ● - ●
1 ○ ○ ○ ○ \ | /
● - ● - ● ● 0
A B C D E A B C D E
"""
def test_to_numeric():
assert _to_numeric("A1") == (1, 1)
assert _to_numeric("A2") == (1, 2)
assert _to_numeric("A3") == (1, 3)
def test_to_alpha():
assert _to_alpha(1, 1) == ("A1")
assert _to_alpha(1, 2) == ("A2")
assert _to_alpha(1, 3) == ("A3")
def test_is_valid_move():
assert not is_valid_move("A0")
assert is_valid_move("A1")
assert not is_valid_move("A2")
assert is_valid_move("A3")
assert not is_valid_move("A4")
assert is_valid_move("A5")
assert not is_valid_move("A6")
assert not is_valid_move("B0")
assert not is_valid_move("B1")
assert is_valid_move("B2")
assert not is_valid_move("B3")
assert is_valid_move("B4")
assert not is_valid_move("B5")
assert not is_valid_move("B6")
assert not is_valid_move("F2")
def test_valid_moves():
VALID_MOVES = {
"A1",
"A3",
"A5",
"B2",
"B4",
"C1",
"C3",
"C5",
"D2",
"D4",
"E1",
"E3",
"E5",
}
assert set(valid_moves(M=3)) == VALID_MOVES
def test_display_moves():
assert display_moves(["A5", "C5", "C3", "A1"]) == "A5, c5, C3, a1"
def test_game():
b = BridgIt()
for move in ["A1", "C3", "C1", "C5", "E1"]:
assert not b.white_has_won()
assert not b.black_has_won()
b = b.move(move)
assert b.white_has_won()
assert not b.black_has_won()
def test_bridg_it_long_game():
# from "Bridg-It – Beating Shannon’s Analog Heuristic" by Thomas Fisher
# https://www.minet.uni-jena.de//math-net/reports/sources/2009/09-07report.pdf
b = BridgIt()
moves = ["A5", "c5", "C3", "a1", "B4", "e3", "E1", "d2", "C1", "b2", "E5", "d4"]
for move in moves:
assert not b.white_has_won()
assert not b.black_has_won()
b = b.move(move)
print()
print(b)
assert not b.white_has_won()
assert b.black_has_won()
def test_birdcage_long_game():
bc = BirdCage()
moves = ["A5", "c5", "C3", "a1", "B4", "e3", "E1", "d2", "C1", "b2", "E5", "d4"]
for move in moves:
assert not bc.white_has_won()
assert not bc.black_has_won()
bc = bc.move(move)
print()
print(bc)
assert not bc.white_has_won()
assert bc.black_has_won()
def test_random():
bc = BirdCage()
r = Random()
move = r.play(bc)
bc.move(move)
print(bc)
def test_shannon_voltage_diffs():
bc = BirdCage(moves=["A5", "C5"])
s = Shannon(use_extra_resistors=False)
voltage_diffs = s._get_voltage_diffs(bc)
assert voltage_diffs["C3"] == Rational(129 - 58, 129)
def test_shannon_game_M3_no_pull_ups():
bc = BirdCage()
s = Shannon(use_extra_resistors=False)
moves = ["A5", "c5", "C3", "a1", "B4", "e3", "E1", "d2", "C1", "b2", "E5", "d4"]
for m1, m2 in zip(*[iter(moves)] * 2):
move = s.play(bc)
if move != m1:
# moves differ, but check their voltage differences are the same
# this checks that Shannon is consistent with the expected moves
voltage_diffs = s._get_voltage_diffs(bc)
assert voltage_diffs[move] == voltage_diffs[m1]
bc.move(m1)
bc.move(m2)
assert not bc.white_has_won()
assert bc.black_has_won()
def test_shannon_game_M3():
bc = BirdCage()
s = Shannon()
moves = ["A1", "c1", "C3", "a5", "B2", "e3", "E5", "d4", "D2", "e1", "B4", "c5"]
for m1, m2 in zip(*[iter(moves)] * 2):
move = s.play(bc)
if move != m1:
# moves differ, but check their voltage differences are the same
# this checks that Shannon is consistent with the expected moves
voltage_diffs = s._get_voltage_diffs(bc)
assert voltage_diffs[move] == voltage_diffs[m1]
bc.move(m1)
bc.move(m2)
assert not bc.white_has_won()
assert bc.black_has_won()
def test_shannon_game_M4():
# from "Bridg-It – Beating Shannon’s Analog Heuristic" by Thomas Fisher
# section 4.2
# note that the last two moves are different, but this doesn't change the result
bc = BirdCage(M=4)
s = Shannon()
moves = [
"A1",
"c1",
"C3",
"e3",
"E5",
"a7",
"A5",
"d4",
"C5",
"g5",
"G7",
"f6",
"E7",
"d6",
"F4",
"g3",
"G1",
"f2",
"C7",
"b6",
"D2", #"E1",
"e1", #"d2",
]
for m1, m2 in zip(*[iter(moves)] * 2):
move = s.play(bc)
assert move == m1
bc.move(m1)
bc.move(m2)
assert not bc.white_has_won()
assert bc.black_has_won()
def test_part_of_circuit_not_connected():
bc = BirdCage(moves=["E1", "E3", "D2", "B2", "D4", "B4", "E5"])
s = Shannon()
# following should not raise an error
s._get_voltage_diffs(bc)
s = Shannon(use_extra_resistors=False)
with pytest.raises(Exception):
s._get_voltage_diffs(bc)
def test_discrete_voltage_discrimination():
# There are circuits where the Shannon heuristic breaks down
# when implemented using a 10-bit analog to digital converter (like the Arduino).
# This example was found using find_min_delta.py.
bc = BirdCage(M=4, moves=["A1", "B4", "E3", "D6"])
s = Shannon(use_extra_resistors=False)
voltage_diffs = s._get_voltage_diffs(bc)
assert voltage_diffs["C1"] > voltage_diffs["G3"]
assert round((voltage_diffs["C1"] * 1024).evalf()) == round((voltage_diffs["G3"] * 1024).evalf())