Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
168 changes: 95 additions & 73 deletions code/pacman/search/search.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
# search.py
<html>
<head><script src="//archive.org/includes/analytics.js?v=cf34f82" type="text/javascript"></script>
<script type="text/javascript">window.addEventListener('DOMContentLoaded',function(){var v=archive_analytics.values;v.service='wb';v.server_name='wwwb-app19.us.archive.org';v.server_ms=258;archive_analytics.send_pageview({});});</script><script type="text/javascript" src="/static/js/wbhack.js?v=1527197507.0" charset="utf-8"></script>

<script type="text/javascript">
__wbhack.init('https://web.archive.org/web');
</script>
<link rel="stylesheet" type="text/css" href="/static/css/banner-styles.css?v=1527197507.0" />
<link rel="stylesheet" type="text/css" href="/static/css/iconochive.css?v=1527197507.0" />

<!-- End Wayback Rewrite JS Include -->
<title>search.py</title>
</head>
<body>
<h3>search.py (<a href="http://ai.berkeley.edu/projects/release/search/v1/001/search.zip">original</a>)</h3>
<hr>
<pre>
<span style="color: green; font-style: italic"># 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
Expand All @@ -12,90 +29,68 @@
# Pieter Abbeel (pabbeel@cs.berkeley.edu).


"""
</span><span style="color: darkred">"""
In search.py, you will implement generic search algorithms which are called by
Pacman agents (in searchAgents.py).
"""

import util
</span><span style="color: blue; font-weight: bold">import </span>util

class SearchProblem:
"""
<span style="color: blue; font-weight: bold">class </span>SearchProblem<span style="font-weight: bold">:
</span><span style="color: darkred">"""
This class outlines the structure of a search problem, but doesn't implement
any of the methods (in object-oriented terminology: an abstract class).

You do not need to change anything in this class, ever.
"""

def getStartState(self):
"""
</span><span style="color: blue; font-weight: bold">def </span>getStartState<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""
Returns the start state for the search problem.
"""
util.raiseNotDefined()
</span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()

def isGoalState(self, state):
"""
</span><span style="color: blue; font-weight: bold">def </span>isGoalState<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state<span style="font-weight: bold">):
</span><span style="color: darkred">"""
state: Search state

Returns True if and only if the state is a valid goal state.
"""
util.raiseNotDefined()
</span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()

def getSuccessors(self, state):
"""
</span><span style="color: blue; font-weight: bold">def </span>getSuccessors<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>state<span style="font-weight: bold">):
</span><span style="color: darkred">"""
state: Search state

For a given state, this should return a list of triples, (successor,
action, stepCost), where 'successor' is a successor to the current
state, 'action' is the action required to get there, and 'stepCost' is
the incremental cost of expanding to that successor.
"""
util.raiseNotDefined()
</span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()

def getCostOfActions(self, actions):
"""
</span><span style="color: blue; font-weight: bold">def </span>getCostOfActions<span style="font-weight: bold">(</span><span style="color: blue">self</span><span style="font-weight: bold">, </span>actions<span style="font-weight: bold">):
</span><span style="color: darkred">"""
actions: A list of actions to take

This method returns the total cost of a particular sequence of actions.
The sequence must be composed of legal moves.
"""
util.raiseNotDefined()
</span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()


def tinyMazeSearch(problem):
"""
</span><span style="color: blue; font-weight: bold">def </span>tinyMazeSearch<span style="font-weight: bold">(</span>problem<span style="font-weight: bold">):
</span><span style="color: darkred">"""
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]

</span><span style="color: blue; font-weight: bold">from </span>game <span style="color: blue; font-weight: bold">import </span>Directions
s <span style="font-weight: bold">= </span>Directions<span style="font-weight: bold">.</span>SOUTH
w <span style="font-weight: bold">= </span>Directions<span style="font-weight: bold">.</span>WEST
<span style="color: blue; font-weight: bold">return </span><span style="font-weight: bold">[</span>s<span style="font-weight: bold">, </span>s<span style="font-weight: bold">, </span>w<span style="font-weight: bold">, </span>s<span style="font-weight: bold">, </span>w<span style="font-weight: bold">, </span>w<span style="font-weight: bold">, </span>s<span style="font-weight: bold">, </span>w<span style="font-weight: bold">]

def nullHeuristic(state, problem=None):
"""
A heuristic function estimates the cost from the current state to the nearest
goal in the provided SearchProblem. This heuristic is trivial.
"""
return 0

def generalPurposeSearch(problem, stack, heuristic=nullHeuristic):
marked = set()
stack.push(([], problem.getStartState(), 0, 0))
while not stack.isEmpty():
(directions, nextState, costSoFar, stateValue) = stack.pop()
if problem.isGoalState(nextState):
return directions
if nextState not in marked:
successors = problem.getSuccessors(nextState)
for (successor, direction, cost) in successors:
hCost = heuristic(successor, problem)
stack.push((directions + [direction], successor, costSoFar + cost, costSoFar + cost + hCost))
marked.add(nextState)

def depthFirstSearch(problem):
"""
</span><span style="color: blue; font-weight: bold">def </span>depthFirstSearch<span style="font-weight: bold">(</span>problem<span style="font-weight: bold">):
</span><span style="color: darkred">"""
Search the deepest nodes in the search tree first.

Your search algorithm needs to return a list of actions that reaches the
Expand All @@ -108,32 +103,59 @@ def depthFirstSearch(problem):
print "Is the start a goal?", problem.isGoalState(problem.getStartState())
print "Start's successors:", problem.getSuccessors(problem.getStartState())
"""
# Problem with this code: does not use linear space
return generalPurposeSearch(problem, util.Stack())


