What you’ll build
The focus of this tutorial is on the programming of the environment part of a multi-agent system.
You will program agents situated in a simple shared environment with artifacts that agents can observe and/or act on.
What you’ll need
-
Your favorite text editor or IDE
-
JDK 1.8 or later
-
A JaCaMo platform installed and configured
-
To have done the JaCaMo Hello World Tutorial
In this tutorial, you will program a multi-agent system with an environment shared among agents and possibly deployed on multiple machines.
The code is organized into two JaCaMo project files ( |
-
The whole code of this tutorial (it contains all the necessary code for each of the following steps) is available here (Download ZIP). If you prefer, you can also download each particular piece of code in each of the corresponding step.
STEP 1. Creating a simple MAS
-
Get the code used in step 1: here (zip)
Learned features
In this step, you will learn:
You will see:
|
Understand
The multi-agent system is built from artifact types, agent programs and jacamo project files to launch and deploy the multi-agent system.
Env.java
-
This artifact has one operation
printMsg(String msg)
. This operation prints on the standard output themsg
followed by the name of the agent executing the action. -
This artifact will be enriched step by step with operations and observable properties.
majordomo.asl
-
The
majordomo
agent program leaves a message on the MAS console when deployed in the MAS
1// majordomo agent program
2!setup_and_monitor.
3
4+!setup_and_monitor
5 <- print("ready. Your turn!").
6
7...
guest_agent.asl
-
The
guest_agent
agent program defines plans for observing and acting in the environment. -
For now, the plan in this program makes the agent use the
printMsg
operation of the artifact of typeEnv
. -
This agent program will be enriched step by step during this tutorial
1// guest_agent agent program
2/* Initial goals */
3!greet.
4
5/* Plans */
6+!greet : true
7 <- printMsg("Hello from guest").
8
9...
server.jcm
and client.jcm
-
These two jacamo project files are used to distribute agents and artifacts in the multi-agent system
-
The
server.jcm
creates aserver
workspace and deploys artifacts and agents in it -
The
client.jcm
creates aclient
workspace and deploys artifacts and agents both in this workspace and in theserver
workspace. The agents created by this project file will join also theserver
workspace to use artifacts that are deployed in it.
-
Practice
-
Execute first the
server.jcm
project file on one machine and theclient.jcm
project file on another machine (specify the IP in the node information of this file) or on the same machine (use of localhost). -
Add an action in the agent’s plan so that the agent leaves a message consisting of its name.
-
You can use the internal actions
.my_name(Name)
to get the name of the agent and.concat(str1,str2,Result)
to concat str1 to str2 into Result.
-
-
Refactor the code of the
guest_agent
agent program and of theclient.jcm
jacamo project file so thatguest_agent
(i) has one initial goalgreet
and (ii) joins remote workspace through explicit use ofjoinRemoteWorkspace
operation.This agent has now two plans for achieving
greet
goal andsetup
subgoal (created within execution of plan to achievegreet
). The plan for achievingsetup
consists in joining the remote workspace.
guest_agent.asl
1/* Initial goals */
2!greet.
3
4/* Plans */
5+!greet : true
6 <- !setup;
7 .my_name(Name);
8 .concat("Hello from ",Name,Msg);
9 printMsg(Msg).
10+!setup
11 <- joinRemoteWorkspace("server","localhost",_).
12
13...
-
Execute first the
server.jcm
and then theclient.jcm
project files. -
Refactor the code of the
majordomo
agent program and of theserver.jcm
project file so that initialisation of goals and beliefs of this agent, creation of workspaces and artifacts are realized in themajordomo
agent program.
majordomo.asl
1/* Initial goals */
2!setup_and_monitor.
3
4+!setup_and_monitor
5 <- createWorkspace("server");
6 joinWorkspace("server",Id);
7 !setupArtifacts.
8
9+!setupArtifacts
10 <- makeArtifact("env","tools.Env",[],Id);
11 focus(Id);
12 print("ready. Your turn!").
13
14...
-
Execute first the
server.jcm
and then theclient.jcm
project files.
STEP 2. Acting on artifacts and observing artifacts
-
If you didn’t realize previous step, you can get the code used in this step here (zip) otherwise just use the result of previous step
Learned features
In this step, you will learn how to make agents:
You will see:
|
Understand
In this step:
-
The artifact of type
Env
is enriched with an observable property ‘numMsg’ showing the number of messages left so far on the artifact by the different agents -
A couple of agents of type
guest_agent
repeatedly place messages on the artifact of typeEnv
created in the workspace
You can execute this step in group where one member of the group executes the "server" and each member of the group execute the MAS containing the guest_agent that joins the remote workspace of the server.
|
You can specify the number of agents of a certain agent types to be executed by using the keyword instances: followed by the number of instances in the definition of an agent of the jacamo script file.
|
1mas client {
2 agent guest_agent {
3 instances: 5
4 }
5...
6}
-
An
observer_agent
agent program is added to define agents able of observing artifacts of typeEnv
and reacting to changes related to its observable propertynumMsg
.
observer_agent.asl
The observer_agent
agent program and the client.jcm
make this agent joins the server
remote workspace, lookups for the artifact env
, focuses on this artifact and gets the value of the numMsg
observable property of this artifact.
1// oberver agent program
2+numMsg(N)
3 <- .println("new message: total number is ",N).
4...
Practice
-
Execute the
server.jcm
jacamo project file and theclient.jcm
as soon as the "'majordomo'" agent invites you to do so on the console. -
Refactor the
client.jcm
jacamo project file and theobserver_agent
program so that the agent joins the remote workspaceserver
, looks up for artifact, focuses on it instead of having this done within theclient.jcm
project file. -
Refactor the code of the
guest_agent
program so that it leaves a message for ever. -
Refactor the observing plan of the
observer_agent
program, so that the agent prints a message only when the number of messages is in the range 10 to 40. -
Adapt the
client.jcm
project file so that severalguest_agent
are executed.
STEP 3. Computing with Artifacts
-
If you didn’t realize previous step, you can get the code used in this step here (zip) otherwise just use the result of previous step
Learned features
In this step you will learn:
-
how to define operations with feedbacks (output parameters) 'OpFeedbackParams API' in an artifact
-
how to use operations on the same artifact in a sequence within a plan body
Understand
In this step:
-
The artifact
env
has been enriched with acomputePi
action (with output parameters "'OpFeedbackParams'") -
Agents defined from the
observer_agent
andguest_agent
agent programs of the previous step are still present in the MAS -
A new agent program
computer_agent
is added with a plan for joining the remote workspace, then using thecomputePi
action and finally printing the value concataneted to your name using the actionprintMsg
Practice
-
Program the
computer_agent
as specified above -
Modify the
client.jcm
for commenting the launching of the execution of the agents from the previous steps and for adding the execution of this new agent -
Execute the client part as soon as the
majordomo
agent invites you to do so by printing a message in the console -
Modify the
client.jcm
for executing now all the agents -
Execute the client part as soon as the
majordomo
agent invites you to do so by printing a message in the console
STEP 4. Coordination with the help of the artifact
-
If you didn’t realize previous step, you can get the code used in this step here (zip) otherwise just use the result of previous step
Learned features
In this step you will learn:
-
How to use an artifact as a coordination mean
-
How to use GUARDs in artifact
Understand
In this step,
-
The artifact of type
Env
has been enriched to install mutual exclusion in the actions for leaving messages.It has a private attribute
locked
and two operationslock
andunlock
insuring the mutual exclusion. -
The agent that you will develop for this step in the client part, will use these actions to access in an exclusive way to the artifact and print the messages in sequence.
-
The
observer
agent as in the previous steps, joins the workspace, lookups for the artifact of typeEnv
, focuses on it and gets the value of thenumMsg
observable property.
Practice
-
Develop your own agent
yourname_agent
(yourname
has to be replaced by your name) based on theguest_agent
code so that your agent leaves three messages in sequence. -
Execute the client part as soon as the "'majordomo'" agent invites you to do so by printing a message in the console
-
Transform the behaviour of your agent using the
lock
andunlock
operations to leave three messages in sequence in a synchronized way. -
Execute the client part again.
STEP 5. Coordination again by the way of artifact
-
If you didn’t realize previous step, you can get the code used in this step here (zip) otherwise just use the result of previous step
Learned features
In this step, you will learn:
-
how to initialise artifacts with parameters
-
use artifacts as synchronization means
Understand
In this step:
-
The artifact of type
Env
has been enriched with a synchronisation operationsync
and with the possibility to setup a number of participants for the synchronizationsetupBarrier
or via initialisation. -
The
majordomo
agent has been enriched to setup the barrier using theenv
artifact.
majordomo.asl
1// majordomo agent program
2
3!setup_and_monitor.
4
5+!setup_and_monitor
6 <- createWorkspace("server");
7 joinWorkspace("server",Id);
8 !setupArtifacts.
9
10+!setupArtifacts
11 <- makeArtifact("env","tools.Env",[3]);
12 printMsg("ready. Your turn!").
13...
Practice
-
Write a
yourname_bis_agent
writing 5 messages in sequenceTo that aim, you have to use the synchronisation between all agents provided by the artifact operation
sync
|
STEP 6. Modularity / Instances of Artifacts
-
If you didn’t realize previous step, you can get the code used in this step here (zip) otherwise just use the result of previous step
Learned features
In this step, you will learn:
-
how to improve extensibility, reusability
-
how to create and use multiple artifacts
Understand
-
With the previous implementation, we had a monolithic
Env
artifact type. It could cause some problems, for instance, if adding a long-term action such as computePi. Why? -
A better way could be to decompose this
Env
artifact type in multiple artifacts (types and instances). Let’s do a little bit of refactoring in this step. -
Thus, for this step, the artifact type
Env
is refactored and decomposed intoGUIConsole
(with instancemsg_console
),Calculator
(with instancecalcularo
),Lock
(with instancelock
) andBarrier
(with instancebarrier
). -
The agents and the
.jcm
project files will have to be changed to take into account this refactoring.
Practice
-
Refactor the
Env
artifact type as specified above (you can do this refactoring in an incremental way) -
Refactor the agents code for using these artifacts.
STEP 7. Creating a Counter Artifact
-
If you didn’t realize previous step, you can get the code used in this step here (zip) otherwise just use the result of previous step
Learned features
In this step, you will learn:
-
how to program artifact
-
how to program operations, observable properties, signals
Understand
In this step, you are going to program your own artifacts building a simple counting world example. In this world, ticker agents and observer agents are situated in the default workspace and use counting artifacts.
-
The
Counter
artifact is a simple artifact composed of acount
observable property initialized to 0 when the artifact is created.The artifact has a single operation
inc
without any parameters. This operation incrementscount
and sends atick(v)
signal every time it is executed by an agent, wherev
is the current value of thecount
observable property. -
The
ticker
agent:-
creates the
counter
artifact of typeCounter
and tells to all the agents that are in the MAS the name of this artifact.broadcast(tell, artifact_counter_is(counter))
, -
starts to use this artifact as soon as it receives an answer of one of the agents that it reached
-
Being a lazy agent, it rests for a while (let’s say 100 ticks), after each increment. It does this job for 100 cycles. Every cycle, it increments the artifact and prints a message.
-
1// Agent ticker code
2/* Initial beliefs and rules */
3
4/* Initial goals */
5!setup.
6
7/* Plans */
8
9+!setup : true <-
10 !setupCounter(Id);
11 +counter(Id);
12 !increment.
13
14+!increment : ready[source(Ag)] & counter(Id) <-
15 for (.range(I,1,100)) {
16 .wait(100);
17 inc[artifact_id(Id)];
18 .print("incrementing");
19 }.
20
21+!increment : not ready[source(Ag)] <-
22 !increment.
23
24+!setupCounter(C) : true <-
25 makeArtifact("counter","tools.Counter",[10],C);
26 .broadcast(tell,artifact_counter_is(counter)).
-
The
observer
agent:-
answers to the message sent by the
ticker
agent, telling this agent that it isready
-
lookup for this artifact with the name sent by
ticker
, and starts focusing on this artifact -
stops focusing on the artifact as soon as the observable property
count
is equal to 60 -
prints a message
observed new value
with the value, each time the observable property is changed. -
prints a message
perceived a tick
with the name of the artifact in which a tick has been done.
-
Practice
-
Write the code of the
Counter
artifact type -
Write the code of the
ticker
agent -
Write the code of the
observer
agent
STEP 8. Creating a Bounded Counter Artifact
-
If you didn’t realize previous step, you can get the code used in this step here (zip) otherwise just use the result of previous step
Learned features
In this step, you will learn:
-
to create and manipulate artifacts
-
to handle failure of actions in artifacts
-
to handle failure of actions in agent plans
Understand
In this step, we are going to extend the previous simple counting world example.
-
Keeping the same agents, the
Counter
artifact is transformed into a "bounded" counter artifact, i.e. the observable propertycount
cannot be higher than a maximum value fixed at the initialisation of the artifact. -
The artifact generates a failure signal when its action
inc
is no more possible.
-
The failed primitive is used to specify the failure of an operation:
1failed(String failureMsg)
2failed(String failureMsg, String descr, Object... args)
-
An action feedback is generated, reporting a failure msg and optionally also a tuple descr(Object…) describing the failure.
-
The annotation that you can use in the agent code, is a follows:
[error_msg(Msg),env_failure_reason(inc_failed("max_value_reached",Value))]
Practice
-
Create a 'jacamo' project called
step8
. -
Reuse the code of the previous step and install it in
step8
-
Change the code of the
Counter
artifact type, so that it behaves as described above -
Change the code of the
ticker
agent so that it executes a plan in case of failure of the actioninc