-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
315 lines (263 loc) · 13.1 KB
/
main.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
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
import tkinter as tk
import random
from queue import PriorityQueue, Queue
class PathFindingGame:
def __init__(self, rows, columns, cell_size):
self.rows = rows
self.columns = columns
self.cell_size = cell_size
self.grid = [[None for _ in range(columns)] for _ in range(rows)]
self.rat_location = None
self.cheese_location = None
self.dx = [1, -1, 0, 0]
self.dy = [0, 0, 1, -1]
self.vis = [[None for _ in range(columns)] for _ in range(rows)]
self.obstacle_prob = 0.3 # Probability of a cell being an obstacle
self.delay = 0 # Delay in milliseconds
self.path_delay = 20 # Delay between cells in path visualization
self.ok=False
self.wall_color="darkgreen"
self.roads_color="lightgreen"
self.path_color="olive"
def create_grid(self):
for i in range(self.rows):
for j in range(self.columns):
# if self.ok is False:
if random.random() < self.obstacle_prob:
color = self.wall_color # Cell is an obstacle
else:
color = "white" # Cell is open
# else:
# if self.grid[i][j]["bg"] =="brown":
# color="brown"
# else:
# color="white"
# self.grid[i][j]=None
# self.tempgrid[i][j]=None
cell = tk.Canvas(root, width=self.cell_size, height=self.cell_size, bg=color, bd=1)
cell.grid(row=i, column=j)
cell.bind("<Button-1>", lambda event, row=i, col=j: self.cell_click(event, row, col))
self.grid[i][j] = cell
self.vis[i][j] = None
self.ok=True
def cell_click(self, event, row, col):
if self.rat_location is None:
self.grid[row][col].config(bg="gray")
self.rat_location = (row, col)
elif self.cheese_location is None:
self.grid[row][col].config(bg="gray")
self.cheese_location = (row, col)
def update_color_with_delay(self, point, color, delay):
row, col = point
if self.grid[row][col]["bg"]!="gray":
self.grid[row][col].after(delay, lambda: self.grid[row][col].config(bg=color))
def h1(self,curr):
return abs(curr[0] - self.cheese_location[0]) + abs(curr[1] - self.cheese_location[1])
# BBBBBBBBBBBBBBBBBBFFFFFFFFFFFFFFFFFFFFFFFFFSSSSSSSSSSSSSSSSSSSSSSSSS
def bfs(self):
queue = Queue()
queue.put(self.rat_location)
while not queue.empty():
current = queue.get()
if current == self.cheese_location:
path = []
temp = current
while temp != self.rat_location:
path.append(temp)
temp = self.vis[temp[0]][temp[1]]
path.pop(0)
path.reverse()
# Update the path with delay between cells
delay = self.path_delay
for point in path:
self.update_color_with_delay(point, self.path_color, delay)
delay += self.path_delay
return path
for i in range(4):
new_x = current[0] + self.dx[i]
new_y = current[1] + self.dy[i]
if 0 <= new_x < self.rows and 0 <= new_y < self.columns and \
self.grid[new_x][new_y]["bg"] != self.wall_color and self.vis[new_x][new_y] is None:
self.vis[new_x][new_y] = current
queue.put((new_x, new_y))
if self.grid[new_x][new_y]["bg"]!="gray":
self.update_color_with_delay((new_x, new_y), self.roads_color, 0) # Change color to indicate exploration
return []
# DDDDDDDDDDDDDDDDDDFFFFFFFFFFFFFFFFFFFFFFFFFSSSSSSSSSSSSSSSSSSSSSSSSS
def dfs(self,curr_x,curr_y):
self.vis[curr_x][curr_y]=(curr_x,curr_y)
path=[(curr_x,curr_y)]
if (curr_x,curr_y) == self.cheese_location:
return path
for i in range(4):
new_x = curr_x + self.dx[i]
new_y = curr_y + self.dy[i]
if 0 <= new_x < self.rows and 0 <= new_y < self.columns and \
self.grid[new_x][new_y]["bg"] != self.wall_color and self.vis[new_x][new_y] is None:
self.vis[new_x][new_y] = (curr_x,curr_y)
if self.grid[new_x][new_y]["bg"]!="gray":
self.update_color_with_delay((new_x, new_y), self.roads_color, 0) # Change color to indicate exploration
ppath=game.dfs(new_x,new_y)
if len(ppath)!=0:
return path+ppath
return []
# GGGGGGGGGGGGGGRRRRRRRRRRRRREEEEEEEEEEEEEEEEEEEEEEEDDDDDDDDDYYYYYYYYY
def greedy_best_first(self):
queue = PriorityQueue()
queue.put((0, self.rat_location)) # Initialize with the rat's location and priority 0
while not queue.empty():
_, current = queue.get() # Discard the priority, just need the node location
if current == self.cheese_location:
path = []
temp = current
while temp != self.rat_location:
path.append(temp)
temp = self.vis[temp[0]][temp[1]]
path.pop(0)
path.reverse()
# Update the path with delay between cells
delay = self.path_delay
for point in path:
self.update_color_with_delay(point, self.path_color, delay)
delay += self.path_delay
return path
for i in range(4):
new_x = current[0] + self.dx[i]
new_y = current[1] + self.dy[i]
if 0 <= new_x < self.rows and 0 <= new_y < self.columns and \
self.grid[new_x][new_y]["bg"] != self.wall_color and self.vis[new_x][new_y] is None:
self.vis[new_x][new_y] = current
# Calculate heuristic cost (Manhattan distance in this case)
heuristic_cost = self.h1((new_x,new_y))
queue.put((heuristic_cost, (new_x, new_y))) # Priority based on heuristic cost
if self.grid[new_x][new_y]["bg"]!="gray":
self.update_color_with_delay((new_x, new_y), self.roads_color, 0) # Change color to indicate exploration
return []
# AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
def A_star(self):
start = self.rat_location
goal = self.cheese_location
# Priority queue for A* algorithm
frontier = PriorityQueue()
frontier.put((0, start))
# Cost to reach each node
cost_so_far = {start: 0}
while not frontier.empty():
_, current = frontier.get()
if current == goal:
path = []
temp = current
while temp != start:
path.append(temp)
temp = self.vis[temp[0]][temp[1]]
path.reverse()
# Update the path with delay between cells
delay = self.path_delay
for point in path:
self.update_color_with_delay(point, self.path_color, delay)
delay += self.path_delay
return path
for i in range(4):
new_x = current[0] + self.dx[i]
new_y = current[1] + self.dy[i]
if 0 <= new_x < self.rows and 0 <= new_y < self.columns and \
self.grid[new_x][new_y]["bg"] != self.wall_color:
new_cost = cost_so_far[current] + 1
if (new_x, new_y) not in cost_so_far or new_cost < cost_so_far[(new_x, new_y)]:
cost_so_far[(new_x, new_y)] = new_cost
priority = new_cost + self.h1((new_x, new_y))
frontier.put((priority, (new_x, new_y)))
self.vis[new_x][new_y] = current
self.update_color_with_delay((new_x, new_y), self.roads_color, 0)
return []
#limiteddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd
def dfs_limited(self,curr_x,curr_y,curr_len,limit):
self.vis[curr_x][curr_y]=limit
path=[(curr_x,curr_y)]
if (curr_x,curr_y) == self.cheese_location:
return path
for i in range(4):
new_x = curr_x + self.dx[i]
new_y = curr_y + self.dy[i]
if 0 <= new_x < self.rows and 0 <= new_y < self.columns and \
self.grid[new_x][new_y]["bg"] != self.wall_color and self.vis[new_x][new_y] != limit and curr_len!=limit:
self.vis[new_x][new_y] = (curr_x,curr_y)
if self.grid[new_x][new_y]["bg"]!="gray":
self.update_color_with_delay((new_x, new_y), self.roads_color, 0) # Change color to indicate exploration
ppath=game.dfs_limited(new_x,new_y,curr_len+1,limit)
if len(ppath)!=0:
return path+ppath
return []
def find_bfs(self):
if self.rat_location is None or self.cheese_location is None:
tk.messagebox.showinfo("Error", "Please place the rat and the cheese before finding a path.")
return
path = self.bfs()
if not path:
tk.messagebox.showinfo("No Path", "No path found.")
def find_greedy(self):
if self.rat_location is None or self.cheese_location is None:
tk.messagebox.showinfo("Error", "Please place the rat and the cheese before finding a path.")
return
path = self.greedy_best_first()
if not path:
tk.messagebox.showinfo("No Path", "No path found.")
def find_dfs(self):
if self.rat_location is None or self.cheese_location is None:
tk.messagebox.showinfo("Error", "Please place the rat and the cheese before finding a path.")
return
path = self.dfs(self.rat_location[0],self.rat_location[1])
for point in path:
self.update_color_with_delay(point, self.path_color, self.delay)
self.delay += self.path_delay
if not path:
tk.messagebox.showinfo("No Path", "No path found.")
def find_A(self):
if self.rat_location is None or self.cheese_location is None:
tk.messagebox.showinfo("Error", "Please place the rat and the cheese before finding a path.")
return
path = self.A_star()
# for point in path:
# self.update_color_with_delay(point, self.path_color, self.delay)
# self.delay += self.path_delay
if not path:
tk.messagebox.showinfo("No Path", "No path found.")
def find_dfs_limited(self):
if self.rat_location is None or self.cheese_location is None:
tk.messagebox.showinfo("Error", "Please place the rat and the cheese before finding a path.")
return
l =2
while l < 10000:
path = self.dfs_limited(self.rat_location[0], self.rat_location[1], 0, l)
if path:
for point in path:
self.update_color_with_delay(point,self.path_color, self.delay)
self.delay += self.path_delay
break
l *= 2
if not path:
tk.messagebox.showinfo("No Path", "No path found.")
def reset(self):
for i in range(self.rows):
for j in range(self.columns):
if self.grid[i][j]["bg"]!= self.wall_color:
self.update_color_with_delay((i,j), "white", 0)
self.vis[i][j]=None
# Create the main window
root = tk.Tk()
root.title("Path-Finding Game")
game = PathFindingGame(rows=30, columns=40, cell_size=15)
game.create_grid()
find_path_button = tk.Button(root, text="bridth-fs", command=game.find_bfs)
find_path_button.grid(row=game.rows, column=0, columnspan=game.columns, pady=10)
find_path_button = tk.Button(root, text="depth-fs", command=game.find_dfs)
find_path_button.grid(row=game.rows, column=6, columnspan=game.columns, pady=10)
find_path_button = tk.Button(root, text="greedy-bs", command=game.find_greedy)
find_path_button.grid(row=game.rows, column=12, columnspan=game.columns, pady=10)
find_path_button = tk.Button(root, text="A*", command=game.find_A)
find_path_button.grid(row=game.rows, column=17, columnspan=game.columns, pady=10)
find_path_button = tk.Button(root, text="limited-dfs", command=game.find_dfs_limited)
find_path_button.grid(row=game.rows, column=22, columnspan=game.columns, pady=10)
find_path_button = tk.Button(root, text="Reset", command=game.reset)
find_path_button.grid(row=game.rows, column=29, columnspan=game.columns, pady=10)
root.mainloop()