-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.py
313 lines (222 loc) · 10.8 KB
/
maze.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
import random as rd
import numpy as np
import matplotlib.pyplot as pyplot
import time as t
from queue import PriorityQueue
#--- the only significant change is that you enter (item, value) with put and receive item with get ---#
class my_priority_queue(PriorityQueue):
def __init__(self):
PriorityQueue.__init__(self)
self.counter = 0
def put(self, item, priority):
PriorityQueue.put(self, (priority, self.counter, item))
self.counter += 1
def get(self, *args, **kwargs):
_, _, item = PriorityQueue.get(self, *args, **kwargs)
return item
#--- numpy aplicattion to color the maze by using arrays ---#
palletes = {"maze": np.array([[1.0, 1.0, 1.0], #--- 0 = white (path) ---#
[0.0, 0.0, 0.0], #--- 1 = black (wall) ---#
[0.0, 1.0, 0.0], #--- 2 = green (init) ---#
[1.0, 0.0, 0.0], #--- 3 = red (target) ---#
[1.0, 1.0, 0.0]])} #--- 4 = yellow (winning path) ---#
def create_image(boolean_array, palette_name):
return palletes[palette_name][boolean_array.astype(int)]
#-------------------------------- Maze Generator using Randomized Prim's Algorithm --------------------------------#
#--- every point in the grid is a wall that can turn into a path ---#
class Wall:
def __init__(self, x, y):
self.is_visited = False
self.position = [x, y]
self.is_path = False
self.neighbors = []
self.weight = rd.randint(1,100)
self.open_neighbors = 0
#--- simple function that returns all neighbors of one position in grid ---#
def gen_neighbors(self, x, y, grid):
lines = len(grid)
columns = len(grid[0])
#--- if it's on the border of the grid, we add two or three neighbors. Else, we add four neighbors ---#
if x == lines - 1:
self.neighbors.append(grid[x - 1][y]) #--- upper ---#
elif x == 0:
self.neighbors.append(grid[x + 1][y]) #--- lower ---#
else:
self.neighbors.append(grid[x - 1][y])
self.neighbors.append(grid[x + 1][y])
if y == columns - 1:
self.neighbors.append(grid[x][y - 1]) #--- left ---#
elif y == 0:
self.neighbors.append(grid[x][y + 1]) #--- right ---#
else:
self.neighbors.append(grid[x][y - 1])
self.neighbors.append(grid[x][y + 1])
#--- count the number of paths neighbors ---#
def count_open_neigh(self):
for neigh in self.neighbors:
if neigh.is_path:
self.open_neighbors += 1
#--- if it's a path, returns 0; if it's a wall, return 1 ---#
def draw(self):
if self.is_path:
return 0
else:
return 1
def make_maze(x, y):
wall_list = my_priority_queue()
#--- using three matrix, it was easier to localize whe points that I needed during the code ---#
grid = [[Wall(i, j) for j in range(y)] for i in range(x)] #--- to get track of all objects ---#
matrix = [[j for j in range(y)] for i in range(x)] #--- to be drawn ---#
matrix_weight = [[j for j in range(y)] for i in range(x)] #--- matrix with all objects weight ---#
for i in range(x):
for j in range(y):
matrix_weight[i][j] = grid[i][j].weight
#--- generating all neighbors for all walls on grid ---#
for i in range(x):
for j in range(y):
if i == 0 or i == x - 1 or j == 0 or j == y - 1: #--- if it's in the borders ---#
grid[i][j].is_visited = True
grid[i][j].gen_neighbors(i, j, grid)
#--- using Randomized Prim's Algorithm to create Maze ---#
first_cell = grid[1][1] #--- starting point ---#
first_cell.is_path = True
first_cell.is_visited = True
for neigh in first_cell.neighbors:
if not neigh.is_visited:
wall_list.put(neigh, neigh.weight)
while not wall_list.empty():
cell = wall_list.get()
cell.count_open_neigh()
if cell.open_neighbors <= 1: #--- if it has less than 1 adjacent neighbor that is a path ---#
cell.is_path = True
cell.is_visited = True
if cell.position[1] == 1 or cell.position[1] == y - 2: #--- exit must be on ther border and between maze's middle and bottom ---#
if cell.position[0] > np.floor(x/2):
last_cell = cell #--- the last cell that is in the border to turn into a path is the target point in the maze ---#
for neigh in cell.neighbors:
if not neigh.is_visited:
if neigh.open_neighbors <= 1:
wall_list.put(neigh, neigh.weight)
#--- drawing Maze as a matrix (0 - path; 1 - wall) ---#
for i in range(x):
for j in range(y):
matrix[i][j] = grid[i][j].draw()
matrix[1][0] = 2 #--- entrance point = green ---#
if last_cell.position[1] == 1: #--- exit point = red ---#
matrix[last_cell.position[0]][last_cell.position[1] - 1] = 3
else:
matrix[last_cell.position[0]][last_cell.position[1] + 1] = 3
#--- turning matrix into numpy array ---#
maze = np.array([np.array(i) for i in matrix])
#--- printing the size of maze (without the borders) ---#
print(" Real Size:", x-2,"x",y-2)
#--- ploting Maze ---#
image = create_image(boolean_array=maze, palette_name='maze')
pyplot.figure(figsize=(16, 8))
pyplot.imshow(image, interpolation='nearest')
pyplot.xticks([]), pyplot.yticks([])
pyplot.show()
return matrix, last_cell
#-------------------------------------- Path Finder using A* Search Algorithm --------------------------------------#
class Maze:
def __init__(self, matrix, position, father):
self.matrix = matrix
self.position = position
self.father = father
self.neighbors = []
self.distance = 0
#--- finding distance between current position in the maze and the exit ---#
def find_distance(self, target):
dX = abs(target[0] - self.position[0])
dY = abs(target[1] - self.position[1])
self.distance = dX + dY
#--- generating all neighbors for the current position in the maze ---#
def gen_neighbors(self, queue, maze_obj, target):
if self.matrix[self.position[0] + 1][self.position[1]] == 0:
enqueue_neighbor(queue,
maze_obj,
(self.position[0] + 1, self.position[1]),
target)
if self.matrix[self.position[0] - 1][self.position[1]] == 0:
enqueue_neighbor(queue,
maze_obj,
(self.position[0] - 1, self.position[1]),
target)
if self.matrix[self.position[0]][self.position[1] + 1] == 0:
enqueue_neighbor(queue,
maze_obj,
(self.position[0], self.position[1] + 1),
target)
if self.matrix[self.position[0]][self.position[1] - 1] == 0:
enqueue_neighbor(queue,
maze_obj,
(self.position[0], self.position[1] - 1),
target)
#--- unrolling all the path when it reaches the exit until it goes back to the start position ---#
def unroll(maze_obj):
movements = []
while maze_obj.father != 0:
father = maze_obj.father
movements.append(father.position)
maze_obj = father
return movements
#--- critical optimization that stops the code to look into a neighbors's maze if it's already a grandparent ---#
def is_grandparent(maze_obj, position):
grandparent = maze_obj.father
if grandparent != 0:
if grandparent.position == position:
return True
return False
#--- used to put neighbors into the maze-tree and to make it possible to use the actual maze as a argument ---#
def enqueue_neighbor(queue, maze_obj, position, target):
if not is_grandparent(maze_obj, position):
neighbor_obj = Maze(maze_obj.matrix, position, maze_obj)
neighbor_obj.find_distance(target)
queue.put(neighbor_obj, neighbor_obj.distance)
#--- test if it's reached the exit ---#
def is_goal(maze, target):
if maze.position[0] == target[0]:
if maze.position[1] == target[1]:
return True
return False
#--- auxiliar solve function that calls all other functions and return the list of movements when the exit is reached ---#
def solve_aux(queue, target):
maze_obj = queue.get()
if is_goal(maze_obj, target):
return unroll(maze_obj)
else:
maze_obj.gen_neighbors(queue, maze_obj, target)
return solve_aux(queue, target)
#--- main function of this code ---#
def path_finder(x, y):
start= t.time()
#--- plotting the initial maze with the start as a green square and exit as a red square ---#
print(" #--- Initial Maze ---#")
matrix, target = make_maze(x, y)
#--- maze-tree is a priority queue that make it possible for us to use the A* search algorithm ---#
maze_tree = my_priority_queue()
target = target.position
maze_obj = Maze(matrix, (1,1), 0)
maze_obj.find_distance(target)
maze_tree.put(maze_obj, maze_obj.distance)
#--- list of movements to get to the exit of the maze ---#
solution = solve_aux(maze_tree, target)[-1::-1]
#--- changing the main matrix paths to another color if they are in the winning path ---#
for i in solution:
matrix[i[0]][i[1]] = 4
matrix[1][0] = 4
matrix[target[0]][target[1]] = 4
if target[1] == 1:
matrix[target[0]][target[1] - 1] = 4
else:
matrix[target[0]][target[1] + 1] = 4
#--- turning matrix into numpy array ---#
maze = np.array([np.array(i) for i in matrix])
#--- printing final solution to the maze with the elapsed time ---#
print(" #--- Final Solution ---#")
image = create_image(boolean_array=maze, palette_name='maze')
pyplot.figure(figsize=(16, 8))
pyplot.imshow(image, interpolation='nearest')
pyplot.xticks([]), pyplot.yticks([])
pyplot.show()
print(" Elapsed Time: ", t.time() - start, "s")