def depthFirstSearch(problem): """ Search the deepest nodes in the search tree first. 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()) """ #Initialize the frontier using the initial state of the problem node = Node((problem.getStartState(), 'None', 0), None) if problem.isGoalState(node.state): return node.getPath() frontier = util.Stack() frontier.push(node) #Initialize the explored set to be empty explored = set() while True: if frontier.isEmpty(): print "Failure!" break #Choose a leaf node and remove it from the frontier node = frontier.pop() if problem.isGoalState(node.state): return node.getPath() #If the node contains a goal state, return the solution #if problem.isGoalState(node.state): #return node.getPath() #Add the state of the node to the explored set explored.add(node.state) #Expand the chosen node successors = problem.getSuccessors(node.state) for succ in successors: child = Node(succ, node) if child.state not in explored and child not in frontier.list: frontier.push(child) print "Solution not found!" util.raiseNotDefined()
def breadthFirstSearch(problem): """Search the shallowest nodes in the search tree first.""" node = Node((problem.getStartState(), 'None', 0), None) if problem.isGoalState(node.state): return node.getPath() #A FIFO queue with node as the only element frontier = util.Queue() frontier.push(node) #An empty set explored = set() while True: if frontier.isEmpty(): break node = frontier.pop() if problem.isGoalState(node.state): return node.getPath() #Add node's state to explored explored.add(node.state) successors = problem.getSuccessors(node.state) for succ in successors: child = Node(succ, node) if (child.state not in explored) and (child not in frontier.list): frontier.push(child) explored.add(child.state) print "Failure!" return None
def uniformCostSearch(problem): """Search the node of least total cost first.""" node = Node((problem.getStartState(), "None", 0), None) if problem.isGoalState(node.state): return node.getPath() frontier = util.PriorityQueue() frontier.push(node, node.pathCost) explored = set() while True: if frontier.isEmpty(): break #Object of class Node node = frontier.pop() if problem.isGoalState(node.state): return node.getPath() if node.state not in explored: successors = problem.getSuccessors(node.state) for succ in successors: child = Node(succ, node) if (child.state not in explored) and (child not in frontier.heap): frontier.update(child, child.pathCost) explored.add(node.state) print "Failure!" return None
def aStarSearch(problem, heuristic=nullHeuristic): """Search the node that has the lowest combined cost and heuristic first.""" #actions = path node = node = Node((problem.getStartState(), "None", 0), None) frontier = util.PriorityQueue() frontier.push(node, heuristic(node.state, problem)) explored = set() while True: if frontier.isEmpty(): break node = frontier.pop() #if problem.isGoalState(node.state): #return node.getPath() explored.add(node.state) successors = problem.getSuccessors(node.state) for succ in successors: child = Node(succ, node) if (child.state not in explored) and (child not in frontier.heap): if problem.isGoalState(child.state): return child.getPath() frontier.push(child, child.pathCost + heuristic(child.state, problem)) elif child in frontier.heap: frontier.update( child, child.pathCost + heuristic(child.state, problem)) print "Failure!" return None