You are on page 1of 9

CSL302 Artificial Intelligence

L ab 1

D ue on 27/1/2016 11.55pm
Ins truc tions : Upload to your moodle account one z ip file containing the following. P leas e do
not s ubmit hardcopy of your s olutions . In cas e moodle is not acces s ible email the z ip file to the
ins tructor at ckn@ L ate s ubmis sion is not allowed without prior approval of the
ins tructor. Y ou are expected to follow the honor code of the cours e while doing this homework.
1. T his lab s hould be attempted individually.
2. A neatly formatted P D F document with your ans wers for each of the ques tions in the
homework. Y ou can us e latex, MS word or any other s oftware to create the P D F .
3. Include a separate folder named as code containing the scripts for the homework along
with the neces s ary data files .
4. Include a R E AD ME file explaining how to execute the s cripts .
5. Name the Z IP file us ing the following convention rollnumberhwnumber.z ip

S earc h in P ac man
In this as s ignment, you will be experimenting with different AI s earch techniques that w e
dis cus s ed in the clas s in a P acman environment. T his is part of the P acman projects developed
at UC B erkeley [1]. T he P acman agent needs to find paths through the maz e world, both to
reach a location and to collect food efficiently.
Y ou are provided with a s tarter code for this project. T he code cons is ts of s everal P ython files ,
s ome of which you will need to read and unders tand to complete the as s ignment, and s ome of
which you can ignore.
F iles you will edit

where all your s earch algorithms will res ide. where all your s earch-bas ed agents will res ide.

F iles you m ig ht want to look at: T he main file that runs P acman games . T his file des cribes a P acman G ameS tate
type, which you us e in this project
T he logic behind how the P acman world works . T his file des cribes s everal
s upporting types like AgentS tate, Agent, D irection, and G rid.
Us eful data s tructures for implementing s earch algorithms
F iles you c an ig nore

G raphics for P acman S upport for P acman graphics

AS C II graphics for P acman

Agents to control ghos ts

K eyboard interfaces to control P acman

C ode for reading layout files and s toring their contents

P roject autograder

P ars es autograder tes t and s olution files

G eneral autograding tes t clas s es


D irectory containing the tes t cas es for each ques tion

P roject 1 s pecific autograding tes t clas s es

T he z ip file als o includes an autograder s cript for you to grade your ans wers on your machine.
T his can be run with the command:
Y ou mus t only include and as part of the final lab s ubmis s ion.
Y our code mus t be well commented. P leas e do not change the other files or s ubmit other files .

Welc ome to P ac man

After downloading the code (l1.z ip), unz ipping it, and changing to the directory, you s hould be
able to play a game of P acman by typing the following at the command line:
P acman lives in a s hiny blue world of twis ting corridors and tas ty round treats . Navigating this
world efficiently will be P acman's firs t s tep in mas tering his domain. T he s imples t agent in is called the GoWestAgent, which always goes W es t (a trivial reflex
agent). T his agent can occas ionally win:
python --layout testMaze --pacman GoWestAgent

B ut, things get ugly for this agent when turning is required:

python --layout tinyMaze --pacman GoWestAgent

If P acman gets s tuck, you can exit the game by typing C T R L -c into your terminal.
S oon, your agent will s olve not only tinyMaze, but any maz e you want.
Note that s upports s everal options that can each be expres s ed in a long way (e.g.,
--layout) or a s hort way (e.g., -l). Y ou can s ee the lis t of all options and their default values
python -h
Als o, all the commands that appear in this project als o appear in commands.txt, for eas y
copying and pas ting. In UNIX /Mac O S X , you can even run all thes e commands in order with
bash commands.txt.
Note: if you get error mes s ages regarding T kinter, s ee this page.

Ques tion 1 (5 points ): F inding a F ix ed F ood D ot us ing D epth F irs t S earc h

