After downloading the "code.tar.gz" file and installing "gzip" and "tar", you should move the "code.tar.gz" file to a folder that will be your folder for CS430/530. I use "d:/c/430/code/". Then open an MSDOS window and issue the commands
d:\c\430> gunzip code.tar.gz d:\c\430> tar xvf code.tarThe first line with create a file named "code.tar.gz". The second line will create a tree of folders in the current directory that contain the code for CS430/530. You now be able to start up LispWorks (for example), and issue the commands described below.
(cd "d:/c/430") (load "code/aima.lisp") (aima-load 'agents) ; this loads the AGENTS module
Note that if you unpacked the tar file in a different directory, you
will first need to edit "code/aima.lisp" to change the variable
*aima-root*
to point to the appropriate directory.
The code also comes with a set of HTML files that you can browse.
Point your web browser to the file
code/doc/overview.html
..
In order to get this code to work with Harlequin LispWorks, you should compile it. You can do this from inside lisp as follows:
(aima-compile 'all)
/usr/local/classes/cs/cs450/code/
is the root directory
for a package of lisp routines written by Peter Norvig. You can
browse the code using Netscape if you are logged in to a machine on
the ENGR or CS Research Networks using the following URL file:/usr/local/classes/cs/cs430/code/doc/overview.html.
If you are on an ENGR (or CS) machine, then from inside lisp, you can load this code by typing the following:
(load "/usr/local/classes/cs/cs430/code/aima.lisp") (aima-load 'agents) ; this loads the AGENTS module
On the ENGR machines, the code has been compiled for use with gcl (gnu common lisp) on HP workstations. It will NOT work on with gcl on SUN workstations, so to use the code, you must login to an HP workstation. Most of the machines with two-letter names (e.g., al.engr.orst.edu, ak.engr.orst.edu, etc.) are HP workstations. You can verify this by issuing the shell command
% uname -aOn an HP machine, this should display something like this:
HP-UX al B.10.20 A 9000/712 2011262678 two-user license
Each module has some builtin test routines. You can test the AGENTS module by typing
(test 'agents)
I recommend that you connect to your directory where you will store your files and use the load commands above to load in the AIMA code. This avoids the need to type long path names (except for the AIMA pathname!).
(defstructure (CLASS-NAME (:include PARENT-CLASS-NAME (field1 value1) (field2 value2) ... (fieldn valuen))) "optional documentation string")This defines the data fields of the class and specifies their initial values. It also defines the function
make-class-name
(here class-name
is the name
of the class in the definition), which creates instances of the class.
When you invoke make-class-name
, you can pass initial
values as keyword arguments:
(make-class-name :field2 val2 :field4 val4)This will create an instance of the class with default values for all fields except
field2
and field4
. Symbols
whose initial letter is a colon are keywords. They evaluate to
themselves and cannot have values or function definitions.
The defstructure
also defines accessor functions for each
of the fields. To continue the above example, the following accessor
functions will be defined:
(CLASS-NAME-field1 instance) (CLASS-NAME-field2 instance) ... (CLASS-NAME-fieldn instance)Where
instance
is a variable containing an instance
belonging to the class. The accessor functions can be used to access
the value of the field or to change the value of the field using
setf
.
Instead of methods (or member functions, in C++ terminology), Commonlisp has generic functions. A generic function can have many definitions as long as they all differ in the type of the first argument. You should not need to define any generic functions for this assignment, but here is how it is done:
(defmethod method1 ((arg1 type1) arg2 arg3 ... argn) "optional documentation string" code)This looks just like a function definition except that an explicit type is given with the first argument. You can take a look at the code in
agents/environments/vacuum.lisp
to see how
defmethod
is used to define methods for
performance-measure
, legal-actions
, and so
forth.
The full CLOS supports full generic functions that dispatch using the types of all arguments rather than just the first argument. This generality is not needed that often, however.
Here is a complete example of defining a class of vehicles:
(defstructure vehicle (price 0) (date-purchased 0) (mileage 0)) ;; manufacture a new vehicle. Creates and returns a default vehicle. (defun manufacture-vehicle () (make-vehicle)) ;; purchase the vehicle. Updates the price and date-purchased fields. ;; Returns the vehicle (defun purchase ((v vehicle) amount date) (setf (vehicle-price v) amount) (setf (vehicle-date-purchased) date) v)