-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdemo.py
330 lines (260 loc) · 10.2 KB
/
demo.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
# Thomas Fiorilla
# File specifically for project demo to TA
import matplotlib.pyplot as plt # import class for making a plot
import board # import code for creating the board
import draw # import code for visually displaying the board on screen
import evaluate # import code for bfs
import hill # import code for hill-climbing
import AStarEval # import code for A* search
import genetic # import code for genetic algorithm
size = 16 # size of the array
numberPer = size/4 # with default 4 'n' sizes, number of sizes of each 'n'
boards = [None for x in range(size)] # array for 'size' board ojects
hills = [None for x in range(size)] # array for hill-climbing objects to make a more difficult board
gene = [None for x in range(size)] # array for genetic algorithm objects
bfs = [None for x in range(size)] # array for the BFS objects evaluating the original board
bfsHill = [None for x in range(size)] # array for the BFS objects evaluating the hill-climbing board
aStar = [None for x in range(size)] # array for the A* objects evaluating the original board
aStarHill = [None for x in range(size)] # array for the A* objects evaluating the hill-climbing board
sizeArray = [None for x in range(size)] # holds the 'n' sizes at their proper indices
iterations = 100 # number of iterations for hill climbing
markers = ["o", "v" , "^" , "<", ">", "o", "v" , "^" , "<", ">"]
colors = ['r','g','b','c','m', 'y', 'k', 'pink']
i = 0
j = 0
n = 5
# Make numberPer amount of boards of the four default sizes, create bfs objects for all of them, make hill-climbing objects for all of them, then iterate each object 50 times
while i < size:
while j < numberPer:
boards[i] = board.Board(n)
hills[i] = hill.Hill(boards[i]) # create hill-climbing objects
hills[i].climb(iterations) # iterate hill-climbing objects
bfs[i] = evaluate.evaluate(boards[i].boardBuilt, boards[i].boardSize) # create BFS objects to evaluate original board
bfsHill[i] = evaluate.evaluate(hills[i].puzzle.boardBuilt, hills[i].puzzle.boardSize) # create BFS object to evaluate hill-climbing board
gene[i] = genetic.Genetic(boards[i])
gene[i].run(bfs[i], iterations)
aStar[i] = AStarEval.AStarEval(boards[i].boardBuilt, boards[i].boardSize) # create A* objects to evaluate original board
aStarHill[i] = AStarEval.AStarEval(hills[i].puzzle.boardBuilt, hills[i].puzzle.boardSize) # create A* objects to evaluate hill-climbing board
sizeArray[i] = n
i += 1
j += 1
n += 2
j = 0
bfsScore = [n.value for n in bfs] # array with the bfs scores
bfsTime = [n.evalTime for n in bfs] # array with the bfs time evaluation
bfsTimeHill = [n.evalTime for n in bfsHill]
geneTime = [n.evalTime for n in gene] # array with genetic algorithm time evaluation
geneScore = [n.score for n in gene] #
hillScore = [n.score for n in hills] # array with the hill-climb scores
hillTime = [n.evalTime for n in hills] # array with the hill-climb time evaluation
scoreDif = [i - j for i, j in zip(hillScore, bfsScore)] # array with the difference between new score and original score
geneDif = [i - j for i, j in zip(geneScore, bfsScore)] #
aTime = [n.evalTime for n in aStar] # array with the evaluation times for A*, original board
aTimeHill = [n.evalTime for n in aStarHill] # array with the evaluation times for A*, hill-climb board
# Function to plot running time for the bfs
def plotBFS():
plt.subplot(121)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], bfsTime[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each BFS")
# Plot showing the score of each puzzle
plt.subplot(122)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], bfsScore[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Score")
plt.title("Score of each board")
plt.suptitle("Data for Breadth-First-Search")
plt.show()
plt.clf()
# Function to plot the running time for each hill-climb
def plotHill():
plt.subplot(221)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], hillTime[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each set of iterations")
# Plot the increase in difficulty
plt.subplot(222)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], scoreDif[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Score")
plt.title("Increase in score")
# Plot the original score
plt.subplot(223)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], bfsScore[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Score")
plt.title("Original score")
# Plot the ending score
plt.subplot(224)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], hillScore[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Score")
plt.title("New score")
plt.suptitle("Data for " + str(iterations) + " hill-climbing iterations")
plt.show()
plt.clf()
# Plot runtime of BFS vs runtime of A*
def plotComp():
plt.subplot(221)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], bfsTime[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each BFS, original")
#
plt.subplot(222)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], aTime[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each A*, original")
#
plt.subplot(223)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], bfsTimeHill[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each BFS, hill-climb")
#
plt.subplot(224)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], aTimeHill[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each A*, hill-climb")
plt.suptitle("BFS vs A* for Time")
plt.show()
plt.clf()
# Plot average runtime of BFS vs average runtime of A*
def plotCompAverage():
plt.subplot(221)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], bfsTime[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each BFS, original")
#
plt.subplot(222)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], aTime[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each A*, original")
#
plt.subplot(223)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], bfsTimeHill[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each BFS, hill-climb")
#
plt.subplot(224)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], aTimeHill[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each A*, hill-climb")
plt.suptitle("BFS vs A* for Time")
plt.show()
plt.clf()
def plotGene():
plt.subplot(221)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], geneTime[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Time (ms)")
plt.title("Time for each set of iterations")
# Plot the increase in difficulty
plt.subplot(222)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], geneDif[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Score")
plt.title("Increase in score")
# Plot the original score
plt.subplot(223)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], bfsScore[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Score")
plt.title("Original score")
# Plot the ending score
plt.subplot(224)
i = 0
while i < int(numberPer):
plt.plot(sizeArray[i::int(numberPer)], geneScore[i::int(numberPer)], color = colors[i], marker = markers[i], linestyle = 'none')
i += 1
plt.xlabel("Size of board, 'n'")
plt.ylabel("Score")
plt.title("New score")
plt.suptitle("Data for " + str(iterations) + " genetic algorithm iterations")
plt.show()
plt.clf()
# Create loop for user input
while True:
print()
userInput = input("Type '1' for BFS data, '2' for hill-climbing data, '3' to compare BFS to A*, '4' for genetic data, 'q' to exit. ")
if userInput == '1':
print("> BFS data displayed.")
print("> Time; " + str(bfsTime))
print("> Scores: " + str(bfsScore))
plotBFS()
elif userInput == '2':
print("> Hill-climbing data displayed.")
print("> Time; " + str(hillTime))
print("> Scores: " + str(hillScore))
plotHill()
elif userInput == '3':
print("> BFS vs A* displayed.")
#print("> BFS time; " + str(bfsTime))
#print("> A* time: " + str(aTime))
plotComp()
elif userInput == '4':
print("> Genetic algorithm data displayed.")
print("> Time; " + str(geneTime))
print("> Scores: " + str(geneScore))
plotGene()
elif userInput == 'q':
break