Lab 2: Search in Pac-Man
Due Feb. 12 by midnight

The Pac-Man code was developed by John DeNero and Dan Klein at UC Berkeley.

There are eight exercises in this lab:
1: DFS
2: BFS
3: UCS
4: A*
5: CornersProblem
6: CornersProblem heuristic
7: FoodSearchProblem heuristic
8: Sub-optimal search

Starting point code

This lab may be done alone or with a partner of your choice. Go through the following steps to setup your directory for this lab. This is the same sequence of steps you did last time, except using a different lab number.

  1. First you need to run setup63 to create a git repository for the lab. If you want to work alone do:
    setup63 labs/02 none
    If you want to work with a partner, then one of you needs to run the following while the other one waits until it finishes.
    setup63 labs/02 partnerUsername
    Once the script finishes, the other partner should run it on their account.

  2. For the next step only one partner should copy over the starting point code.
    cd ~/cs63/labs/02
    cp -r ~meeden/public/cs63/labs/02/* ./
    This will copy over the starting point files for this lab.

  3. Whether you are working alone or with a partner, you should now add all of the files to your git repo, commit, and push them as shown below.
    git add *
    git commit -m "lab2 start"
    git push

  4. If you are working with a partner, your partner can now pull the changes in.
    cd ~/cs63/labs/02
    git pull


In this lab, your Pac-Man 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 many different Pac-Man scenarios.

Files you'll edit: Where all of your search algorithms will reside. Where all of your search-based agents will reside.
Files you should look at but NOT edit: Useful data structures for implementing search algorithms. The main file that runs Pac-Man games. This file describes a Pac-Man GameState type, which you use in this lab. The logic behind how the Pac-Man world works. This file describes several supporting types like AgentState, Agent, Direction, and Grid.

Finding a fixed food dot using Uninformed Search

In, you'll find a fully implemented SearchAgent, which plans out a path through Pac-Man's world and then executes that path step-by-step. The search algorithms for formulating a plan are not implemented -- that's your job. As you work through the following questions, you might need to refer to this glossary of objects in the code. First, test that the SearchAgent is working correctly by running:
python -l tinyMaze -p SearchAgent -a fn=tinyMazeSearch
The command above tells the SearchAgent to use tinyMazeSearch as its search algorithm, which is implemented in This simply follows a fixed sequence of actions to demonstrate how the code works. Pac-Man should navigate the maze successfully.

Now it's time to write full-fledged generic search functions to help Pac-Man plan routes! Pseudocode for the depth-first search and breadth-first search algorithms you'll write is shown below.

UninformedSearch(problem) returns a list of actions
   initialize the frontier using the initial state of the problem
   #For explored, use Pacman position as the key with a value True
   initialize a dictionary of states already explored 
      if the frontier is empty
         return an empty list
      choose a leaf node and remove it from the frontier
      if the node contains a goal state
         return list of actions from start state to goal state
      add the state key to the explored dictionary
      for each successor of the node state 
         if the key of the successor state is not in explored
            add node of the successor onto the frontier

Important note: All of your search functions need to return a list of actions that will lead the agent from the start to the goal. These actions all have to be legal moves (valid directions, no moving through walls).

Hint: Algorithms for DFS and BFS differ only in the details of how the fringe is managed. So, concentrate on getting DFS right and then BFS should be relatively straightforward. Indeed, one possible implementation requires only a single generic search method which is configured with an algorithm-specific queuing strategy. (Your implementation need not be of this form to receive full credit).

Hint: Make sure to check out the Stack, Queue, and PriorityQueue types provided to you in

Exercise 1 Implement the depth-first search algorithm in the depthFirstSearch function in You should begin by creating a Node class to use in all of your search algorithms. Recall that a search node contains:

Although DFS and BFS ignore the costs, you'll need them for later search methods.

Your code should quickly find a solution for:

python -l tinyMaze -p SearchAgent
python -l mediumMaze -p SearchAgent
python -l bigMaze -z .5 -p SearchAgent
The Pac-Man board will show an overlay of color for the states explored, and the order in which they were explored (brighter red means earlier exploration). Is the exploration order what you would have expected? Does Pac-Man actually go to all the explored squares on his way to the goal?

Hint: If you use a Stack as your data structure, the solution found by your DFS algorithm for mediumMaze should have a length of 130 (provided you push successors onto the fringe in the order provided by getSuccessors; you might get 244 if you push them in the reverse order). Is this a least cost solution? If not, think about what depth-first search is doing wrong.

Exercise 2 Implement the breadth-first search algorithm in the breadthFirstSearch function in Use the same algorithm as shown in the above pseudocode. Test your code the same way you did for depth-first search.

python -l mediumMaze -p SearchAgent -a fn=bfs
python -l bigMaze -p SearchAgent -a fn=bfs -z .5
Does BFS find a least cost solution? If not, check your implementation.

Hint: If Pac-Man moves too slowly for you, try the option --frameTime 0.

Note: If you've written your search code generically, your code should work equally well for the eight-puzzle search problem (textbook section 3.2) without any changes.


Remember to use git to add, commit, and push your updates before going on to the next exercise.

Solving problems with varying costs using Informed Search

While BFS will find a fewest-actions path to the goal, we might want to find paths that are "best" in other senses. Consider mediumDottedMaze and mediumScaryMaze, which you can find in the layouts directory. By changing the cost function, we can encourage Pac-Man to find different paths. For example, we can charge more for dangerous steps in ghost-ridden areas or less for steps in food-rich areas, and a rational Pac-Man agent should adjust its behavior in response.

To properly implement informed search we need to update our search pseudocode. One key difference is that the frontier will be implemented as a priority queue. We will choose the node from the frontier with the lowest path cost. Another important difference is that we will need to track which states are on the frontier. If we find a new path to the same state that has a lower path cost, we want to be sure to focus on that new node. To do this we will add a second dictionary called considering. The updated pseudocode is given below:

InformedSearch(problem) returns a list of actions
   initialize a PQ frontier using the initial state of the problem
   #For explored, use Pacman position as the key, value True
   initialize a dictionary of states already explored
   #For considering, use Pacman position as the key, value pathCost
   initialize a dictionary of states being considered
      if the frontier is empty
         return an empty list
      choose a leaf node and remove it from the frontier
      if node state is in explored
         continue with next iteration of the loop #Ignore it
      add state key to explored dictionary
      remove state key from considering dictionary
      if the node contains a goal state
         return list of actions from start state to goal state
      for each successor of the node state
         calculate the new pathCost
         if key of successor state not in explored and considering
            add key of successor state to considering with pathCost
            push the successor node on the frontier
         elif key of successor state in considering and  
          new pathCost < old pathCost
            #Found a better path to a state we were considering
            update the considering dictionary with the new pathCost
            push the successor node on the frontier

Exercise 3 Implement the uniform-cost search algorithm in the uniformCostSearch function in We encourage you to look through for some data structures that may be useful in your implementation. You should now observe successful behavior in all three of the following layouts, where the agents below are all UCS agents that differ only in the cost function they use (the agents and cost functions are written for you):

python -l mediumMaze -p SearchAgent -a fn=ucs
python -l mediumDottedMaze -p StayEastSearchAgent
python -l mediumScaryMaze -p StayWestSearchAgent

Note: You should get very low and very high path costs for the StayEastSearchAgent and StayWestSearchAgent respectively, due to their exponential cost functions (see for details).

Remember to use git to add, commit, and push your updates before going on to the next exercise.

A* search

Exercise 4 Implement A* search in the empty function aStarSearch in A* should be implemented using the same InformedSearch pseudocode given above. One difference between A* and UCS is that 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 is a trivial example. A* uses this heuristic function to estimate the distance from a state to the goal. The PQ should sort nodes based on the sum of the cost to get to the node plus the estimated distance to the goal.

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

python -l bigMaze -z .5 -p SearchAgent -a fn=astar,heuristic=manhattanHeuristic 
You should see that A* finds the optimal solution slightly faster than uniform cost search (about 549 vs. 620 search nodes expanded in our implementation, but ties in priority may make your numbers differ slightly). What happens on openMaze for the various search strategies?

Finding All the Corners

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 tinyCorners, the shortest path does not always go to the closest food first! Hint: the shortest path through tinyCorners takes 28 steps.

Exercise 5 Implement the CornersProblem search problem in 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 -l tinyCorners -p SearchAgent -a fn=bfs,prob=CornersProblem
python -l mediumCorners -p SearchAgent -a fn=bfs,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 Pac-Man 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 Pac-Man position and the location of the four corners.

Our implementation of breadthFirstSearch expands just under 2000 search nodes on mediumCorners. However, heuristics (used with A* search) can reduce the amount of searching required.

Exercise 6 Implement a heuristic for the CornersProblem in cornersHeuristic. Grading: inadmissible heuristics will get no credit. 1 point for any admissible heuristic. 1 point for expanding fewer than 1600 nodes. 1 point for expanding fewer than 1200 nodes. Expand fewer than 800, and you're doing great!

python -l mediumCorners -p AStarCornersAgent -z 0.5

Hint: Heuristic functions just return numbers, which, to be admissible, must be lower bounds on the actual shortest path cost to the nearest goal.

Note: AStarCornersAgent is a shortcut for -p SearchAgent -a fn=aStarSearch,prob=CornersProblem,heuristic=cornersHeuristic.

Remember to use git to add, commit, and push your updates before going on to the next exercise.

Eating All The Dots

Now we'll solve a hard search problem: eating all the Pac-Man food in as few steps as possible. For this, we'll need a new search problem definition which formalizes the food-clearing problem: FoodSearchProblem in (implemented for you). A solution is defined to be a path that collects all of the food in the Pac-Man world. For the present lab, solutions do not take into account any ghosts or power pellets; solutions only depend on the placement of walls, regular food and Pac-Man. (Of course ghosts can ruin the execution of a solution! We'll get to that in a later lab.) If you have written your general search methods correctly, A* with a null heuristic (equivalent to uniform-cost search) should quickly find an optimal solution to testSearch with no code change on your part (total cost of 7).
python -l testSearch -p AStarFoodSearchAgent

Note: AStarFoodSearchAgent is a shortcut for -p SearchAgent -a fn=astar,prob=FoodSearchProblem,heuristic=foodHeuristic.

You should find that UCS starts to slow down even for the seemingly simple tinySearch. As a reference, our implementation takes 2.5 seconds to find a path of length 27 after expanding 4902 search nodes.

Exercise 7 Fill in foodHeuristic in with a consistent heuristic for the FoodSearchProblem. Try your agent on the trickySearch board:

python -l trickySearch -p AStarFoodSearchAgent
Our UCS agent finds the optimal solution in about 13 seconds, exploring over 16,000 nodes. If your heuristic is admissible, you will receive 1 point. You will receive additional points, depending on how many nodes your heuristic expands:

Nodes expandedAdditional points
12000 > n < 150001
9000 > n < 120002
n < 90003

If your heuristic is inadmissible, you will receive no credit, so be careful! Think through admissibility carefully, as inadmissible heuristics may manage to produce fast searches and even optimal paths. Can you solve mediumSearch in a short time? If so, we're either very, very impressed, or your heuristic is inadmissible.

Admissibility vs. Consistency? Technically, admissibility isn't enough to guarantee correctness in graph search -- you need the stronger condition of consistency. For a heuristic to be consistent, it must hold that if an action has cost c, then taking that action can only cause a drop in heuristic of at most c. If your heuristic is not only admissible, but also consistent, you will receive 1 additional point for this question.

Almost always, admissible heuristics are also consistent, especially if they are derived from problem relaxations. Therefore it is probably 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. Inconsistency can sometimes be detected by verifying that your returned solutions are non-decreasing in f-value. Moreover, if UCS and A* ever return paths of different lengths, your heuristic is inconsistent.

Remember to use git to add, commit, and push your updates before going on to the next exercise.

Sub-optimal Search

Sometimes, even with A* and a good heuristic, finding the optimal path through all the dots is hard. In these cases, we'd still like to find a reasonably good path, quickly. In this section, you'll write an agent that always eats the closest dot. ClosestDotSearchAgent is implemented for you in, but it's missing a key function that finds a path to the closest dot.

Exercise 8 Implement the function findPathToClosestDot in Our agent solves this maze (sub-optimally!) in under a second with a path cost of 350:

python -l bigSearch -p ClosestDotSearchAgent -z .5 

Hint: The quickest way to complete findPathToClosestDot is to fill in the AnyFoodSearchProblem, which is missing its goal test. Then, solve that problem with an appropriate search function. The solution should be very short!

Your ClosestDotSearchAgent won't necessarily find the shortest possible path through the maze. In fact, you can do better if you try.

Submitting your code

To submit your code, you need to use git to add, commit, and push the files you modified. You should only have changed and
cd ~/cs63/labs/02
git add
git commit -m "the latest version"
git push

Object Glossary

Here's a glossary of the key objects in the code base related to search problems, for your reference:

SearchProblem (
A SearchProblem is an abstract object that represents the state space, successor function, costs, and goal state of a problem. You will interact with any SearchProblem only through the methods defined at the top of
PositionSearchProblem (
A specific type of SearchProblem that you will be working with --- it corresponds to searching for a single pellet in a maze.
CornersProblem (
A specific type of SearchProblem that you will define --- it corresponds to searching for a path through all four corners of a maze.
FoodSearchProblem (
A specific type of SearchProblem that you will be working with --- it corresponds to searching for a way to eat all the pellets in a maze.
Search Function
A search function is a function which takes an instance of SearchProblem as a parameter, runs some algorithm, and returns a sequence of actions that lead to a goal. Example of search functions are depthFirstSearch and breadthFirstSearch, which you have to write. You are provided tinyMazeSearch which is a very bad search function that only works correctly on tinyMaze
SearchAgent is a class which implements an Agent (an object that interacts with the world) and does its planning through a search function. The SearchAgent first uses the search function provided to make a plan of actions to take to reach the goal state, and then executes the actions one at a time.