-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.py
331 lines (264 loc) · 9.1 KB
/
search.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
from collections import deque
from queue import PriorityQueue
import argparse
import copy
#takes maze txt files and converts them to arrays of strings (or 2D matrices, since we are programming in python)
def txtToMat(fname):
retM = []
with open(fname) as myfile:
for line in myfile:
lineStr = ""
line = line.strip()
for tok in line:
lineStr += tok
retM.append(lineStr)
return retM
#function to print the maze for visualization
def printMaze(mat):
for i in mat:
print(i)
#find our starting spot
def findStart(mat):
row = 0
for i in mat:
col = 0
row += 1
for j in i:
col += 1
if (j == 'P'):
return row - 1, col - 1
else:
continue
print("P wasn't found if you can see this...feels bad man")
#function to find the goal point
def findEnd(mat):
row = 0
for i in mat:
col = 0
row += 1
for j in i:
col += 1
if (j == '.'):
return (row - 1, col - 1)
else:
continue
print("P wasn't found if you can see this...feels bad man")
#function for safe transition for the frontier
def safeTransitions(mat, row, col, visited):
return ((mat[row][col] != '%') and (
(row, col) not in visited))
def findPathDFS(maze):
if maze == []:
return maze
strt = findStart(maze)
final = DFS(strt[0], strt[1], maze) # Go to DFS.
return final
def findPathBFS(maze):
if maze == []:
return maze
strt = findStart(maze)
final = BFS(strt[0], strt[1], maze) # Go to BFS.
return final
def findPathGBFS(maze):
if maze == []:
return maze
strt = findStart(maze)
final = GreedyBest(strt[0], strt[1], maze) # Go to Greedy Best.
return final
def findPathAstar(maze):
if maze == []:
return maze
strt = findStart(maze)
final = Astar(strt[0], strt[1], maze)
return final
def BFS(r, c, maze):
q = deque()
visited = set()
q.append([(r, c)])
while(len(q) != 0):
#possible solution (list)
current_list = q[0]
# Last coordinate in the list is the current state... We want to know where to go from there.
current_state = current_list[-1]
# popleft from the queue
q.popleft()
# get coordinates
row = current_state[0]
col = current_state[1]
#add current to visited set
visited.add(current_state)
# Found a possible goal state. Yes.
if (maze[row][col] == "."):
print("found baby", row, col)
return current_list
possible_actions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for coord in possible_actions:
dx = coord[0]
dy = coord[1]
if (safeTransitions(maze, row + dx, col + dy, visited)):
temp_list = current_list.copy()
temp_list.append((row + dx, col + dy))
q.insert(0, temp_list)
def DFS(r, c, maze):
visited = set()
stack = []
stack.append([(r, c)])
while (len(stack) != 0):
# Possible solution (a list)
current_list = stack[-1]
#print("current List:", current_list)
# Last coordinate in the list is the current state... We want to know where to go from there.
current_state = current_list[-1]
# pop from the stack
stack = stack[:len(stack) - 1]
# Get coordinates
row = current_state[0]
col = current_state[1]
# Add current state to the visited set
visited.add(current_state)
# yah = input("continue?") Credit to Ernest Quant for this debugging method
# Found a possible goal state. Yes.
if (maze[row][col] == "."):
print("found baby", row, col)
return current_list
possible_actions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for coord in possible_actions:
dx = coord[0]
dy = coord[1]
if (safeTransitions(maze, row + dx, col + dy, visited)):
temp_list = current_list.copy()
temp_list.append((row + dx, col + dy))
stack.insert(0, temp_list)
def ManhatDist(x1, y1, goal):
return (abs(x1 - goal[0]) + abs(y1-goal[1]))
'''
def PqAdd(pq, r, c, goal):
heuristic = (r, c, goal)
for counter in range(len(pq)):
if(heuristic < item[0]):
pq.insert(counter, ())'''
def GreedyBest(r, c, maze):
visited = set()
goal = findEnd(maze)
pq = [] #priority queue a list of solutions with the heuristics
heuristic = ManhatDist(r, c, goal)
pq.append(([(r,c)], heuristic))
while(len(pq) != 0):
#possible solution (list)
current_list = pq[0][0]
# Last coordinate in the list is the current state... We want to know where to go from there.
current_state = current_list[-1]
# popleft from the queue
pq.pop(0)
# get coordinates
row = current_state[0]
col = current_state[1]
#add current to visited set
visited.add(current_state)
# Found a possible goal state. Yes.
if (maze[row][col] == "."):
print("found baby", row, col)
return current_list
possible_actions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for coord in possible_actions:
dx = coord[0]
dy = coord[1]
if (safeTransitions(maze, row + dx, col + dy, visited)):
temp_list = current_list.copy()
temp_list.append((row + dx, col + dy))
temp_tup = (temp_list, ManhatDist(row+dx, col+dy, goal))
pq.append(temp_tup)
pq = sorted(pq, key=lambda x: x[1])
def Astar(r, c, maze):
visited = set()
goal = findEnd(maze)
pq = [] #priority queue a list of solutions with the heuristics
heuristic = ManhatDist(r, c, goal)
counter = 1
func = counter + heuristic
pq.append(([(r,c)], func))
while(len(pq) != 0):
#possible solution (list)
current_list = pq[0][0]
# Last coordinate in the list is the current state... We want to know where to go from there.
current_state = current_list[-1]
# popleft from the queue
pq.pop(0)
# get coordinates
row = current_state[0]
col = current_state[1]
#add current to visited set
visited.add(current_state)
# Found a possible goal state. Yes.
if (maze[row][col] == "."):
print("found baby", row, col)
return current_list
possible_actions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
counter += 1
for coord in possible_actions:
dx = coord[0]
dy = coord[1]
if (safeTransitions(maze, row + dx, col + dy, visited)):
temp_list = current_list.copy()
temp_list.append((row + dx, col + dy))
heur = ManhatDist(row+dx, col+dy, goal)
new_func = counter + heur
temp_tup = (temp_list, new_func)
pq.append(temp_tup)
pq = sorted(pq, key=lambda x: x[1])
def update_maze(maze, path):
#convert every string in the list into another list
stringLis = []
for row in maze:
stringLis.append(list(row))
# mark the spots that are in the solution (path)
for coord in path:
stringLis[coord[0]][coord[1]] = "1"
# maze with solution
updated = []
for i in stringLis:
updated.append("".join(i))
return updated
# you gotta print the maze a row at a time.
def print_maze(maze):
for row in maze:
print(row)
def main():
argScan = argparse.ArgumentParser()
argScan.add_argument("--method", required=True, help="mazeMethIn")
argScan.add_argument("mazeNameIn")
args = argScan.parse_args()
mazeMethod = args.method
print(mazeMethod)
mazeName = args.mazeNameIn
print(mazeName)
maze2 = txtToMat(mazeName)
if(mazeMethod == 'Greedy'):
path2 = findPathGBFS(maze2)
maze0 = update_maze(maze2, path2)
print_maze(maze0)
elif(mazeMethod == 'Depth'):
path3 = findPathDFS(maze2)
maze1 = update_maze(maze2, path3)
print_maze(maze1)
elif(mazeMethod == 'Breadth'):
path4 = findPathBFS(maze2)
maze2 = update_maze(maze2, path4)
print_maze(maze2)
elif(mazeMethod == 'Astar'):
path5 = findPathAstar(maze2)
maze3 = update_maze(maze2, path5)
print_maze(maze3)
main()
'''Manhattan Distance Heuristic on a given node / space
for rows
for cols \\preemptively assign each space its Manhattan distance value going linearly through the matrix.
if (node/space != %):
funct heuristic(node)
dx = absVal(node.x - goal.x)
dy = absVal(node.y - goal.y)
For Best-First (Greedy) - Comparative
Just take the lowest out of a given set of heuristics from a node.
Require us to implement a priority queue in order to keep track of costs of nodes.'''
"""To Do's using the nodes:
"""