Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Coursework 1
(Version 1.5)
1 Overview
For this coursework, you will have to implement a classifier. You will use this classifier in some code
that has to make a decision. The code will be controlling Pacman, in the classic game, and the
decision will be about how Pacman chooses to move. Your classifier probably won’t help Pacman
to make particularly good decisions (I will be surprised if it helps Pacman win games, my version
certainly didn’t), but that is not the point. The point is to write a classifier and use it.
No previous experience with Pacman (either in general, or with the specific UC Berkeley AI implementation that we will use) is required.
This coursework is worth 10% of the marks for the module.
Note: Failure to follow submission instructions will result in a deduction of 10% of the marks you
earn for this coursework.
2 Getting started
2.1 Start with Pacman
The Pacman code that we will be using for the coursework was developed at UC Berkeley for their AI
course. The folk who developed this code then kindly made it available to everyone. The homepage
for the Berkeley AI Pacman projects is here:
http://ai.berkeley.edu/
Note that we will not be doing any of their projects. Note also that the code only supports Python
3, so that is what we will use.1
You should:
(a) Download:
pacman-cw1.zip
from KEATS.
(b) Save that file to your account at KCL (or to your own computer).
(c) Unzip the archive.
This will create a folder pacman
1
If you use anything other than Python 3, you are on your own in terms of support, and if the code you
submit does not work (which is likely), you will lose marks.
1 yuan-yannakoudakis-6ccs3ml1-cw1
Figure 1: Pacman
(d) From the command line (you will need to use the command line in order to use the various
options), switch to the folder pacman.
(e) Now type:
python3 pacman.py
This will open up a window that looks like that in Figure 1
(f) The default mode is for keyboard control, so you should be able to play this game out using
the arrow keys.
Playing Pacman is not the object here — don’t worry if there is an issue with controlling Pacman
using the keys, that can happen on some platforms — but you will need to run this code to do the
coursework. So, if the code causes an error, get help.
When you are tired of running Pacman, move on to the next section.
2.2 Code to control Pacman
Now we work towards controlling Pacman by writing code. The file sampleAgents.py contains
several simple pieces of code for controlling Pacman. You can see one of these run by executing:
python3 pacman.py --pacman RandomAgent
This is not a good player (it is just picking from the available actions at random), but it shows you
a couple of things.
First, you execute an agent that you write by using the --pacman option, followed by the name of
a Python class. The Pacman code looks for this class in files called:
<something>Agents.py
2 yuan-yannakoudakis-6ccs3ml1-cw1
and, when it finds the class, will compile the relevant class. If the class isn’t in an appropriately
named file, you will get the error:
Traceback (most recent call last):
File "pacman.py", line 679, in <module>
args = readCommand( sys.argv[1:] ) # Get game components based on input
File "pacman.py", line 541, in readCommand
pacmanType = loadAgent(options.pacman, noKeyboard)
File "pacman.py", line 608, in loadAgent
raise Exception(’The agent ’ + pacman + ’ is not specified in any *Agents.py.’)
Now open your favourite editor and look at sampleAgents.py. If you look at RandomAgent you
will see that all it does is to define a function getAction(). This function is the only thing that is
required to control Pacman.2 The function is called by the game every time that it needs to know
what Pacman does — at every “tick” of the game clock — and what it needs to return is an action.
That means returning expressions that the rest of the code can interpret to tell Pacman what to do.
In the basic Pacman game, getAction() returns commands like:
Directions.STOP
which tells Pacman to not move, or:
Directions.WEST
which tells Pacman to move towards the left side of its grid (North is up the grid).
However, for your coursework, you have to pass this direction to the function api.makeMove() first,
just as the classes in sampleAgents.py do.
sampleAgents.py contains a second agent, RandomishAgent. Try running it. RandomishAgent
picks a random action and then keeps doing that as long as it can.
2.3 Towards a classifier
For this coursework you’ll work from some skeleton code that is in the folder pacman-cw1. The file
to look for is classifier.py which is used in classifierAgents.py. You will ONLY need to
modify classifier.py and no other file. Two things to note about this:
(a) The skeleton in classifier.py defines a class Classifier, and classifierAgents.py
defines a class ClassifierAgent. When we mark your coursework, we will do so by running
the ClassifierAgent class. If this doesn’t exist (or, similarly, if class Classifier doesn’t
exist, because you decided to rename things), we will mark your code as if it doesn’t work.
So make life easy for yourself, and use the classes and functions provided as the basis for your
code. Again, you will ONLY need to modify / use the skeleton in classifier.py and no
other file. We cannot accept code after the deadline has passed even if errors are of accidental
nature.
2Of course, controlling Pacman to do something well may require a number of functions in addition to
getAction().
3 yuan-yannakoudakis-6ccs3ml1-cw1
(b) The ClassifierAgent class provides some simple data handling. It reads data from a file
called good-moves.txt and turns it into arrays target and data which are similar to the
ones you have used with scikit-learn. When we test your code, it will have to be able to
read data in the same format as good-moves.txt, from a file called good-moves.txt. If it
doesn’t, we will mark your code as not working. So make life easy for yourself and stick to
the (admittedly, but intentionally, limited) data format that we have provided.
To run the code in classifierAgents.py, you use:
python3 pacman.py --pacman ClassifierAgent
Note the difference in capitalisation between file name and class name.
Now open your editor and take a look at the code for ClassifierAgent. There are six functions in it:
(a) __init__()
The constructor. Run when an instance of the class is created. Because the game doesn’t
exist at this point, it is of limited use.
(b) loadData()
This is a simple utility. The data in good-moves.txt is stored as a string. We need it as an
array of integers. This does the conversion.
(c) registerInitialState()
This function gets run once the game has started up. Unlike __init()__, because the game
has started, there is game state information available. Thus it is possible for Pacman to “look”
at the world around it.
Right now this is the only function that is doing any real work. It opens the file good-moves.txt,
and extracts the data from it, where data is parsed into the arrays data and target. These arrays are accessible from any function. (They are data members of the class ClassifierAgent.)
(d) final()
This function is run at the end of a game, when Pacman has either won or lost.
(e) convertNumberToMove()
Another simple utility. The data in good-moves.txt encodes moves that Pacman made in
the past using integers. What you need to do is to produce moves of the form:
Directions.NORTH
since that is the format which the game engine requires. This function converts from one to
the other in a way that respects the original conversion from moves to integers.
(f) getAction()
This function is called by the game engine every time step. What it returns controls what
Pacman does. Right now it just returns Directions.EAST or a random move (see predict()
in classifier.py). (The function also does some other stuff, but we will get to that later).