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