-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.py
More file actions
169 lines (144 loc) · 4.26 KB
/
Copy pathalgorithm.py
File metadata and controls
169 lines (144 loc) · 4.26 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
import pygame
import time
import queue
from queue import PriorityQueue
from utils import manhattan_distance, Node, reconstruct_path, draw
import random
def generate_walls(draw, grid, size, start, end):
for line in grid:
for node in line:
if node != start and node != end:
if random.randint(0,9)<3:
node.make_blocked()
# THIS FUNCTION RUNS WHEN YOU PRESS SPACE IN THE VISUALISER
# IT IS THE SOUL OF THIS APP AND IS THE ONE RESPONSIBLE FOR RUNNING
# THE WHOLE ALGORITHM
def aStar_algorithm(draw, grid, start, end):
# pass the initial values
count = 0
open_set = PriorityQueue()
open_set.put((0, count, start))
came_from = {}
a_score = {node: float("inf") for row in grid for node in row}
a_score[start] = 0
b_score = {node: float("inf") for row in grid for node in row}
b_score[start] = manhattan_distance(start.get_pos(), end.get_pos())
open_set_hash = {start}
# this while loop runs as long as we have squares to check
while not open_set.empty():
# check for quit event
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = open_set.get()[2]
open_set_hash.remove(current)
# if the current node is the end than reconstruct path and stop
if current == end:
end.make_end()
reconstruct_path(came_from, end, draw, len(grid), grid, start)
end.make_end()
start.make_start()
return True
# this is how we make the decision depending on the manhattan distace from start to end
for neighbor in current.neighbors:
temp_a_score = a_score[current] + 1
if temp_a_score < a_score[neighbor]:
came_from[neighbor] = current
a_score[neighbor] = temp_a_score
b_score[neighbor] = temp_a_score + manhattan_distance(neighbor.get_pos(), end.get_pos())
if neighbor not in open_set_hash:
count += 1
open_set.put((b_score[neighbor], count, neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
# refresh the screen after each iteration
pygame.display.update()
size = len(grid)
if size<11:
time.sleep(0.06)
elif size<20:
time.sleep(0.03)
else:
time.sleep(0.01)
draw()
if current != start:
current.make_closed()
return False
def Dijkstra_algorithm(draw, grid, start, end):
open_set = PriorityQueue()
open_set.put((0, start))
came_from = {}
f_score = {spot: float("inf") for row in grid for spot in row}
f_score[start] = 0
open_set_hash = {start}
while not open_set.empty():
current = open_set.get()[1]
open_set_hash.remove(current)
if current == end:
end.make_end()
reconstruct_path(came_from, end, draw, len(grid), grid, start)
end.make_end()
start.make_start()
return True
for neighbor in current.neighbors:
temp_f_score = f_score[current] + 1
if temp_f_score < f_score[neighbor]:
came_from[neighbor] = current
f_score[neighbor] = temp_f_score
if neighbor not in open_set_hash:
open_set.put((f_score[neighbor], neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
# refresh the screen after each iteration
pygame.display.update()
draw()
if current != start:
current.make_closed()
size = len(grid)
if size<11:
time.sleep(0.02)
elif size<20:
time.sleep(0.01)
else:
time.sleep(0.01)
return False
# ;-; does not work really
def bfs(draw, grid, start, end):
open_set = queue.Queue()
open_set.put((0, start))
came_from = {}
f_score = {spot: float("inf") for row in grid for spot in row}
open_set_hash = {start}
while not open_set.empty():
current = open_set.get()[1]
open_set_hash.remove(current)
if current == end:
end.make_end()
reconstruct_path(came_from, end, draw, len(grid), grid, start)
end.make_end()
start.make_start()
return True
for neighbor in current.neighbors:
temp_f_score = 0
if temp_f_score < f_score[neighbor]:
came_from[neighbor] = current
f_score[neighbor] = temp_f_score
if neighbor not in open_set_hash:
open_set.put((f_score[neighbor], neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
# refresh the screen after each iteration
start.make_start()
end.make_end()
pygame.display.update()
draw()
if current != start:
current.make_closed()
size = len(grid)
if size<11:
time.sleep(0.02)
elif size<20:
time.sleep(0.01)
else:
time.sleep(0.01)
return False