-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtests.py
More file actions
242 lines (197 loc) · 9.38 KB
/
tests.py
File metadata and controls
242 lines (197 loc) · 9.38 KB
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
'''
Team 4 - Ben Duggan & Connor Altic
12/9/18
Script used to test different AIs and output there data
'''
import time, csv
from Cube import *
from AIs import *
from Heuristic import *
# Test BFS
def BFS_Test(n, scramble_length, tests, outputs, output_name, time_out):
print('Testing BFS: n=' + str(n) + ', scramble_length: ' + str(scramble_length) + ', tests: ' + str(tests))
results = [['Trial', 'scrambleLength', 'scramblePattern', 'scrambleHash', 'obviousSolution', 'correct', 'solvedTime(sec)', 'aiPath']]
for i in range(tests):
c = Cube(n)
scramble_pattern = c.trueScramble(scramble_length)
scramble_hash = c.__hash__()
obvious_solution = Cube.obviousSolution(scramble_pattern)
ai = BFS(c)
start_time = time.time()
try:
ai_path = ai.solve(time_out)
# Run didn't time out!
run_time = time.time() - start_time
correct = ai_path[-1][1].isSolved()
print(ai_path[-1][1].state)
better_ai_path = []
for j in ai_path:
better_ai_path.append((j[0], j[1].state, j[1].__hash__()))
except Exception as e:
run_time = 'timed out (>' + str(time_out) + ' secs)'
better_ai_path = []
correct = False
print('Timed out')
print('\tTrial ' + str(i+1) + ': scramble_length: ' + str(scramble_length) + '; scramble_pattern: ' + str(scramble_pattern) + '; correct: ' + str(correct) + '; solved time: ' + str(run_time) + ' sec; ai_length: ' + str(len(better_ai_path)) + '; ai_path: ' + str(better_ai_path))
results.append([i+1, scramble_length, scramble_pattern, scramble_hash, obvious_solution, correct, run_time, better_ai_path])
if 'csv' in outputs:
file = open(output_name+'.csv', 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerows(results)
# Test better BFS
def Better_BFS_Test(n, scramble_length, tests, outputs, output_name, time_out):
print('Testing BBFS: n=' + str(n) + ', scramble_length: ' + str(scramble_length) + ', tests: ' + str(tests))
results = [['Trial', 'scrambleLength', 'scramblePattern', 'scrambleHash', 'obviousSolution', 'correct', 'solvedTime(sec)', 'aiPath']]
for i in range(tests):
c = Cube(n)
scramble_pattern = c.trueScramble(scramble_length)
scramble_hash = c.__hash__()
obvious_solution = Cube.obviousSolution(scramble_pattern)
ai = Better_BFS(c)
start_time = time.time()
try:
ai_path = ai.solve(time_out)
# Run didn't time out!
run_time = time.time() - start_time
correct = ai_path[-1][1].isSolved()
better_ai_path = []
for j in ai_path:
better_ai_path.append((j[0], j[1].state, j[1].__hash__()))
except Exception as e:
run_time = 'timed out (>' + str(time_out) + ' secs)'
better_ai_path = []
correct = False
print('Timed out')
print('\tTrial ' + str(i+1) + ': scramble_length: ' + str(scramble_length) + '; scramble_pattern: ' + str(scramble_pattern) + '; correct: ' + str(correct) + '; solved time: ' + str(run_time) + ' sec; ai_length: ' + str(len(better_ai_path)) + '; ai_path: ' + str(better_ai_path))
results.append([i+1, scramble_length, scramble_pattern, scramble_hash, obvious_solution, correct, run_time, better_ai_path])
if 'csv' in outputs:
file = open(output_name+'.csv', 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerows(results)
# Test A*
def A_star_Test(n, scramble_length, tests, outputs, output_name, time_out, heuristic):
print('Testing A*: n=' + str(n) + ', scramble_length: ' + str(scramble_length) + ', tests: ' + str(tests))
results = [['Trial', 'scrambleLength', 'scramblePattern', 'scrambleHash', 'obviousSolution', 'correct', 'solvedTime(sec)', 'aiPath']]
for i in range(tests):
c = Cube(n)
scramble_pattern = c.trueScramble(scramble_length)
scramble_hash = c.__hash__()
obvious_solution = Cube.obviousSolution(scramble_pattern)
ai = A_Star(c, heuristic)
start_time = time.time()
try:
ai_path = ai.solve(time_out)
# Run didn't time out!
run_time = time.time() - start_time
correct = ai_path[-1][1].isSolved()
better_ai_path = []
for j in ai_path:
better_ai_path.append((j[0], j[1].state, j[1].__hash__()))
except Exception as e:
run_time = 'timed out (>' + str(time_out) + ' secs)'
better_ai_path = []
correct = False
print('Timed out')
print('\tTrial ' + str(i+1) + ': scramble_length: ' + str(scramble_length) + '; scramble_pattern: ' + str(scramble_pattern) + '; correct: ' + str(correct) + '; solved time: ' + str(run_time) + ' sec; ai_length: ' + str(len(better_ai_path)) + '; ai_path: ' + str(better_ai_path))
results.append([i+1, scramble_length, scramble_pattern, scramble_hash, obvious_solution, correct, run_time, better_ai_path])
if 'csv' in outputs:
file = open(output_name+'.csv', 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerows(results)
# Test IDA*
def IDA_star_Test(n, scramble_length, tests, outputs, output_name, time_out, heuristic):
print('Testing IDA*: n=' + str(n) + ', scramble_length: ' + str(scramble_length) + ', tests: ' + str(tests))
results = [['Trial', 'scrambleLength', 'scramblePattern', 'scrambleHash', 'obviousSolution', 'correct', 'solvedTime(sec)', 'aiPath']]
for i in range(tests):
c = Cube(n)
scramble_pattern = c.trueScramble(scramble_length)
scramble_hash = c.__hash__()
obvious_solution = Cube.obviousSolution(scramble_pattern)
ai = IDA_Star(c, heuristic)
start_time = time.time()
try:
ai_path = ai.solve(time_out)
print(ai_path)
# Run didn't time out!
run_time = time.time() - start_time
correct = ai_path[-1][1].isSolved()
better_ai_path = []
for j in ai_path:
better_ai_path.append((j[0], j[1].state, j[1].__hash__()))
except Exception as e:
run_time = 'timed out (>' + str(time_out) + ' secs)'
better_ai_path = []
correct = False
print('Timed out')
print('\tTrial ' + str(i+1) + ': scramble_length: ' + str(scramble_length) + '; scramble_pattern: ' + str(scramble_pattern) + '; correct: ' + str(correct) + '; solved time: ' + str(run_time) + ' sec; ai_length: ' + str(len(better_ai_path)) + '; ai_path: ' + str(better_ai_path))
results.append([i+1, scramble_length, scramble_pattern, scramble_hash, obvious_solution, correct, run_time, better_ai_path])
if 'csv' in outputs:
file = open(output_name+'.csv', 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerows(results)
# Test Mini
def Mini_Test(n, scramble_length, tests, outputs, output_name, time_out, heuristic, depth):
print('Testing Mini: n=' + str(n) + ', scramble_length: ' + str(scramble_length) + ', tests: ' + str(tests))
results = [['Trial', 'scrambleLength', 'scramblePattern', 'scrambleHash', 'obviousSolution', 'correct', 'solvedTime(sec)', 'aiPath']]
for i in range(tests):
c = Cube(n)
scramble_pattern = c.trueScramble(scramble_length)
scramble_hash = c.__hash__()
obvious_solution = Cube.obviousSolution(scramble_pattern)
ai = Mini(c, heuristic)
start_time = time.time()
try:
ai_path = ai.solve(depth, time_out)
# Run didn't time out!
run_time = time.time() - start_time
correct = ai_path[-1][1].isSolved()
better_ai_path = []
for j in ai_path:
better_ai_path.append((j[0], j[1].state, j[1].__hash__()))
except Exception as e:
run_time = 'timed out (>' + str(time_out) + ' secs)'
better_ai_path = []
correct = False
print('Timed out')
print('\tTrial ' + str(i+1) + ': scramble_length: ' + str(scramble_length) + '; scramble_pattern: ' + str(scramble_pattern) + '; correct: ' + str(correct) + '; solved time: ' + str(run_time) + ' sec; ai_length: ' + str(len(better_ai_path)) + '; ai_path: ' + str(better_ai_path))
results.append([i+1, scramble_length, scramble_pattern, scramble_hash, obvious_solution, correct, run_time, better_ai_path])
if 'csv' in outputs:
file = open(output_name+'.csv', 'w', newline='')
with file:
writer = csv.writer(file)
writer.writerows(results)
if __name__ == '__main__':
tests = ['bfs', 'bbfs', 'a*', 'ida*', 'mini'] # Which tests do you want to perform
n = 2 # Cube size
start_scramble = 0 # Starting scramble
end_scramble = 14 # Ending scramble
num_tests = 5 # How many tests to run
output_types = ['csv'] # Output types (csv only option)
output_name = 'test1' # Name of the output file
start_depth = 1 # Mini start depth
end_depth = 5 # Mini end depth
if 'bfs' in tests:
print('BFS tests')
for i in range(start_scramble, end_scramble+1):
BFS_Test(n, i, num_tests, output_types, 'tests/BFS/'+str(n)+'x'+str(n)+'_scramble('+str(i)+')_'+output_name, 10*60)
if 'bbfs' in tests:
print('BBFS tests')
for i in range(start_scramble, end_scramble+1):
Better_BFS_Test(n, i, num_tests, output_types, 'tests/BBFS/'+str(n)+'x'+str(n)+'_scramble('+str(i)+')_'+output_name, 5*60)
if 'a*' in tests:
print('A* tests')
for i in range(start_scramble, end_scramble+1):
A_star_Test(n, i, num_tests, output_types, 'tests/A_Star/'+str(n)+'x'+str(n)+'_scramble('+str(i)+')_'+output_name, 5*60, Heuristic.hammingDistance)
if 'ida*' in tests:
print('IDA* tests')
for i in range(start_scramble, end_scramble+1):
IDA_star_Test(n, i, num_tests, output_types, 'tests/IDA_Star/'+str(n)+'x'+str(n)+'_scramble('+str(i)+')_'+output_name, 10*60, Heuristic.manhattanDistance)
if 'mini' in tests:
print('Mini tests')
for i in range(start_scramble, end_scramble+1):
for j in range(start_depth, end_depth+1):
Mini_Test(n, i, num_tests, output_types, 'tests/Mini/'+str(n)+'x'+str(n)+'_scramble('+str(i)+')_depth('+str(j)+')_'+output_name, 10*60, Heuristic.manhattanDistance, j)