/
search.py
277 lines (239 loc) · 9.81 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
# search.py
# ---------------
# Licensing Information: You are free to use or extend this projects for
# educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear
# attribution to the University of Illinois at Urbana-Champaign
#
# Created by Michael Abir (abir2@illinois.edu) on 08/28/2018
"""
This is the main entry point for MP1. You should only modify code
within this file -- the unrevised staff files will be used for all other
files and classes when code is run, so be careful to not modify anything else.
"""
# Search should return the path and the number of states explored.
# The path should be a list of tuples in the form (row, col) that correspond
# to the positions of the path taken by your search algorithm.
# Number of states explored should be a number.
# maze is a Maze object based on the maze from the file specified by input filename
# searchMethod is the search method specified by --method flag (bfs,dfs,greedy,astar)
import collections
import heapq
import itertools
import queue
import copy
def search(maze, searchMethod):
return {
"bfs": bfs(maze),
"dfs": dfs(maze),
"greedy": greedy(maze),
"astar": astar(maze),
}.get(searchMethod, [])
def bfs(maze):
num_states_explored = 0
start_position = maze.getStart()
end_position = maze.getObjectives()
visited, queue = [], collections.deque([start_position])
parent_map = {start_position:start_position}
not_found = True
while queue and not_found:
num_states_explored += 1
vertex = queue.popleft()
visited.append((vertex[0], vertex[1]))
if (vertex[0], vertex[1]) == end_position[0]:
not_found = False
break
for neighbour in maze.getNeighbors(vertex[0], vertex[1]):
if neighbour not in queue and neighbour not in visited :
queue.append(neighbour)
parent_map[neighbour] = (vertex[0], vertex[1])
parent_list = []
current = end_position[0]
while current != start_position:
parent_list.append(current)
current = parent_map[current]
parent_list.append(current)
parent_list.reverse()
return parent_list, num_states_explored
def dfs(maze):
num_states_explored = 0
start_position = maze.getStart()
end_position = maze.getObjectives()
visited, queue = list(), collections.deque([start_position])
parent_map = {start_position: start_position}
not_found = True
while queue and not_found:
num_states_explored += 1
vertex = queue.pop()
visited.append((vertex[0], vertex[1]))
if (vertex[0], vertex[1]) == end_position[0]:
not_found = False
break
for neighbour in maze.getNeighbors(vertex[0], vertex[1]):
if neighbour not in queue and neighbour not in visited :
queue.append(neighbour)
parent_map[neighbour] = (vertex[0], vertex[1])
# backtrack to find the trace
parent_list = list()
current = end_position[0]
while current != start_position:
parent_list.append(current)
current = parent_map[current]
parent_list.append(current)
parent_list.reverse()
return parent_list, num_states_explored
def greedy(maze):
# initialization
num_states_explored = 0
start_position = maze.getStart()
end_position = maze.getObjectives()
# book keeping, which node is visited and what is in the priority queue
visited, queue = [], [(abs(end_position[0][0]-start_position[0])+abs(end_position[0][1]-start_position[1]),(start_position))]
parent_map = {start_position: start_position}
not_found = True
while queue and not_found:
num_states_explored += 1
vertex = heapq.heappop(queue)[1]
visited.append((vertex[0], vertex[1]))
if (vertex[0], vertex[1]) == end_position[0]:
not_found = False
break
for neighbour in maze.getNeighbors(vertex[0], vertex[1]):
if neighbour not in visited and neighbour not in (x[1] for x in queue):
heapq.heappush(queue, (
abs(end_position[0][0] - neighbour[0]) + abs(end_position[0][1] - neighbour[1]), (neighbour[0], neighbour[1])))
parent_map[neighbour] = (vertex[0], vertex[1])
# backtrack to find the trace
parent_list = list()
current = end_position[0]
while current != start_position:
parent_list.append(current)
current = parent_map[current]
parent_list.append(current)
parent_list.reverse()
return parent_list, num_states_explored
def astar(maze):
global globalpath
global globallen
global not_found
# initialization
num_states_explored = 0
start_position = maze.getStart()
end_position = maze.getObjectives()
if (len(end_position) > 1):
globalpath = []
globallen = 0
not_found =True
visited = []
num_states_explored = 0
path = []
astarMultiple(maze, start_position, end_position, visited, num_states_explored, path)
return globalpath, globallen
# book keeping, which node is visited and what is in the priority queue
visited, queue = [], [(distance(start_position, end_position[0]), start_position, 0)]
parent_map = {start_position: start_position}
not_found = True
while queue and not_found:
num_states_explored += 1
node = heapq.heappop(queue)
vertex = node[1]
visited.append(vertex)
if (vertex[0], vertex[1]) == end_position[0]:
not_found = False
break
for neighbour in maze.getNeighbors(vertex[0], vertex[1]):
# if neighbour not in visited and neighbour not in (x[1] for x in queue):
if neighbour not in visited:
position_queue = [x[1] for x in queue]
if neighbour not in position_queue:
heapq.heappush(queue, (distance(neighbour, end_position[0])+node[2]+1, neighbour, node[2]+1))
parent_map[neighbour] = (vertex[0], vertex[1])
else:
index = position_queue.index(neighbour)
if (distance(neighbour, end_position[0])+node[2]+1) < queue[index][0]:
heapq.heappush(queue,
(distance(neighbour, end_position[0]) + node[2] + 1, neighbour, node[2] + 1))
parent_map[neighbour] = (vertex[0], vertex[1])
# backtrack to find the trace
parent_list = list()
current = end_position[0]
while current != start_position:
parent_list.append(current)
current = parent_map[current]
parent_list.append(current)
parent_list.reverse()
return parent_list, num_states_explored
def astarMultiple(maze, start_position, end_position, visited, num_states_explored, oldpath):
# book keeping, which node is visited and what is in the priority queue
queue = [(0, start_position, [start_position])]
global not_found
while queue:
node = heapq.heappop(queue)
path = node[2]
(row, col) = (node[1][0], node[1][1])
if node[1] not in visited:
visited.append(node[1])
if (row, col) in end_position:
if len(end_position) == 1:
global globalpath
global globallen
if len(oldpath + path) < len(globalpath) or globalpath == []:
globalpath = oldpath + path
globallen = len(visited) + num_states_explored
# print("newlength!!!")
# print(len(globalpath))
not_found = False
return
else:
new_visited = []
new_start_position = node[1]
new_end_position = copy.copy(end_position)
new_end_position.remove((row, col))
astarMultiple(maze, new_start_position, new_end_position, new_visited, len(visited) + num_states_explored,
oldpath + path)
if not not_found:
return
neighbors = maze.getNeighbors(row, col)
for i, neighbor in enumerate(neighbors):
position_queue = [x[1] for x in queue]
if neighbor not in position_queue:
heapq.heappush(queue, (distance_sum(neighbor, end_position)+len(path)+1, neighbor, path + [neighbor]))
else:
index = position_queue.index(neighbor)
if (distance_sum(neighbor, end_position)+len(path)+1) < queue[index][0]:
heapq.heappush(queue, (
distance_sum(neighbor, end_position) + len(path) + 1, neighbor, path + [neighbor]))
return
def distance(start_pos, end_pos):
dis = abs(end_pos[0] - start_pos[0]) + abs(end_pos[1] - start_pos[1])
return dis
def distance_sum(current, end_pos):
end_list = copy.copy(end_pos)
min = -1
minindex = 0
total_dis=0
while end_list:
for i, item in enumerate(end_list):
currentdis = distance(current, item)
if currentdis < min or min == -1:
minindex=i
min = currentdis
total_dis +=min
min =-1
current = end_list[minindex]
del end_list[minindex]
return total_dis
def distance_max(current, end_pos):
max = -1
maxindex=0
min = -1
minindex = 0
for i, item in enumerate(end_pos):
currentdis = distance(current, item)
if currentdis > max or max == -1:
maxindex=i
max = currentdis
if currentdis < min or max == -1:
minindex=i
min = currentdis
return min+distance(end_pos[minindex],end_pos[maxindex])