An Agent Program for Search and Retrieval Tasks

An Agent Program for Search and Retrieval Tasks
Exercise for the EASSS Agent-Oriented Programming Tutorial 2014
Koen V. Hindriks, [email protected]
1
Introduction
There are many tasks that involve search and retrieval, and, in case multiple agents take part
in the task, coordination between these agents. Some example domains that require search and
retrieval include search and rescue, surveillance, and package delivery. In order to design sound
agent strategies for search and retrieval, however, we do not need to model all details of these
various domains. In this exercise, we use the Blocks World for Teams environment (BW4T) as
a model that captures some of the more important strategic aspects of the problem. The BW4T
is an office-like simulation environment that consists of rooms in which coloured blocks may be
located, and a drop zone where blocks can be delivered. The main goal in the BW4T environment
is to deliver an ordered sequence of coloured blocks in that order to the drop zone. The BW4T goal
can be accomplished by a single agent or by multiple agents. In this exercise, where the focus is
on learning to write an agent program, we will use a single agent. A typical performance measure
for achieving the BW4T goal is to measure the time-to-complete the goal. In this exercise, we will
only be concerned with achieving the goal, not with performance.
2
Getting Ready
You need to install (1) Eclipse Kepler, (2) the Eclipse plug-in for Goal, (3) Repast which is
needed to be able to run the BW4T environment, and (4) create an initial Goal project for the
BW4T. If you have Eclipse Kepler already installed on your machine, you can skip step (1).
Note: Below, we assume Internet access but you can also copy the required
software from a USB stick during the tutorial. In that case, we assume that you
are running a 64 bit architecture.
Install Eclipse Kepler Download Eclipse Kepler using this link. Unzip the Eclipse compressed
file for your Windows, OSX, or Linux system to a location of your choice. Please note that Eclipse
requires Mac OS X 10.5 (Leopard) or greater, if you are running on a Mac.
Install GOAL Plug-in For installing the plug-in for Goal, launch Eclipse and perform the
following steps:
1. Click Add after selecting the Install New Software in the Help menu and add a repository
named GOAL Eclipse Plug-in using the following URL: http://ii.tudelft.nl/trac/
goal/export/head/GOALplugin/ECLIPSE/dist.
2. Select the GOAL Agent Programming field (make sure Group items by category is enabled),
click Next and follow the installation steps. During installation, ignore (accept) any warnings
about unsigned content.
1
3. Restart Eclipse.
4. Select Goal listed under the Other... option that can be selected in the Open Perspective
in the Window menu.
There is a quick start guide for the plug-in available at this link. Tip: If you want to keep the
plug-in up to date and download the latest when available, it is a good idea to enable automatic
updates via the Window → Preferences → Install/Update menu.
Install Repast Download Repast from the URL: http://ii.tudelft.nl/repast/. Install
Repast using the installer for your system if you are running Windows or Mac OSX (please do not
change the suggested install directory). Linux users should install Repast using Eclipse following
the same steps as for the Goal plug-in and the update site provided on the link at the beginning
of this paragraph.
Create BW4T Project From the File → New → GOAL Example Project menu, select BW4T2
from the menu. Alternatively, right click in the Script Explorer window to locate the GOAL Example Project menu item.
You are ready to start the exercise now!
(a) Complete information view
(b) Incomplete information view
Figure 1: BW4T view: omniscient perspective (left) and agent perspective (right)
2
3
The Blocks World for Teams
Figure ?? shows two screenshots of the BW4T environment. The left picture shows all blocks and
all agents in the environment. The blocks are located in nine rooms (room RoomA1-RoomC3),
and there are two agents in this session: Alice and Bob. Bob is in the left hall and holding a
pink block. Alice is in room RoomC3 and not holding any block. The right picture shows what
agent Alice can see at this point in time. As Alice is in room RoomC3, she can only see the
red block in this room.
To deliver a block successfully, an agent first has to go to a room using the action goTo(PlaceID)
(note the use of camel case!), go to a block of the right color using the action goToBlock(BlockID),
pick it up using the action pickUp, go to the drop zone at the bottom of the environment using
the action goTo(PlaceID) and drop the block in the drop zone using the action putDown. An
agent can only carry one block at a time. The status bar below the dropzone shows which blocks
need to be and have been delivered. In this example, a dark blue and a white block have already
been delivered (indicated by the green triangles), and a red, light blue, orange, and pink block still
need to be delivered. All agents can see the status bar, i.e., they receive percepts with information
about the current state of the status bar. When an agent drops a block in the drop zone or in a
hall, the block disappears from the environment. A block stays in the game if it is dropped in a
room other than the drop zone. Two agents cannot be in a room at the same time. Agents cannot
see each other, but when an agent tries to enter an occupied room, a red bar indicating that the
room is occupied appears.
Start the BW4T Server The BW4T environment is run as a server to which clients (agents)
can connect. Before launching agents that try to connect to the server, we therefore need to start
the server. Start the BW4T server by going into the BW4T2\BW4TServer folder in the BW4T
project in Eclipse and by double clicking on the BW4TServer.bat file located in that folder.
4
Developing an Agent Program for BW4T
The objective of this exercise is to implement an agent program that is able to achieve the main
goal of the BW4T by searching and delivering blocks of the right color in the right order. At the
end of this exercise document we will also briefly discuss how to extend the agent program so that
it is able to coordinate its actions with other agents.
4.1
Getting Started
Inspect the Agent Program The BW4T project that we created above already contains a
MAS file bw4t.mas2g that references an agent program file robot.goal. Open this file by
double clicking on it in Eclipse. You should see the following:
ˆ The init module which defines a knowledge predicate room/1 in the knowledge section,
three rules two of which process initial percepts and one that introduces an initial goal in
the program section, and an action specification goTo(Location) in the actionspec
section.
ˆ The main module with a single rule that instructs the agent to go to a location if it wants
to in the program section.
ˆ The event module that consists of a single rule for updating the predicate state/1 in the
program section. The state/1 predicate keeps track of the travelling state of the agent.
Launch the Multi-Agent System Let’s connect the agent to the server. Select the file
bw4t.mas2g, right click the file, select Debug As, and then Goal. When Eclipse asks to open
the Goal Debug Perspective, select Yes (and remember my decision).
3
Inspect the Agent’s Mind We have started the agent using Debug As mode because that
allows us to inspect the mind of the agent. You should see the agent’s beliefs in the upper right
corner of the Debug Perspective that inform us that the agent’s name is ’Bot1’. Other tabs in
this Introspector window show the agent’s goals, percepts, and messages. Inspect the percepts
to see which initial percepts the agent receives.
Step through the rules in the init module Let’s see what happens when the rules in the
init module are applied:
ˆ Change back to the Beliefs tab and step through the first two rules by using the F5 key or
by using the Step Into item on the tool bar. Amongst others, you should see that the agent
has a new belief state(arrived).
ˆ Change to the Goals tab and step the third rule. You should see now that the agent has a
new goal to be in some room.
Run the Agent Run the agent by using the F8 key or the Resume item on the tool bar.
Suspend the agent when it has arrived in the room it wanted to go to. Inspect the agent’s goals.
As you will see, the agent still has the goal to the room it is in! Check the agent’s beliefs. Does
it believe it is in the room? Tip: You can search the beliefs of the agent by selecting the Beliefs
tab and using the usual key combination CTRL-F.
4.2
Restarting the Server and MAS
There are a few steps you need to perform to test and inspect the consequences of changes you
made to your agent program. Importantly, you need to terminate any running MAS before you
can make changes, and reset the server before starting the (modified) MAS again.
Terminate the MAS Use CTRL-F2 or the Terminate item on the tool bar to terminate the
MAS in the Debug Perspective.
Reset the Server Press the Reset button in the BW4T gui to reset the server.
Restart the MAS To restart simply means to launch the MAS again. In the GOAL Debug
Perspective, use the Debug item on the tool bar to restart. In the GOAL Perspective, as before,
select the file bw4t.mas2g, right click the file, select Debug As, and then Goal. Tip: You can
switch between the two perspectives using the buttons in right upper corner in Eclipse.
Step the Agent Again Step through the first two rules in the init module again. You should
now see 10 possible instantiations for variables in the third rule in the Evaluation window. Step
into the rule. Which goal did it select this time? When a rule of the form
if bel( room(PlaceID) ) then adopt( in(PlaceID) ).
has multiple instantiations that can be applied, a random choice is made to apply one of the
possible instantiations. Run the agent as before and suspend it when it has arrived in the room
it wanted to go to.
4.3
Percept Processing
We noted above that the agent still has a goal to be in some room even if it has already arrived in
that room. The reason is that the agent does not believe it has arrived. To make the agent believe
this it needs to process percepts and we are going to modify the agent program. In order to do
so, make sure that you have terminated the MAS and switched back to the GOAL Perspective.
4
Processing the Send on Change Percept in/1 The in percept informs the agent that it is
in a room. A percept of the form percept(in(Place)) is sent when the agent enters a room
and a negated version percept(not(in(Place))) is sent again when the agent leaves the
room. A percept like that is called a send on change with negation percept. It is processed using
two rules. Add the following rules at the beginning of the event module in the agent program:
% Update whether the robot is in a room or not. @SendOnChangeWithNegation(in/1).
if bel( percept(in(RoomID)) ) then insert( in(RoomID) ).
if bel( percept(not(in(RoomID))) ) then delete( in(RoomID) ).
This takes care of the updating of where the agent is, but additional information is needed to
achieve the main goal. Most importantly, information about blocks in rooms needs to be stored
in the agent’s belief base.
Processing Send Always Percept color/2 A percept color/2 provides information about
the name and the color of blocks in a room. Because we also want to keep track of the location of
a block, we introduce the predicate block(BlockID, ColorID, RoomID). Add the following
rules to the event module directly after the rules for processing the in percept:
% If a robot sees blocks and is in a room, then update info on blocks in that room.
% @SendAlways(color/2)
forall
bel(percept(color(BlockID,ColorID)), in(RoomID), not(block(BlockID,ColorID,RoomID)))
do insert(block(BlockID, ColorID, RoomID)).
forall
bel(block(BlockID,ColorID,RoomID), in(RoomID), not(percept(color(BlockID,ColorID))))
do delete(block(BlockID, ColorID, RoomID)).
Note that rule order does matter! The rules above check whether the agent is in a room, which
requires that the agent’s beliefs about this fact need to have been updated already. Because rules
in the event module are evaluated and applied in linear order, this is ensured by first performing
the rules for updating the in predicate.
We used rules of the form if ... then ... above because a robot agent can be in at most one
room at a time. Because there can be multiple blocks in a room and we want to process percepts
for all of these blocks, we used rules of the form forall ... do ... here. Note that we used the fact
that color/2 is a send always percept by implicitly assuming that if a robot is in a room and
does not see a block, the block is not (no longer) in a room.
Processing Send on Change with Negation Percept holding/1 Another important piece
of information the agent needs to keep track of is whether the agent is holding a block or not. The
agent receives percepts of the form
ˆ percept(holding(BlockID)), and
ˆ percept(not(holding(BlockID))).
Use these to write rules to update the beliefs about the predicate holding/1 by processing these percepts. Tip: Note that an agent can only hold one block at a time similar to the
fact that it can be in one room at a time.
Run the Agent Don’t forget to first reset the server! Launch the agent again in Debug As
mode and start it. When it arrives in a room, pause it again. Check the agent’s goal base. Has
the goal to go to the room been removed?
5
4.4
Goal Setting
We have seen that the robot initially sets a goal to explore a room. We have also introduced rules
now to update the agent’s beliefs with information it collects in a room. It is time to introduce
rules now to manage the goals the agent sets. The main design guideline we will use to do so is to
have the agent set at most one goal at a time. That is, we don’t want our agent to adopt multiple
goals.
Adopt a Goal to Go to a Room We want the agent to adopt a goal to go to a room if it has
no goals and it has not yet visited a room. To make sure the agent does not revisit a room, we have
first to keep track of the rooms it has visited. There is a simple solution for doing so: introduce
a predicate visited(RoomID) to keep track of the rooms that have been visited. Using the +
operator, add an insert(visited(RoomID)) action to the first rule for updating the
in percept introduced above that adds a visited/1 fact to the agent’s beliefs.
We can now using the visited/1 predicate write a rule for adopting a goal to go to a room
the agent has not visited. Add the following template rule to the end of the event module:
if not(goal(in(_))), ... then adopt(in(RoomID)).
Note: the underscore _ used in the rule is the special don’t care variable of Prolog. Replace the
dots in the rule above with a mental state condition that
ˆ checks whether the agent believes RoomID is a room, and
ˆ checks whether the agent does not believe it has visited room RoomID.
Run the Agent It is good practice to often run the agent to test whether it behaves as expected
after making a few modifications to the agent program. Launch the agent in Debug As mode and
start it. When it does not move any more, pause it again. Check the agent’s belief base and search
it for visited facts. Did the agent visit all rooms?
We have made an agent that explores the BW4T environment, but it does nothing yet to
achieve the main BW4T goal: deliver blocks of the right color in the right order to the drop zone.
Adopt Main BW4T Goal The main goal is communicated to the agent using a percept
sequence/1. This is a send once percept that is only received once when the agent is launched.
We will add this sequence of colors as a goal to the agent’s goal base. Because this is a send once
predicate, add the following rule template to the program section of the init module:
% Adopt main BW4T goal.
if bel(percept(sequence(List))) then ... + insert(sequence([])).
Replace the dots with an adopt action that adds a sequence/1 fact to the agent’s goals.
The insert action makes the agent believe that it has not delivered anything yet (represented by
the empty list []).
Compute Next Block Needed In order to compute which block the agent needs to retrieve
next, we need to inspect the agent’s beliefs to find out which Done list of blocks has already been
delivered and the agent’s goals to find out which ToDo list of blocks needs to be delivered. The
idea is to introduce a macro nextColorNeeded(NextColor) that is defined by a mental state
condition that compares both lists by using the Prolog predicate append/3. At the start of
the program section of the event module add:
#define nextColorNeeded(NextColor)
goal(sequence(ToDo)), bel(sequence(Done), append(Done, [NextColor|_], ToDo)).
6
Adopt Goal to Hold Block in a Room
one-but-last rule in the event module:
Add the following rule template right before the
if ...,...,nextColorNeeded(ColorID),... then adopt(holding(BlockID), in(RoomID)).
We adopt a conjunctive goal because we want to simultaneously achieve the goals of (1) holding
a block (2) in a particular room. This will also prevent our agent from adopting a new goal to go
to a room as long as it is not yet holding a block when it arrived in the room it wants to be in.
Replace the dots in the rule template above by
ˆ First set of dots: by a condition that checks the agent does not have a goal of holding( )
a block already. Tip: Compare the rule for adopting a goal to be in a room above.
ˆ Second set of dots: by a condition that checks the agent does not believe it is already holding
a block,
ˆ Third set of dots: by a condition that checks the agent believes that BlockID is a block
that has color ColorID and can be found in room RoomID.
Run the Agent You might want to run the agent again to see whether it still behaves as you
expect. It may be useful to check what the agent’s goal base looks like.
Setting a Goal to go to the Drop Zone We need to direct our agent to go to the drop zone
if it is holding a block. The assumption here is that the agent only will be holding a block if that
contributes to the main goal. As a final goal setting rule, add the following rule directly after
the previous one:
if not(goal(in(_))), bel(holding(_)) then adopt(in(’DropZone’)).
4.5
Acting to Achieve Goals
So far we have mainly been concerned with updating the agent’s mental state. It is time now
to start thinking about the actions the agent should perform to achieve its new goals. Only one
action has been specified in the agent program for going some place. We now will add actions
for handling blocks. The most important thing we need to think about is when an action can be
performed, i.e., its precondition. What will happen when an action is performed we’ll leave for
the agent to perceive (i.e., percept processing needs to take care of that).
Going to a Block In the BW4T, being in a room does not mean the agent is close enough to
a block to pick it up. It first needs to move towards a block using the goToBlock(BlockID)
action. Add the following action specification to the init module’s actionspec section:
% Precondition of going to a block is that the robot is in the same room as the block
% is in and the robot is not already going somewhere.
goToBlock(BlockID) {
pre { not(state(traveling)), block(BlockID,_,RoomID), in(RoomID) }
post { true }
}
Picking Up a Block When at a block, the agent should be able to pick it up. Add the
following action specification template:
7
% Precondition of picking up a block is to be close enough to the block.
pickUp {
pre { ... }
post { true }
}
Replace the dots above with an appropriate precondition. Tip: Use the atBlock/1
predicate.
Putting a Block Down When holding a block, the agent should be able to put it down. Add
the following action specification template:
% Precondition of putting down a block is that the robot is holding a block.
putDown {
pre { ... }
post { true }
}
Replace the dots above with an appropriate precondition.
4.6
Defining an Action Strategy
We have now everything in place except for a strategy that selects when to perform one of the
actions we have just specified above. We will add such a strategy to the main module in the agent
program. The main module is designed to select actions. Different from the event module, only
the first applicable rule in the main module is applied. We can exploit this feature to prioritize
action selection.
Putting Down a Block Most importantly, an agent should put down a block if it is in the
drop zone. The idea is that the agent should only be holding a block in the drop zone if that is
part of achieving the main BW4T goal. Add the following rule to the beginning of the
main module:
if bel(in(’DropZone’)) then putDown.
Notice that the rule only checks whether the agent is in the drop zone but not whether it is holding
a block. The check whether the agent is holding a block is performed by the precondition of the
putDown action.
Picking Up a Block The second most important thing to do is to pick up a block that the
agent wants to hold. Add a rule directly after the first in the main module that selects
the pickUp action when the agent wants to hold a block. Tip: Use the holding(BlockID)
predicate. You do not need to check whether the agent is at a block as this is already done by the
precondition of the pickUp action.
Going to a Block The third most important thing to do is to move towards a block the agent
wants to hold (whenever possible). Add a rule directly after the previous one you added
to select the goToBlock(BlockID) action if the agent wants to hold block BlockID.
Run the Agent Run the agent again to see whether it behaves as you expect. Does it already
achieve the main BW4T goal?
8
4.7
Tracking Overall Progress
The main reason why the agent is not yet accomplishing its main goal is that it does not keep track
of the progress it makes with respect to this goal. The agent gets a percept sequenceIndex(N)
as an update of the progress that has been made with respect to the goal which we will use.
Although with some background in Prolog it is probably feasible to come up with a solution to
updating the agent’s beliefs with respect to the sequence predicate, below we will provide a
solution for you to add to the program.
Keeping Track of Progress
that were introduced above:
Add the following rule just before the goal management rules
% Update progress with respect to the main goal.
if goal(sequence(ToDo)),
bel( percept(sequenceIndex(N)), sequence(Done), nth1(N,ToDo,Color),
append(Done,[Color],NewDone))
then delete(sequence(Done)) + insert(sequence(NewDone)).
The End Upon achieving the BW4T goal, the agent can stop acting. One way to do this is
to add an exit option to the mainmodule that tells the agent to quit upon achieving all of its
goals. Modify the main module to look like the following:
main module [exit = nogoals] {
Congratulations, at this point you should have a pretty good though perhaps not an optimal
agent able to achieve the BW4T task alone!
4.8
Short Questionnaire
You have been using the Eclipse plug-in that we recently released for Goal. We would very much
like to get an impression of how you experienced working with this tool. Therefore we would like
to ask you to complete a three-minute questionnaire on the plug-in. Can you please complete the
questionnaire at this link? Thanks in advance!
5
Coordinating BW4T Agents
For those of you who would like to continue exploring the BW4T, there is the challenge of developing a coordinated team of multiple agents that achieve the BW4T goal more efficiently than a
single agent can.
In this third part of the exercise document we suggest some additional modifications you can
make to the agent program we have created so far such that your agent is able to solve the BW4T
task in collaboration with other agents. But we do not provide any step-by-step guidance any
more below.
1. Understanding teamwork. In order to run multiple agents, you have to adapt the
bw4t.mas2g file. In the MAS file, go to the init rule and change the agentcount from
1 to 2. Now run the simulation, and describe what happens. Do the agents efficiently solve
the task as a team? How could the team’s performance be improved?
2. Implementation of coordination (1). As a first step towards a coordinating agent, make
the agent able to deal with agents that remove blocks. This is needed when the agent has
adopted the goal to deliver a block that has been removed by another agent. Note that this
can only happen when the agent adopts the goal to deliver the block while the agent is not
9
inside the room of the block. Namely, if the agent already is inside the room, other agents
cannot enter that room and pickup the block. Thus, when the agent enters a room to deliver
a particular block, it should drop that goal when the agent observes that the block is no
longer there. Tip: Use the action drop(Goal).
3. Implementation of coordination (2). Another surprise that agents can encounter in a
team is that another agent delivers a block of the color that you are delivering. For example,
the goal sequence indicates that a red block needs to be delivered, and you are on your way
to the dropzone while carrying a red block. If another agent then delivers a red block (and
the next subgoal in the goal sequence is not a red block), you have to change your plans.
Extend the agent such that it is capable to deal with this situation, that is, the agent should
drop the block and adopt a new goal. Make sure that if the block may be needed at a
later moment, it should not be dropped in a hallway or in the dropzone because then it will
disappear.
4. Understanding coordination by communication (1). In the previous two steps, you
improved the agent’s coordination capabilities by non-verbal behavior. Now you are going to
improve its coordination capabilities through communication. The agent can send different
types of messages. For instance, it can send messages about the blocks it finds in rooms,
or it can inform the other agents about the rooms it intends to visit. Choose one type
of communication of which you expect that it will have the most positive effect on team
performance and explain your choice.
5. Implementation of communication. In this step you will implement the communication behavior of your choice. This requires the implementation of a) sending messages,
and b) perceiving messages. To send messages, insert send-actions in the code of the type
allother.send(.....). Send-actions can be added to rules in the main module or in
the event module with the + operator. To perceive messages, insert one or more rules in
the event module.
6. Understanding coordination by communication (2). Run the agents with communication behavior in the BW4T environment. What are the effects of adding this type of
communication? Is this as expected?
Congratulations, at this point you should have a pretty good agent able to achieve the BW4T
task with a team!
Improving Your Agent Even Further After completing the above, the agent team still does
not perform optimal, and there is much room for additional improvement. The team’s performance
can for instance be improved by adding other types of communication, adding other non-verbal
communication mechanisms, or improving the agent’s strategy for searching and delivering blocks.
Have fun! and don’t hesitate to contact us at [email protected]. Also check out Goal’s website
at http://ii.tudelft.nl/trac/goal.
10