-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
441 lines (342 loc) · 14.6 KB
/
main.py
File metadata and controls
441 lines (342 loc) · 14.6 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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
import pygame
import coordinates
import path_algorithms
# class for the gui of the application
class GUI():
# gui constants
FPS = 60
WIDTH = 590
HEIGHT = 590
# actual grid size
g_restrict_size = 575
def __init__(self, coords):
# gui variables
self.grid_size = 30
self.box_width = self.g_restrict_size / self.grid_size
self.coords = coords
self.placing_walls = False
self.removing_walls = False
self.animation_speed = 2
# start pygame application
pygame.init()
self.win = pygame.display.set_mode((self.WIDTH, self.HEIGHT)) # Updated window size
self.clock = pygame.time.Clock()
pygame.display.set_caption("Visualizing Pathfinding Algorithms")
# main function for gui
def main(self, running=False):
self.clock.tick(self.FPS)
self.mouse_x, self.mouse_y = pygame.mouse.get_pos()
# if the mouse button was pressed down continue placing obstacle
if not running:
if self.placing_walls == True:
self.place_wall()
elif self.removing_walls == True:
self.remove()
# get mouse and key presses
self.event_handle(running)
# redraw and update the display
self.redraw()
pygame.display.update()
# handles key and mouse presses
def event_handle(self, running):
run_keys = {"q", "w", "e", "r", "a", "t", "y","v"}
checkpoint_keys = {"1", "2"}
# gets key presses
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
exit()
# key presses
elif event.type == pygame.KEYDOWN:
key = chr(event.key)
if running == False:
# spacebar to generate blobs
if key == " ":
self.coords.generate_blobs(gui)
# if key == "`":
# self.coords.generate_crescents(gui)
# run algorithm
if key in run_keys: # q, w, e and r
self.run_algorithm(key)
# clear the whole board
elif key == "x":
self.coords.remove_all()
elif key == "l":
loop = dataLoop(5, "a", "v")
loop.theblobloop()
# place checkpoints with number keys
elif key in checkpoint_keys: # 1 and 2
self.place_check_point(key)
else:
# case to avoid our program crashing
print(key)
# increase speed of the pathfinding
elif (key == "+" or key == "=") and self.animation_speed > 0:
if self.animation_speed <= 2:
self.animation_speed = 1
else:
self.animation_speed = int(self.animation_speed * 0.5) + 1
# decrease speed of pathfinding
elif key == "-":
self.animation_speed = int(self.animation_speed * 2) + 1
else:
print(key)
# mouse button down
elif event.type == pygame.MOUSEBUTTONDOWN:
if running == False:
# place walls
if event.button == 1: # left down
self.placing_walls = True
# debugging: print("This works.")
# remove walls
elif event.button == 3: # right down
self.removing_walls = True
# cases for when the mouse button is lifted up
# otherwise it would keep it held down
elif event.type == pygame.MOUSEBUTTONUP:
# stop placing walls
if event.button == 1: # left up
self.placing_walls = False
# stop removing walls
elif event.button == 3: # right up
self.removing_walls = False
# redrawing the gui
# contains (and hides) most of our interface
def redraw(self):
# background color
self.win.fill((0, 40, 50))
self.draw_points()
# redrawing grids
self.draw_grid()
# screen borders
pygame.draw.rect(self.win, (10, 80, 80), (-2, 0, 22, self.HEIGHT)) # left
pygame.draw.rect(self.win, (10, 80, 80), (572, 0, 19, self.HEIGHT)) # right
pygame.draw.rect(self.win, (10, 80, 80), (0, 0, self.HEIGHT, 18)) # top
pygame.draw.rect(self.win, (10, 80, 80), (0, 573, self.HEIGHT, 20)) # bottom
# pygame.draw.rect(self.win,(0, 40, 50),(591,0,330, self.HEIGHT))
# draw the grid lines
def draw_grid(self):
for i in range(self.grid_size - 1):
# vertical grids
pygame.draw.rect(self.win, (10, 80, 80),
(((i + 1) * self.box_width - 2), 18, 2.8, self.g_restrict_size))
# horizontal
pygame.draw.rect(self.win, (10, 80, 80),
(20, ((i + 1) * self.box_width - 2), self.g_restrict_size, 2.8))
# draws all the squares for the walls, checkpoints ect
def draw_points(self):
# currently searching this box
for node in self.coords.open_list:
# self.draw_circle(self.box_center(node.position), int(self.box_width / 3), (0, 133, 109))
self.draw_box(node.position, (58, 255, 208))
# 58, 255, 208
# this box has already been searched
for node in self.coords.closed_list:
self.draw_box(node.position, (0, 133, 109))
# path found between point A and B
for wall in self.coords.final_path:
self.draw_box(wall, (244, 196, 2))
# obstacles
for wall in self.coords.obstacle:
self.draw_box(wall, (0, 0, 0))
# targets
for i, point in enumerate(self.coords.check_points):
if point != "None":
self.draw_box(point, (255, 78, 0))
self.display_text(str(i + 1), (255, 255, 255),
self.box_center(point), int(self.box_width))
# start
if self.coords.start != None:
self.draw_box(self.coords.start, (255, 0, 0))
self.display_text("S", (255, 255, 255),
self.box_center(self.coords.start), int(self.box_width))
# end
if self.coords.end != None:
self.draw_box(self.coords.end, (255, 0, 0))
self.display_text("E", (255, 255, 255),
self.box_center(self.coords.end), int(self.box_width))
# gets the center point of a node
def box_center(self, box):
boxX, boxY = box
center = ((boxX * self.box_width + (self.box_width / 2)),
(boxY * self.box_width + (self.box_width / 2)))
return center
# used to draw the boxed given colors and position
def draw_box(self, box, color):
boxX, boxY = box
pygame.draw.rect(self.win, color,
(boxX * self.box_width, boxY * self.box_width,
self.box_width, self.box_width))
def draw_circle(self, center, radius, color):
pygame.draw.circle(self.win, color, center, radius)
# getting the coordinates of clicked cells
# restricts the user and algorithm from accessing cells outside of the area
def get_box_coords(self):
adjusted_mouse_x = min(max(self.mouse_x - 1, 0), self.g_restrict_size - 1)
adjusted_mouse_y = min(max(self.mouse_y - 1, 0), self.g_restrict_size - 1)
# checking if the adjusted mouse position is within the leftmost column
if adjusted_mouse_x < self.box_width:
adjusted_mouse_x = self.box_width
# checking if the adjusted mouse position is within the top row
if adjusted_mouse_y < self.box_width:
adjusted_mouse_y = self.box_width
boxX = int(adjusted_mouse_x / self.box_width)
boxY = int(adjusted_mouse_y / self.box_width)
# returning the grid cell coordinates
return (boxX, boxY)
# placing checkpoints
def place_check_point(self, index):
coords = self.get_box_coords()
if (coords != self.coords.start and coords != self.coords.end
and coords not in self.coords.obstacle and coords
not in self.coords.check_points):
while len(self.coords.check_points) <= int(index) - 1:
self.coords.check_points.append("None")
self.coords.check_points[int(index) - 1] = coords
# placing obstacle
def place_wall(self):
coords = self.get_box_coords()
if (coords != self.coords.start and coords != self.coords.end
and coords not in self.coords.obstacle and coords
not in self.coords.check_points):
self.coords.obstacle.append(coords)
# removing nodes such as obstacle checkpoints ect
def remove(self):
coords = self.get_box_coords()
if coords in self.coords.obstacle:
self.coords.obstacle.remove(coords)
elif coords in self.coords.check_points:
self.coords.check_points.remove(coords)
elif coords == self.coords.start:
self.coords.start = None
elif coords == self.coords.end:
self.coords.end = None
# function that prepares for a pathfind and runs pathfind function
def run_algorithm(self, key, f=None):
self.placing_walls == False
self.removing_walls == False
self.coords.remove_last()
# for now we'll only have 1 checkpoint
# if we have 2 or more checkpoints
if len(self.coords.check_points) > 1:
# create the maze array and remove missed checkpoint numbers
self.coords.create_maze(gui)
check_points = self.coords.check_points[:]
check_points = [point for point in check_points if point != "None"]
# iterate through every checkpoint and pathfind to it
for i, point in enumerate(check_points):
if i != len(check_points) - 1:
start = point
end = check_points[i + 1]
new_path = path_algorithms.pathfind(self.coords.maze, start, end,
self, self.coords, key, f=f)
if new_path == None:
new_path = []
self.coords.final_path.extend(new_path)
# to show numbers (1 and 2)
def display_text(self, txt, color, center, size):
font = pygame.font.Font(None, size)
text_surf = font.render(txt, True, color)
text_rect = text_surf.get_rect()
text_rect.center = (center)
self.win.blit(text_surf, text_rect)
class dataLoop():
def __init__(self, l, k, k2):
self.loopfor = l
self.key = k
self.key2 = k2
def theblobloop(self):
f = open("resultsoutput.txt", "w")
f.write("Now comparing " + self.key + " and " + self.key2 + " for " + str(self.loopfor))
f.write("\n")
ind = 1
for x in range(self.loopfor):
# print("trial: ",ind)
f.write("trial: " + str(ind))
f.write("\n")
gui.coords.generate_blobs(gui)
f.write("alg " + self.key + " ")
# f.write("\n")
gui.run_algorithm(self.key, f=f)
f.write("alg " + self.key2 + " ")
# f.write("\n")
gui.run_algorithm(self.key2, f=f)
ind += 1
f.close()
k1, k2, kd1, kd2, lt1, lt2, ld1, ld2 = self.computeavg()
f = open("resultsoutput.txt", "a")
f.write("\n")
f.write("average for " + self.key + ": " + str(k1))
f.write("\n")
f.write(str(lt1))
f.write("\n")
f.write("average for " + self.key2 + ": " + str(k2))
f.write("\n")
f.write(str(lt2))
f.write("\n")
f.write("average for path length for " + self.key + ": " + str(kd1))
f.write("\n")
f.write(str(ld1))
f.write("\n")
f.write("average path length for " + self.key2 + ": " + str(kd2))
f.write("\n")
f.write(str(ld2))
f.close()
def thecurveloop(self):
pass
def has_numbers(self, inputString):
return any(char.isdigit() for char in inputString)
def computeavg(self):
f = open("resultsoutput.txt")
key1distvals = []
key2distvals = []
key1vals = []
key2vals = []
key1loc = "alg " + self.key + " "
key2loc = "alg " + self.key2 + " "
key1distloc = "algo " + self.key + " "
key2distloc = "algo " + self.key2 + " "
for line in f.readlines():
if key1loc in line and self.has_numbers(line) == True:
trash, trahs2, val = line.split(" ")
val = float(val.replace("\n", ""))
key1vals.append(val)
if key1distloc in line and self.has_numbers(line) == True:
trash, trahs2, tra3, tra4, val = line.split(" ")
val = float(val.replace("\n", ""))
key1distvals.append(val)
if key2loc in line and self.has_numbers(line) == True:
trash, trahs2, val = line.split(" ")
val = float(val.replace("\n", ""))
key2vals.append(val)
if key2distloc in line and self.has_numbers(line) == True:
trash, trahs2, tra3, tra4, val = line.split(" ")
val = float(val.replace("\n", ""))
key2distvals.append(val)
key1avg = None
key2avg = None
key1distavg = None
key2distavg = None
listtime1 = key1vals
listtime2 = key2vals
listdist1 = key1distvals
listdist2 = key2distvals
if (len(key1vals) > 0):
k1l = len(key1vals)
key1avg = sum(key1vals) / k1l
if (len(key2vals) > 0):
k2l = len(key2vals)
key2avg = sum(key2vals) / k2l
if (len(key1distvals) > 0):
k1dl = len(key1distvals)
key1distavg = sum(key1distvals) / k1dl
if (len(key2distvals) > 0):
k2dl = len(key2distvals)
key2distavg = sum(key2distvals) / k2dl
return key1avg, key2avg, key1distavg, key2distavg, listtime1, listtime2, listdist1, listdist2
f.close()
# main loop
if __name__ == "__main__":
gui = GUI(coordinates.Coordinates())
while True:
gui.main()