Implement search procedures cleanly.
This commit is contained in:
@@ -74,7 +74,7 @@ def tinyMazeSearch(problem):
|
||||
return [s, s, w, s, w, w, s, w]
|
||||
|
||||
|
||||
def genericSearch(problem, costFunction):
|
||||
def genericSearch(problem, getNewCostAndPriority):
|
||||
fringe = util.PriorityQueue()
|
||||
startState = problem.getStartState()
|
||||
fringe.push((startState, [], 0), 0)
|
||||
@@ -86,11 +86,11 @@ def genericSearch(problem, costFunction):
|
||||
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):
|
||||
newCost, priority = getNewCostAndPriority(cost, stepCost, successor)
|
||||
if successor in visited and visited[successor] <= newCost:
|
||||
continue
|
||||
newActions = list(actions) + [action]
|
||||
fringe.push((successor, newActions, newCost), newCost)
|
||||
fringe.push((successor, newActions, newCost), priority)
|
||||
print("No path found.")
|
||||
raise Exception()
|
||||
|
||||
@@ -102,24 +102,26 @@ def depthFirstSearch(problem):
|
||||
Your search algorithm needs to return a list of actions that reaches the
|
||||
goal. Make sure to implement a graph search algorithm.
|
||||
"""
|
||||
def costFunction(currentCost, stepCost):
|
||||
return currentCost - 1
|
||||
return genericSearch(problem, costFunction)
|
||||
def getNewCostAndPriority(cost, stepCost, successor):
|
||||
newCost = cost + 1
|
||||
return newCost, -newCost
|
||||
return genericSearch(problem, getNewCostAndPriority)
|
||||
|
||||
|
||||
def breadthFirstSearch(problem):
|
||||
"""Search the shallowest nodes in the search tree first."""
|
||||
def costFunction(currentCost, stepCost):
|
||||
return currentCost + 1
|
||||
return genericSearch(problem, costFunction)
|
||||
def getNewCostAndPriority(cost, stepCost, successor):
|
||||
newCost = cost + 1
|
||||
return newCost, newCost
|
||||
return genericSearch(problem, getNewCostAndPriority)
|
||||
|
||||
|
||||
def uniformCostSearch(problem):
|
||||
"""Search the node of least total cost first."""
|
||||
"*** YOUR CODE HERE ***"
|
||||
def costFunction(currentCost, stepCost):
|
||||
return currentCost + stepCost
|
||||
return genericSearch(problem, costFunction)
|
||||
def getNewCostAndPriority(cost, stepCost, successor):
|
||||
newCost = cost + stepCost
|
||||
return newCost, newCost
|
||||
return genericSearch(problem, getNewCostAndPriority)
|
||||
|
||||
|
||||
def nullHeuristic(state, problem=None):
|
||||
@@ -133,9 +135,11 @@ def nullHeuristic(state, problem=None):
|
||||
def aStarSearch(problem, heuristic=nullHeuristic):
|
||||
"""Search the node that has the lowest combined cost and heuristic first."""
|
||||
"*** YOUR CODE HERE ***"
|
||||
def costFunction(currentCost, stepCost):
|
||||
return currentCost + 1
|
||||
return genericSearch(problem, costFunction)
|
||||
def getNewCostAndPriority(cost, stepCost, successor):
|
||||
newCost = cost + stepCost
|
||||
newPriority = newCost + heuristic(successor, problem)
|
||||
return newCost, newPriority
|
||||
return genericSearch(problem, getNewCostAndPriority)
|
||||
|
||||
|
||||
# Abbreviations
|
||||
|
||||
Reference in New Issue
Block a user