-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_finding.py
273 lines (201 loc) · 7.74 KB
/
path_finding.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
import pygame
import heuristic
from queue import PriorityQueue
def build_path(parents, start, end):
"""
Iterate backwards from end to start
using the set parents.
"""
while end != start:
if not end:
break
end.make_path()
end = parents[end]
def a_star_path_finding(draw, grid, start, end, heuristic_func):
"""
A* path finding algorthim.
draw : lambda function. draw is called each iteration of the while loop.
grid : 2D list of Nodes
start : Node
end : Node
heuristic_func : The heuristic function takes in two arugments.
Both arugments are tuples (int, int). The function should return an integer.
return boolean True if path is found. False if path doesn't exist.
"""
count = 0
open_set = PriorityQueue()
open_set.put((0, count, start))
previous_parent = {}
# f(n) = g(n) + h(n)
g_score = {node: float("inf") for row in grid for node in row}
g_score[start] = 0
f_score = {node: float("inf") for row in grid for node in row}
f_score[start] = heuristic_func(
start.get_pos(), end.get_pos()) + g_score[start]
# Keeps track of whats in the open_set
open_set_tracker = {start}
while not open_set.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
# Retreives the node with the best f_score
current = open_set.get()[2]
open_set_tracker.remove(current)
if current == end:
build_path(previous_parent, start, end)
end.make_end()
start.make_start()
return True
for neighbor in current.get_neighbors():
current_to_neighbor_distance = g_score[current] + 1
if current_to_neighbor_distance < g_score[neighbor]:
previous_parent[neighbor] = current
g_score[neighbor] = current_to_neighbor_distance
f_score[neighbor] = g_score[neighbor] + \
heuristic_func(neighbor.get_pos(), end.get_pos())
if neighbor not in open_set_tracker:
count += 1
open_set.put((f_score[neighbor], count, neighbor))
open_set_tracker.add(neighbor)
neighbor.make_open()
draw()
if current != start:
current.make_closed()
return False
def dijkstra_shortest_path(draw, grid, start, end):
"""
draw: lambda function that is run each iteration to upgrade grid.
grid: 2D array containing Node objects.
start: Node object
end: Node object
"""
count = 0
open_set = PriorityQueue()
# Set distance for all nodes to infinity but start node.
distance_set = {node: float("inf") for row in grid for node in row}
distance_set[start] = 0
# Parent set for building path.
previous_parent = {}
# Start open set by placing start node in side.
# PriorityQueue by distance.
open_set.put((distance_set[start], count, start))
# Tracks whats in the open set.
open_set_tracker = {start}
while not open_set.empty():
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = open_set.get()[2]
open_set_tracker.remove(current)
if current == end:
build_path(previous_parent, start, end)
end.make_end()
start.make_start()
return True
for neighbor in current.get_neighbors():
current_to_neighbor = distance_set[current] + 1
if current_to_neighbor < distance_set[neighbor]:
distance_set[neighbor] = current_to_neighbor
previous_parent[neighbor] = current
if neighbor not in open_set_tracker:
count += 1
open_set.put((distance_set[neighbor], count, neighbor))
open_set_tracker.add(neighbor)
neighbor.make_open()
draw()
if current != start:
current.make_closed()
return False
class BFS_DATA:
"""
Holds the necessary data structures for Bi-Directional BFS.
"""
# Constructor
def __init__(self, grid, start):
self.count = 0
self.open_set = PriorityQueue()
self.distance_set = {node: float("inf")
for row in grid for node in row}
self.distance_set[start] = 0
self.open_set.put((self.distance_set[start], self.count, start))
self.open_set_tracker = {start}
self.previous_parent = {}
def get_open_set(self):
return self.open_set
def get_open_set_tracker(self):
return self.open_set_tracker
def get_distance_set(self):
return self.distance_set
def get_previous_parent(self):
return self.previous_parent
def get_count(self):
return self.count
def increment_count(self):
self.count = self.count + 1
def bi_directional_search(draw, grid, start, end):
"""
draw: lambda function that draws and updates the grid.
grid: 2D list of Nodes
start: Node
end: Node
"""
bfs_start = BFS_DATA(grid, start)
bfs_end = BFS_DATA(grid, end)
while not bfs_start.get_open_set().empty() and not bfs_end.get_open_set().empty():
previous, collision = bi_directional_search_helper(
start, bfs_start, bfs_end)
if collision:
# build path.
build_bi_directional_path(
bfs_start, bfs_end, previous, start, collision, end)
start.make_start()
end.make_end()
return True
previous, collision = bi_directional_search_helper(
end, bfs_end, bfs_start)
if collision:
build_bi_directional_path(
bfs_end, bfs_start, previous, end, collision, start)
start.make_start()
end.make_end()
return True
draw()
return False
def build_bi_directional_path(bfs_start, bfs_end, previous, start, collision, end):
"""
Builds the bi-directional path using start, collision, and end.
"""
build_path(bfs_end.get_previous_parent(), end, collision)
previous_parent = bfs_start.get_previous_parent()
previous_parent[previous] = collision
build_path(previous_parent, start, collision)
def bi_directional_search_helper(start, bfs_source, bfs_data):
"""
start: Node
bfs_source: BFS_DATA
bfs_data: BFS_DATA
Does one interation of a Bi-Directional BFS.
If it BFS visits a node in bfs_node. It found a collision.
"""
open_set_source = bfs_source.get_open_set()
open_set_tracker_source = bfs_source.get_open_set_tracker()
distance_set_source = bfs_source.get_distance_set()
previous_parent_source = bfs_source.get_previous_parent()
current = open_set_source.get()[2]
open_set_tracker_source.remove(current)
if current in bfs_data.get_open_set_tracker():
return None, current
for neighbor in current.get_neighbors():
current_to_neighbor = distance_set_source[current] + 1
if current_to_neighbor < distance_set_source[neighbor]:
distance_set_source[neighbor] = current_to_neighbor
previous_parent_source[neighbor] = current
if neighbor not in open_set_tracker_source:
bfs_source.increment_count()
open_set_source.put(
(distance_set_source[neighbor], bfs_source.get_count(), neighbor))
open_set_tracker_source.add(neighbor)
neighbor.make_open()
if current != start:
current.make_closed()
return current, None