-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithm.py
More file actions
189 lines (142 loc) · 5.7 KB
/
algorithm.py
File metadata and controls
189 lines (142 loc) · 5.7 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
from queue import PriorityQueue
from collections import deque
import heapq
import pygame
class Algorithm:
def __init__(self) -> None:
pass
def hurestic_function(self, p1, p2):
"""
This willl be the hurestic function that would provide the
estimation of the distance b/w the nodes
"""
x1, y1 = p1
x2, y2 = p2
return abs(x1 - x2) + abs(y1 - y2)
def reconstruct_path(self, came_from, current, draw):
while current in came_from:
current = came_from[current]
current.make_path()
draw()
def dijkstra_algorithm(self, draw, grid, start, end):
"""This is Dijkstra's Algorithm for pathfinding"""
# Priority queue for the nodes to be explored
pq = [(0, start)]
came_from = {}
distances = {spot: float("inf") for row in grid for spot in row}
distances[start] = 0
visited = set()
while pq:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current_distance, current_spot = heapq.heappop(pq)
if current_spot in visited:
continue
visited.add(current_spot)
if current_spot == end:
self.reconstruct_path(came_from, end, draw)
end.make_end()
return True
for neighbor in current_spot.neighbors:
new_distance = current_distance + 1 # The cost for each step is 1
if new_distance < distances[neighbor]:
came_from[neighbor] = current_spot
distances[neighbor] = new_distance
heapq.heappush(pq, (new_distance, neighbor))
neighbor.make_open()
draw()
if current_spot != start:
current_spot.make_closed()
return False
def prim_algorithm(self, draw, grid, start, end):
"""This is an adaptation of Prim's Algorithm for pathfinding"""
# Initialize the priority queue (min-heap) and visited set
min_heap = [(0, start)]
came_from = {}
visited = {spot: False for row in grid for spot in row}
visited[start] = True
while min_heap:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current_cost, current = heapq.heappop(min_heap)
if current == end:
self.reconstruct_path(came_from, end, draw)
end.make_end()
return True
for neighbor in current.neighbors:
if not visited[neighbor]:
visited[neighbor] = True
came_from[neighbor] = current
heapq.heappush(
min_heap, (1, neighbor)
) # The cost is 1 for unweighted
neighbor.make_open()
draw()
if current != start:
current.make_closed()
return False
def bfs_algorithm(self, draw, grid, start, end):
"""This is the BFS Algorithm for pathfinding"""
queue = deque([start])
came_from = {}
visited = {spot: False for row in grid for spot in row}
visited[start] = True
while queue:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
current = queue.popleft()
if current == end:
self.reconstruct_path(came_from, end, draw)
end.make_end()
return True
for neighbor in current.neighbors:
if not visited[neighbor]:
came_from[neighbor] = current
queue.append(neighbor)
visited[neighbor] = True
neighbor.make_open()
draw()
if current != start:
current.make_closed()
return False
def a_star_algorithm(self, draw, grid, start, end):
"""This would be the actual A*Algorithm for the path finding"""
count = 0
open_set = PriorityQueue()
open_set.put((0, count, start))
came_from = {}
g_score = {spot: float("inf") for row in grid for spot in row}
g_score[start] = 0
f_score = {spot: float("inf") for row in grid for spot in row}
f_score[start] = self.hurestic_function(start.get_pos(), end.get_pos())
open_set_hash = {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_hash.remove(current)
if current == end:
self.reconstruct_path(came_from, current, draw)
end.make_end()
return True
for neighbor in current.neighbors:
temp_g_score = g_score[current] + 1
if temp_g_score < g_score[neighbor]:
came_from[neighbor] = current
g_score[neighbor] = temp_g_score
f_score[neighbor] = temp_g_score + self.hurestic_function(
neighbor.get_pos(), end.get_pos()
)
if neighbor not in open_set_hash:
count += 1
open_set.put((f_score[neighbor], count, neighbor))
open_set_hash.add(neighbor)
neighbor.make_open()
draw()
if current != start:
current.make_closed()
return False