DM828 - Assignment 4: Multi-Agent Pac-Man

Pac-Man, now with ghosts.
Minimax, Expectimax,
Evaluation.

Introduction

In this assignment, you will design agents for the classic version of Pac-Man, 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 assignment 1, but please start with a fresh installation, rather than intermingling files from project 1. You can, however, use your search.py and searchAgents.py in any way you want.

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

Key files to read
multiAgents.py Where all of your multi-agent search agents will reside. The mini-contest part of this assignment series has been removed, hence ignore ContestAgent.
pacman.py The main file that runs Pac-Man games. This file also describes a Pac-Man GameState type, which you will use extensively in this project
game.py The logic behind how the Pac-Man world works. This file describes several supporting types like AgentState, Agent, Direction, and Grid.
util.py Useful data structures for implementing search algorithms.
Files you can ignore
graphicsDisplay.py Graphics for Pac-Man
graphicsUtils.py Support for Pac-Man graphics
textDisplay.py ASCII graphics for Pac-Man
ghostAgents.py Agents to control ghosts
keyboardAgents.py Keyboard interfaces to control Pac-Man
layout.py Code for reading layout files and storing their contents

 

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

Deadline:

23.59 of Tuesday, December 27, 2011

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 the autograder. However, the correctness of your implementation -- not the autograder's output -- will be the final judge of your score.

The grade serves only as a form of feedback and it will not be used for the final grade that depends only by the written exam. For this assignment: if you make an honest effort at questions 2 and 3 you will pass, otherwise fail. Do question 1 but don't spend too much time on it as the questions 2 and 3 are more relevant for the application of the material discussed in class. The points corresponding to a question are left to indicate the level of difficulty.

Getting Help: You are not alone! If you find yourself stuck on something, ask your colleagues or contact the teacher for help. He doesn't know when or how to help unless you ask. But in any case do not ask for code, ask only verbal help. You must implement the required procedures yourself. One more piece of advice: if you don't know what a variable does or what kind of values it takes, print it out.

 

Multi-Agent Pac-Man

First, play a game of classic Pac-Man:

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

Question 1 (3 points)  Improve the ReflexAgent in multiAgents.py to play respectably. The provided reflex agent code exhibits 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 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):
python pacman.py --frameTime 0 -p ReflexAgent -k 1
python 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.

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

python pacman.py -p ReflexAgent -l openClassic -n 10 -q

Question 2 (5 points) 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 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: A single search ply is considered to be one Pac-Man move and all the ghosts' responses, so depth 2 search will involve Pac-Man and each ghost moving two times.

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 smallClassic should run in just a few seconds per move or faster.

python pacman.py -p AlphaBetaAgent -a depth=3 -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.

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 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 observe a more cavalier approach in close quarters with ghosts. In particular, if Pac-Man 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 pacman.py -p AlphaBetaAgent -l trappedClassic -a depth=3 -q -n 10
python pacman.py -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 behavior here differs from the minimax case.

Question 5 (6 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 two random ghosts more than half the time and still run at a reasonable rate (to get full credit, Pac-Man should be averaging around 1000 points when he's winning).

python pacman.py -l smallClassic -p ExpectimaxAgent -a evalFn=better -q -n 10

Hints and Observations

This was the last Assignment for this year! Go Pac-Man!