-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdfs_pathfinder.py
268 lines (202 loc) · 7.48 KB
/
dfs_pathfinder.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
# -*- coding: utf-8 -*-
"""
Created on Fri Sep 11 10:16:38 2020
@author: Raul Ortega Ochoa
"""
import pygame, time, argparse, csv
import numpy as np
from time import sleep
from numpy.random import randint
def is_in_map(pos, grid_dim):
"""
Parameters
----------
pos : tuple of 2 ints
x, y coordinates in the grid system of current
position
grid_dim : tuple of ints
x, y dimension of the grid system
Returns
true if pos in map
false if not in map
"""
(max_x, max_y) = grid_dim # unroll the dimensions
(x, y) = pos # unroll the position coordinates
x_in = (x <= max_x) & (x >= 0) # logical x in map
y_in = (y <= max_y) & (y >= 0) # logical y in map
return bool(x_in*y_in) # only true if both true
# ===========================
class Node:
def __init__(self, pos, parent):
self.x = pos[0]
self.y = pos[1]
self.parent = parent
def __getitem__(self):
return (self.x, self.y)
def position(self):
return (self.x, self.y)
class Queue:
def __init__(self):
self.Queue = []
def __len__(self):
return len(Queue)
def append(self, node):
self.Queue.append(node)
def get_node(self):
return self.Queue[-1]
def get_node_position(self):
node = self.Queue[-1]
return node.position()
def remove(self):
self.Queue = self.Queue[0:len(self.Queue)-1]
class DFS:
def __init__(self, start_pos, goal_pos, grid_dim):
self.grid_dim = grid_dim
self.start_pos = start_pos
self.goal_pos = goal_pos
self.explored = Queue()
self.frontier = Queue()
node = Node(pos=start_pos, parent=None)
self.frontier.append(node)
def backtrack_solution(self, grid, curr_node):
solution = []
while curr_node.parent != None:
solution.append(curr_node.position())
curr_node = curr_node.parent
return solution
def compute_successors(self, grid):
curr_node = self.frontier.get_node()
x, y = curr_node.position()
buffer_nodes = []
for movement in [(1,0), (-1,0), (0,1), (0,-1)]:
dx, dy = movement
new_pos = (x+dx, y+dy)
if (is_in_map(new_pos, self.grid_dim)) and (grid[new_pos[0], new_pos[1]] in [1, 3]) :
new_node = Node(pos=new_pos, parent=curr_node)
buffer_nodes.append(new_node)
if set(new_pos) == set(goal_pos):
self.explored.append(self.frontier.get_node())
self.frontier.remove()
done = True
solution = self.backtrack_solution(grid, curr_node=new_node)
return solution, done
self.explored.Queue.append(curr_node)
self.frontier.remove()
[self.frontier.Queue.append(new_node) for new_node in buffer_nodes]
done = False
return [], done
if __name__ == "__main__":
start_t0 = time.time()
# parsing user input
# example: python dfs_pathfinder.py --display=True --maze_file=maze_1.csv
parser = argparse.ArgumentParser()
parser.add_argument("--display", help="Display generating process 0: False, 1:True", default=1, type=int)
parser.add_argument("--maze_file", help="filename (csv) of the maze to load.", default="maze_1.csv", type=str)
args = parser.parse_args()
address = "mazes/" + args.maze_file
grid = np.genfromtxt(address, delimiter=',', dtype=int)
num_rows = len(grid)
num_columns = len(grid[0])
# define start, define goal
start_pos = (0,0)
goal_pos = (num_rows-1, num_columns-1)
# define start and goal
grid[0, 0] = 2
grid[-1, -1] = 3
grid_dim = (num_rows-1, num_columns-1)
if args.display == 1:
# define the two colors of the grid RGB
black = (0,0,0)
white = (255, 255, 255)
green = (50,205,50)
red = (255,99,71)
grey = (211,211,211)
blue = (153,255,255)
magenta = (255,0,255)
idx_to_color = [black, white, green, red, blue, magenta]
# set the height/width of each location on the grid
height = 7
width = height # i want the grid square
margin = 1 # sets margin between grid locations
# initialize pygame
pygame.init()
# congiguration of the window
WINDOW_SIZE = [330, 330]
screen = pygame.display.set_mode(WINDOW_SIZE)
pygame.display.set_caption(f"DFS Pathfinder. Solving: {address}")
# loop until done
done = False
run = False
close = False
clock = pygame.time.Clock() # to manage how fast the screen updates
dfs = DFS(start_pos=start_pos, goal_pos=goal_pos, grid_dim=grid_dim)
# main program
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# wait for user to press any key to start
elif event.type == pygame.KEYDOWN:
run = True
screen.fill(grey) # fill background in grey
for row in range(num_rows):
for column in range(num_columns):
color = idx_to_color[grid[row, column]]
pygame.draw.rect(screen, color,
[(margin + width) * column + margin,
(margin + height) * row + margin,
width, height])
clock.tick(60) # set limit to 60 frames per second
pygame.display.flip() # update screen
if run == True:
sleep(0.01)
solution, done = dfs.compute_successors(grid=grid)
explored = [node.position() for node in dfs.explored.Queue]
for pos in explored:
grid[pos[0], pos[1]] = 4
if done == True:
for pos in solution:
grid[pos[0], pos[1]] = 5
grid[0, 0] = 2
grid[-1, -1] = 3
screen.fill(grey) # fill background in grey
for row in range(num_rows):
for column in range(num_columns):
color = idx_to_color[grid[row, column]]
pygame.draw.rect(screen, color,
[(margin + width) * column + margin,
(margin + height) * row + margin,
width, height])
clock.tick(60) # set limit to 60 frames per second
pygame.display.flip() # update screen
print("Solved! Click exit.")
while not close:
for event in pygame.event.get():
if event.type == pygame.QUIT:
close = True
# wait for user to press any key to start
elif event.type == pygame.KEYDOWN:
close = True
pygame.quit()
else:
print(f"Pathfinder DFS. solving: {address}")
# loop until done
done = False
dfs = DFS(start_pos=start_pos, goal_pos=goal_pos, grid_dim=grid_dim)
# main program
while not done:
solution, done = dfs.compute_successors(grid=grid)
explored = [node.position() for node in dfs.explored.Queue]
for pos in explored:
grid[pos[0], pos[1]] = 4
# lets save result in csv
for pos in solution:
grid[pos[0], pos[1]] = 5
grid[0, 0] = 2
grid[-1, -1] = 3
# export maze to .csv file
with open(f"mazes_solutions/dfs_{args.maze_file}", "w", newline="") as f:
writer = csv.writer(f)
writer.writerows(grid)
print(f"--- finished {time.time()-start_t0:.3f} s---")
exit(0)