def breadthFirstSearch(problem):
"""Search the shallowest nodes in the search tree first."""
return generalPurposeSearch(problem, util.Queue())
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()

</span><span style="color: blue; font-weight: bold">def </span>breadthFirstSearch<span style="font-weight: bold">(</span>problem<span style="font-weight: bold">):
</span><span style="color: darkred">"""Search the shallowest nodes in the search tree first."""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()

def uniformCostSearch(problem):
"""Search the node of least total cost first."""
return generalPurposeSearch(
problem,
util.PriorityQueueWithFunction(lambda x: x[2]))

</span><span style="color: blue; font-weight: bold">def </span>uniformCostSearch<span style="font-weight: bold">(</span>problem<span style="font-weight: bold">):
</span><span style="color: darkred">"""Search the node of least total cost first."""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()

def aStarSearch(problem, heuristic=nullHeuristic):
"""Search the node that has the lowest combined cost and heuristic first."""
return generalPurposeSearch(
problem,
util.PriorityQueueWithFunction(lambda x: x[3]),
heuristic)


# Abbreviations
bfs = breadthFirstSearch
dfs = depthFirstSearch
astar = aStarSearch
ucs = uniformCostSearch
</span><span style="color: blue; font-weight: bold">def </span>nullHeuristic<span style="font-weight: bold">(</span>state<span style="font-weight: bold">, </span>problem<span style="font-weight: bold">=</span><span style="color: blue">None</span><span style="font-weight: bold">):
</span><span style="color: darkred">"""
A heuristic function estimates the cost from the current state to the nearest
goal in the provided SearchProblem. This heuristic is trivial.
"""
</span><span style="color: blue; font-weight: bold">return </span><span style="color: red">0

</span><span style="color: blue; font-weight: bold">def </span>aStarSearch<span style="font-weight: bold">(</span>problem<span style="font-weight: bold">, </span>heuristic<span style="font-weight: bold">=</span>nullHeuristic<span style="font-weight: bold">):
</span><span style="color: darkred">"""Search the node that has the lowest combined cost and heuristic first."""
</span><span style="color: red">"*** YOUR CODE HERE ***"
</span>util<span style="font-weight: bold">.</span>raiseNotDefined<span style="font-weight: bold">()


</span><span style="color: green; font-style: italic"># Abbreviations
</span>bfs <span style="font-weight: bold">= </span>breadthFirstSearch
dfs <span style="font-weight: bold">= </span>depthFirstSearch
astar <span style="font-weight: bold">= </span>aStarSearch
ucs <span style="font-weight: bold">= </span>uniformCostSearch

</pre>
</body>
</html>
<!--
FILE ARCHIVED ON 07:39:29 Sep 09, 2016 AND RETRIEVED FROM THE
INTERNET ARCHIVE ON 07:21:33 May 28, 2018.
JAVASCRIPT APPENDED BY WAYBACK MACHINE, COPYRIGHT INTERNET ARCHIVE.

ALL OTHER CONTENT MAY ALSO BE PROTECTED BY COPYRIGHT (17 U.S.C.
SECTION 108(a)(3)).
-->
<!--
playback timings (ms):
LoadShardBlock: 97.813 (3)
esindex: 0.007
captures_list: 118.108
CDXLines.iter: 15.825 (3)
PetaboxLoader3.datanode: 102.281 (4)
exclusion.robots: 0.243
exclusion.robots.policy: 0.231
RedisCDXSource: 0.797
PetaboxLoader3.resolve: 98.035
load_resource: 121.276
-->
Loading