Table of Contents
As to evaluate your work done during the tutorial, you have to send your source code (in an archive) at the end of the session to Gauthier Picard
I. Preliminary Concepts and Setup
Jason Environment
In the MAOP course you have already installed a Jason environment. In this tutorial, we will only use the core Jason functionalities (no Jade, Cartago, etc.), and will concentrate on the agent behaviours to solve a toy constraint satisfaction problem: the n-queens problem.
The goal of this tutorial is to implement the Asynchronous Backtracking 1 (ABT) algorithm presented in the course using the Jason language and environment.
We propose to develop ABT in an iterative way. Even if not specified in the exercises, you should test your development step-by-step, using the Jason console and debugger.
Asynchronous Backtracking (ABT)
ABT is the most famous multi-agent algorithm to asynchronously solve distributed constraint satisfaction problems (DisCSP). For more information, have a look at your lecture notes .
As a reminder, here are the main procedures in ABT, excerpt from 1:
ABT Procedures
Therefore, the objective of the session is to develop Jason agents that implement these procedures and a multi-agent system that launches some ABT agents to solve a CSP (n-Queens Problem). Since agents will have the same code concerning the ABT implementation, you will create a simple abt.asl
file that will be included by all the Jason agent files.
Initializing the project
To sum up, you will have to develop:
- a
.mas2j
file describing the multi-agent system - a
abt.asl
file containing the code of ABT procedures - as many
.asl
file as agents in the system, including theabt.asl
code using the{ include("abt.asl") }
pre-processing directive
To simplify the development, we will only consider for the moment the 4-queens problem.
Exercise 1
- Launch the Jason editor
- Create a new project and save it as
4queens.mas2j
in a dedicated directory - Add 4 agents in the
4queens.mas2j
file, for instance:MAS abt { infrastructure: Centralised agents: q1; q2; q3; q4; }
- Create the
abt.asl
file and include it into each agent file// Agent q1_4 in project 4queens.mas2j { include("abt.asl") }
Now, your project is ready for development. You can run it and see agents talking to you.
Data structures
Views and Nogoods representation in ABT
In the remainder of the session, we will consider assignment lists (views and nogoods) as being lists of pairs [X,D]
where X
is the name of an agent and D
is a value. For instance, an agent view or a nogood can be represented as follows for simple integer values:
[[X1,2],[X2,3]]
Values and Domains in n-Queens Problem
Concerning the n-queens problem, we will represent possible values for agents as [X,Y]
pairs. So the domain for the agent 1
, in the 4-queens problem, should be:
[[1,1],[1,2],[1,3],[1,4]]
In fact, it is obvious that queens can't be on the same row, so it is useless to put values for different rows in each queen's domain.
Some useful functions for manipulating structures
Even if shortly described using pseudo-code, ABT relies on prerequisite functions to manipulate the different data structures (views, nogoods, etc.) used during the solving process by each agent. In order to only focus on ABT and agent communication, we provide a set of useful functions in the abt-utils.asl
file, that should be included in the abt.asl
file using the { include("abt-utils.asl") }
file.
Here a short description of each of these functions:
remove(V,L,UL)
is true ifUL
is equal toL
withoutV
contains(L,X)
is true ifL
contains at least oneX
includes(L1,L2)
is true if all elements ofL2
are inL1
value_of(X,L,D)
is true ifD
is the value ofX
in the assignment listL
contains_entry(L,X)
is true ifL
is an assignment list andX
is on entry in this listadd_to_view([Xi,Di],L1,L2)
is true ifL2
is the same assignment list than the assignment listL1
with a new valueDi
forXi
add_all_to_view(V,L1,L2)
is true ifL2
is the same list thanL1
with all new values fromV
add_to_nogoods(Ng,Ngs1,Ngs2)
adds a nogoodNg
in a nogood listNgs1
, resulting inNgs2
list, only if it is not already in (nogoods are equal if they are reciprocally compatible)get_agents_from_view(Agents,View)
is true ifAgents
is the list of all agents inView
lowest_priority_view_in_agent_view([Xj,Dj],View)
is true ifXj
is the lowest priority agent inView
with the valueDj
missing_values_from_links(Links,L,UL)
is true ifUL
is the assignment list containing all assignments[Xi,Di]
fromL
whereXi
is not inLinks
missing_values_from_view(Xi,View,U,UL)
is true ifUL
is the assignment list containing all assignments[Xj,Dj]
from L whereXj
is not an entry inView
andXi
≠Xj
Consistency checking
Back to Yokoo's definition of consistency
#consistency
In ABT, agents have to check consistency of some values wrt their current view and nogoods (in the procedure check_agent_view
). Let us look at the definition of consistency provided in Makoto Yokoo's book:
(An) assignment is consistent with the agent_view if all constraints the agent evaluates are true under the value assignments described in the agent_view and (xi, current_value), and if all communicated nogoods are not compatible with the agent_view and (xi, current_value).
This means that an agent checks the consistency of a value by :
- (check 1) checking whether the value has no conflict with values from its view (to avoid conflicts with neighbours)
- (check 2) checking whether the value and view are not in the same nogood, in order to avoid past errors
Both checks have to success for a value to be consistent. The second check relies on the compatibility definition :
A nogood is compatible with the agent_view and (xi, current_value) if all variables in the nogood have the same value in the agent_view and (xi, current_value).
Some useful functions for checking consistency
As for data structures, we provide some useful functions to check consistency in the abt-utils.asl
file:
full_consistent(Agent,Value,View,Nogoods)
is true ifValue
is consistent withAgent
'sView
and allNogoods
are not compatible with[Agent,Value]|View
(definition of consistency, see above)consistent(D,L)
is true isD
has no conflict with every value in assignment listL
(check 1, see above)uncompatible(Nogoods,View)
is true if allNogoods
are not compatible withView
(check 2, see above)consistent_values(Values,Agent,Domain,View,Nogoods)
is true ifValues
is the set of all consistent values inDomain
wrt theView
compatible(Nogood,View)
is true if all variables inNogood
have the same values in theView
Constraints in n-Queens Problem
If you look at the consistent
rule in abt-utils.asl
you will see a call to a rule called conflict
. This rule represents the constraints. It is true if the two input values violate a constraint. These rule is not provided because it is problem dependent (conflict
differs from a CSP to another).
In the n-Queens Problem, two values are in conflict if they represent positions on a chessboard that are respectively attackable by queens.
Exercise 2
- Save
abt-utils.asl
into your project directory - Read it carefully as to handle all the provided functions (data structures and consistency checking)
- Include
abt-utils.asl
intoabt.asl
file// abt.asl: // { include("abt-utils.asl") } ...
- Create a file called
queen-conflict.asl
, and implement theconflict
rule for the n-queens problem. - Include
queen-conflict.asl
into each agent file.// Agent q1 in project queen_4.mas2j { include("abt.asl") } { include("queen_conflict.asl") } ...
II. Adding Agent Initial Beliefs and Plans
Initial Beliefs
In ABT agents encapsulate several data they manipulate during the solving process:
Exercise 3
Add the five initial beliefs an agent begins with. Initially, an agent has empty agent_view
and nogood_list
, and outgoing links to agent with lower priority (lexicographical order). The current_value
is the first value from the domain
.
As a reminder, initial beliefs are facts that are add using the following syntax:
my_belief(<parameters>).
For instance, for agent q1
, we will have :
current_value([1,1]).
ABT Message Handlers
Reminder: Messages in Jason
In Jason message handlers are specified using beliefs under some conditions, by using the following syntax:
+my_message(<parameters>)[source(P)]: <conditions> <- <actions>
which means "the agent is able to receive messages called my_message
with some parameters
from an agent called P
. Once the agent receives this message under the given conditions
, the agent will perform some actions
.
For instance, the following code:
+propose(Id,Offer)[source(A)] : all_proposals_received(Id) <- !contract(Id).
means the agent can receive proposals using propose
messages. If all the proposals are received, the agent will sign a contract for this proposal.
To send messages, we use the .send
internal action using the following syntax:
.send(receiver,illocutionary_force,propositional_content)
In ABT, agents only communicate directly with other agents, so we will only consider the tell
illocutionary force. For instance:
.send(r,tell,open(left_door));
means the agent sends a message open
with the parameter left_door
to the agent r
.
Messages in ABT
In ABT (see the figure), we consider three different kinds of messages (represented as beliefs):
ok(Xi,Xj,Dj)
message:Xi
believesXj
's new value isDj
nogood(Xi,Xj,Nogood)
message:Xi
believesXj
discovered a new nogoodNogood
add_link(Xi,Xj)
message:Xi
believes it has to add a link toXj
Exercise 4
Add in the abt.asl
file the new (empty) message handlers for the ABT
algorithm. For now, the agent does nothing when receiving these
messages. These handlers should be atomic2, which is obtained using the atomic
annotation, e.g.:
@lmy_message01[atomic] +my_message(...)...
where lmy_message01
is a unique label.
Initial Plan and Goal
Commonly, we call !start
the initial plan of an agent (e.g. look at the generated agent source files):
/* Initial goals */ !start. /* Plans */ +!start : true <- .print("hello world.").
These means that the initial goal to achieve for the agent is !start
. To do so, the agent will follow the plan declared using +!start
…, which consist here in displaying a message in the console.
In ABT, the first action that agents do is to send their current_value
to their outgoing links.
Exercise 5
- Create a new
!start
plan inabt.asl
. It will be the starting point for agents. Remove the!start
plan from all the agent files. - Modify the
!start
declaration in order to sendok
messages to all the outgoing links. You can implement first a plan to sendok
messages to outgoing links and then use it into the!start
plan.
III. Deep into ABT
ok
Message Handler
When receiving a ok
message, an agent simply add/replace the new value in its view and check if its current value is still consistent. To check consistency we will implement the check_agent_view
procedure.
Exercise 6
Implement the atomic ok
message handler. You should add an empty !check_agent_view
plan.
check_agent_view
Plan
The check_agent_view
procedure will be implemented using a plan. In this procedure, three cases are possible:
- the current value is consistent: the agent does nothing and rests.
- there is another value in the domain that is consistent: the agent sets its current value to this consistent one, and informs all its outgoing link using
ok
messages. - there is no consistent value in the domain: the agent has to backtrack as to ask to its ingoing link to change its value. Backtrack will be implemented using a plan named
backtrack
.
Exercise 7
Implement the atomic !check_agent_view
plan for the three presented cases. You should add an empty !backtrack
plan.
backtrack
Plan
The backtrack
procedure in ABT consists in sending nogoods to ingoing agents as soon as a consistency check fails in the check_agent_view
plan for instance.
As to simplify the implementation and the computational cost of some checks, we will take a shortcut (proposed by Yokoo in his book). In fact, computing the set of all inconsistent subsets of the agent view requires a certain computation cost. So Yokoo proposes to only consider the current view as a nogood, even if not minimal.
Therefore, we propose to consider only one nogood V (see (iii-a) in the algorithm) which is the current view.
Exercise 8
Implement the atomic !backtrack
plan wrt the proposed simplification.
nogood
Message Handler
When receiving a nogood, an agent adds the nogood to its nogood list, adds unknown agents (and values) appearing in the nogood and asks for adding links, checks its view wrt the new nogoods, changes its value if necessary and then inform outgoing links of the new value.
Exercise 9
Implement the atomic nogood
message handler. You should add an empty !add_link
plan.
add_link
Messages Handler
Sometimes, all the links are not specified at the beginning of the process, but agents can add links when receiving nogood messages fro mother agent. In short, agents enlarge their social network during the solving process.
Exercise 10
Implement the add_link
message handler.
IV. Testing and Debugging ABT
Now, all the procedures required for running ABT properly should be implemented. But does it really work?
Exercise 11
- Test and debug ABT on the 4-queens problem.
- Test and debug ABT on another n-queens problem.
V. Extra Developments
You have completed all the levels and you are not replete, lets have a look at the following exercises…
Exercise X1
Implement (in Java for instance) an MAS generator that produces a .mas2j
, all the .asl
agent files, given the size of the n-queens problem you consider.
Exercise X2
Add a termination procedure in ABT, because it does not terminate properly in Jason.
Exercise X3
Extend your ABT solver to implement AWCS (see the lecture notes).
Footnotes:
1 M. Yokoo. Distributed Constraint Satisfaction: Foundations of Cooperation in Multi-Agent Systems. Springer, 2001.
2 Atomic: if an instance of a plan with an atomic annotation is chosen for execution by the intention selection function, this intention will be selected for execution in the subsequent reasoning cycles until that plan is finished. In short, an atomic plan cannot be interrupted by another plan.