In this project, your Pacman agent will find paths through his maze world, both to reach a particular location and to collect food efficiently. You will build general search algorithms and apply them to Pacman scenarios.
As in Project 0, this project includes an autograder for you to grade your answers on your machine. This can be run with the command:
1
python autograder.py
See the autograder tutorial in Project 0 for more information about using the autograder.
The code for this project consists of several Python files, some of which you will need to read and understand in order to complete the assignment, and some of which you can ignore. You can download all the code and supporting files as a search.zip.
Files you’ll edit:
search.py
Where all of your search algorithms will reside.
searchAgents.py
Where all of your search-based agents will reside.
Files you might want to look at:
pacman.py
The main file that runs Pacman games. This file describes a Pacman GameState type, which you use in this project.
game.py
The logic behind how the Pacman world works. This file describes several supporting types like AgentState, Agent, Direction, and Grid.
util.py
Useful data structures for implementing search algorithms.
Supporting files you can ignore:
graphicsDisplay.py
Graphics for Pacman
graphicsUtils.py
Support for Pacman graphics
textDisplay.py
ASCII graphics for Pacman
ghostAgents.py
Agents to control ghosts
keyboardAgents.py
Keyboard interfaces to control Pacman
layout.py
Code for reading layout files and storing their contents
autograder.py
Project autograder
testParser.py
Parses autograder test and solution files
testClasses.py
General autograding test classes
test_cases/
Directory containing the test cases for each question
searchTestClasses.py
Project 1 specific autograding test classes
Files to Edit and Submit: You will fill in portions of search.py and searchAgents.py during the assignment. Once you have completed the assignment, you will submit a token generated by submission_autograder.py. Please do not change the other files in this distribution or submit any of our original files other than these files.
Evaluation: Your code will be autograded for technical correctness. Please do not change the names of any provided functions or classes within the code, or you will wreak havoc on the autograder. However, the correctness of your implementation – not the autograder’s judgements – will be the final judge of your score. If necessary, we will review and grade assignments individually to ensure that you receive due credit for your work.
Academic Dishonesty: We will be checking your code against other submissions in the class for logical redundancy. If you copy someone else’s code and submit it with minor changes, we will know. These cheat detectors are quite hard to fool, so please don’t try. We trust you all to submit your own work only; please don’t let us down. If you do, we will pursue the strongest consequences available to us.
Getting Help: You are not alone! If you find yourself stuck on something, contact the course staff for help. Office hours, section, and the discussion forum are there for your support; please use them. If you can’t make our office hours, let us know and we will schedule more. We want these projects to be rewarding and instructional, not frustrating and demoralizing. But, we don’t know when or how to help unless you ask.
Discussion: Please be careful not to post spoilers.
Welcome to Pacman
After downloading the code, unzipping it, and changing to the directory, you should be able to play a game of Pacman by typing the following at the command line:
1
python pacman.py
Pacman lives in a shiny blue world of twisting corridors and tasty round treats. Navigating this world efficiently will be Pacman’s first step in mastering his domain.
The simplest agent in searchAgents.py is called the GoWestAgent, which always goes West (a trivial reflex agent). This agent can occasionally win:
If Pacman gets stuck, you can exit the game by typing CTRL-c into your terminal.
Soon, your agent will solve not only tinyMaze, but any maze you want.
Note that pacman.py supports a number of options that can each be expressed in a long way (e.g., --layout) or a short way (e.g., -l). You can see the list of all options and their default values via:
1
python pacman.py -h
Also, all of the commands that appear in this project also appear in commands.txt, for easy copying and pasting. In UNIX/Mac OS X, you can even run all these commands in order with bash commands.txt.
This is annotating the type of the arguments that Python should expect for this function. In the example below, a should be an int – integer, b should be a tuple of 2 ints, c should be a List of Lists of anything – therefore a 2D array of anything, d is essentially the same as not annotated and can by anything, and e should be a float. e is also set to 1.0 if nothing is passed in for it, i.e.:
The above call fits the type annotations, and doesn’t pass anything in for e. Type annotations are meant to be an adddition to the docstrings to help you know what the functions are working with. Python itself doesn’t enforce these. When writing your own functions, it is up to you if you want to annotate your types; they may be helpful to keep organized or not something you want to spend time on.
Q1 (3 pts): Finding a Fixed Food Dot using Depth First Search
In searchAgents.py, you’ll find a fully implemented SearchAgent, which plans out a path through Pacman’s world and then executes that path step-by-step. The search algorithms for formulating a plan are not implemented – that’s your job.
First, test that the SearchAgent is working correctly by running:
1
python pacman.py -l tinyMaze -p SearchAgent -a fn=tinyMazeSearch
The command above tells the SearchAgent to use tinyMazeSearch as its search algorithm, which is implemented in search.py. Pacman should navigate the maze successfully.
Now it’s time to write full-fledged generic search functions to help Pacman plan routes! Pseudocode for the search algorithms you’ll write can be found in the lecture slides. Remember that a search node must contain not only a state but also the information necessary to reconstruct the path (plan) which gets to that state.
Important note: All of your search functions need to return a list of actions that will lead the agent from the start to the goal. These actions all have to be legal moves (valid directions, no moving through walls).
Important note: Make sure to use the Stack, Queue and PriorityQueue data structures provided to you in util.py! These data structure implementations have particular properties which are required for compatibility with the autograder.
Hint: Each algorithm is very similar. Algorithms for DFS, BFS, UCS, and A* differ only in the details of how the fringe is managed. So, concentrate on getting DFS right and the rest should be relatively straightforward. Indeed, one possible implementation requires only a single generic search method which is configured with an algorithm-specific queuing strategy. (Your implementation need not be of this form to receive full credit).
Implement the depth-first search (DFS) algorithm in the depthFirstSearch function in search.py. To make your algorithm complete, write the graph search version of DFS, which avoids expanding any already visited states.
The Pacman board will show an overlay of the states explored, and the order in which they were explored (brighter red means earlier exploration). Is the exploration order what you would have expected? Does Pacman actually go to all the explored squares on his way to the goal?
Hint: If you use a Stack as your data structure, the solution found by your DFS algorithm for mediumMaze should have a length of 130 (provided you push successors onto the fringe in the order provided by getSuccessors; you might get 246 if you push them in the reverse order). Is this a least cost solution? If not, think about what depth-first search is doing wrong.
Grading: Please run the below command to see if your implementation passes all the autograder test cases.
1
python autograder.py -q q1
Answer
The following code uses the Stack as frontier, which means that the node pushed into it first will be dealt with last, and when a node n is pop out, it will push all of its successors on the frontier to be dealt with before other nodes as the same depth with n have been processed
defdepthFirstSearch(problem: SearchProblem): """ 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 ***" from util import Stack frontier = Stack() visited = set()
frontier.push((problem.getStartState(), []))
whilenot frontier.isEmpty(): node, path = frontier.pop() if problem.isGoalState(node): return path
if node notin visited: visited.add(node) for successor in problem.getSuccessors(node): frontier.push((successor[0], path + [successor[1]]))
return []
Q2 (3 pts): Breadth First Search
Implement the breadth-first search (BFS) algorithm in the breadthFirstSearch function in search.py. Again, write a graph search algorithm that avoids expanding any already visited states. Test your code the same way you did for depth-first search.
1 2
python pacman.py -l mediumMaze -p SearchAgent -a fn=bfs python pacman.py -l bigMaze -p SearchAgent -a fn=bfs -z .5
Does BFS find a least cost solution? If not, check your implementation.
Hint: If Pacman moves too slowly for you, try the option –frameTime 0.
Note: If you’ve written your search code generically, your code should work equally well for the eight-puzzle search problem without any changes.
1
python eightpuzzle.py
Grading: Please run the below command to see if your implementation passes all the autograder test cases.
1
python autograder.py -q q2
Answer
The following code uses the Queue as frontier, which means that the node pushed into it first will be dealt with first, and when a node n is pop out, it will push all of its successors on the frontier to be dealt with after other nodes as the same depth with n have been processed
defbreadthFirstSearch(problem: SearchProblem): """Search the shallowest nodes in the search tree first.""" "*** YOUR CODE HERE ***" from util import Queue frontier = Queue() visited = set()
frontier.push((problem.getStartState(), []))
whilenot frontier.isEmpty(): node, path = frontier.pop() if problem.isGoalState(node): return path
if node notin visited: visited.add(node) for successor in problem.getSuccessors(node): frontier.push((successor[0], path + [successor[1]]))
return []
Q3 (3 pts): Varying the Cost Function
While BFS will find a fewest-actions path to the goal, we might want to find paths that are “best” in other senses. Consider mediumDottedMaze and mediumScaryMaze.
By changing the cost function, we can encourage Pacman to find different paths. For example, we can charge more for dangerous steps in ghost-ridden areas or less for steps in food-rich areas, and a rational Pacman agent should adjust its behavior in response.
Implement the uniform-cost graph search algorithm in the uniformCostSearch function in search.py. We encourage you to look through util.py for some data structures that may be useful in your implementation. You should now observe successful behavior in all three of the following layouts, where the agents below are all UCS agents that differ only in the cost function they use (the agents and cost functions are written for you):
Note: You should get very low and very high path costs for the StayEastSearchAgent and StayWestSearchAgent respectively, due to their exponential cost functions (see searchAgents.py for details).
Grading: Please run the below command to see if your implementation passes all the autograder test cases.
1
python autograder.py -q q3
Answer
The following code uses the PriorityQueue as frontier, which means that the node pushed into it will be dealt with by the order of priority. Each node’s priority is equal to the cost of the last node to this node add the priority of the last node. The start state’s priority is equal to 0.
defuniformCostSearch(problem: SearchProblem): """Search the node of least total cost first.""" "*** YOUR CODE HERE ***" # util.raiseNotDefined() from util import PriorityQueue frontier = PriorityQueue() visited = set()
whilenot frontier.isEmpty(): node, path, priority = frontier.pop() if problem.isGoalState(node): return path if node notin visited: visited.add(node) for successor in problem.getSuccessors(node): frontier.push((successor[0], path + [successor[1]], successor[2] + priority),\ successor[2] + priority)
return []
Q4 (3 pts): A* search
Implement A* graph search in the empty function aStarSearch in search.py. A* takes a heuristic function as an argument. Heuristics take two arguments: a state in the search problem (the main argument), and the problem itself (for reference information). The nullHeuristic heuristic function in search.py is a trivial example.
You can test your A* implementation on the original problem of finding a path through a maze to a fixed position using the Manhattan distance heuristic (implemented already as manhattanHeuristic in searchAgents.py).
1
python pacman.py -l bigMaze -z .5 -p SearchAgent -a fn=astar,heuristic=manhattanHeuristic
You should see that A* finds the optimal solution slightly faster than uniform cost search (about 549 vs. 620 search nodes expanded in our implementation, but ties in priority may make your numbers differ slightly). What happens on openMaze for the various search strategies?
Grading: Please run the below command to see if your implementation passes all the autograder test cases.
defaStarSearch(problem: SearchProblem, heuristic=nullHeuristic): """Search the node that has the lowest combined cost and heuristic first.""" "*** YOUR CODE HERE ***" # util.raiseNotDefined() from util import PriorityQueue frontier = PriorityQueue() visited = set()
if node notin visited: visited.add(node) for successor in problem.getSuccessors(node): frontier.push((successor[0], path + [successor[1]], successor[2] + priority),\ successor[2] + priority + heuristic(successor[0], problem))
return []
Q5 (3 pts): Finding All the Corners
The real power of A* will only be apparent with a more challenging search problem. Now, it’s time to formulate a new problem and design a heuristic for it.
In corner mazes, there are four dots, one in each corner. Our new search problem is to find the shortest path through the maze that touches all four corners (whether the maze actually has food there or not). Note that for some mazes like tinyCorners, the shortest path does not always go to the closest food first! Hint: the shortest path through tinyCorners takes 28 steps.
Note: Make sure to complete Question 2 before working on Question 5, because Question 5 builds upon your answer for Question 2.
Implement the CornersProblem search problem in searchAgents.py. You will need to choose a state representation that encodes all the information necessary to detect whether all four corners have been reached. Now, your search agent should solve:
1 2
python pacman.py -l tinyCorners -p SearchAgent -a fn=bfs,prob=CornersProblem python pacman.py -l mediumCorners -p SearchAgent -a fn=bfs,prob=CornersProblem
To receive full credit, you need to define an abstract state representation that does not encode irrelevant information (like the position of ghosts, where extra food is, etc.). In particular, do not use a Pacman GameState as a search state. Your code will be very, very slow if you do (and also wrong).
Hint 1: The only parts of the game state you need to reference in your implementation are the starting Pacman position and the location of the four corners.
Hint 2: When coding up getSuccessors, make sure to add children to your successors list with a cost of 1.
Our implementation of breadthFirstSearch expands just under 2000 search nodes on mediumCorners. However, heuristics (used with A* search) can reduce the amount of searching required.
Grading: Please run the below command to see if your implementation passes all the autograder test cases.
1
python autograder.py -q q5
Answer(simple)
The following code uses a Tuple to store the state of the food in each corners
1 2 3 4 5 6 7 8 9
defgetStartState(self): """ Returns the start state (in your state space, not the full Pacman state space) """ "*** YOUR CODE HERE ***" # util.raiseNotDefined() res = (self.startingPosition, False, False, False, False) return res
The following code uses a for loop to confirm whether the current state is the goal state.
1 2 3 4 5 6 7 8 9
defisGoalState(self, state: Any): """ Returns whether this search state is a goal state of the problem. """ "*** YOUR CODE HERE ***" # util.raiseNotDefined() for item in state[1:]: ifnot item: returnFalse
defgetSuccessors(self, state: Any): """ Returns successor states, the actions they require, and a cost of 1. As noted in search.py: 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 """
successors = [] for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: # Add a successor state to the successor list if the action is legal # Here's a code snippet for figuring out whether a new position hits a wall: # x,y = currentPosition # dx, dy = Actions.directionToVector(action) # nextx, nexty = int(x + dx), int(y + dy) # hitsWall = self.walls[nextx][nexty]
"*** YOUR CODE HERE ***" x, y = state[0] dx, dy = Actions.directionToVector(action) nextx, nexty = int(x + dx), int(y + dy) hitsWall = self.walls[nextx][nexty] ifnot hitsWall: nextState = (nextx, nexty) corners = state[1:] if nextState in self.corners: corners = list(corners) corners[self.corners.index(nextState)] = True corners = tuple(corners) successors.append(((nextState, *corners), action, 1))
self._expanded += 1# DO NOT CHANGE return successors
classCornersProblem(search.SearchProblem): """ This search problem finds paths through all four corners of a layout. You must select a suitable state space and successor function """
def__init__(self, startingGameState: pacman.GameState): """ Stores the walls, pacman's starting position and corners. """ self.walls = startingGameState.getWalls() self.startingPosition = startingGameState.getPacmanPosition() top, right = self.walls.height-2, self.walls.width-2 self.corners = ((1,1), (1,top), (right, 1), (right, top)) for corner in self.corners: ifnot startingGameState.hasFood(*corner): print('Warning: no food in corner ' + str(corner)) self._expanded = 0# DO NOT CHANGE; Number of search nodes expanded # Please add any code here which you would like to use # in initializing the problem "*** YOUR CODE HERE ***"
defgetStartState(self): """ Returns the start state (in your state space, not the full Pacman state space) """ "*** YOUR CODE HERE ***" # util.raiseNotDefined() res = (self.startingPosition, False, False, False, False) return res
defisGoalState(self, state: Any): """ Returns whether this search state is a goal state of the problem. """ "*** YOUR CODE HERE ***" # util.raiseNotDefined() for item in state[1:]: ifnot item: returnFalse
returnTrue
defgetSuccessors(self, state: Any): """ Returns successor states, the actions they require, and a cost of 1. As noted in search.py: 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 """
successors = [] for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: # Add a successor state to the successor list if the action is legal # Here's a code snippet for figuring out whether a new position hits a wall: # x,y = currentPosition # dx, dy = Actions.directionToVector(action) # nextx, nexty = int(x + dx), int(y + dy) # hitsWall = self.walls[nextx][nexty]
"*** YOUR CODE HERE ***" x, y = state[0] dx, dy = Actions.directionToVector(action) nextx, nexty = int(x + dx), int(y + dy) hitsWall = self.walls[nextx][nexty] ifnot hitsWall: nextState = (nextx, nexty) corners = state[1:] if nextState in self.corners: corners = list(corners) corners[self.corners.index(nextState)] = True corners = tuple(corners) successors.append(((nextState, *corners), action, 1))
self._expanded += 1# DO NOT CHANGE return successors
Answer (optimize)
The following code use a tuple to store position and corners as a state
1 2 3 4 5 6 7 8
defgetStartState(self): """ Returns the start state (in your state space, not the full Pacman state space) """ "*** YOUR CODE HERE ***" # util.raiseNotDefined() return (self.startingPosition, self.corners)
The problem is solved when Pacman’s position is equal to the only remaining unvisited corner.
1 2 3 4 5 6 7 8
defisGoalState(self, state: Any): """ Returns whether this search state is a goal state of the problem. """ "*** YOUR CODE HERE ***" # util.raiseNotDefined() pacmanPos, unvisitedCorners = state return unvisitedCorners == (pacmanPos,)
The successors are computed by filtering illegal locations and then constructing the next state by filtering the unvisited corners to ensure that none is Pacman’s current position.
defgetSuccessors(self, state: Any): """ Returns successor states, the actions they require, and a cost of 1. As noted in search.py: 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 """ pacmanPos, unvisitedCorners = state
successors = [] for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: # Add a successor state to the successor list if the action is legal # Here's a code snippet for figuring out whether a new position hits a wall: # x,y = currentPosition # dx, dy = Actions.directionToVector(action) # nextx, nexty = int(x + dx), int(y + dy) # hitsWall = self.walls[nextx][nexty]
"*** YOUR CODE HERE ***" x, y = state[0] dx, dy = Actions.directionToVector(action) nextx, nexty = int(x + dx), int(y + dy) hitsWall = self.walls[nextx][nexty] if hitsWall: continue nextPacmanPos = (nextx, nexty) nextUnvisitedCorners = tuple( corner for corner in unvisitedCorners if corner != pacmanPos ) nextState = (nextPacmanPos, nextUnvisitedCorners) successors.append((nextState, action, 1))
self._expanded += 1# DO NOT CHANGE return successors
defgetStartState(self): """ Returns the start state (in your state space, not the full Pacman state space) """ "*** YOUR CODE HERE ***" # util.raiseNotDefined() return (self.startingPosition, self.corners)
defisGoalState(self, state: Any): """ Returns whether this search state is a goal state of the problem. """ "*** YOUR CODE HERE ***" # util.raiseNotDefined() pacmanPos, unvisitedCorners = state return unvisitedCorners == (pacmanPos,)
defgetSuccessors(self, state: Any): """ Returns successor states, the actions they require, and a cost of 1. As noted in search.py: 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 """ pacmanPos, unvisitedCorners = state
successors = [] for action in [Directions.NORTH, Directions.SOUTH, Directions.EAST, Directions.WEST]: # Add a successor state to the successor list if the action is legal # Here's a code snippet for figuring out whether a new position hits a wall: # x,y = currentPosition # dx, dy = Actions.directionToVector(action) # nextx, nexty = int(x + dx), int(y + dy) # hitsWall = self.walls[nextx][nexty]
"*** YOUR CODE HERE ***" x, y = state[0] dx, dy = Actions.directionToVector(action) nextx, nexty = int(x + dx), int(y + dy) hitsWall = self.walls[nextx][nexty] if hitsWall: continue nextPacmanPos = (nextx, nexty) nextUnvisitedCorners = tuple( corner for corner in unvisitedCorners if corner != pacmanPos ) nextState = (nextPacmanPos, nextUnvisitedCorners) successors.append((nextState, action, 1))
self._expanded += 1# DO NOT CHANGE return successors
Q6 (3 pts): Corners Problem: Heuristic
Note: Make sure to complete Question 4 before working on Question 6, because Question 6 builds upon your answer for Question 4.
Implement a non-trivial, consistent heuristic for the CornersProblem in cornersHeuristic.
-p SearchAgent -a fn=aStarSearch,prob=CornersProblem,heuristic=cornersHeuristic
Admissibility vs. Consistency: Remember, heuristics are just functions that take search states and return numbers that estimate the cost to a nearest goal. More effective heuristics will return values closer to the actual goal costs. To be admissible, the heuristic values must be lower bounds on the actual shortest path cost to the nearest goal (and non-negative). To be consistent, it must additionally hold that if an action has cost c, then taking that action can only cause a drop in heuristic of at most c.
Remember that admissibility isn’t enough to guarantee correctness in graph search – you need the stronger condition of consistency. However, admissible heuristics are usually also consistent, especially if they are derived from problem relaxations. Therefore it is usually easiest to start out by brainstorming admissible heuristics. Once you have an admissible heuristic that works well, you can check whether it is indeed consistent, too. The only way to guarantee consistency is with a proof. However, inconsistency can often be detected by verifying that for each node you expand, its successor nodes are equal or higher in in f-value. Moreover, if UCS and A* ever return paths of different lengths, your heuristic is inconsistent. This stuff is tricky!
Non-Trivial Heuristics: The trivial heuristics are the ones that return zero everywhere (UCS) and the heuristic which computes the true completion cost. The former won’t save you any time, while the latter will timeout the autograder. You want a heuristic which reduces total compute time, though for this assignment the autograder will only check node counts (aside from enforcing a reasonable time limit).
Grading: Your heuristic must be a non-trivial non-negative consistent heuristic to receive any points. Make sure that your heuristic returns 0 at every goal state and never returns a negative value. Depending on how few nodes your heuristic expands, you’ll be graded:
Number of nodes expanded
Grade
more than 2000
0/3
at most 2000
1/3
at most 1600
2/3
at most 1200
3/3
Remember: If your heuristic is inconsistent, you will receive no credit, so be careful!
Grading: Please run the below command to see if your implementation passes all the autograder test cases.
1
python autograder.py -q q6
Answer(simple)
The following code uses the manhattan distance between Pacman’s position and the corner as the corners Heuristic value. The heuristic resulted in the expansion of 1136 nodes.
defcornersHeuristic(state: Any, problem: CornersProblem): """ A heuristic for the CornersProblem that you defined. state: The current search state (a data structure you chose in your search problem) problem: The CornersProblem instance for this layout. This function should always return a number that is a lower bound on the shortest path from the state to a goal of the problem; i.e. it should be admissible (as well as consistent). """ corners = problem.corners # These are the corner coordinates walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
"*** YOUR CODE HERE ***" value = 0 for idx inrange(len(corners)): ifnot state[idx + 1]: value = max(value, util.manhattanDistance(state[0], corners[idx])) return value # Default to trivial solution
Answer (optimize)
This approach will be efficient. Consider an intermediate state which may have already visited any of the four corners. List out the unvisited corners and compute the Manhattan distance to each of them. Now select the corner with a minimum manhattan distance. Note down the distance. This is the minimum number of steps needed to reach the corner irrespective of the board. Update the current position of Pacman to this corner. Remove this corner from the unvisited corners list. Loop over until the unvisited corners are empty. The sum of these distances will be an Admissible and Consistent Heuristic. Furthermore, this heuristic solved the problem optimally by only expanding 702 nodes.
defcornersHeuristic(state: Any, problem: CornersProblem): """ A heuristic for the CornersProblem that you defined. state: The current search state (a data structure you chose in your search problem) problem: The CornersProblem instance for this layout. This function should always return a number that is a lower bound on the shortest path from the state to a goal of the problem; i.e. it should be admissible (as well as consistent). """ corners = problem.corners # These are the corner coordinates walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
"*** YOUR CODE HERE ***" defdistanceArgmin(pos, points): index, minDist = None, 1e9 for curIdx, curPoint inenumerate(points): curDist = util.manhattanDistance(pos, curPoint) # curDist = mazeDistance(pos, curPoint, problem.startingGameState) if curDist < minDist: index, minDist = curIdx, curDist return index, minDist
Another thought is to use the MazeDistance function to compute the heuristic value. This method will solve the medium search problem by only expanding 129 nodes. But it will ***** FAIL: inconsistent heuristic ** in the 3rd test case. I don’t know how to fix it. Readers can try it if you are interested in it.
Q7 (4 pts): Eating All The Dots
Now we’ll solve a hard search problem: eating all the Pacman food in as few steps as possible. For this, we’ll need a new search problem definition which formalizes the food-clearing problem: FoodSearchProblem in searchAgents.py (implemented for you). A solution is defined to be a path that collects all of the food in the Pacman world. For the present project, solutions do not take into account any ghosts or power pellets; solutions only depend on the placement of walls, regular food and Pacman. (Of course ghosts can ruin the execution of a solution! We’ll get to that in the next project.) If you have written your general search methods correctly, A* with a null heuristic (equivalent to uniform-cost search) should quickly find an optimal solution to testSearch with no code change on your part (total cost of 7).
-p SearchAgent -a fn=astar,prob=FoodSearchProblem,heuristic=foodHeuristic
You should find that UCS starts to slow down even for the seemingly simple tinySearch. As a reference, our implementation takes 2.5 seconds to find a path of length 27 after expanding 5057 search nodes.
Note: Make sure to complete Question 4 before working on Question 7, because Question 7 builds upon your answer for Question 4.
Fill in foodHeuristic in searchAgents.py with a consistent heuristic for the FoodSearchProblem. Try your agent on the trickySearch board:
Our UCS agent finds the optimal solution in about 13 seconds, exploring over 16,000 nodes.
Any non-trivial non-negative consistent heuristic will receive 1 point. Make sure that your heuristic returns 0 at every goal state and never returns a negative value. Depending on how few nodes your heuristic expands, you’ll get additional points:
Number of nodes expanded
Grade
more than 15000
1/4
at most 15000
2/4
at most 12000
3/4
at most 9000
4/4 (full credit; medium)
at most 7000
5/4 (optional extra credit; hard)
Remember: If your heuristic is inconsistent, you will receive no credit, so be careful! Can you solve mediumSearch in a short time? If so, we’re either very, very impressed, or your heuristic is inconsistent.
Grading: Please run the below command to see if your implementation passes all the autograder test cases.
1
python autograder.py -q q7
Answer (simple)
The following code uses the Maze Distance between the Pacman and the food closest to the Pacman as a heuristic value. This heuristic solved the problem optimally by expanding 4137 nodes.
deffoodHeuristic(state: Tuple[Tuple, List[List]], problem: FoodSearchProblem): """ Your heuristic for the FoodSearchProblem goes here. This heuristic must be consistent to ensure correctness. First, try to come up with an admissible heuristic; almost all admissible heuristics will be consistent as well. If using A* ever finds a solution that is worse uniform cost search finds, your heuristic is *not* consistent, and probably not admissible! On the other hand, inadmissible or inconsistent heuristics may find optimal solutions, so be careful. The state is a tuple ( pacmanPosition, foodGrid ) where foodGrid is a Grid (see game.py) of either True or False. You can call foodGrid.asList() to get a list of food coordinates instead. If you want access to info like walls, capsules, etc., you can query the problem. For example, problem.walls gives you a Grid of where the walls are. If you want to *store* information to be reused in other calls to the heuristic, there is a dictionary called problem.heuristicInfo that you can use. For example, if you only want to count the walls once and store that value, try: problem.heuristicInfo['wallCount'] = problem.walls.count() Subsequent calls to this heuristic can access problem.heuristicInfo['wallCount'] """ position, foodGrid = state "*** YOUR CODE HERE ***" value = 0 foods = foodGrid.asList() for food in foods: value = max(value, mazeDistance(food, position, problem.startingGameState)) return value
Answer (optimize)
The approach for this solution was construct a fully-connected graph between all the food items were the weight between two items was the distance between them within the maze (i.e. the maze distance). The was computed using a breadth-first search for each pair of points and storing the result in an adjacency matrix. The heuristic was constructed by taking the maze distance to the closest food item plus the total weight across the minimum spanning tree of uneaten food items. Computing the adjacency matrix was an expensive operation, and therefore the result was cached and variants on the matrix were constructed by zeroing out the rows and columns corresponding to already eaten food pellets. The figure below shows the full and partial adjacency matrices for the medium search problem.
This approach was graded 5/4 and expanded 255 nodes.
Experiments with Manhattan distance rather than maze distance yielded a grade of 4/4 and expanded 7137 nodes. Furthermore, this program version took significantly longer to run, confirming that computing the expensive adjacency matrix before searching for the solution reduced the overall cost.
However, the process of computing the initial adjacency matrix could be hugely optimized with more aggressive caching. The current solution independently computes the pairwise distances between food pellets, despite the computation for any given pair (that aren’t a distance=1 from each other) necessarily traversing over many intermediate food pellets. Given that BFS runs in O(V+E), where V is the number of legal positions in the maze and E is the number of valid transitions between positions. Therefore, the current computation of the matrix is O(F^2(V+E)) for F food pellets. As F <= V and the grid structure of the graph restricts E = cV, 1 < c < 4, in the worst case this is becomes O(V^3). An improvement to this algorithm would be to use Prim’s algorithm to construct a minimum spanning from each food pellet and record cumulative distances as the tree is built. Prim’s algorithm has time complexity O(E ln V), therefore, this algorithm would be O(FE ln V) = O(V^2 ln V).
The following is the core bits of code for the solution:
classFoodSearchProblem: """ A search problem associated with finding the a path that collects all of the food (dots) in a Pacman game. A search state in this problem is a tuple ( pacmanPosition, foodGrid ) where pacmanPosition: a tuple (x,y) of integers specifying Pacman's position foodGrid: a Grid (see game.py) of either True or False, specifying remaining food """ def__init__(self, startingGameState: pacman.GameState): self.start = (startingGameState.getPacmanPosition(), startingGameState.getFood()) self.walls = startingGameState.getWalls() self.startingGameState = startingGameState self._expanded = 0# DO NOT CHANGE self.heuristicInfo = { 'initialFood': startingGameState.getFood().asList(), 'foodIndices': getFoodIndices(startingGameState), 'foodAdjMatrix': getFoodAdjacencyMatrix(startingGameState), 'mazeDistFn': partial(mazeDistance, startingGameState) } # A dictionary for the heuristic to store information ...
defminimumSpanningTreeTotalWeight(adjM): g = nx.from_numpy_matrix(adjM) mst = nx.minimum_spanning_tree(g) returnsum(e[2]['weight'] for e in mst.edges(data=True))
deffoodAdjMatrixAfterEating(problem, foodLeft): foodList = problem.heuristicInfo['initialFood'] foodIndices = problem.heuristicInfo['foodIndices'] M = problem.heuristicInfo['foodAdjMatrix'].copy() for f inset(foodList).difference(set(foodLeft)): i = foodIndices[f] M[i, :] = M[:, i] = 0 return M
deffoodHeuristic(state: Tuple[Tuple, List[List]], problem: FoodSearchProblem): """ Your heuristic for the FoodSearchProblem goes here. This heuristic must be consistent to ensure correctness. First, try to come up with an admissible heuristic; almost all admissible heuristics will be consistent as well. If using A* ever finds a solution that is worse uniform cost search finds, your heuristic is *not* consistent, and probably not admissible! On the other hand, inadmissible or inconsistent heuristics may find optimal solutions, so be careful. The state is a tuple ( pacmanPosition, foodGrid ) where foodGrid is a Grid (see game.py) of either True or False. You can call foodGrid.asList() to get a list of food coordinates instead. If you want access to info like walls, capsules, etc., you can query the problem. For example, problem.walls gives you a Grid of where the walls are. If you want to *store* information to be reused in other calls to the heuristic, there is a dictionary called problem.heuristicInfo that you can use. For example, if you only want to count the walls once and store that value, try: problem.heuristicInfo['wallCount'] = problem.walls.count() Subsequent calls to this heuristic can access problem.heuristicInfo['wallCount'] """ position, foodGrid = state "*** YOUR CODE HERE ***" foodLeft = foodGrid.asList() adjM = foodAdjMatrixAfterEating(problem, foodLeft) mstTotalW = minimumSpanningTreeTotalWeight(adjM) distToPacman = partial(problem.heuristicInfo['mazeDistFn'], position) closestFood = min(map(distToPacman, foodLeft), default=0) return closestFood + mstTotalW
... defmazeDistance(gameState: pacman.GameState, point1: Tuple[int, int], point2: Tuple[int, int]) -> int: """ Returns the maze distance between any two points, using the search functions you have already built. The gameState can be any game state -- Pacman's position in that state is ignored. Example usage: mazeDistance( (2,4), (5,6), gameState) This might be a useful helper function for your ApproximateSearchAgent. """ x1, y1 = point1 x2, y2 = point2 walls = gameState.getWalls() assertnot walls[x1][y1], 'point1 is a wall: ' + str(point1) assertnot walls[x2][y2], 'point2 is a wall: ' + str(point2) prob = PositionSearchProblem(gameState, start=point1, goal=point2, warn=False, visualize=False) returnlen(search.bfs(prob))
Q8 (3 pts): Suboptimal Search
Sometimes, even with A* and a good heuristic, finding the optimal path through all the dots is hard. In these cases, we’d still like to find a reasonably good path, quickly. In this section, you’ll write an agent that always greedily eats the closest dot. ClosestDotSearchAgent is implemented for you in searchAgents.py, but it’s missing a key function that finds a path to the closest dot.
Implement the function findPathToClosestDot in searchAgents.py. Our agent solves this maze (suboptimally!) in under a second with a path cost of 350:
Hint: The quickest way to complete findPathToClosestDot is to fill in the AnyFoodSearchProblem, which is missing its goal test. Then, solve that problem with an appropriate search function. The solution should be very short!
Your ClosestDotSearchAgent won’t always find the shortest possible path through the maze. Make sure you understand why and try to come up with a small example where repeatedly going to the closest dot does not result in finding the shortest path for eating all the dots.
Grading: Please run the below command to see if your implementation passes all the autograder test cases.
deffindPathToClosestDot(self, gameState: pacman.GameState): """ Returns a path (a list of actions) to the closest dot, starting from gameState. """ # Here are some useful elements of the startState startPosition = gameState.getPacmanPosition() food = gameState.getFood() walls = gameState.getWalls() problem = AnyFoodSearchProblem(gameState)
"*** YOUR CODE HERE ***" # util.raiseNotDefined() return search.bfs(problem) classAnyFoodSearchProblem(PositionSearchProblem): """ A search problem for finding a path to any food. This search problem is just like the PositionSearchProblem, but has a different goal test, which you need to fill in below. The state space and successor function do not need to be changed. The class definition above, AnyFoodSearchProblem(PositionSearchProblem), inherits the methods of the PositionSearchProblem. You can use this search problem to help you fill in the findPathToClosestDot method. """
def__init__(self, gameState): "Stores information from the gameState. You don't need to change this." # Store the food for later reference self.food = gameState.getFood()
# Store info for the PositionSearchProblem (no need to change this) self.walls = gameState.getWalls() self.startState = gameState.getPacmanPosition() self.costFn = lambda x: 1 self._visited, self._visitedlist, self._expanded = {}, [], 0# DO NOT CHANGE
defisGoalState(self, state: Tuple[int, int]): """ The state is Pacman's position. Fill this in with a goal test that will complete the problem definition. """ x,y = state
"*** YOUR CODE HERE ***" # util.raiseNotDefined() return self.food[x][y]
Question q8 =========== [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_1.test *** pacman layout: Test 1 *** solution length: 1 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_10.test *** pacman layout: Test 10 *** solution length: 1 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_11.test *** pacman layout: Test 11 *** solution length: 2 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_12.test *** pacman layout: Test 12 *** solution length: 3 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_13.test *** pacman layout: Test 13 *** solution length: 1 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_2.test *** pacman layout: Test 2 *** solution length: 1 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_3.test *** pacman layout: Test 3 *** solution length: 1 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_4.test *** pacman layout: Test 4 *** solution length: 3 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_5.test *** pacman layout: Test 5 *** solution length: 1 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_6.test *** pacman layout: Test 6 *** solution length: 2 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_7.test *** pacman layout: Test 7 *** solution length: 1 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_8.test *** pacman layout: Test 8 *** solution length: 1 [SearchAgent] using function depthFirstSearch [SearchAgent] using problem type PositionSearchProblem *** PASS: test_cases/q8/closest_dot_9.test *** pacman layout: Test 9 *** solution length: 1