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

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 (.jcm): one for a so-called server workspace, one for a client one.

  • 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

Learned features

In this step, you will learn:

  • how to make agents

    • create workspaces and deploy artifacts in it

    • do actions on artifact

    • join a (remote) workspace

    • act in different workspaces

You will see:

  • definition of a basic artifact exposing some operations

  • distributed execution of a MAS

  • the concurrent use of an artifact by multiple agents (mutual exclusive access, no race conditions)

Understand

The multi-agent system is built from artifact types, agent programs and jacamo project files to launch and deploy the multi-agent system.

A simple type of GUI artifact: Env.java
  • This artifact has one operation printMsg(String msg). This operation prints on the standard output the msg followed by the name of the agent executing the action.

  • This artifact will be enriched step by step with operations and observable properties.

The majordomo agent program: 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...
The guest_agent agent program: 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 type Env.

  • 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 a server workspace and deploys artifacts and agents in it

    • The client.jcm creates a client workspace and deploys artifacts and agents both in this workspace and in the server workspace. The agents created by this project file will join also the server workspace to use artifacts that are deployed in it.

Practice

  • Execute first the server.jcm project file on one machine and the client.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 the client.jcm jacamo project file so that guest_agent (i) has one initial goal greet and (ii) joins remote workspace through explicit use of joinRemoteWorkspace operation.

    This agent has now two plans for achieving greet goal and setup subgoal (created within execution of plan to achieve greet). The plan for achieving setup consists in joining the remote workspace.

The new guest_agent agent program: 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 the client.jcm project files.

  • Refactor the code of the majordomo agent program and of the server.jcm project file so that initialisation of goals and beliefs of this agent, creation of workspaces and artifacts are realized in the majordomo agent program.

The new majordomo 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 the client.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:

  • lookup or discover an artifact

  • observe an artifact

You will see:

  • definition of an artifact including observable properties

  • concurrent use and observation

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 type Env 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 type Env and reacting to changes related to its observable property numMsg.

The observer_agent agent program: 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 the client.jcm as soon as the "'majordomo'" agent invites you to do so on the console.

  • Refactor the client.jcm jacamo project file and the observer_agent program so that the agent joins the remote workspace server, looks up for artifact, focuses on it instead of having this done within the client.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 several guest_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 a computePi action (with output parameters "'OpFeedbackParams'")

  • Agents defined from the observer_agent and guest_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 the computePi action and finally printing the value concataneted to your name using the action printMsg

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 operations lock and unlock 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 type Env, focuses on it and gets the value of the numMsg observable property.

Practice

  • Develop your own agent yourname_agent (yourname has to be replaced by your name) based on the guest_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 and unlock 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 operation sync and with the possibility to setup a number of participants for the synchronization setupBarrier or via initialisation.

  • The majordomo agent has been enriched to setup the barrier using the env artifact.

The majordomo agent program: 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 sequence

    To that aim, you have to use the synchronisation between all agents provided by the artifact operation sync

  • You can do this exercise using a common env server and connecting the agents developed by different students.

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 into GUIConsole (with instance msg_console), Calculator (with instance calcularo), Lock (with instance lock) and Barrier (with instance barrier).

  • 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 a count observable property initialized to 0 when the artifact is created.

    The artifact has a single operation inc without any parameters. This operation increments count and sends a tick(v) signal every time it is executed by an agent, where v is the current value of the count observable property.

  • The ticker agent:

    • creates the counter artifact of type Counter 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 is ready

    • 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 property count 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.

Hints: Handling failure
  • 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 action inc