In, you'll find a fully implemented SearchAgent, which plans a path
through P acman's world and then executes that path s tep-by-s tep. T he s earch algorithms for
formulating a plan are not implemented -- that's your job.
F irs t, tes t that the SearchAgent is working correctly by running:
python -l tinyMaze -p SearchAgent -a fn=tinyMazeSearch
T he command above tells the SearchAgent to us e tinyMazeSearch as its s earch
algorithm, which is implemented in P acman s hould navigate the maz e
s ucces s fully.
Now it's time to write full-fledged generic s earch functions to help P acman plan routes !
R emember that a s earch node mus t contain not only a s tate but als o the information neces s ary
to recons truct the path (plan) which gets to that s tate.
Im portantnote:Allyour search functions need to return a listof actions thatw illlead the agent
from the start to the goal. These actions m ust be legal m oves (valid directions, no m oving
through w alls).
Im portant note: M ake sure to use the Stack, Queue and PriorityQueue data structures
provided to you in! These data structure im plem entations have properties w hich are
required forcom patibility w ith the autograder.

Implement the depth-firs t s earch (D F S ) algorithm in the depthFirstSearch function in T o make your algorithm complete, write the graph s earch vers ion of D F S , which
avoids expanding any already vis ited s tates .
Y our code s hould quickly find a s olution for:
python -l tinyMaze -p SearchAgent
python -l mediumMaze -p SearchAgent
python -l bigMaze -z .5 -p SearchAgent
T he P acman board will s how an overlay of the s tates explored, and the order in which they were
explored (brighter red means earlier exploration). Is the exploration order what you would have
expected? D oes P acman go to all the explored s quares on his way to the goal?
H int: If you us e a S tack as your data s tructure, the s olution found by your D F S algorithm for
mediumMaze s hould have a length of 130 (provided you pus h s ucces s ors onto the fringe in the
order provided by getSuccessors; you might get 246 if you pus h them in the revers e order).
Is this a leas t cos t s olution? If not, think about what depth-firs t s earch is doing wrong.

Ques tion 2 (5 points ): B readth F irs t S earc h

Implement the breadth-firs t s earch (B F S ) algorithm in the breadthFirstSearch function in Again, write a graph s earch algorithm that avoids expanding any already vis ited
s tates . T es t your code the s ame way you did for depth-firs t s earch.
python -l mediumMaze -p SearchAgent -a fn=bfs
python -l bigMaze -p SearchAgent -a fn=bfs -z .5
D oes B F S find a leas t cos t s olution? If not, check your implementation.
H int:If P acman moves too s lowly for you, try the option --frameTime 0.
N ote:If you've written your s earch code generically, your code s hould work equally well for the
eight-puz z le s earch problem without any changes .

Ques tion 3 (5 points ): Varying the C os t F unc tion

While B F S will find a fewes t-actions path to the goal, we might want to find paths that are "bes t"
in other s ens es . C ons ider mediumDottedMaze and mediumScaryMaze.
B y changing the cos t function, we can encourage P acman to find different paths . F or example,
we can charge more for dangerous s teps in ghos t-ridden areas or les s for s teps in food-rich
areas , and a rational P acman agent s hould adjus t its behavior in res pons e.

Implement the uniform-cos t graph s earch algorithm in the uniformCostSearch function in W e encourage you to look through for s ome data s tructures that may be
us eful in your implementation. Y ou s hould now obs erve s ucces s ful behavior in all three of the
following layouts , where the agents below are all UC S agents that differ only in the cos t function
they us e (the agents and cos t functions are written for you):
python -l mediumMaze -p SearchAgent -a fn=ucs
python -l mediumDottedMaze -p StayEastSearchAgent
python -l mediumScaryMaze -p StayWestSearchAgent
Note: Y ou s hould get very low and very high path cos ts for the StayEastSearchAgent and
StayWestSearchAgent res pectively, due to their exponential cos t functions (s ee for details ).

Ques tion 4 (6 points ): A * s earc h

