Start to implement search procedures.

This commit is contained in:
2021-10-19 14:23:29 -04:00
parent 9fb9c4985a
commit de62787790

View File

@@ -4,7 +4,7 @@
# educational purposes provided that (1) you do not distribute or publish # educational purposes provided that (1) you do not distribute or publish
# solutions, (2) you retain this notice, and (3) you provide clear # solutions, (2) you retain this notice, and (3) you provide clear
# attribution to UC Berkeley, including a link to http://ai.berkeley.edu. # attribution to UC Berkeley, including a link to http://ai.berkeley.edu.
# #
# Attribution Information: The Pacman AI projects were developed at UC Berkeley. # Attribution Information: The Pacman AI projects were developed at UC Berkeley.
# The core projects and autograders were primarily created by John DeNero # The core projects and autograders were primarily created by John DeNero
# (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu). # (denero@cs.berkeley.edu) and Dan Klein (klein@cs.berkeley.edu).
@@ -19,6 +19,7 @@ Pacman agents (in searchAgents.py).
import util import util
class SearchProblem: class SearchProblem:
""" """
This class outlines the structure of a search problem, but doesn't implement This class outlines the structure of a search problem, but doesn't implement
@@ -70,7 +71,29 @@ def tinyMazeSearch(problem):
from game import Directions from game import Directions
s = Directions.SOUTH s = Directions.SOUTH
w = Directions.WEST 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): def depthFirstSearch(problem):
""" """
@@ -78,27 +101,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.
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 ***" def costFunction(currentCost, stepCost):
print "Start:", problem.getStartState() return currentCost - 1
util.raiseNotDefined() return genericSearch(problem, costFunction)
def breadthFirstSearch(problem): def breadthFirstSearch(problem):
"""Search the shallowest nodes in the search tree first.""" """Search the shallowest nodes in the search tree first."""
"*** YOUR CODE HERE ***" def costFunction(currentCost, stepCost):
util.raiseNotDefined() return currentCost + 1
return genericSearch(problem, costFunction)
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 ***" "*** YOUR CODE HERE ***"
util.raiseNotDefined() def costFunction(currentCost, stepCost):
return currentCost + stepCost
return genericSearch(problem, costFunction)
def nullHeuristic(state, problem=None): def nullHeuristic(state, problem=None):
""" """
@@ -107,10 +129,13 @@ def nullHeuristic(state, problem=None):
""" """
return 0 return 0
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 ***"
util.raiseNotDefined() def costFunction(currentCost, stepCost):
return currentCost + 1
return genericSearch(problem, costFunction)
# Abbreviations # Abbreviations