-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvdkhuat16_mbvo14_search_algorithms.py
333 lines (270 loc) · 11 KB
/
vdkhuat16_mbvo14_search_algorithms.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
331
332
333
# Vuong Khuat & Minh Vo
# CS365 - AI & Machine Learning
# Lab A
# vdkhuat16_mbvo14_search_algorithms.py
from vdkhuat16_mbvo14_maze import Maze
from queue import Queue, PriorityQueue
import sys
def single_bfs(filename):
"""
The function that uses Breadth-first search strategy
to find the path to the prize in the single-prize case.
@param:
filename: a file containing a maze.
"""
maze = Maze(open(filename, 'r').read())
frontier = Queue()
# a separate list to keep track of states we add to the frontier
# as we cannot look inside elements of a queue.
frontier_tracker = []
explored = []
frontier.put(maze.get_initial_state())
frontier_tracker.append(maze.get_initial_state())
while not frontier.empty():
state = frontier.get()
explored.append(state)
for neighbor in maze.moveable_nodes(state):
if not has_visited(neighbor, explored) and not neighbor in frontier_tracker:
if (neighbor.has_collected_all()): # goal is achieved.
display_solution(filename, "single_bfs", maze, neighbor, explored)
return
frontier.put(neighbor)
frontier_tracker.append(neighbor)
print("Failed to collected all prizes.")
def single_dfs(filename):
"""
The function that uses Depth-first search strategy
to find the path to the prize in the single-prize case.
@param:
filename: a file containing a maze.
"""
maze = Maze(open(filename, 'r').read())
frontier = [] # a list used as a stack
# a separate list to keep track of states we add to the frontier
# as we cannot look inside elements of a stack.
frontier_tracker = []
explored = []
frontier.append(maze.get_initial_state())
frontier_tracker.append(maze.get_initial_state())
while frontier:
state = frontier.pop()
explored.append(state)
for neighbor in maze.moveable_nodes(state):
if not has_visited(neighbor, explored) and not neighbor in frontier_tracker:
if (neighbor.has_collected_all()): # goal is achieved.
display_solution(filename, "single_dfs", maze, neighbor, explored)
return
frontier.append(neighbor)
frontier_tracker.append(neighbor)
print("Failed to collected all prizes.")
def single_gbfs(filename):
"""
The function that uses Greedy best-first search strategy
to find the path to the prize in the single-prize case.
@param:
filename: a file containing a maze.
"""
maze = Maze(open(filename, 'r').read())
frontier = PriorityQueue()
# a separate list to keep track of states we add to the frontier
# as we cannot look inside elements of a queue.
frontier_tracker = []
explored = []
curr_pos = maze.get_initial_state()
frontier_tracker.append(curr_pos)
prize_location = next(iter(curr_pos.prizes_locations))
# we use the Manhattan distance from the current position
# to the prize as our heuristic function.
frontier.put((maze.manhattan_distance(curr_pos.id, prize_location),
curr_pos))
while not frontier.empty():
state = frontier.get()[1]
explored.append(state)
for neighbor in maze.moveable_nodes(state):
if not has_visited(neighbor, explored) and not neighbor in frontier_tracker:
if (neighbor.has_collected_all()): # goal is achieved.
display_solution(filename, "single_gbfs", maze, neighbor, explored)
return
frontier.put((maze.manhattan_distance(neighbor.id, prize_location),
neighbor))
frontier_tracker.append(neighbor)
print("Failed to collected all prizes.")
def single_astar(filename):
"""
The function that uses A* search strategy
to find the path to the prize in the single-prize case.
@param:
filename: a file containing a maze.
"""
maze = Maze(open(filename, 'r').read())
frontier = PriorityQueue()
# a separate list to keep track of states we add to the frontier
# as we cannot look inside elements of a queue.
frontier_tracker = []
explored = []
curr_pos = maze.get_initial_state()
frontier_tracker.append(curr_pos)
prize_location = next(iter(curr_pos.prizes_locations))
# we use the Manhattan distance from the current position
# to the prize as our heuristic function.
frontier.put((single_astar_cost(maze, curr_pos, prize_location),
curr_pos))
while not frontier.empty():
state = frontier.get()[1]
explored.append(state)
for neighbor in maze.moveable_nodes(state):
if not has_visited(neighbor, explored) and not neighbor in frontier_tracker:
if (neighbor.has_collected_all()): # goal is achieved.
display_solution(filename, "single_astar", maze, neighbor, explored)
return
frontier.put((single_astar_cost(maze, neighbor, prize_location),
neighbor))
frontier_tracker.append(neighbor)
print("Failed to collected all prizes.")
def multi_astar(filename):
"""
The function that uses A* search strategy
to find the path to the prizes in the multi-prize case.
@param:
filename: a file containing a maze.
"""
maze = Maze(open(filename, 'r').read())
frontier = PriorityQueue()
# a separate list to keep track of states we add to the frontier
# as we cannot look inside elements of a queue.
frontier_tracker = []
explored = []
curr_pos = maze.get_initial_state()
frontier_tracker.append(curr_pos)
frontier.put((multi_astar_cost(maze, curr_pos),
curr_pos))
while not frontier.empty():
state = frontier.get()[1]
explored.append(state)
for neighbor in maze.moveable_nodes(state):
if not has_visited(neighbor, explored) and not neighbor in frontier_tracker:
if (neighbor.has_collected_all()):
display_solution(filename, "multi_astar", maze, neighbor, explored)
return
frontier.put((multi_astar_cost(maze, neighbor),
neighbor))
frontier_tracker.append(neighbor)
print("Failed to collected all prizes.")
def single_astar_cost(maze, state, prize_location):
"""
The function to estimate the cost for the astar algorithm in the
single-prize case.
The heuristic function is the Manhattan distance from the current location
to the prize.
@param:
maze: the maze.
state: the current state.
prize_location: the id of the prize in the maze.
"""
cost_to_reach_node = state.cost
estimate_cost_to_prize = maze.manhattan_distance(state.id, prize_location)
return cost_to_reach_node + estimate_cost_to_prize
def multi_astar_cost(maze, state):
"""
The function to estimate the cost for the astar algorithm in the
single-prize case.
The heuristic function in this case is the minimum Manhattan distance between
two states from every possible pairs of prize nodes and the current location.
@param:
maze: the maze.
state: the current state.
"""
cost_to_reach_node = state.cost
locations = list(state.prizes_locations)
locations.append(state.id)
min_distance = sys.maxsize
for i in range(len(locations)):
for j in range(i + 1, len(locations)):
distance = maze.manhattan_distance(locations[i], locations[j])
if distance < min_distance: distance = min_distance
estimate_cost_to_prize = min_distance * (len(locations) - 1)
return cost_to_reach_node + estimate_cost_to_prize
def has_visited(state, explored):
"""
Return True if a node has already been visited, False otherwise. A node is
considered visited if there is a node with the same location and prizes found
in the list of explored nodes.
@param:
algo_name: name of the algorithm used to generate the solution.
maze: the maze.
node: the node which contains the final state of the solution.
explored: the list of nodes all the nodes expanded by the algorithm.
"""
for visited_node in explored:
if state == visited_node: return True
return False
def display_solution(maze_name, algo_name, maze, state, explored):
"""
Print the solution of maze along with the cost and the number of
nodes expanded.
@param:
maze_name: name of the file containing a maze.
algo_name: name of the algorithm used to generate the solution.
maze: the maze.
state: the state of the solution.
explored: the list of nodes all the nodes expanded by the algorithm.
"""
input = maze_name
algo = algo_name
path_cost = len(state.path)
nodes_expanded = len(explored)
with open("output.txt", "a") as output:
output.write("\n\n\n---- Maze: %s ----\n" %(input))
output.write("---- Algorithm: %s ----\n" %(algo))
if algo == "multi_astar":
print_maze_astar(output, maze, state)
else:
print_maze(output, maze, state)
output.write("\nThe past cost of the solution is %s." %(path_cost))
output.write("\nThe number of nodes expanded by the search algorithm is %s." %(len(explored)))
output.close()
def print_maze(file, maze, state):
"""
Print the solution of the maze.
@param:
file: name of the output file.
maze: the maze.
state: the state of the solution.
"""
source = maze.get_source()
for i in range(len(source)):
if i not in state.path:
file.write(source[i])
else:
file.write(maze.PATH)
file.write("")
def print_maze_astar(file, maze, state):
"""
Print the solution of the maze in the multi_astar case.
@param:
file: name of the output file.
maze: the maze.
state: the state of the solution.
"""
prizes_locations = maze.get_initial_state().prizes_locations
orders = {}
index = 0
for id in state.path:
if id in prizes_locations and not id in orders:
orders[id] = index
index += 1
for i in range(len(maze.source)):
if i in orders:
file.write(convert_prize_count(orders[i]))
else:
file.write(maze.source[i])
file.write("")
def convert_prize_count(prize_count):
"""
Convert the order that the prize that was found into a single-character format
@param:
prize_count: the order that the prize was found.
"""
count = prize_count
orders = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
return orders[prize_count]