-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinitial_tests.py
331 lines (261 loc) · 11.8 KB
/
initial_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
#!/usr/bin/env python
import os # for time functions
# import student's functions
from agent import *
smallboards = [((0, 0, 0, 0), (0, 2, 1, 0), (0, 1, 1, 1), (0, 0, 0, 0)),
((0, 1, 0, 0), (0, 1, 1, 0), (0, 1, 2, 1), (0, 0, 0, 2)),
((0, 0, 0, 0), (0, 2, 1, 0), (0, 1, 1, 1), (0, 1, 1, 0)),
((0, 1, 0, 0), (0, 2, 2, 0), (0, 1, 2, 1), (0, 0, 2, 2)),
((1, 0, 0, 2), (1, 1, 2, 0), (1, 1, 1, 1), (1, 2, 2, 2)),
((0, 1, 0, 0), (0, 1, 1, 0), (2, 2, 2, 1), (0, 0, 0, 2))]
bigboards = [((0, 0, 0, 0, 0, 0), (0, 0, 2, 2, 0, 0), (0, 1, 1, 2, 2, 0), (2, 2, 1, 2, 0, 0), (0, 1, 0, 1, 2, 0), (0, 0, 0, 0, 0, 0)),
((0, 0, 0, 0, 0, 0), (0, 0, 1, 2, 0, 0), (0, 1, 1, 1, 1, 0), (2, 2, 1, 2, 0, 0), (0, 1, 0, 1, 2, 0), (0, 0, 0, 0, 0, 0)),
((0, 0, 0, 0, 1, 0), (0, 0, 1, 1, 0, 0), (0, 1, 1, 1, 1, 0), (2, 2, 1, 2, 0, 0), (0, 2, 0, 1, 2, 0), (0, 0, 2, 2, 1, 0)),
((0, 0, 0, 0, 0, 0), (0, 0, 0, 2, 0, 0), (0, 1, 2, 2, 2, 0), (0, 2, 2, 2, 0, 0), (0, 1, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0)),
((0, 0, 0, 0, 0, 0), (0, 0, 0, 2, 0, 0), (0, 1, 2, 1, 1, 0), (0, 2, 2, 2, 0, 0), (0, 1, 0, 0, 0, 0), (0, 0, 0, 0, 0, 0))]
#Select what to test
test_compute_utility = True
test_alphabeta_min_node_1 = True
test_alphabeta_max_node_1 = True
test_minimax_min_node_1 = True
test_minimax_max_node_1 = True
test_alphabeta_min_node_2 = True
test_alphabeta_max_node_2 = True
test_minimax_min_node_2 = True
test_minimax_max_node_2 = True
test_caching_big = True
test_ordering = True
test_select_move_minimax = True
test_select_move_alphabeta = True
test_select_move_equal = True
if test_compute_utility:
##############################################################
print('Testing Utility')
correctvalues = [3, 3, 5, -2, 3, 0]
correct = 0
for i in range(0,len(smallboards)):
board = smallboards[i]
value1 = compute_utility(board, 1)
value2 = compute_utility(board, 2)
if (value1 == correctvalues[i] and value2 == correctvalues[i]*-1):
correct+=1
print("You computed correct utilities for {} of {} small boards".format(correct, len(correctvalues)))
if test_select_move_minimax:
correctmoves_1 = [(0,0),(2,3),(0,0),(3,0),(3,1), (0,3)]
correctmoves_2 = [(3,3),(0,0),(3,3),(0,2),(3,1),(0,0)]
correct = 0
for i in range(0,len(smallboards)):
board = smallboards[i]
value1 = select_move_minimax(board, 1, 6, 1)
value2 = select_move_minimax(board, 2, 6)
if (value1 == correctmoves_1[i] and value2 == correctmoves_2[i]):
correct+=1
print('Testing Minimax (with Depth Limit of 6)')
print("You computed correct minimax moves for {} of {} small boards".format(correct, len(correctmoves_1)))
if test_select_move_alphabeta:
correctmoves_1 = [(0,0),(2,3),(0,0),(3,0),(3,1), (0,3)]
correctmoves_2 = [(3,3),(0,0),(3,3),(0,2),(3,1),(0,0)]
correct = 0
for i in range(0,len(smallboards)):
board = smallboards[i]
value1 = select_move_alphabeta(board, 1, 6)
value2 = select_move_alphabeta(board, 2, 6)
if (value1 == correctmoves_1[i] and value2 == correctmoves_2[i]):
correct+=1
print('Testing Alphabeta (with Depth Limit of 6)')
print("You computed correct alphabeta moves for {} of {} small boards".format(correct, len(correctmoves_1)))
if test_select_move_equal:
correctmoves_1 = [(0,0),(2,3),(0,0),(3,0),(3,1)]
correctmoves_2 = [(3,3),(0,0),(3,3),(0,2),(3,1)]
correct = 0
for i in range(0,len(correctmoves_1)):
board = smallboards[i]
value1_minimax = select_move_minimax(board, 1, 6)
value2_minimax = select_move_minimax(board, 2, 6)
value1_ab = select_move_alphabeta(board, 1, 6)
value2_ab = select_move_alphabeta(board, 2, 6)
if (value1_minimax == value1_ab == correctmoves_1[i] and value2_minimax == value2_ab == correctmoves_2[i]):
correct+=1
print('Testing Minimax and Alphabeta Moves Equality (with Depth Limit of 6)')
print("You computed correct moves for {} of {} tests".format(correct, len(correctmoves_1)))
if test_caching_big:
print('Testing Caching Big')
check_1 = 0
check_2 = 0
for i in range(0,len(bigboards)):
start_time_1 = os.times()[0]
no_cache = select_move_alphabeta(bigboards[i], 1, 6)
end_time_1 = os.times()[0]
start_time_2 = os.times()[0]
with_cache = select_move_alphabeta(bigboards[i], 1, 6, 1)
end_time_2 = os.times()[0]
if (end_time_1 - start_time_1) >= (end_time_2 - start_time_2):
check_1 += 1
if (with_cache == no_cache):
check_2 += 1
print("State caching improved the time of your alpha-beta for {} of {} boards".format(check_1, len(bigboards)))
print("Move choice with and without caching is the same for {} of {} boards".format(check_2, len(bigboards)))
if test_ordering:
print('Testing Ordering')
check_1 = 0
check_2 = 0
for i in range(0,len(bigboards)):
start_time_1 = os.times()[0]
no_order = select_move_alphabeta(bigboards[i], 1, 6, 0, 0)
end_time_1 = os.times()[0]
start_time_2 = os.times()[0]
with_order = select_move_alphabeta(bigboards[i], 1, 6, 0, 1)
end_time_2 = os.times()[0]
if (end_time_1 - start_time_1) >= (end_time_2 - start_time_2):
check_1 += 1
if (with_order == no_order):
print(with_order)
print(no_order)
check_2 += 1
print("Node ordering improved the time of your alpha-beta for {} of {} boards".format(check_1, len(bigboards)))
if test_alphabeta_min_node_1:
print('Testing Alpha Beta Min Node - Player 1')
answers = [((2,4),-10),((1,1),-4),((3,0),-6),((0,1),-8),((5,2),-6)]
correct = 0
correctval = 0
for i in range(0,len(bigboards)):
board = bigboards[i]
color = 1
(move, value) = alphabeta_min_node(board, color, float("-Inf"), float("Inf"), 1, 0, 0)
answer = answers[i][0]
answer_value = answers[i][1]
if (answer[0] == move[0] and answer[1] == move[1]):
correct+=1
if (answer_value == value):
correctval+=1
print("You computed correct alpha-beta min moves for {} of {} boards".format(correct, len(bigboards)))
print("You computed correct alpha-beta min values for {} of {} boards".format(correctval, len(bigboards)))
if test_alphabeta_max_node_1:
print('Testing Alpha Beta Max Node - Player 1')
answers = [(),((5,5),8),((1,5),12),(),((3,4),4)]
correct = 0
correctval = 0
selected = [1,2,4] #some boards have moves that are tied in value
for i in selected:
board = bigboards[i]
color = 1
(move, value) = alphabeta_max_node(board, color, float("-Inf"), float("Inf"), 1, 0, 0)
answer = answers[i][0]
answer_value = answers[i][1]
if (answer[0] == move[0] and answer[1] == move[1]):
correct+=1
if (answer_value == value):
correctval+=1
print("You computed correct alpha-beta max moves for {} of {} boards".format(correct, len(selected)))
print("You computed correct alpha-beta max values for {} of {} boards".format(correctval, len(selected)))
if test_minimax_min_node_1:
##############################################################
# Must program some trees where we know cut set
##############################################################
print('Testing Minimax Min Node - Player 1')
answers = [((2,4),-10),((1,1),-4),((3,0),-6),((0,1),-8),((5,2),-6)]
correct = 0
correctval = 0
for i in range(0,len(bigboards)):
board = bigboards[i]
color = 1
(move, value) = minimax_min_node(board, color, 1, 0)
answer = answers[i][0]
answer_value = answers[i][1]
if (answer[0] == move[0] and answer[1] == move[1]):
correct+=1
if (answer_value == value):
correctval+=1
print("You computed correct minimax min moves for {} of {} boards".format(correct, len(bigboards)))
print("You computed correct minimax min values for {} of {} boards".format(correctval, len(bigboards)))
if test_minimax_max_node_1:
print('Testing Minimax Max Node - Player 1')
answers = [(),((5,5),8),((1,5),12),(),((3,4),4)]
correct = 0
correctval = 0
selected = [1,2,4] #some boards have moves that are tied in value
for i in selected:
board = bigboards[i]
color = 1
(move, value) = minimax_max_node(board, color, 1, 0)
answer = answers[i][0]
answer_value = answers[i][1]
if (answer[0] == move[0] and answer[1] == move[1]):
correct+=1
if (answer_value == value):
correctval+=1
print("You computed correct minimax max moves for {} of {} boards".format(correct, len(selected)))
print("You computed correct minimax max values for {} of {} boards".format(correctval, len(selected)))
if test_alphabeta_min_node_2:
print('Testing Alpha Beta Min Node - Player 2')
answers = [((3,0),-6),((5,5),-8),((1,5),-12),((5,2),-2),((3,4),-4)]
correct = 0
correctval = 0
for i in range(0,len(bigboards)):
board = bigboards[i]
color = 2
(move, value) = alphabeta_min_node(board, color, float("-Inf"), float("Inf"), 1, 0, 0)
answer = answers[i][0]
answer_value = answers[i][1]
if (answer[0] == move[0] and answer[1] == move[1]):
correct+=1
if (answer_value == value):
correctval+=1
print("You computed correct alpha-beta min moves for {} of {} boards".format(correct, len(bigboards)))
print("You computed correct alpha-beta min values for {} of {} boards".format(correctval, len(bigboards)))
if test_alphabeta_max_node_2:
print('Testing Alpha Beta Max Node - Player 2')
answers = [((0,0),0),((1,1),4),((3,0),6),((0,0),0),((5,2), 6),((0,0), 0)]
correct = 0
correctval = 0
selected = [1,2,4] #some boards have moves that are tied in value
for i in selected:
board = bigboards[i]
color = 2
(move, value) = alphabeta_max_node(board, color, float("-Inf"), float("Inf"), 1, 0, 0)
answer = answers[i][0]
answer_value = answers[i][1]
if (answer[0] == move[0] and answer[1] == move[1]):
correct+=1
if (answer_value == value):
correctval+=1
print("You computed correct alpha-beta max moves for {} of {} boards".format(correct, len(selected)))
print("You computed correct alpha-beta max values for {} of {} boards".format(correctval, len(selected)))
if test_minimax_min_node_2:
##############################################################
# Must program some trees where we know cut set
##############################################################
print('Testing Minimax Min Node - Player 2')
answers = [((3,0),-6),((5,5),-8),((1,5),-12),((5,2),-2),((3,4),-4)]
correct = 0
correctval = 0
for i in range(0,len(bigboards)):
board = bigboards[i]
color = 2
(move, value) = minimax_min_node(board, color, 1, 0)
answer = answers[i][0]
answer_value = answers[i][1]
if (answer[0] == move[0] and answer[1] == move[1]):
correct+=1
if (answer_value == value):
correctval+=1
print("You computed correct minimax min moves for {} of {} boards".format(correct, len(bigboards)))
print("You computed correct minimax min values for {} of {} boards".format(correctval, len(bigboards)))
if test_minimax_max_node_2:
print('Testing Minimax Max Node - Player 2')
answers = [((0,0),0),((1,1),4),((3,0),6),((0,0),0),((5,2), 6),((0,0), 0)]
correct = 0
correctval = 0
selected = [1,2,4] #some boards have moves that are tied in value
for i in selected:
board = bigboards[i]
color = 2
(move, value) = minimax_max_node(board, color, 1, 0)
answer = answers[i][0]
answer_value = answers[i][1]
if (answer[0] == move[0] and answer[1] == move[1]):
correct+=1
if (answer_value == value):
correctval+=1
print("You computed correct minimax max moves for {} of {} boards".format(correct, len(selected)))
print("You computed correct minimax max values for {} of {} boards".format(correctval, len(selected)))