Project 2 - Paths for Pacman
Categories:
9 minute read
In this assignment, you will utilize the graph search methods developed in Lab 1 and Lab 2 within the Pacman game. The basis for this game and the course code for the game itself were developed by Berkerly AI (http://ai.berkeley.edu).
Tasks
-
Create a new directory and copy over all files (and subdirectories) from your completed Informed Search lab .
-
Complete the programming tasks below (tasks 1 - 4). Each task has tests cases to help verify your code.
-
Submit your code to Gradescope.
-
We will have a post-project discussion where you may be called upon to explain your heuristics and code in class or to me.
Task 1 Finding All Corners with Breadth First Search
The corner mazes problems consists of a food pellet in each corner
of the maze.
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 tinyCorners
,
the shortest path does not always go to the closest food dot first!
Note: the shortest path through tinyCorners
takes 28 steps.
Your task is to complete the CornersProblem
search problem/class
in searchAgents.py
.
You will need to create a state representation that
encodes all the information necessary to detect whether all
four corners have been reached. To receive full credit, you
must 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 incorrect).
Hints
-
As discussed in class, list the items that you need to track in order to solve this problem. These are the only items you should track in your state variables.
-
You can augment the constructor (
__init__
) function to create instance variables. In Python, instance variables are always prefixed with self. -
When coding
isGoalState
, ask yourself what consistutes a goal state (when the game can end). -
When coding
getSuccessors
method inside theCornersProblem
class you can directly copy the example code to detect walls/legal moves (this is commented out immediately before the for loop). The work you need to do in this function is to consider if the proposed action modifies the game’s state, and if it does, update the state that is returned bygetSuccessors
for that action.
Your search agent should solve these problem instances:
python pacman.py -l tinyCorners -p SearchAgent -a fn=bfs,prob=CornersProblem
python pacman.py -l mediumCorners -p SearchAgent -a fn=bfs,prob=CornersProblem
Expect breadthFirstSearch to expand just under 2000 search nodes on mediumCorners. However, heuristics (used with A* search) can reduce the amount of searching required (see the next task).
You can test your code against the same tests as Gradescope using the following command:
python autograder.py -q q5
Task 2 Corners Problem Heuristic
The real power of A* becomes more apparent on more challenging search
problems. Now, it’s time to design a heuristic for the CornersProblem
.
Implement a non-trivial, consistent heuristic
in the cornersHeuristic
function within the searchAgents.py
file.
The function as provided just returns zero (and thus, the examples
below will complete, but with a good heuristic you can reduce the
number of expanded states).
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 a problem state and return an estimate of the cost(a number) to the nearest goal. More effective heuristics will return values closer to the actual goal costs. To be admissible, the heuristic values must be a 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 decrease in the heuristic value h(x) 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 optimal heuristic computes the true remaining 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:
Nodes Expanded | Points |
---|---|
> 2000 | 10/25 |
> 1601 and <= 2000 | 15/25 |
> 1201 and <= 1600 | 20/25 |
<= 1200 | 25/25 |
Remember If you heuristic is inconsistent or not admissible, you will receive no credit.
You can test your code against the same tests as Gradescope using the following command:
python autograder.py -q q6
Task 3 Eat All the Dots Heuristic
This problem asks for a plan where Pacman eats all the food (dots) in as few steps as possible.
A new search problem definition which formalizes the food-clearing problem
named FoodSearchProblem
is already implemented for you in searchAgents.py
.
A solution is defined to be a path that collects all of the food in the
Pacman world. For the present project, solutions do not take into account
any ghosts or power pellets; solutions only depend on the placement of
walls, regular food and Pacman. Of course ghosts can ruin the execution of
a solution! We’ll get to that in the next project.
If you have written your
general search methods correctly, you can use A* with a null heuristic (equivalent to
uniform-cost search) to quickly find an optimal solution to the testSearch
problem (should return a cost of 7):
python pacman.py -l testSearch -p AStarFoodSearchAgent
UCS starts to slow down even for the seemingly simple tinySearch (to
run this test, in the command above replace testSearch with tinySearch).
As a reference, my implementation takes 2.5 seconds to find a path of
length 27 after expanding 5057 search nodes. I gave up waiting
on the mediumSearch problem (I waited more than 4 hours).
You should try the tinySearch
and verify you get similar numbers.
Your job in Task 3 is to complete the foodHeuristic
function within searchAgents.py
.
Your heuristic must be admissible and consistent. Try your UCS agent on
the trickySearch
board:
python pacman.py -l trickySearch -p SearchAgent -a fn=astar,prob=FoodSearchProblem,heuristic=nullHeuristic
Mine takes about 20 seconds to run and expands 16668 nodes.
A few notes on heuristic development:
- any non-trivial non-negative consistent heuristic will receive 1 point.
- make sure your heuristic returns 0 when at a goal state.
- your score for this part of the PA will depend on the number of nodes expanded
To test your foodHeuristic
on the trickySearch
board, you can use
the following command:
python pacman.py -l trickySearch -p SearchAgent -a fn=astar,prob=FoodSearchProblem,heuristic=foodHeuristic
Your score for this section will be based on the number of expand operations and is outlined in the following table:
Nodes Expanded | Points |
---|---|
expands > 15000 | 10/25 |
12000 < expands <= 15000 | 15/25 |
9000 < expands <= 12000 | 20/25 |
7000 < expands <= 9000 | 25/25 |
expands <= 7000 | 30/25 |
You can test your code against the same tests as Gradescope using the following command:
python autograder.py -q q7
Task 4 An Approximation of Eat All the Food
Sometimes, even with A* and a good heuristic, finding the optimal path through all the dots is hard (think of the mediumSearch problem from Task 3). In these cases, we would still like to find a reasonably good path and quickly.
In this task, you’ll write an agent that greedily eats the closest dot.
The ClosestDotSearchAgent
class is implemented for you in searchAgents.py
,
but it’s missing a key function that finds a path to the closest dot.
Implement the function findPathToClosestDot
in searchAgents.py
. Your
agent should be able to solve this maze (suboptimally!) in under a second with a path cost of 350.
Hints:
-
The quickest way to complete
findPathToClosestDot
is to create anAnyFoodSearchProblem
. This problem is completed for you EXCEPT for the goal test. Then, solve this problem using one of your already completed and appropriate search functions. -
Notice that
AnyFoodSearchProblem
does not take a goal state in its constructor. This is ON PURPOSE. Think of a way you can writeisGoalState
without an explicit goal state.
The solution should be very short!
Your ClosestDotSearchAgent
won’t always find the shortest possible
path through the maze. Make sure you understand why and try to come
up with a small example where repeatedly going to the closest dot
does not result in finding the shortest path for eating all the dots.
Here are some examples you can use to test your methods.
python pacman.py -l mediumSearch -p ClosestDotSearchAgent -z .5 --frameTime 0.07
python pacman.py -l bigSearch -p ClosestDotSearchAgent -z .5 --frameTime 0.06
You can use this command to run the autograder for this task:
python autograder.py -q q8
Submission and Grading
You should never start design or construction until you completely understand the project.
You should start by carefully reading the project specifications. (In general it is a good idea to print a paper copy so that you can take notes and perform calculations as you read.)
Complete the tasks in the order specified (as sometimes one task depends on the prior tasks) and submit them to gradescope.
You are not required to submit tests cases for these classes. Submit the following files:
- search.py
- searchAgents.py
Your grade will be computed as follows:
Project Part | Weight |
---|---|
Task 1 | 25% |
Task 2 | 25% |
Task 3 | 25% |
Task 4 | 20% |
Quality | 5% |
The code quality grade will be based on such things as:
- Comment clarity
- Code clarity (including variable names)
- Code duplication
- Elegance
- Acknowledgements (as appropriate)
You may submit to Gradescope an unlimited number of times.