Project 2 - Paths for Pacman (2024)

Pacman needs your help to learn the subtleties of different mazes. His job at the moment is just to clear away the food pellets as effifiently as possible. Sounds easy, right? Well….

Tags:
  • Uninformed Search
  • Informed Search
  • Heuristic Development
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 forthis game and the course code for the game itself were developed by Berkerly AI (http://ai.berkeley.edu).

Pacman Maze

Project 2 - Paths for Pacman (1)

Tasks

  1. Create a new directory and copy over all files (and subdirectories)from your completed Informed Search lab .

  2. Complete the programming tasks below (tasks 1 - 4). Each task has tests casesto help verify your code.

  3. Submit your code to Gradescope.

  4. We will have a post-project discussion where you may be calledupon 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 cornerof the maze.Our new search problem is to find the shortest path throughthe maze that touches all four corners (whether the maze actuallyhas 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/classin searchAgents.py.You will need to create a state representation thatencodes all the information necessary to detect whether allfour corners have been reached. To receive full credit, youmust define an abstract state representation that does not encodeirrelevant information (like the position of ghosts, where extrafood is, etc.). In particular, do not use a PacmanGameState as a search state. Your code will be very,very slow if you do (and also incorrect).

Hints

  1. As discussed in class, list the items that you need to track in order to solvethis problem. These are the only items you should track in your state variables.

  2. You can augment the constructor (__init__) function to create instance variables. In Python, instance variables are always prefixed with self.

  3. When coding isGoalState, ask yourself whatconsistutes a goal state (when the game can end).

  4. When coding getSuccessors method inside the CornersProblem class you can directly copy the examplecode 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 thegame’s state, and if it does, update the state that is returned by getSuccessorsfor 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 onmediumCorners. However, heuristics (used with A* search) can reduce theamount of searching required (see the next task).

You can test your code against the same tests as Gradescope using thefollowing command:

python autograder.py -q q5

Task 2 Corners Problem Heuristic

The real power of A* becomes more apparent on more challenging searchproblems. Now, it’s time to design a heuristic for the CornersProblem.Implement a non-trivial, consistent heuristicin the cornersHeuristic function within the searchAgents.py file.The function as provided just returns zero (and thus, the examplesbelow will complete, but with a good heuristic you can reduce thenumber 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 functionsthat take a problem state and return an estimate of the cost(a number) to thenearest goal. More effective heuristics will return values closer to theactual goal costs. To be admissible, the heuristic values must be a lowerbounds on the actual shortest path cost to the nearest goal (andnon-negative). To be consistent, it must additionally hold that if anaction has cost c, then taking that action can only cause a decrease inthe heuristic value h(x) of at most c.

Remember that admissibility isn’t enough to guarantee correctness in graphsearch – you need the stronger condition of consistency. However,admissible heuristics are usually also consistent, especially if theyare derived from problem relaxations. Therefore it is usually easiest tostart out by brainstorming admissible heuristics. Once you have anadmissible heuristic that works well, you can check whether it isindeed consistent, too. The only way to guarantee consistency iswith a proof. However, inconsistency can often be detected by verifyingthat for each node you expand, its successor nodes are equal or higherin in f-value. Moreover, if UCS and A* ever return paths of differentlengths, your heuristic is inconsistent. This stuff is tricky!

Non-Trivial Heuristics: The trivial heuristics are the ones that returnzero everywhere (UCS) and the optimal heuristic computes the true remainingcost. The former won’t save you any time, while the latter will timeoutthe 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 ExpandedPoints
> 200010/25
> 1601 and <= 200015/25
> 1201 and <= 160020/25
<= 120025/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 thefollowing 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 problemnamed FoodSearchProblem is already implemented for you in searchAgents.py.A solution is defined to be a path that collects all of the food in thePacman world. For the present project, solutions do not take into accountany ghosts or power pellets; solutions only depend on the placement ofwalls, regular food and Pacman. Of course ghosts can ruin the execution ofa solution! We’ll get to that in the next project.

If you have written yourgeneral search methods correctly, you can use A* with a null heuristic (equivalent touniform-cost search) to quickly find an optimal solution to the testSearchproblem (should return a cost of 7):

python pacman.py -l testSearch -p AStarFoodSearchAgent

UCS starts to slow down even for the seemingly simple tinySearch (torun this test, in the command above replace testSearch with tinySearch).As a reference, my implementation takes 2.5 seconds to find a path oflength 27 after expanding 5057 search nodes. I gave up waitingon 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 onthe 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 usethe 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 operationsand is outlined in the following table:

Nodes ExpandedPoints
expands > 1500010/25
12000 < expands <= 1500015/25
9000 < expands <= 1200020/25
7000 < expands <= 900025/25
expands <= 700030/25

You can test your code against the same tests as Gradescope using thefollowing 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 paththrough 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. Youragent should be able to solve this maze (suboptimally!) in under a second with a path cost of 350.

Hints:

  1. The quickest way to complete findPathToClosestDot is to create an AnyFoodSearchProblem. This problem is completed for youEXCEPT for the goal test. Then, solve this problem usingone of your already completed and appropriate search functions.

  2. Notice that AnyFoodSearchProblem does not take a goal statein its constructor. This is ON PURPOSE. Think of a way youcan write isGoalState without an explicit goal state.

The solution should be very short!

Your ClosestDotSearchAgent won’t always find the shortest possiblepath through the maze. Make sure you understand why and try to comeup with a small example where repeatedly going to the closest dotdoes 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 onthe prior tasks) and submit them to gradescope.

You are not required to submit tests cases for these classes. Submit the followingfiles:

  • search.py
  • searchAgents.py

Your grade will be computed as follows:

Project PartWeight
Task 125%
Task 225%
Task 325%
Task 420%
Quality5%

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.

Last modified April 21, 2024: Update deploy.yml (3125f7b)

Project 2 - Paths for Pacman (2024)

References

Top Articles
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 5672

Rating: 4.8 / 5 (78 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.