Implement A * graph s earch in the empty function aStarSearch in A* takes a
heuris tic function as an argument. H euris tics take two arguments : a s tate in the s earch problem
(the main argument), and the problem its elf (for reference information). T he nullHeuristic
heuris tic function in is a trivial example.
Y ou can tes t your A* implementation on the original problem of finding a path through a maz e to
a fixed pos ition us ing the Manhattan dis tance heuris tic (implemented already as
manhattanHeuristic in
python -l bigMaze -z .5 -p SearchAgent -a
Y ou s hould s ee that A* finds the optimal s olution s lightly fas ter than uniform cos t s earch (about
549 vs . 620 s earch nodes expanded in our implementation, but ties in priority may make your
numbers differ s lightly). W hat happens on openMaze for the various s earch s trategies ?

Ques tion 5 (6 points ): F inding A ll the C orners

T he real power of A* will only be apparent with a more challenging s earch problem. Now, it's
time to formulate a new problem and des ign a heuris tic for it.

In corner maz es , there are four dots , one in each corner. O ur new s earch problem is to find the
s hortes t path through the maz e that touches all four corners (whether the maz e has food there
or not). Note that for s ome maz es like tinyCorners, the s hortes t path does not always go to
the clos es t food firs t! Hint: the s hortes t path through tinyCorners takes 28 s teps .

N ote: Make s ure to complete Ques tion 2 before working on Q ues tion 5, becaus e Q ues tion 5
builds upon your ans wer for Q ues tion 2.
Implement the CornersProblem s earch problem in Y ou will need to
choos e a s tate repres entation that encodes all the information neces s ary to detect whether all
four corners have been reached. N ow, your s earch agent s hould s olve:
python -l tinyCorners -p SearchAgent -a
python -l mediumCorners -p SearchAgent -a
T o receive full credit, you need to define an abs tract s tate repres entation that does not encode
irrelevant information (like the pos ition of ghos ts , where extra food is , etc.). D o not us e a
P acman GameState as a s earch s tate. Y our code will be very, very s low if you do (and wrong).
H int: T he only parts of the game s tate you need to reference in your implementation are the
s tarting P acman pos ition and the location of the four corners .
O ur implementation of breadthFirstSearch expands jus t under 2000 s earch nodes on
mediumCorners. H owever, heuris tics (us ed with A* s earch) can reduce the amount of
s earching required.

Ques tion 6 (6 points ): C orn ers P roblem : H euris tic

N ote: Make s ure to complete Ques tion 4 before working on Q ues tion 6, becaus e Q ues tion 6
builds upon your ans wer for Q ues tion 4.
Implement a non-trivial, cons is tent heuris tic for the CornersProblem in cornersHeuristic.
python -l mediumCorners -p AStarCornersAgent -z 0.5
Note: AStarCornersAgent is a s hortcut for
-p SearchAgent -a
Admis s ibility vs . C ons is tency: R emember, heuris tics are jus t functions that take s earch s tates
and return numbers that es timate the cos t to a neares t goal. More effective heuris tics will return
values clos er to the actual goal cos ts . T o be admis s ible, the heuris tic values mus t be lower
bounds on the actual s hortes t path cos t to the neares t goal (and non-negative). T o be
cons is tent, it mus t additionally hold that if an action has cos t c, then taking that action can only
caus e a drop-in heuris tic of at mos t c.
R emember that admis s ibility is n't enough to guarantee correctnes s in graph s earch -- you need
the s tronger condition of cons is tency. H owever, admis s ible heuris tics are us ually als o
cons is tent, es pecially if they are derived from problem relaxations . T herefore it is us ually eas ies t
to s tart out by brains torming admis s ible heuris tics . O nce you have an admis s ible heuris tic that

works well, you can check whether it is indeed cons is tent, too. T he only way to guarantee
cons is tency is with a proof. H owever, incons is tency can often be detected by verifying that for
each node you expand, its s ucces s or nodes are equal or higher in in f-value. Moreover, if UC S
and A* ever return paths of different lengths , your heuris tic is incons is tent. T his s tuff is tricky!
N on-TrivialH euristics:T he trivial heuris tics are the ones that return z ero everywhere (UC S ) and
the heuris tic which computes the true completion cos t. T he former won't s ave you any time,
while the latter will timeout the autograder. Y ou want a heuris tic which reduces total compute
time, though for this as s ignment the autograder will only check node counts (as ide from
enforcing a reas onable time limit).
G rading: Y our heuris tic mus t be a non-trivial non-negative cons is tent heuris tic to receive any
points . Make s ure that your heuris tic returns 0 at every goal s tate and never returns a negative
value. D epending on how few nodes your heuris tic expands , you'll be graded:
Number of nodes expanded
G rade
More than 2000
At mos t 2000
At mos t 1600
At mos t 1200
R emember: If your heuris tic is incons is tent, you will receive no credit, s o be careful!

Ques tion 7 (6 points ): E ating A ll T he D ots

Now we'll s olve a hard s earch problem: eating all the P acman food in as few s teps as pos s ible.
F or this , we'll need a new s earch problem definition which formaliz es the food-clearing problem:
FoodSearchProblem in (implemented for you). A s olution is defined to
be a path that collects all the food in the P acman world. F or the pres ent project, s olutions do not
cons ider any ghos ts or power pellets ; s olutions only depend on the placement of walls , regular
food and P acman. (O f cours e ghos ts can ruin the execution of a s olution! W e'll get to that in the
next project.) If you have written your general s earch methods correctly, A* with a null heuris tic
(equivalent to uniform-cos t s earch) s hould quickly find an optimal s olution to testSearch with
no c ode change on your part (total cos t of 7).
python -l testSearch -p AStarFoodSearchAgent
s hortcut


Y ou s hould find that UC S s tarts to s low down even for the s eemingly s imple tinySearch. As
a reference, our implementation takes 2.5 s econds to find a path of length 27 after expanding
5057 s earch nodes .
N ote: Make s ure to complete Ques tion 4 before working on Q ues tion 7, becaus e Q ues tion 7
builds upon your ans wer for Q ues tion 4.

F ill in foodHeuristic in with a cons is tent heuris tic

FoodSearchProblem. T ry your agent on the trickySearch board:

for the

python -l trickySearch -p AStarFoodSearchAgent

O ur UC S agent finds the optimal s olution in about 13 s econds , exploring over 16,000 nodes .
Any non-trivial non-negative cons is tent heuris tic will receive 1 point. Make s ure that your
heuris tic returns 0 at every goal s tate and never returns a negative value. D epending on how
few nodes your heuris tic expands , you'll get additional points :
Number of nodes expanded
More than 15000
At mos t 15000
At mos t 12000
At mos t 9000
At mos t 7000

G rade

R emember: If your heuris tic is incons is tent, you will receive no credit, s o be careful! C an you
s olve mediumSearch in a s hort time? If s o, we're either very, very impres s ed, or your heuris tic
is incons is tent.

Ques tion 8 (6 points ): S uboptim al S earc h

S ometimes , even with A* and a good heuris tic, finding the optimal path through all the dots is
hard. In thes e cas es , we'd s till like to find a reas onably good path, quickly. In this s ection, you'll
write an agent that always greedily eats the clos es t dot. ClosestDotSearchAgent is
implemented for you in s earchAgents .py, but it's mis s ing a key function that finds a path to the
clos es t dot.
Implement the function findPathToClosestDot in O ur agent s olves
this maz e (s uboptimally!) in under a s ec ond with a path cos t of 350:

python -l bigSearch -p ClosestDotSearchAgent -z .5

H int: T he quickes t way to complete findPathToClosestDot is to fill in the
AnyFoodSearchProblem, which is mis s ing its goal tes t. T hen, s olve that problem with an
appropriate s earch function. T he s olution s hould be very s hort!
Y our ClosestDotSearchAgent won't always find the s hortes t pos s ible path through the
maz e. Make s ure you unders tand why and try to come up with a s mall example where
repeatedly going to the clos es t dot does not res ult in finding the s hortes t path for eating all the
dots .

S ubm is s ion
Include a neatly formatted pdf document that des cribes your heuris tics , and other obs ervations
while implementing the lab.

R eference