From 515d3f6cd632ce42802131c760a669fcd18fff5a Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Wed, 20 Oct 2021 20:18:30 -0400 Subject: [PATCH] Implement search procedures cleanly. --- p1_search/search.py | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/p1_search/search.py b/p1_search/search.py index 207b0e1..6a332f8 100644 --- a/p1_search/search.py +++ b/p1_search/search.py @@ -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