Question
I am working on the well-known Pacman Python project that was created at Berkeley University to explore search techniques. I am specifically working on question
I am working on the well-known Pacman Python project that was created at Berkeley University to explore search techniques. I am specifically working on question 1, in which we are suppose to implement the depth-first search algorithm (DFS) in the depthFirstSearch function in search.py. To make this algorithm complete, i am supposed to write the graph search version of DFS, which avoids expanding any already visited states. When executed, pacman does in fact quickly find the fixed food dot in the tinyMaze, the mediumMaze, and the bigMaze when executed in the command prompt window. Here is a link to the entire assignment, but again I am only doing Question 1. This link should include everything you would need to know, any python files you may want to look at and also has the search.py file that contais the DFS that i am to complete. https://inst.eecs.berkeley.edu/~cs188/sp12/projects/search/search.html The following is a link to the pacman-search.zip on my oneDrive that I am using if you need it. https://1drv.ms/u/s!AjASgdA0u-m9g95N7NpZNijmQaWzvQ Here is my current code (which i found on GitHub as a solution). I simply need it modified to be more readable and understandable either through changing it or commenting out each line so i can understand what is happening: It seems like alot but most of the work is done, i just need to be able to walk through the code and understand each line and if I am doing DFS correctly.
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()) """ "*** MY CODE HERE ***" print ("Start:", problem.getStartState()) print ("Is the start a goal?", problem.isGoalState(problem.getStartState())) print ("Start's successors:", problem.getSuccessors(problem.getStartState())) from game import Directions n = Directions.NORTH s = Directions.SOUTH e = Directions.EAST w = Directions.WEST start = problem.getStartState() if problem.isGoalState(start): return [] from util import Stack statesStack = Stack() visitedset = set([start]) tuple = (start, []) statesStack.push(tuple) while not statesStack.isEmpty(): tuple1 = statesStack.pop() state = tuple1[0] path = tuple1[1] if problem.isGoalState(state): return path successors = problem.getSuccessors(state) for succ in successors: coor = succ[0] move = succ[1] # cost would be succ[2] tempPath = list(path) if not coor in visitedset: visitedset.add(coor) if move == 'North': tempPath.append(n) elif move == 'South': tempPath.append(s) elif move == 'East': tempPath.append(e) elif move == 'West': tempPath.append(w) statesStack.push((coor, tempPath)) return [] util.raiseNotDefined()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started