G54DIA: Designing Intelligent Agents

G54DIA:
Designing Intelligent Agents
Tutorial 1: Java Agent Package
Natasha Alechina
School of Computer Science
[email protected]
Outline of this tutorial
•  group tutorials cover the use of the Java agent package(s)
•  agent toolkits
•  uk.ac.nott.cs.g54dia Java agent packages
– library package
– demo package
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
2
Implementing your agent
•  many agent systems are implemented using an agent toolkit or library
•  faster development because some of the work has been done for you
•  may have to compromise – a toolkit may impose a particular
architecture or may lack some facilities
•  overhead of learning to use the toolkit, which can be significant
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
3
Types of toolkit
•  libraries: which provide particular capabilities, e.g., inter-agent
communication, and abstract ‘agent classes’ which you can subclass
to create your own agent (e.g., JADE, JADEX)
•  agent programming languages: high-level languages tailored for
writing agents, e.g., rule-based languages or languages for specifying
sequences of actions to perform in particular situations (PRS, JACK,
Jason, 2APL, 3APL)
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
4
Toolkits and architectures
•  both libraries and agent programming languages may be more or less
architecture-specific:
– some libraries presuppose a particular architecture, whereas others
allow you to define your own architecture
– agent programming languages usually presuppose a particular
agent architecture which executes the agent program
•  a toolkit may also assume a particular environment
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
5
G54DIA Java agent package(s)
•  a very simply agent library is provided as a starting point for your
project
•  about 1250 lines of code (25 java files), mostly written by Neil
Madden & Julian Zappala
•  consists of two main parts:
– library package: core library defining the task environment and
an abstract agent
– demo package: simple demo agent and simulator built using the
core library
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
6
Java agent package(s)
•  code assumes familiarity with interfaces, abstract classes, inheritance,
polymorphism, etc.
•  (basic) Javadoc for all classes
•  links to some useful Oracle tutorials on the G54DIA moodle page
•  download also includes a README file and two scripts
–  make file for use on unix/linux/OS X systems
–  .bat file for use on MS-Windows systems
•  that can be used to build the demo, and generate Javadoc documentation
for the core library and the demo code
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
7
library package
•  time-stepped environment
•  parameterised Environment class, which implements the agent’s
environment including Station, Task, and Well classes
•  agent’s percepts are a 2D array of Cells containing the things the
agent can ‘see’
•  an Action interface with implementations for some basic agent
actions, e.g., Move, Refuel, LoadWater, DeliverWater, etc
•  abstract Tanker class which has an abstract senseAndAct method
called once per timestep for each agent
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
8
Environment
•  notionally infinite 2D grid
•  contains Stations, Wells, a (single) FuelPump and
EmptyCells all of which implement the Cell interface
•  passes percepts to each agent and executes the Action returned by
the agent to update the Environment
• tick() method advances the time step
•  a run is 100,000 ticks
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
9
Tasks
• Stations randomly generate Tasks requiring delivery of a
specified amount of water
• Tasks persist until the required amount of water has been delivered
•  a Station has at most one Task at any given time
•  when a Task is completed, a Station may generate a new Task
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
10
Percepts
•  agent’s percepts are a 2D array of Cells containing the things the
agent can ‘see’
•  agent can determine the type of object in each Cell, and if it is a
Station, whether it has a task and how much water is required for
that task
•  each Cell in the grid has a position (a Point)
•  agents can test if two points are the same (equals), and can move to
a point, but doesn’t have access to the x and y values of points (more
on this in a later tutorial)
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
11
Actions
• LoadWaterAction: loads water from a well (agent must be at the well)
• DeliverWaterAction: delivers water to a station (agent must be at
the station)
• MoveAction: move one cell NORTH, NORTHEAST, EAST,
SOUTHEAST, SOUTH, SOUTHWEST, WEST, NORTHWEST (agent
must not be out of fuel)
• MoveTowardsAction: move one cell towards a specified Point
(agent must not be out of fuel)
• RefuelAction: refuel the agent (agent must be at the fuel station)
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
12
Tanker
•  at each timestep each Tanker’s senseAndAct method is called with
the agent’s view of the environment (a 2D array of Cell objects) as an
argument
• Tanker should return an Action which is then executed by the
Environment
• Tankers have a fuel level which is updated by movement/refuelling
actions
• Tankers have a water level which is updated by loading/delivering
actions
•  attempting to perform an illegal Action throws an
ActionFailedException
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
13
TankerViewer
•  a simple GUI that draws the region of the Environment around an
agent
•  also shows the timestep and the agent’s position, fuel level, water
level, number of completed tasks, amount of water delivered and
score
•  a version for multiple agents is also available (will be useful for the
second coursework)
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
14
demo package
•  illustrates the use of the library package
•  it provides
– DemoTanker class which extends the abstract Tanker class and
defines a very basic senseAndAct method
– DemoSimulator class which creates instances of the standard
Environment and DemoTanker, and displays them in a
TankerViewer
• DemoSimulator also controls the rate at which the simulation runs,
by defining the real time between calls to tick()
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
15
DemoTanker
public class DemoTanker extends Tanker{
public DemoTanker() { }
public Action senseAndAct(Cell[][] view, long timestep) {
// If fuel is low and not at fuel station, move there
if ((getFuelLevel() < (MAX_FUEL / 2) + 5) &&
!(getCurrentCell(view) instanceOf FuelStation)) {
return new MoveTowardsAction(FUEL_PUMP_LOCATION);
} else {
return new MoveAction((int)(Math.random()*8));
}
}
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
16
Using the code in your project
•  you can use the supplied code as part of your coursework submission
•  don’t change any library code (I should be able to compile and run
your submission with the original library classes)
•  if you want your own GUI, you can copy TankerViewer.java and
TankerViewerIconFactory.java into the demo package (and rename
them) and make any modifications you want, or simply write your
own GUI. If you do this, you will also need to modify
DemoSimulator.java.
•  if you use other code, e.g., from textbooks or the web, remember to
include a reference to the source of the code you use in a comment
•  see the module page for details of what needs to be cited and how to
cite it
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
17
The next tutorial
Exploring the environment
© Brian Logan 2013
G54DIA Tutorial 1: Java Agent Package
18