All those colored walls,
Mazes give Pacman the blues,
So teach him to search.
This project consists of two parts: search and multiagent games.
In the search part, 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.
In the games part, you will design agents for the classic version of Pacman, including ghosts. Along the way, you will implement both reflex agents and minimax search and try your hand at evaluation function design.
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:
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 and unzip all the code and supporting files from search_and_games.zip.
||Where all of your search algorithms will reside.|
||Where all of your search-based agents will reside.|
||Where all of your multi-agent search agents will reside.|
||The main file that runs Pacman games. This file describes a Pacman GameState type,
which you use in this project.
||The logic behind how the Pacman world works. This file describes several supporting types
like AgentState, Agent, Direction, and Grid.
||Useful data structures for implementing search algorithms.|
||Project 1 specific autograding test classes|
||Graphics for Pacman|
||Support for Pacman graphics|
||ASCII graphics for Pacman|
||Agents to control ghosts|
||Keyboard interfaces to control Pacman|
||Code for reading layout files and storing their contents|
||Parses autograder test and solution files|
||General autograding test classes|
||Directory containing the test cases for each question|
Files to Edit and Submit: You will fill in portions of
multiAgents.py during the assignment. You should submit these files with your code and comments. 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, recitation, and Piazza 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.
This assignment is based on the Pacman AI projects developed at UC Berkeley, http://ai.berkeley.edu.
After downloading the code (search_and_games.zip), 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:
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:
python36 pacman.py --layout testMaze --pacman GoWestAgent
But, things get ugly for this agent when turning is required:
python36 pacman.py --layout tinyMaze --pacman GoWestAgent
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.
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:
python36 pacman.py -h
Also, all of the commands that appear in this portion of the 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
Note: if you get error messages regarding Tkinter, see this page
iterativeDeepeningSearch function in
search.py, implement an iterative-deepening search algorithm. Begin by modifying the graph search algorithm presented in lecture to implement depth-limited DFS graph search. You will probably want to make use of the
Node class in
Test your code using:
python36 pacman.py -l threeByOneMaze -p SearchAgent -a fn=ids
python36 pacman.py -l testMaze -p SearchAgent -a fn=ids
python36 pacman.py -l mediumMaze -p SearchAgent -a fn=ids
python36 pacman.py -l contoursMaze -p SearchAgent -a fn=ids
python36 pacman.py -l bigMaze -p SearchAgent -a fn=ids -z .5
In addition to the pacman mazes, you can test you code with the autograder given to you. You can run the full autograder, run one specific question (
-q), or run one specific test case (
python36 autograder.py -q q1
python36 autograder.py -t test_cases/graph_backtrack
You can see both the test cases and the test solutions by viewing the text in the
*.solution files, respectively.
A few additional notes:
Implement A* graph search in the empty function
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 will probably want to make use of the
Node class in
search.py and the
PriorityQueue class in
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
python36 pacman.py -l bigMaze -z .5 -p SearchAgent -a fn=astar,heuristic=manhattanHeuristic
Our implementation expands 549 search nodes, but ties in priority may make your numbers differ slightly. What happens on
openMaze for the various search strategies?
Note: Make sure to complete Question 2 before working on Question 3, because Question 3 builds upon your answer for Question 2.
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
layouts/tinyCorners.lay, the shortest path does not always go to the closest food first! Hint: the shortest path through
tinyCorners takes 28 steps.
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:
python36 pacman.py -l tinyCorners -p SearchAgent -a fn=astar,prob=CornersProblem
python36 pacman.py -l mediumCorners -p SearchAgent -a fn=astar,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: 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.
Our implementation of
aStarSearch with the null heuristic expands just under 2000 search nodes on
layouts/mediumCorners.lay. However, nontrivial heuristics (used with A* search) can reduce the amount of searching required.
Note: Make sure to complete Question 2 before working on Question 4, because Question 4 builds upon your answer for Question 2.
Implement a non-trivial, consistent heuristic for the
python36 pacman.py -l mediumCorners -p AStarCornersAgent -z 0.5
AStarCornersAgent is a shortcut for
-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!
Note: Make sure to complete Question 2 before working on Question 5, because Question 5 builds upon your answer for Question 2.
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:
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 set of questions.) 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
layouts/testSearch.lay with no code change on your part (total cost of 7).
python36 pacman.py -l testSearch -p AStarFoodSearchAgent
AStarFoodSearchAgent is a shortcut for
-p SearchAgent -a fn=astar,prob=FoodSearchProblem,heuristic=foodHeuristic.
searchAgents.py with a consistent heuristic for the
FoodSearchProblem. Try your agent on the
python36 pacman.py -l trickySearch -p AStarFoodSearchAgent
Our A* agent with the null heuristic 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
layouts/mediumSearch.lay in a short time? If so, we're either very, very impressed, or your heuristic is inconsistent.
Now Pacman will play against other agents, first play a quick game:
Now, run the provided
python36 pacman.py -p ReflexAgent
Note that it plays quite poorly even on simple layouts:
python36 pacman.py -p ReflexAgent -l testClassic
Inspect its code (in
multiAgents.py) and make sure you understand what it's doing.
multiAgents.py to play respectably. The provided reflex agent code provides some helpful examples of methods that query the
GameState for information. A capable reflex agent will have to consider both food locations and ghost locations to perform well. Your agent should easily and reliably clear the
python36 pacman.py -p ReflexAgent -l testClassic
Try out your reflex agent on the default
mediumClassic layout with one ghost or two (and animation off to speed up the display):
python36 pacman.py --frameTime 0 -p ReflexAgent -k 1
python36 pacman.py --frameTime 0 -p ReflexAgent -k 2
How does your agent fare? It will likely often die with 2 ghosts on the default board, unless your evaluation function is quite good.
Note: you can never have more ghosts than the layout permits.
Note: As features, try the reciprocal of important values (such as distance to food) rather than just the values themselves.
Note: The evaluation function you're writing is evaluating state-action pairs; in later parts of the project, you'll be evaluating states.
Options: Default ghosts are random; you can also play for fun with slightly smarter directional ghosts using
-g DirectionalGhost. If the randomness is preventing you from telling whether your agent is improving, you can use
-f to run with a fixed random seed (same random choices every game). You can also play multiple games in a row with
-n. Turn off graphics with
-q to run lots of games quickly.
Grading: we will run your agent on the
openClassic layout 10 times. You will receive 0 points if your agent times out, or never wins. You will receive 1 point if your agent wins at least 5 times, or 2 points if your agent wins all 10 games. You will receive an addition 1 point if your agent's average score is greater than 1000. You can try your agent out under these conditions with
python36 autograder.py -q q6
To run it without graphics, use:
python36 autograder.py -q q6 --no-graphics
Don't spend too much time on this question, though, as there is more work to be done in the last several questions.
Now you will write an adversarial search agent in the provided
MinimaxAgent class stub in
multiAgents.py. Your minimax agent should work with any number of ghosts, so you'll have to write an algorithm that is slightly more general than what you've previously seen in lecture. In particular, your minimax tree will have multiple min layers (one for each ghost) for every max layer.
Your code should also expand the game tree to an arbitrary depth. Score the leaves of your minimax tree with the supplied
self.evaluationFunction, which defaults to
MultiAgentSearchAgent, which gives access to
self.evaluationFunction. Make sure your minimax code makes reference to these two variables where appropriate as these variables are populated in response to command line options.
Important: A single search ply is considered to be one Pacman move and all the ghosts' responses, so depth 2 search will involve Pacman and each ghost moving two times.
Grading: We will be checking your code to determine whether it explores the correct number of game states. This is the only way reliable way to detect some very subtle bugs in implementations of minimax. As a result, the autograder will be very picky about how many times you call
GameState.generateSuccessor. If you call it any more or less than necessary, the autograder will complain. To test and debug your code, run
python36 autograder.py -q q7
This will show what your algorithm does on a number of small trees, as well as a pacman game. To run it without graphics, use:
python36 autograder.py -q q7 --no-graphics
Hints and Observations
self.evaluationFunction). You shouldn't change this function, but recognize that now we're evaluating *states* rather than actions, as we were for the reflex agent. Look-ahead agents evaluate future states whereas reflex agents evaluate actions from the current state.
minimaxClassiclayout are 9, 8, 7, -492 for depths 1, 2, 3 and 4 respectively. Note that your minimax agent will often win (665/1000 games for us) despite the dire prediction of depth 4 minimax.
python36 pacman.py -p MinimaxAgent -l minimaxClassic -a depth=4
GameStates, either passed in to
getActionor generated via
GameState.generateSuccessor. In this project, you will not be abstracting to simplified states.
mediumClassic(the default), you'll find Pacman to be good at not dying, but quite bad at winning. He'll often thrash around without making progress. He might even thrash around right next to a dot without eating it because he doesn't know where he'd go after eating that dot. Don't worry if you see this behavior, question 9 will clean up all of these issues.
python36 pacman.py -p MinimaxAgent -l trappedClassic -a depth=3Make sure you understand why Pacman rushes the closest ghost in this case.
Minimax and alpha-beta are great, but they both assume that you are playing against an adversary who makes optimal decisions. As anyone who has ever won tic-tac-toe can tell you, this is not always the case. In this question you will implement the
ExpectimaxAgent, which is useful for modeling probabilistic behavior of agents who may make suboptimal choices.
As with the search and constraint satisfaction problems covered so far in this class, the beauty of these algorithms is their general applicability. To expedite your own development, we've supplied some test cases based on generic trees. You can debug your implementation on small game trees using the command:
python36 autograder.py -q q8
Debugging on these small and manageable test cases is recommended and will help you to find bugs quickly. Make sure when you compute your averages that you use floats. (Integer division in older versions of Python truncated values, so that
1/2 = 0, unlike the case with floats where
1.0/2.0 = 0.5).
Once your algorithm is working on small trees, you can observe its success in Pacman. Random ghosts are of course not optimal minimax agents, and so modeling them with minimax search may not be appropriate.
ExpectimaxAgent, will no longer take the min over all ghost actions, but the expectation according to your agent's model of how the ghosts act. To simplify your code, assume you will only be running against an adversary which chooses amongst their
getLegalActions uniformly at random.
To see how the ExpectimaxAgent behaves in Pacman, run:
python36 pacman.py -p ExpectimaxAgent -l minimaxClassic -a depth=3
You should now observe a more cavalier approach in close quarters with ghosts. In particular, if Pacman perceives that he could be trapped but might escape to grab a few more pieces of food, he'll at least try.
The correct implementation of expectimax will lead to Pacman losing some of the tests. This is not a problem: as it is correct behavior, it will pass the tests.
Write a better evaluation function for pacman in the provided function
betterEvaluationFunction. The evaluation function should evaluate states, rather than actions like your reflex agent evaluation function did. You may use any tools at your disposal for evaluation, including your search code from the previous parts. With depth 2 search, your evaluation function should clear the
smallClassic layout with one random ghost more than half the time and still run at a reasonable rate (to get full credit, Pacman should be averaging around 1000 points when he's winning).
python36 autograder.py -q q9
Grading: the autograder will run your agent on the
smallClassic layout 10 times. We will assign points to your evaluation function in the following way:
python36 pacman.py -p ExpectimaxAgent -l threeByOneMaze -a evalFn=better
Complete Questions 1 through 9 as specified in the project instructions. Then upload
search.py, searchAgents.py, multiAgents.py to Gradescope.
Prior to submitting, be sure you run the autograder on your own machine. Running the autograder locally will help you to debug and expediate your development process. The autograder can be invoked on your own machine using the command:
To run the autograder on a single question, such as question 3, invoke it by
python36 autograder.py -q q3
Note that running the autograder locally will not register your grades with us. Remember to submit your code below when you want to register your grades for this assignment.
The autograder on Gradescope might take a while but don't worry: so long as you submit before the due date, it's not late.