Project 2: Multi-Agent Pacman

Due Monday, 10/1 at 11:59pm (Extended!)
*NOTE*: Important changes were made to this description and on Thursday 9/20 @ 5pm

Pacman, now with ghosts.
Minimax, Expectimax,


In this project, you will design agents for the classic version of Pacman, including ghosts. Along the way, you will implement both minimax and expectimax search and try your hand at evalution function design.

The code base has not changed significantly from the previous project, but please start with a fresh installation of this code for the project, rather than intermingling files from previous checkpoints. You can, however, use your and in any way you see fit.

The code for this project contains the following files, available as a zip archive.

Key files to read: Where all of your multi-agent search agents will reside. Updated! The main file that runs Pacman games. This file also describes a Pacman GameState type, which you will use extensively 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.
Supporting files: 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


What to submit: You will fill in portions of during the assignment. You should submit this file with your code and comments. You may also submit supporting files (like,, etc.). Please do not change the other files in this distribution.

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 newsgroup 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.


Multi-Agent Pacman

Question 0 (0 points)  Play a game of classic Pacman:

Now run the provided ReflexAgent in
python -p ReflexAgent
Note that it plays quite poorly even on simple layouts:
python -p ReflexAgent -l testClassic
Inspect its code (in and make sure you understand what it's doing.

Question 1 (3 points)  Improve the ReflexAgent in 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 testClassic layout:

python -p ReflexAgent -l testClassic
Try out your reflex agent on the default mediumClassic layout with one ghost or two.
python -p ReflexAgent -k 1
python -p ReflexAgent -k 2

Note that you can never have more ghosts than the layout permits. How does your agent fare? It will likely often die with 2 or more ghosts on the default board, unless your evaluation function is quite good.

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. You can also play multiple games in a row with -n.

The autograder will check that your agent can rapidly clear the openClassic layout three times without dying or thrashing (i.e. repeatedly moving back and forth between two positions, making no progress).

python -p ReflexAgent -l openClassic -n 3

Don't spend too much time on this question, though, as the meat of the project lies ahead.

Extra Credit (2 points) The three best reflex agents in the class on trickyClassic with directional ghosts will receive recognition in class and a little extra credit.

python -p ReflexAgent -l trickyClassic -g DirectionalGhost

Question 2 (5 points) Now you will write an adversarial search agent in the provided MinimaxAgent class stub in 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 appears in the textbook. 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 and use a provided evaluation function. MinimaxAgent extends MultiAgentAgent, which gives access to self.depth and 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. Hence, the reflex agent in question 1 is not a full one-ply minimax agent, but instead a "partial-ply" searcher.

Score the leaves of your minimax tree with the inherited self.evaluationFunction, which defaults to scoreEvaluationFunction.

Hints and Observations

Question 3 (3 points) Make a new agent that uses alpha-beta pruning to more efficiently explore the minimax tree, in AlphaBetaAgent. Again, your algorithm will be slightly more general than the pseudo-code in the textbook, so part of the challenge is to extend the alpha-beta pruning logic appropriately to multiple minimizer agents.

You should see a speed-up (perhaps depth 3 alpha-beta will run as fast as depth 2 minimax). Ideally, depth 3 on mediumClassic should run in just a few seconds per move or faster.

python -p AlphaBetaAgent -d 3

The AlphaBetaAgent minimax values should be identical to the MinimaxAgent minimax values, although the actions it selects can vary because of different tie-breaking behavior. Again, the minimax values of the initial state in the minimaxClassic layout are 9, 8, 7 and -492 for depths 1, 2, 3 and 4 respectively.

Question 4 (3 points) Random ghosts are of course not optimal minimax agents, and so modeling them with minimax search may not be appropriate. Fill in ExpectimaxAgent, where your agent agent 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 RandomGhost ghosts, which choose amongst their getLegalActions uniformly at random.

You should now observer a more cavalier approach to 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:

python -p ExpectimaxAgent -l trappedClassic -d 3
You may have to run this scenario a few times to see Pacman's gamble pay off. Make sure you understand why the behavior here differs from the minimax case.

Question 5 (6 points) Write a better evaluation function for pacman in the provided function betterEvaluationFunction. You may use any tools at your disposal for evaluation, including your search code from the last project. Note that you don't need to change your agent code at all; the -b or --betterEvaluation option will load betterEvaluationFunction into the self.evaluationFunction variable of each of your adversarial search agents from questions 2, 3 and 4. With depth 2 search, your evaluation function should clear the default layout with two random ghosts more than half the time for full credit:

python -p ExpectimaxAgent --betterEvaluation
The autograder will run multiple games and bound your average win rate.

Document your evaluation function! You will be graded both on the performance of your evaluation function and the ingenuity of what you tried. We're very curious about what great ideas you have, so don't be shy.

Extra Credit (2 points) To reward you with everlasting glory, we will announce the highest scoring entries in lecture. The top 3 will receive a little extra credit. The conditions of the contest below have been updated (9/24 @ 12pm).

python -p AlphaBetaAgent -b -l trickyClassic -g DirectionalGhost -k 3 

Go Pacman!