Project 2: PACMAN (CS 165A Spring 2022)

Acknowledgments: This assignment is based on the Pacman AI projects developed at UC Berkeley, http://ai.berkeley.edu. We thank CMU instructors Pat Virtue and Stephanie Rosenthal for giving us permission to use their fork of the project as well as the autograder.

Search and Games

Introduction

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.

This project includes an autograder for you to grade your answers on your machine. This can be run with the command:

python autograder.py

We suggest you using python3 (python3.6, python3.7) for the project. 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.

Files you will edit

search.py Where all of your search algorithms will reside. searchAgents.py Where all of your search-based agents will reside. multiAgents.py Where all of your multi-agent search agents will reside.

Files you might want to look at

pacman.pyThe main file that runs Pacman games. This file describes a Pacman GameState type,
which you use in this project. 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.

Files you will not edit

agentTestClasses.py Specific autograding test classes graphicsDisplay.py Graphics for Pacman graphicsUtils.pySupport for Pacman graphics textDisplay.pyASCII graphics for Pacman ghostAgents.pyAgents to control ghosts keyboardAgents.pyKeyboard interfaces to control Pacman layout.py Code for reading layout files and storing their contents autograder.py Project autograder testParser.pyParses autograder test and solution files testClasses.pyGeneral autograding test classes test_cases/Directory containing the test cases for each question

Files to Edit and Submit: You will fill in portions of search.py, searchAgents.py(if you do not edit it please submit the original searchAgents.py) and 'multiAgents.py' during the assignment. You should submit these three 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: As the syllabus stated, discussion amongst your peers and with the TA is allowed, but must be done on the strategy / algorithm level. Each student needs to write his / her own code and submit his / her independnet work. 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.

Welcome to Pacman

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:

python pacman.py

(On some platforms, you will need to replace python pacman.py with python3 pacman.py throughout.)

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: python pacman.py --layout testMaze --pacman GoWestAgent

But, things get ugly for this agent when turning is required: python 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. Note that 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: python pacmen.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 bash commands.txt. Note: if you get error messages regarding Tkinter, see this page

Question 1 (15 points): Iterative Deepening

In the 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 search.py.

Test your code using:

python pacman.py -l threeByOneMaze -p SearchAgent -a fn=ids

python pacman.py -l testMaze -p SearchAgent -a fn=ids

python pacman.py -l mediumMaze -p SearchAgent -a fn=ids

python pacman.py -l contoursMaze -p SearchAgent -a fn=ids

python 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 (-t):

python autograder.py

python autograder.py -q q1

python autograder.py -t test_cases/graph_backtrack

You can see both the test cases and the test solutions by viewing the text in the *.test and *.solution files, respectively.

A few additional notes:

Implement A graph search in the empty function aStarSearch in 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 util.py.

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 manhattanHeuristic in searchAgents.py).

python 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?

Question 3 (Optional / Bonus 6 points): Finding All the Corners

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.

Implement the 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:

python pacman.py -l tinyCorners -p SearchAgent -a fn=astar,prob=CornersProblem

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

Question 4 (Optional / Bonus 6 points): Heuristics

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 CornersProblem in cornersHeuristic.

python pacman.py -l mediumCorners -p AStarCornersAgent -z 0.5

Note: 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!

Question 7 (15 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 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 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 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

python 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:

python autograder.py -q q7 --no-graphics

Hints and Observations

Question 8 (15 points): Expectimax

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:

python 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:

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

Question 9 (Optinal / Bonus 10 points): Better Evaluation Function

Write a better evaluation function for pacman in the provided function betterEvaluationFunction. The evaluation function should evaluate states. 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).

python 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:

Hints and Observations

Submission

Complete Questions 1,2,7,8,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: python autograder.py To run the autograder on a single question, such as question 2, invoke it by python autograder.py -q q2 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. We will evaluate your code with a variety of test cases. Those test cases on the gradescape are not restricted to those that are provided in to the local autograder, please pay attention to corner cases. The hard-coded solutions will not work on gradescope and we will use software to automatically detect any plagiarisms. 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.