DS/CS 442 Spring 2021
Project 2: Multi-Agent Search
Due: Wednesday 3/3 at 11:59 pm
Pacman, now with ghosts.
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 evaluation function design.
The code base has not changed much from the previous project, but please start with a fresh installation, rathe
than intermingling files from project 1.
As in project 1, this project includes an autograder for you to grade your answers on your machine. This can be
un on all questions with the command:
Note: If your python refers to Python 2.7, you may need to invoke python3 autograder.py (and
similarly for all subsequent Python invocations) or create a conda environment as described in Project 0.
It can be run for one particular question, such as q2, by:
It can be run for one particular test by commands of the form:
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.
See the autograder tutorial in Project 0 for more information about using the autograder.
The code for this project contains the following files, available as a zip archive.
Files you'll edit:
multiAgents.py Where all of your multi-agent search agents will reside.
Files you might want to look at:
pacman.py The main file that runs Pacman games. This file also describes a
Pacman GameState type, which you will use extensively in this
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. You don't
need to use these for this project, but may find other functions
defined here to be useful.
Supporting files you can ignore:
graphicsDisplay.py Graphics for Pacman
python autograder.py -q q2
python autograder.py -t test_cases/q2/0-small-tree
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 autograde
testParser.py Parses autograder test and solution files
testClasses.py General autograding test classes
test_cases/ Directory containing the test cases for each question
multiagentTestClasses.py Project 2 specific autograding test classes
Files to Edit and Submit: You will fill in portions of multiAgents.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 this file.
Evaluation: Your code will be autograded for technical co
ectness. 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
ectness 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
edundancy. 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 ou
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 Multi-Agent Pacman
First, play a game of classic Pacman by running the following command:
and using the a
ow keys to move. Now, run the provided ReflexAgent in multiAgents.py
Note that it plays quite poorly even on simple layouts:
Inspect its code (in multiAgents.py ) and make sure you understand what it’s doing.
Question 1 (4 points): Reflex Agent
Improve the ReflexAgent in 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
eliably clear the testClassic layout:
python pacman.py -p ReflexAgent
python pacman.py -p ReflexAgent -l testClassic
python 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):
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: Remember that newFood has the function asList()
Note: As features, try the reciprocal of important values (such as distance to food) rather than just the values
Note: The evaluation function you’re writing is evaluating state-action pairs; in later parts of the project, you’ll
e evaluating states.
Note: You may find it useful to view the internal contents of various objects for debugging. You can do this by
printing the objects’ string representations. For example, you can print newGhostStates with
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 500, or 2
points if it is greater than 1000. You can try your agent out under these conditions with
To run it without graphics, use:
Don’t spend too much time on this question, though, as the meat of the project lies ahead.
Question 2 (5 points): Minimax
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 a
itrary 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: 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 co
ect number of game states.
This is the only 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
This will show what your algorithm does on a number of small trees, as well as a pacman game. To run it without
Hints and Observations
Hint: Implement the algorithm recursively using helper function(s).
python pacman.py --frameTime 0 -p ReflexAgent -k 1
python pacman.py --frameTime 0 -p ReflexAgent -k 2
python autograder.py -q q1
python autograder.py -q q1 --no-graphics
python autograder.py -q q2
python autograder.py -q q2 --no-graphics
ect implementation of minimax will lead to Pacman losing the game in some tests. This is not a
problem: as it is co
ect behaviour, it will pass the tests.
The evaluation function for the Pacman test in this part is already written
( 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 cu
The minimax values of the initial state in the minimaxClassic layout 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.
Pacman is always agent 0, and the agents move in order of increasing agent index.
All states in minimax should be GameStates , either passed in to getAction or generated via
GameState.generateSuccessor . In this project, you will not be abstracting to simplified states.
On larger boards such as openClassic and 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 wo
y if you see this behavior, question 5 will clean up all of these issues.
When Pacman believes that his death is unavoidable, he will try to end the game as soon as possible
ecause of the constant penalty for living. Sometimes, this is the wrong thing to do with random ghosts,
ut minimax agents always assume the worst:
Make sure you understand why Pacman rushes the closest ghost in this case.
Question 3 (5 points): Alpha-Beta Pruning
Make a new agent that uses alpha-beta pruning to more efficiently explore the minimax tree, in