-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsearch.py
319 lines (262 loc) · 13.5 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
# search.py
# ---------
# Licensing Information: You are free to use or extend these 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 UC Berkeley, including a link to http://ai.berkeley.edu.
#
# Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
# Student side autograding was added by Brad Miller, Nick Hay, and
# Pieter Abbeel (pabbeel@cs.berkeley.edu).
"""
In search.py, you will implement generic search algorithms which are called by
Pacman agents (in searchAgents.py).
"""
from functools import lru_cache
from typing import Callable, Dict, List, Tuple, Union
import numpy as np
import util
from heuristicsCorners import cornersHeuristic
from heuristicsPosition import manhattanDistance, manhattanHeuristic, nullHeuristic, euclideanDistance
from searchProblems import PositionSearchProblem, SearchProblem
_namespace = [ manhattanDistance, euclideanDistance, cornersHeuristic ]
def tinyMazeSearch(problem):
"""
Returns a sequence of moves that solves tinyMaze. For any other maze, the
sequence of moves will be incorrect, so only use this for tinyMaze.
"""
from game import Directions
s = Directions.SOUTH
w = Directions.WEST
return [s, s, w, s, w, w, s, w]
# python autograder.py -q q1
# python pacman.py -l tinyMaze -p SearchAgent -a fn=dfs
# python pacman.py -l mediumMaze -p SearchAgent -a fn=dfs
# python pacman.py -l bigMaze -p SearchAgent -a fn=dfs
# find layouts -name '*Maze*' | grep -v Dotted | perl -p -e 's!^.*/|\..*$!!g' | xargs -t -L1 python pacman.py -p SearchAgent -a fn=dfs -l
# Mazes: bigMaze contoursMaze mediumDottedMaze mediumMaze mediumScaryMaze openMaze smallMaze testMaze tinyMaze
def depthFirstSearch(
problem: SearchProblem,
heuristic: Callable[ [Union[str,Tuple[int,int]], SearchProblem], int] = None,
state: Union[str,Tuple[int,int]] = None, # Tuple[int,int] for runtime, str for unit tests
actions: List[str] = None,
visited: Dict[Tuple[int,int],bool] = None,
) -> List[str]:
"""
Search the deepest nodes in the search tree first.
Your search algorithm needs to return a list of actions that reaches the
goal. Make sure to implement a graph search algorithm.
To get started, you might want to try some of these simple commands to
understand the search problem that is being passed in:
print "Start:", problem.getStartState()
print "Is the start a goal?", problem.isGoalState(problem.getStartState())
print "Start's successors:", problem.getSuccessors(problem.getStartState())
"""
state = state or problem.getStartState()
actions = actions or []
visited = visited or {}
visited[state] = True
if problem.isGoalState(state):
return actions
else:
successors = problem.getSuccessors(state)
### Greedy Depth First Search
def sort_fn(successor):
# Will sort by lowest cost first, then add the cost of the (optional) heuristic
# NOTE: problem.goal is not defined in unit tests
(state, action, cost) = successor
if callable(heuristic):
cost += heuristic(state, problem)
return cost
successors = sorted(successors, key=sort_fn)
### Recursively traverse search tree
for (next_state, next_action, next_cost) in successors:
if next_state in visited: continue # avoid searching already explored states
### add the next action to the list, and see if this path finds the goal, else backtrack
next_actions = actions + [next_action]
next_actions = depthFirstSearch(
problem = problem,
heuristic = heuristic,
state = next_state,
actions = next_actions,
visited = visited,
)
if not next_actions: # have we have hit a dead end
continue # try the next available path
else:
return next_actions # return and save results
### if len(successors) == 0 or all successors returned false
return [] # backtrack
# python pacman.py -l tinyMaze -p SearchAgent -a fn=gdfs
# python pacman.py -l mediumMaze -p SearchAgent -a fn=gdfs
# python pacman.py -l bigMaze -p SearchAgent -a fn=gdfs
# find layouts -name '*Corners*' | perl -p -e 's!^.*/|\..*$!!g' | xargs -t -L1 python pacman.py -p SearchAgent -a fn=gdfs -l
# Mazes: bigMaze contoursMaze mediumDottedMaze mediumMaze mediumScaryMaze openMaze smallMaze testMaze tinyMaze
def greedyDepthFirstSearch(
problem: SearchProblem,
heuristic = manhattanHeuristic,
state: Tuple[int,int] = None,
actions: List[str] = None,
visited: Dict[Tuple[int,int],bool] = None,
) -> Union[List[str], bool]:
return depthFirstSearch(
problem = problem,
heuristic = heuristic,
state = state,
actions = actions,
visited = visited,
)
# python autograder.py -q q2
# python pacman.py -l tinyMaze -p SearchAgent -a fn=bfs
# python pacman.py -l mediumMaze -p SearchAgent -a fn=bfs
# python pacman.py -l bigMaze -p SearchAgent -a fn=bfs
# find layouts -name '*Maze*' | grep -v Dotted | perl -p -e 's!^.*/|\..*$!!g' | xargs -t -L1 python pacman.py -p SearchAgent -a fn=bfs -l
# Mazes: bigMaze contoursMaze mediumDottedMaze mediumMaze mediumScaryMaze openMaze smallMaze testMaze tinyMaze
def breadthFirstSearch(
problem: SearchProblem,
heuristic: Callable[ [Union[str,Tuple[int,int]], SearchProblem], int] = None,
) -> List[str]:
"""
Search the shallowest nodes in the search tree first.
# https://en.wikipedia.org/wiki/Breadth-first_search
procedure BFS(G, start_v) is
let Q be a queue
label start_v as discovered
Q.enqueue(start_v)
while Q is not empty do
v := Q.dequeue()
if v is the goal then
return v
for all edges from v to w in G.adjacentEdges(v) do
if w is not labeled as discovered then
label w as discovered
w.parent := v
Q.enqueue(w)
"""
state = problem.getStartState()
frontier = util.PriorityQueue()
visited = {}
visited[state] = True
frontier.push((state, []), 0) # initialize root node, and add as first item in the queue
while not frontier.isEmpty():
(state, action_path) = frontier.pop() # inspect next shortest path
if problem.isGoalState(state):
return action_path
for (child_state, child_action, child_cost) in problem.getSuccessors(state):
if child_state in visited: continue # avoid searching already explored states
visited[child_state] = True # mark as seen
child_path = action_path + [ child_action ] # store path for later retrieval
priority = heuristic(state, problem) if callable(heuristic) else 0 # greedy breadth first search
frontier.push( (child_state, child_path), priority )
### breadthFirstSearch() can terminate early with shortest path when ignoring path cost
### BUGFIX: terminating early will break autograder unit tests
# if problem.isGoalState(child_state):
# return child_path
else:
return [] # breadthFirstSearch() is unsolvable
# python pacman.py -l tinyMaze -p SearchAgent -a fn=gdfs
# python pacman.py -l mediumMaze -p SearchAgent -a fn=gdfs
# python pacman.py -l bigMaze -p SearchAgent -a fn=gdfs
# find layouts -name '*Maze*' | grep -v Dotted | perl -p -e 's!^.*/|\..*$!!g' | xargs -t -L1 python pacman.py -p SearchAgent -a fn=gbfs -l
# Mazes: bigMaze contoursMaze mediumDottedMaze mediumMaze mediumScaryMaze openMaze smallMaze testMaze tinyMaze
def greedyBreadthFirstSearch(
problem: SearchProblem
) -> Union[List[str], bool]:
return breadthFirstSearch(
problem = problem,
heuristic = manhattanHeuristic
)
# python autograder.py -q q3
# python pacman.py -l mediumMaze -p SearchAgent -a fn=ucs
# python pacman.py -l mediumDottedMaze -p StayEastSearchAgent
# python pacman.py -l mediumScaryMaze -p StayWestSearchAgent
# find layouts -name '*Maze*' | grep -v Dotted | perl -p -e 's!^.*/|\..*$!!g' | xargs -t -L1 python pacman.py -p SearchAgent -a fn=ucs -l
def uniformCostSearch(
problem: SearchProblem,
heuristic: Callable[ [Union[str,Tuple[int,int]], SearchProblem], int] = None,
) -> List[str]:
"""Search the node of least total cost first."""
state = problem.getStartState()
frontier = util.PriorityQueue()
visited = { state: True } # True once removed from the frontier
path_costs = { state: (0, []) } # cost, path_actions
frontier.push(state, 0) # initialize root node, and add as first item in the queue
loop = 0
while not frontier.isEmpty():
loop += 1
state = frontier.pop() # inspect next shortest path
visited[state] = True # only mark as visited once removed from the frontier
(cost, action_path) = path_costs[state]
# Uniform cost search must wait for node to be removed from frontier to guarantee least cost
if problem.isGoalState(state):
return action_path
successors = problem.getSuccessors(state)
### python ./pacman.py -p SearchAgent -a fn=astar,prob=CornersProblem,heuristic=cornersHeuristicNull -q -l bigCorners
### Nodes Expanded | default=7949 | reversed %2 = 7907 | random.shuffle = 7869-7949
if loop %2: successors = reversed(successors) # move semi-diagonally in open spaces
# random.shuffle(successors) # move semi-diagonally in open spaces
for (child_state, child_action, edge_cost) in successors:
if child_state in visited: continue # avoid searching already explored states
child_path = action_path + [ child_action ]
child_path_cost = cost + edge_cost
heuristic_cost = child_path_cost + (heuristic(child_state, problem) if callable(heuristic) else 0)
# Check to see if an existing path to this node has already been found
# Compare actual costs and not heuristic costs - which may be different
if child_state in path_costs:
prev_cost, prev_path = path_costs[child_state]
if prev_cost <= child_path_cost:
continue # child is worst than existing, skip to next successor
# process frontier in heuristic_cost order
frontier.update( child_state, heuristic_cost ) # frontier.update() is expensive = 52% runtime
else:
frontier.push( child_state, heuristic_cost ) # frontier.push() is cheap = 2% runtime
path_costs[child_state] = (child_path_cost, child_path) # store path + cost in dict rather than Queue
else:
return [] # uniformCostSearch() is unsolvable
# python pacman.py -l openMaze -p SearchAgent -a fn=astar,heuristic=manhattanHeuristic
# find layouts -name '*Maze*' | grep -v Dotted | perl -p -e 's!^.*/|\..*$!!g' | xargs -t -L1 python pacman.py -p SearchAgent -a fn=astar,heuristic=manhattanHeuristic -l
def aStarSearch(problem, heuristic=nullHeuristic):
"""Search the node that has the lowest combined cost and heuristic first."""
return uniformCostSearch(
problem = problem,
heuristic = heuristic,
)
# Abbreviations
bfs = breadthFirstSearch
gbfs = greedyBreadthFirstSearch
dfs = depthFirstSearch
gdfs = greedyDepthFirstSearch
astar = aStarSearch
ucs = uniformCostSearch
@lru_cache(2**16)
def mazeDistance(point1, point2, gameState, algorithm=gdfs, heuristic=manhattanHeuristic, visualize=False):
"""
Returns the maze distance between any two points, using the search functions
you have already built. The gameState can be any game state -- Pacman's
position in that state is ignored.
Example usage: mazeDistance( (2,4), (5,6), gameState)
This might be a useful helper function for your ApproximateSearchAgent.
"""
try:
x1, y1 = point1
x2, y2 = point2
walls = gameState.getWalls()
# assert not walls[x1][y1], 'point1 is a wall: ' + str(point1)
# assert not walls[x2][y2], 'point2 is a wall: ' + str(point2)
# Optimization: zero distance
if point1 == point2:
return 0
# Optimization: If there are no walls between points A and B, then we can shortcut to manhattanDistance
box = walls[ min(x1,x2):max(x1,x2)+1, min(y1,y2):max(y1,y2)+1 ]
if np.count_nonzero( box ) == 0:
return manhattanDistance(point1, point2)
else:
problem = PositionSearchProblem(gameState, start=point1, goal=point2, warn=False, visualize=visualize)
actions = algorithm(problem, heuristic=heuristic)
return len(actions)
# problem = PositionSearchProblem(gameState, start=point1, goal=point2, warn=False, visualize=False)
# return len( aStarSearch(problem, heuristic=manhattanHeuristic) )
except:
return 0