From de6278779071e687b225a875e575bfe00ea0f00c Mon Sep 17 00:00:00 2001 From: Felix Martin Date: Tue, 19 Oct 2021 14:23:29 -0400 Subject: [PATCH] Start to implement search procedures. --- p1_search/search.py | 57 ++++++++++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 16 deletions(-) diff --git a/p1_search/search.py b/p1_search/search.py index 7d79009..207b0e1 100644 --- a/p1_search/search.py +++ b/p1_search/search.py @@ -4,7 +4,7 @@ # educational purposes provided that (1) you do not distribute or publish # solutions, (2) you retain this notice, and (3) you provide clear # attribution to UC Berkeley, including a link to http://ai.berkeley.edu. -# +# # Attribution Information: The Pacman AI projects were developed at UC Berkeley. # The core projects and autograders were primarily created by John DeNero # (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu). @@ -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 @@ -70,7 +71,29 @@ def tinyMazeSearch(problem): from game import Directions s = Directions.SOUTH w = Directions.WEST - return [s, s, w, s, w, w, s, w] + 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): """ @@ -78,27 +101,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. - - 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