What you’ll build |
||
The focus of this tutorial is on the programming of the environment of a multi-agent system. You will program agents situated in a simple shared environment with a set of resources encapsulated in artifacts that agents can observe and/or act on.
|
||
What you’ll need |
||
|
||
Code |
||
|
1. Creating a simple MAS
-
Download the code used in step 1 (initial and solution in a zipped file)
Learned features in this step |
|
1.1. Explanations
The multi-agent system is programmed by defining artifact templates (in java) and agent programs (in jason agent programming language). The jacamo project files launch and deploy the multi-agent system by creating and executing artifacts, workspaces and agents.
Env.java
-
This artifact (in
src/env/tools/Env.java
) has one operationprintMsg(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 (insrc/agt/majordomo.asl
) prints a message on the MAS console when deployed in the MAS
1
2
3
4
5
6
7
8
// majordomo agent program
!setup_and_monitor.
+!setup_and_monitor
<- print("ready. Your turn!").
{ include("$jacamoJar/templates/common-cartago.asl") }
{ include("$jacamoJar/templates/common-moise.asl") }
guest_agent.asl
-
The
guest_agent
agent program (programmed insrc/agt/guest_agent.asl
) has plans for observing and acting in the environment. -
For now, there in one plan with the action
printMsg
, operation of the artifact templateEnv
. -
This agent program will be enriched step by step during this tutorial
1
2
3
4
5
6
7
8
9
10
// guest_agent agent program
/* Initial goals */
!greet.
/* Plans */
+!greet : true
<- printMsg("Hello from guest").
{ include("$jacamoJar/templates/common-cartago.asl") }
{ include("$jacamoJar/templates/common-moise.asl") }
server.jcm
and client.jcm
-
These two jacamo
.jcm
files are used to create and deploy agents, workspaces and artifacts in the multi-agent system on a single or several nodes:-
The
server.jcm
creates aserver
workspace and deploys artifacts and agents in it on one node -
The
client.jcm
creates aclient
workspace on a different node and deploys artifacts and agents both in this workspace and in theserver
workspace. The agents created by this project file join also theserver
workspace to use artifacts that are deployed in it.
-
1.2. 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.joinRemoteWorkspace(String wspName, String address, OpFeedbackParam<WorkspaceId> res)
wherewspName
is the workspace name,address
is the address andres
is the output parameter receiving the workspace id.This agent has now two plans for achieving
greet
goal andsetup
sub-goal (created within execution of plan to achievegreet
). The plan for achievingsetup
consists in one action joining the remote workspace.
guest_agent.asl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/* Initial goals */
!greet.
/* Plans */
+!greet : true
<- !setup;
.my_name(Name);
.concat("Hello from ",Name,Msg);
printMsg(Msg).
+!setup
<- joinRemoteWorkspace("server","localhost",_).
{ include("$jacamoJar/templates/common-cartago.asl") }
{ include("$jacamoJar/templates/common-moise.asl") }
-
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.
To that aim, you will use the operations createWorkspace
, joinWorkspace
, makeArtifact
and focus
.
|
majordomo.asl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/* Initial goals */
!setup_and_monitor.
+!setup_and_monitor
<- createWorkspace("server");
joinWorkspace("server",Id);
!setupArtifacts.
+!setupArtifacts
<- makeArtifact("env","tools.Env",[],Id);
focus(Id);
print("ready. Your turn!").
{ include("$jacamoJar/templates/common-cartago.asl") }
{ include("$jacamoJar/templates/common-moise.asl") }
-
Execute first the
server.jcm
and then when the server is running, theclient.jcm
project files.
2. Acting on artifacts and observing artifacts
-
Download the code used in this step (initial and solution in a single zipped file) contains a new version of the template artifact 'Env'. You can also reuse the code of the agents that you developed in the previous step.
Learned features in this step
|
2.1. Explanations
-
The template artifact
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 project file (more detailed information on jacamo project file)
|
1
2
3
4
5
6
mas client {
agent guest_agent {
instances: 5
}
...
}
-
An
observer_agent.asl
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.asl
agent program and the client.jcm
make this agent joins the remote workspace server
, lookups for the artifact env
(of type Env
), focuses on this artifact and gets the value of the numMsg
observable property of this artifact.
1
2
3
4
5
6
// oberver agent plan reacting to change of belief
+numMsg(N)
<- .println("new message: total number is ",N).
{ include("$jacamoJar/templates/common-cartago.asl") }
{ include("$jacamoJar/templates/common-moise.asl") }
2.2. 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.asl
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.asl
program so that it leaves a message for ever. -
Refactor the observing plan of the
observer_agent.asl
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.
3. Computing with Artifacts
-
Download the code used in this step (initial and solution in a single zipped file) contains a new version of the template artifact 'Env'. You can reuse the code of the agents that you developed in the previous step.
Learned features in this step |
|
3.1. Explanations
In this step:
-
The template artifact
Env
has been enriched with acomputePi
action (with output parameters "'OpFeedbackParams'") -
Agents defined from the
observer_agent.asl
andguest_agent.asl
agent programs of the previous step are still present in the MAS -
A new agent program
computer_agent.asl
is added with a plan for joining the remote workspace, using thecomputePi
operation and finally printing the value concatanated to your name using the actionprintMsg
3.2. Practice
-
Program the
computer_agent.asl
as specified above -
Modify the
client.jcm
by commenting the launch of the execution of the agents from the previous steps and by adding the launch 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 -
Stop the MAS client side (let the MAS server side run) and Modify the
client.jcm
for executing now all the agents -
Execute the client part
4. Coordination with the help of the artifact
-
Download the code used in this step contains a new version of the template artifact 'Env'. You can reuse the code of the agents that you developed in the previous step.
Learned features in this step |
|
4.1. Explanations
In this step,
-
The template artifact
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.
4.2. 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.
5. Coordination again by the way of artifact
-
Download the code used in this step contains a new version of the template artifact 'Env'. You can reuse the code of the agents that you developed in the previous step.
Learned features in this step |
|
5.1. Explanations
In this step:
-
The template artifact
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 of typeEnv
.
majordomo.asl
1
2
3
4
5
6
7
8
9
10
11
12
13
// majordomo agent program
!setup_and_monitor.
+!setup_and_monitor
<- createWorkspace("server");
joinWorkspace("server",Id);
!setupArtifacts.
+!setupArtifacts
<- makeArtifact("env","tools.Env",[3]);
printMsg("ready. Your turn!").
...
5.2. Practice
-
Write a
yourname_bis_agent.asl
program writing 5 messages in sequenceTo that aim, you have to use the synchronisation between all agents provided by the artifact operation
sync
|
6. Modularity / Instances of Artifacts
-
If you didn’t realize previous step, you can get the code used in this step otherwise just use the result of previous step
Learned features in this step |
|
6.1. Explanations
-
With the previous implementation, we had a monolithic
Env
template artifact. 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 template artifact
Env
in multiple artifacts (templates and instances). -
Let’s do a little bit of refactoring in this step to obtain such a modular set of artifacts.
-
Thus, for this step, you will refactor the template artifact
Env
and decompose it into a templateGUIConsole
artifact (with instancemsg_console
), a templateCalculator
artifact (with instancecalculator
), a templateLock
artifact (with instancelock
) and a templateBarrier
artifact (with instancebarrier
). -
The agents and the
.jcm
project files will have to be changed to take into account this refactoring.
6.2. Practice
-
Refactor the
Env
template artifact as specified above (you can do this refactoring in an incremental way, i.e. creating and testing each new modular artifact) -
Refactor the agents code for using these artifacts.
7. Creating a Counter Artifact
-
The solution of this step is available in a zipped file.
Learned features in this step |
|
7.1. Explanations
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// Agent ticker code
/* Initial beliefs and rules */
/* Initial goals */
!setup.
/* Plans */
+!setup : true <-
!setupCounter(Id);
+counter(Id);
!increment.
+!increment : ready[source(Ag)] & counter(Id) <-
for (.range(I,1,100)) {
.wait(100);
inc[artifact_id(Id)];
.print("incrementing");
}.
+!increment : not ready[source(Ag)] <-
!increment.
+!setupCounter(C) : true <-
makeArtifact("counter","tools.Counter",[10],C);
.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.
-
7.2. Practice
-
Write the code of the
Counter
artifact template -
Write the code of the
ticker
agent -
Write the code of the
observer
agent
8. Creating a Bounded Counter Artifact
-
If you didn’t realize previous step, you can get the code used in this step otherwise just use the result of previous step
Learned features in this step |
|
8.1. Explanations
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 program the failure of an operation:
1
2
failed(String failureMsg)
failed(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))]
8.2. 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
template artifact, 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