This is the multi-page printable view of this section. Click here to print.
Projects
1 - Project 1 - Workstation Configuration
Let’s get ready to code! To make sure we are ready for the labs and the projects for this class, let’s configure your workstation/environment with the required toolsets and construct a few small Python program.
We will test our configuration out using the Pacman game
, which
was developed by Berkerly AI (http://ai.berkeley.edu).
Tasks
-
Install and/or verify that you have Python and the required packages on your computer. The recommended version is 3.10.16 and the minimum version is 3.10.2. Optionally install an IDE for Python (highly recommended). VS Code is a very popular option.
-
Download and run the Pacman game and take a screenshot to show that you workstation is properly configured.
-
Write a small Python program (see below).
Submit files to gradescope
Files
- Download
workstationConfig.zip
.
This file contains the many of the components for the Pacman game so you
can test that the game operates properly on your workstation. It also contains
the
shopSmart.py
(UNFINISHED) andbuyLotsOfFruit.py
(UNFINISHED) files you will need to complete (plus a few support files).
Introduction
CS Lab Computers
All CS lab computes running Ubuntu have the required Python software and packages (imports) for this class. If you do not have a laptop that you can bring to class that supports the software and configuration described in this assignment, please contact me. Having a functional computer during class is a requirement of this class.
Unix/Mac versus Windows
Provided code for this class may occasionally contain UNIX/Mac specific shell scripts. Code for this class also utilizes GUIs, which typically do not run well under VMs.
If you are going to be running Windows, it is advised that you setup and configure the Windows Subsystem for Linux (WSL). Here are some instructions from Microsoft.
If you are going to run Windows, I will not be able to offer a lot of support (since I do not know Windows well nor do I use it). I can offer some basic support and also remind you that the CS lab computers will completely support this class.
Task 1: Install Python and Required Packages
Python 3.10.16 is recommended for this class (a minimum version of 3.10.2). You can download Python for Mac or Windows from python.org's Download page.
Virtual Environments with Python
Python supports its own idea of a virtual environment
, which allows for sets of packages
to be managed independently from one another and without administrative rights on the computer
(which is more secure). You can create Python environments both using venv
(Python’s tool) or
more advanced managers such as Conda
. Since Conda
is universal and works both on Windows
and MacOS, it is recommended and it is what is utilized in this assignment.
Installing Conda
-
Download Conda here and install it.
-
Start Anaconda by selecting the Anaconda-Navigator. From this app you will create your virtual environment. Select the environments on the left side of the navigator (circled below).
-
Create a virtual environment. Click on the “create” button shown below.
-
Create the environment with a reasonable name and an appropriate Python version.
Configuring the Virtual Environment
Open a terminal window and navigate to a folder where you will create your virtual environment for this class.
conda activate cs444_s25_e3
python -m pip install --upgrade pip
curl -O https://w3.cs.jmu.edu/molloykp/teaching/cs444_s25/pas/workstationConfig/cs444Requirements.txt
python -m pip install -r cs444Requirements.txt
IDEs – Microsoft Visual Studio Code
I recommend using Microsoft’s Visual Studio Code. During the development process, you will need to run games that have graphical interfaces (like Pacman) or display/analyze plots created with matplotlib. Neither of these processes work well when using VSCode with remote-ssh. One reason this document was created is to enable you to have a local version of this environment to address this issue.
To enable VSCode to use your new Conda Python environment, follow these steps:
- Open VSCode on your computer.
- Edit any
.py
file now in VSCode. In the lower right corner, you should be able to select your venv environment. If the correct one is not shown, click on it and select it from the list that is presented.
Task 2: Testing your Environment
To test your enviornment, unzip
workstationConfig.zip
.
If you are using VSCode, you can open the workstation
folder this creates by
going to Open
and then Open Folder...
. From the terminal (either directly or from
within VSCode), run the pacman.py
file as shown below.
python pacman.py
If all is well with your installation, a game of Pacman will start. Navigate pacman using the arrow keys on your keyboard.
Take a screen capture of pacman operating on your computer and name this
file pacman_capture.pdf
. You will be uploading this file to Gradescope.
Task 3: Intro to Python Programming
Complete the one function in buyLotsOfFruit.py
and another
function in shopSmart.py
so that it
complies with its docstring (comment block after the function definition).
This code introduces you to the idea of dictionarys in Python (hashmaps) and also has you
write a simple for loop. Note: typically Python does not use
camel case and prefers that
underscores are used to separate out words, alas, some habits are hard to break.
Python utilizes docstrings for documentation (much like Java uses Javadoc).
Here are the
CS 149 instructions for docstrings
which may prove helpful in your Python programming career.
You can test your code by running buyLotsOfFruit_test.py
file, which is included in the
Submission and Grading
Grading
Project Part | Weight |
---|---|
Screen Capture | 52% |
shopSmart.py | 24% |
buyLotsOfFruit.py | 24% |
Submit the following items to gradescope.
- The screen capture of your workstation running the Pacman game and name this
file
pacman_capture.pdf
. - Submit your completed
buyLotsOfFruit.py
file. - Submit your completed
shopSmart.py
file.
2 - Project 2 - Paths for Pacman
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.