-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpathfinding.py
More file actions
199 lines (172 loc) · 5.99 KB
/
pathfinding.py
File metadata and controls
199 lines (172 loc) · 5.99 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
from enums import CellState, color_array
from queue import Queue, PriorityQueue
from time import sleep
import pygame
from dataclasses import dataclass, field
@dataclass
class PriorityIndex:
priority: int
index: list() = field(compare=False)
def __lt__(self, other):
return self.priority < other.priority
def pathfinding(mouse, grid):
#clear grid
for row in grid.cells:
for cell in row:
if cell.state >=5:
cell.set_state(CellState["CLEAR"])
#find start and endpoint
for i in range(len(grid.cells)):
for j in range(len(grid.cells[i])):
if grid.cells[i][j].state == CellState["START"]:
start = [i, j]
if grid.cells[i][j].state == CellState["END"]:
goal = [i, j]
#pipe it into one of the algorithms
if mouse.state == CellState["BREADTH"]:
__breadthsearch(grid, start, goal)
if mouse.state == CellState["DJ_ALGO"]:
__djsearch(grid, start, goal)
if mouse.state == CellState["GREEDY"]:
__greedysearch(grid, start, goal)
if mouse.state == CellState["ASTAR"]:
__astarsearch(grid, start, goal)
def __breadthsearch(grid, start, goal):
#init data structures
#ALWAYS USE INDEXES! NEVER THE ACTUAL CELLS
frontier = Queue()
came_from = dict()
frontier.put(start)
came_from[str(start)] = None
#main loop
while not frontier.empty():
current = frontier.get()
#early exit condition
if current == goal:
break
#color reached cell
grid.path_state(current, CellState["REACHED"])
for neighbor in grid.neighbors(current):
if str(neighbor) not in came_from:
#put in frontier
frontier.put(neighbor)
#color frontier
grid.path_state(neighbor, CellState["FRONTIER"])
#put in came from
came_from[str(neighbor)] = current
#reconstruct path
current = goal
path = []
while current != start:
path.append(list(current))
current = came_from[str(current)]
path.append(start)
path.reverse()
#color path
for index in path:
grid.path_state(index, CellState["PATH"])
return path
def __djsearch(grid, start, goal):
frontier = PriorityQueue()
came_from = dict()
cost_so_far = dict()
frontier.put(PriorityIndex(0, start))
came_from[str(start)] = None
cost_so_far[str(start)] = 0
#main loop
while not frontier.empty():
current = frontier.get().index
#early exit condition
if current == goal:
break
#color reached cell
grid.path_state(current, CellState["REACHED"])
for neighbor in grid.neighbors(current):
new_cost = cost_so_far[str(current)] + grid.cost(neighbor)
if str(neighbor) not in cost_so_far or new_cost < cost_so_far[str(neighbor)]:
#add to frontier
frontier.put(PriorityIndex(new_cost, neighbor))
#color frontier
grid.path_state(neighbor, CellState["FRONTIER"])
#put in reference dictionaries
cost_so_far[str(neighbor)] = new_cost
came_from[str(neighbor)] = current
#reconstruct path
current = goal
path = []
while current != start:
path.append(list(current))
current = came_from[str(current)]
path.append(start)
path.reverse()
for index in path:
grid.path_state(index, CellState["PATH"])
return path
def __greedysearch(grid, start, goal):
frontier = PriorityQueue()
came_from = dict()
frontier.put(PriorityIndex(0, start))
came_from[str(start)] = None
#main loop
while not frontier.empty():
current = frontier.get().index
#early exit condition
if current == goal:
break
#color reached cell
grid.path_state(current, CellState["REACHED"])
for neighbor in grid.neighbors(current):
if str(neighbor) not in came_from:
#add to frontier
priority = grid.heuristic(neighbor, goal)
frontier.put(PriorityIndex(priority, neighbor))
came_from[str(neighbor)] = current
#color frontier
grid.path_state(neighbor, CellState["FRONTIER"])
#reconstruct path
current = goal
path = []
while current != start:
path.append(list(current))
current = came_from[str(current)]
path.append(start)
path.reverse()
for index in path:
grid.path_state(index, CellState["PATH"])
return path
def __astarsearch(grid, start, goal):
frontier = PriorityQueue()
came_from = dict()
cost_so_far = dict()
frontier.put(PriorityIndex(0, start))
came_from[str(start)] = None
cost_so_far[str(start)] = 0
while not frontier.empty():
current = frontier.get().index
#early exit condition
if current == goal:
break
#color reached cell
grid.path_state(current, CellState["REACHED"])
for neighbor in grid.neighbors(current):
new_cost = cost_so_far[str(current)] + grid.cost(neighbor)
if str(neighbor) not in cost_so_far or new_cost < cost_so_far[str(neighbor)]:
#add to frontier
priority = new_cost + grid.heuristic(neighbor, goal)
frontier.put(PriorityIndex(priority, neighbor))
#color frontier
grid.path_state(neighbor, CellState["FRONTIER"])
#put in reference dictionaries
cost_so_far[str(neighbor)] = new_cost
came_from[str(neighbor)] = current
#reconstruct path
current = goal
path = []
while current != start:
path.append(list(current))
current = came_from[str(current)]
path.append(start)
path.reverse()
for index in path:
grid.path_state(index, CellState["PATH"])
return path