Start to implement search procedures.
This commit is contained in:
@@ -19,6 +19,7 @@ Pacman agents (in searchAgents.py).
|
||||
|
||||
import util
|
||||
|
||||
|
||||
class SearchProblem:
|
||||
"""
|
||||
This class outlines the structure of a search problem, but doesn't implement
|
||||
@@ -72,33 +73,54 @@ def tinyMazeSearch(problem):
|
||||
w = Directions.WEST
|
||||
return [s, s, w, s, w, w, s, w]
|
||||
|
||||
|
||||
def genericSearch(problem, costFunction):
|
||||
fringe = util.PriorityQueue()
|
||||
startState = problem.getStartState()
|
||||
fringe.push((startState, [], 0), 0)
|
||||
visited = {}
|
||||
|
||||
while not fringe.isEmpty():
|
||||
state, actions, cost = fringe.pop()
|
||||
if problem.isGoalState(state):
|
||||
return actions
|
||||
visited[state] = cost
|
||||
for successor, action, stepCost in problem.getSuccessors(state):
|
||||
newCost = costFunction(cost, stepCost)
|
||||
if successor in visited and abs(visited[successor]) <= abs(newCost):
|
||||
continue
|
||||
newActions = list(actions) + [action]
|
||||
fringe.push((successor, newActions, newCost), newCost)
|
||||
print("No path found.")
|
||||
raise Exception()
|
||||
|
||||
|
||||
def depthFirstSearch(problem):
|
||||
"""
|
||||
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())
|
||||
"""
|
||||
"*** YOUR CODE HERE ***"
|
||||
print "Start:", problem.getStartState()
|
||||
util.raiseNotDefined()
|
||||
def costFunction(currentCost, stepCost):
|
||||
return currentCost - 1
|
||||
return genericSearch(problem, costFunction)
|
||||
|
||||
|
||||
def breadthFirstSearch(problem):
|
||||
"""Search the shallowest nodes in the search tree first."""
|
||||
"*** YOUR CODE HERE ***"
|
||||
util.raiseNotDefined()
|
||||
def costFunction(currentCost, stepCost):
|
||||
return currentCost + 1
|
||||
return genericSearch(problem, costFunction)
|
||||
|
||||
|
||||
def uniformCostSearch(problem):
|
||||
"""Search the node of least total cost first."""
|
||||
"*** YOUR CODE HERE ***"
|
||||
util.raiseNotDefined()
|
||||
def costFunction(currentCost, stepCost):
|
||||
return currentCost + stepCost
|
||||
return genericSearch(problem, costFunction)
|
||||
|
||||
|
||||
def nullHeuristic(state, problem=None):
|
||||
"""
|
||||
@@ -107,10 +129,13 @@ def nullHeuristic(state, problem=None):
|
||||
"""
|
||||
return 0
|
||||
|
||||
|
||||
def aStarSearch(problem, heuristic=nullHeuristic):
|
||||
"""Search the node that has the lowest combined cost and heuristic first."""
|
||||
"*** YOUR CODE HERE ***"
|
||||
util.raiseNotDefined()
|
||||
def costFunction(currentCost, stepCost):
|
||||
return currentCost + 1
|
||||
return genericSearch(problem, costFunction)
|
||||
|
||||
|
||||
# Abbreviations
|
||||
|
||||
Reference in New Issue
Block a user