Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am to use the pseudocode attached below to write a pacman search code. It has to be python. I have some code below but

I am to use the pseudocode attached below to write a pacman search code. It has to be python. I have some code below but it is not functioning as it should. Can you please help with this?

I am to do Depth first search

Breadth first search

Uniform cost search

A* search.

For more information you can go to the Berkeley AI material site. Go under pacman projects and click on project 1

image text in transcribed

import util class SearchProblem: """ This class outlines the structure of a search problem, but doesn't implement any of the methods (in object-oriented terminology: an abstract class). You do not need to change anything in this class, ever. """ def getStartState(self): """ Returns the start state for the search problem. """ util.raiseNotDefined() def isGoalState(self, state): """ state: Search state Returns True if and only if the state is a valid goal state. """ util.raiseNotDefined() def getSuccessors(self, state): """ state: Search state For a given state, this should return a list of triples, (successor, action, stepCost), where 'successor' is a successor to the current state, 'action' is the action required to get there, and 'stepCost' is the incremental cost of expanding to that successor. """ util.raiseNotDefined() def getCostOfActions(self, actions): """ actions: A list of actions to take This method returns the total cost of a particular sequence of actions. The sequence must be composed of legal moves. """ util.raiseNotDefined() def tinyMazeSearch(problem): """ Returns a sequence of moves that solves tinyMaze. For any other maze, the sequence of moves will be incorrect, so only use this for tinyMaze. """ from game import Directions s = Directions.SOUTH w = Directions.WEST return [s, s, w, s, w, w, s, w] 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()) """ "*** YOUR CODE HERE ***" print "Start:", problem.getStartState() print "Is the start a goal?", problem.isGoalState(problem.getStartState()) print "Start's successor:", problem.getSuccessors(problem.getStartState()) "Search the" #Contains popped nodes and directions closed= set() #This is a defined fringe that connects the codes in util.py to the codes written here. fringe = util.Stack() state = ((problem.getStartState(), [], [])) fringe.push(state) #loop while fringe is not empty and goal is not reached while (not fringe.isEmpty()): #pop from top to stack node,actions, closed = fringe.pop() #check if element is goal if problem.isGoalState(node): #returning a list of actions return node #node #Check if we have vistied node before if node not in closed: closed.append(node) #expand node for s_node, s_path, s_cost in problem.getSuccessors(node): #if successor has not already been visited if s_node not in closed: #make action a list named path if problem.isGoalState(s_node): #append the action just retrived to path return actions + [s_path] #push the node path, list of all actions and sum of cost to fringe. fringe.push((s_node, actions + [s_path], closed + [node])) return [] util.raiseNotDefined() def breadthFirstSearch(problem): """Search the shallowest nodes in the search tree first.""" "*** YOUR CODE HERE ***" #Contains popped nodes and directions closed= set() #This is a defined fringe that connects the codes in util.py to the codes written here. fringe = util.Queue() state = ((problem.getStartState(), [])) fringe.push(state) #loop while fringe is not empty and goal is not reached while (not fringe.isEmpty()): #pop from top to stack node, actions = fringe.pop() #check if element is goal if problem.isGoalState(node): #returning a list of actions return node #node #Check if we have vistied node before if node not in closed: closed.add(node) #expand node for s_node, s_path, s_cost in problem.getSuccessors(node): if s_node not in closed: #make action a list named path if problem.isGoalState(s_node): return actions + [s_path] #push the node path, list of all actions and sum of cost to fringe. fringe.push((s_node, actions + [s_path])) closed.add(s_node) return [] util.raiseNotDefined() def uniformCostSearch(problem): """Search the node of least total cost first.""" "*** YOUR CODE HERE ***" #Contains popped nodes and directions closed= set() #This is a defined fringe that connects the codes in util.py to the codes written here. fringe = util.PriorityQueue() state = ((problem.getStartState(), []),0) fringe.push(state) #loop while fringe is not empty and goal is not reached while (not fringe.isEmpty()): #pop from top to stack node, actions = fringe.pop() #check if element is goal if problem.isGoalState(node): #returning a list of actions return actions #node #Check if we have vistied node before if node not in closed: closed.append(node) #expand node for s_node, s_path, s_cost in problem.getSuccessors(node): if s_node not in closed: #make action a list named path new_actions = actions + [s_path] #push the node path, list of all actions and sum of cost to fringe. fringe.push((s_node, new_actions), problem.getCostOfActions(new_actions)) return [] util.raiseNotDefined() def nullHeuristic(state, problem=None): """ A heuristic function estimates the cost from the current state to the nearest goal in the provided SearchProblem. This heuristic is trivial. """ return 0 def aStarSearch(problem, heuristic=nullHeuristic): """Search the node that has the lowest combined cost and heuristic first.""" "*** YOUR CODE HERE ***" closed= set() fringe = util.PriorityQueue() start = problem.getStartState() fringe.push((start, []), heuristics(start, problem)) while not fringe.isEmpty: node, actions = fringe.pop() if problem.isGoalState(node): return actions closed.append(node) util.raiseNotDefined() # Abbreviations bfs = breadthFirstSearch dfs = depthFirstSearch astar = aStarSearch ucs = uniformCostSearch

O Graph_Search_pseudoc... a Search function GRAPH-SEARCH(problem. fringe) return a solution, or failure closed + an empty set fringe + INSERT (MAKE-NODE(INITIAL-STATE problem]), fringe) loop do if fringe is empty then return failure node + REMOVE-FRONT(fringe) if GOAL-TEST(problem, STATEode]) then return nolle if STATE[node] is not in closed then add STATE[node] to closed for child-node in EXPAND (STATE[node), problem) do fringe + INSERT(child-node, fringe) end end

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Rob, Carlos Coronel

6th International Edition

061921323X, 978-0619213237

More Books

Students also viewed these Databases questions