Project 2: Multi-Agent Pacman

Table of Contents

Pacman, now with ghosts.
Minimax, Expectimax,


In this project, your team 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 evaluation function design.

As in project 1, this project includes an autograder for you to grade your answers on your machine. This can be run on all questions with the command:


It can be run for one particular question, such as q2, by:

python -q q2

It can be run for one particular test by commands of the form:

python -t test_cases/q2/0-small-tree

By default, the autograder displays graphics with the -t option, but doesn't with the -q option. You can force graphics by using the --graphics flag, or force no graphics by using the --no-graphics flag.

The code base has not changed much from the previous project, but please start with a fresh installation, rather than intermingling files from project 1. You can, however, use your and in any way you want.

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. 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.
Files you can ignore 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 Project autograder Parses autograder test and solution files General autograding test classes
test_cases/ Directory containing the test cases for each question Project 2 specific autograding test classes


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

Please use turnin to submit your work. Each team should submit only one copy.

In addition, please send me an email with the following information:

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, clarity, and creativity of your implementation -- not the autograder's judgements -- will be the final judge of your score. Don't forget: I will meet with some of you for code reviews to discuss your work in detail.

Multi-Agent Pacman

First, 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 (5 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 (and animation off to speed up the display):
python --frameTime 0 -p ReflexAgent -k 1
python --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 reciprocals 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: I 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, 2 points if your agent wins at least 8 times, or 3 points if your agent wins all 10 games. You will receive an additional 1 point if your agent's average score is greater than 500. If your agent's average score is greater than 1000, I'll give you one more point. You can try your agent out under these conditions with

python -q q1

To run it without graphics, use:

python -q q1 --no-graphics

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

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. Score the leaves of your minimax tree with the supplied self.evaluationFunction, which defaults to scoreEvaluationFunction. MinimaxAgent extends MultiAgentSearchAgent, 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: Here we'll say that a full move in the search tree is one Pacman move and all the ghosts' responses, so depth 2 search will involve Pacman and each ghost moving two times.

Grading: I 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.getLegalActions. If you call it any more or less than necessary, the autograder will complain. Note, however, that the autograder will accept solutions both with and without the Directions.STOP action available. To test and debug your code, run

python -q q2

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:

python -q q2 --no-graphics

Hints and Observations

Question 3 (5 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).

python -p AlphaBetaAgent -a depth=4 -l smallClassic

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.

Grading: Because the autograder checks your code to determine whether it explores the correct number of states, it is important that you perform alpha-beta pruning without reordering children. In other words, successor states should always be processed in the order returned by GameState.getLegalActions. Again, do not call GameState.generateSuccessor more than necessary.

You must not prune on equality in order to match the set of states explored by the autograder. (Indeed, alternatively, but incompatible with the autograder, would be to also allow for pruning on equality and invoke alpha-beta once on each child of the root node, but this will not match the autograder.)

To test and debug your code, run

python -q q3

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:

python -q q3 --no-graphics

The correct implementation of alpha-beta pruning will lead to Pacman losing some of the tests. This is not a problem; as it is correct behavior, it will pass the tests.

Question 4 (5 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 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 can debug your implementation on small game trees using the command:

python -q q4

Once your algorithm is working on small trees, you can observe its success in Pacman. To see how the ExpectimaxAgent behaves in Pacman, run:

python -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. Investigate the results of these two scenarios:

python -p AlphaBetaAgent -l trappedClassic -a depth=3 -q -n 10
python -p ExpectimaxAgent -l trappedClassic -a depth=3 -q -n 10
You should find that your ExpectimaxAgent wins about half the time, while your AlphaBetaAgent always loses. Make sure you understand why the expectimax behavior differs from the minimax case.

Question 5 (5 points) 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 last project.

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. The autograder provided to you with the assignment tests your evaluation function on smallClassic with 1 ghost 10 times and assigns points (out of 6 possible points) as follows:

Try it out with

python -q q5

Grading: I will run your agent on the smallClassic layout with two ghosts 10 times. I will assign points to your evaluation function in the following way:

Hints and Observations

Mini Contest (3 points extra credit) Pacman's been doing well so far, but things are about to get a bit more challenging. This time, Pacman will be pitted against smarter foes in a trickier maze. In particular, the ghosts will actively chase Pacman instead of wandering around randomly, and the maze features more twists and dead-ends, but also extra pellets to give Pacman a fighting chance. You're free to have Pacman use any search procedure, search depth, and evaluation function you like. The only limit is that games can last a maximum of 3 minutes (with graphics off), so be sure to use your computation wisely. I'll run the contest with the following command:

python -l contestClassic -p ContestAgent -g DirectionalGhost -q -n 5

Provided your individual games never take longer than 3 minutes, I'll discard the highest and lowest scores and then average the remaining three scores. The two teams with the highest scores will receive 3 and 2 extra credit points respectively and can look on with pride as their Pacman agents are shown off in class.

Project 2 is done. Go Pacman!