Programming Agents with JADE for MultiAgent Systems Collected

  • Slides: 118
Download presentation
Programming Agents with JADE for Multi-Agent Systems Collected by Anton Naumenko Industrial Ontologies Group

Programming Agents with JADE for Multi-Agent Systems Collected by Anton Naumenko Industrial Ontologies Group Based on Jade, Java and Eclipse documentation

Content n n n Briefly about components JADE overview Setting up the Development Environment

Content n n n Briefly about components JADE overview Setting up the Development Environment q Installing JADE n q q n Running demo Installing Eclipse Setting Eclipse to work with JADE Book Trading example

Components, Java http: //java. sun. com/ n n Java technology is a portfolio of

Components, Java http: //java. sun. com/ n n Java technology is a portfolio of products that are based on the power of networks and the idea that the same software should run on many different kinds of systems and devices. The Java programming language lets you write powerful, enterprise-worthy programs that run in the browser, from the desktop, on a server, or on a consumer device.

Components, Eclipse http: //www. eclipse. org/ n n Eclipse is an open source community

Components, Eclipse http: //www. eclipse. org/ n n Eclipse is an open source community whose projects are focused on providing an extensible development platform and application frameworks for building software Eclipse provides extensible tools and frameworks that span the software development lifecycle, including support for modeling, language development environments for Java, C/C++ and others, testing and performance, business intelligence, rich client applications and embedded development

Components, JADE http: //jade. tilab. com/ n n JADE (Java Agent DEvelopment Framework) is

Components, JADE http: //jade. tilab. com/ n n JADE (Java Agent DEvelopment Framework) is a software framework fully implemented in Java language. It simplifies the implementation of multi-agent systems through a middle-ware that claims to comply with the FIPA specifications and through a set of tools that supports the debugging and deployment phase.

JADE overview JADE is a middleware that facilitates the development of multi-agent systems. It

JADE overview JADE is a middleware that facilitates the development of multi-agent systems. It includes n A runtime environment where JADE agents can “live” and that must be active on a given host before one or more agents can be executed on that host. n A library of classes that programmers have to/can use (directly or by specializing them) to develop their agents. n A suite of graphical tools that allows administrating and monitoring the activity of running agents.

Containers and Platforms n n n Each running instance of the JADE runtime environment

Containers and Platforms n n n Each running instance of the JADE runtime environment is called a Container as it can contain several agents. The set of active containers is called a Platform. A single special Main container must always be active in a platform and all other containers register with it as soon as they start. See JADE Administrative Tutorial

AMS n The AMS (Agent Management System) that provides q q the naming service

AMS n The AMS (Agent Management System) that provides q q the naming service (i. e. ensures that each agent in the platform has a unique name) represents the authority in the platform (for instance it is possible to create/kill agents on remote containers by requesting that to the AMS).

DF n The DF (Directory Facilitator) that provides a Yellow Pages service by means

DF n The DF (Directory Facilitator) that provides a Yellow Pages service by means of which an agent can find other agents providing the services he requires in order to achieve his goals.

Sample architecture

Sample architecture

JADE Requirements n The only software requirement to execute the system is the Java

JADE Requirements n The only software requirement to execute the system is the Java Run Time Environment version 1. 4

Getting JADE All the software is distributed under the LGPL license limitations and it

Getting JADE All the software is distributed under the LGPL license limitations and it can be downloaded from the JADE web site http: //jade. tilab. com/. Five compressed files are available: n The source code of JADE n The source code of the examples n The documentation, including the javadoc of the JADE API and this programmer's guide n The binary of JADE, i. e. the jar files with all the Java classes n A full distribution with all the previous files

Installing JADE n n Download full distribution of JADE Extract JADE-all-3[1]. 2. zip q

Installing JADE n n Download full distribution of JADE Extract JADE-all-3[1]. 2. zip q n Result is Extract JADE-bin-3. 2. zip and JADE-doc 3. 2. zip to a folder of your choice for the development

Test JADE. Run demo n Your folderjadedemo n See Meeting. Schedule. html for description

Test JADE. Run demo n Your folderjadedemo n See Meeting. Schedule. html for description Run run. Demo. bat to check it out n

Demo coverage n n the registration of an agent with the default DF the

Demo coverage n n the registration of an agent with the default DF the registration of an agent with a remote DF belonging to another platform or, different from the default DF the search within the known DFs for a list of agents and their properties, in particular the list of Meeting. Scheduler agents and the name of the user that they represent the usage of the Fipa. Contract. Net protocol, both the initiator and responder role

Demo n n When the two agents start, two login windows appears. Click OK

Demo n n When the two agents start, two login windows appears. Click OK button without passwords

Demo. RMA agent n Remote Agent Management GUI

Demo. RMA agent n Remote Agent Management GUI

Demo. Appointment Scheduler

Demo. Appointment Scheduler

Demo. Fixing appointments of Tizio n n Then, use the calendar window of Tizio

Demo. Fixing appointments of Tizio n n Then, use the calendar window of Tizio to fix a couple of appointments in different dates with different descriptions. Tizio has fixed two appointments for the 1 st and the 2 nd

Demo. Fixing appointments of Tizio n 1 st is meeting with John

Demo. Fixing appointments of Tizio n 1 st is meeting with John

Demo. Fixing appointments of Tizio n 2 nd is meeting with Jerry

Demo. Fixing appointments of Tizio n 2 nd is meeting with Jerry

Demo. Update known persons n n Switch then to the calendar window of Caio.

Demo. Update known persons n n Switch then to the calendar window of Caio. From the directory menu, execute the item "Update known persons with the facilitator" such that Caio comes to know about Tizio existence.

Demo. Fix appointment of Caio and Tizio n Use the calendar window of Caio

Demo. Fix appointment of Caio and Tizio n Use the calendar window of Caio to fix an appointment with Tizio between 1 st and 4 th.

Demo. Result of negotiation n The Contract. Net protocol should be then executed and

Demo. Result of negotiation n The Contract. Net protocol should be then executed and both agents should converge to the date 3 rd.

Demo. Sniffer agent

Demo. Sniffer agent

Demo. Update of known persons

Demo. Update of known persons

Demo. Request and Inform

Demo. Request and Inform

Demo. Sniffer’s tracks of communication

Demo. Sniffer’s tracks of communication

Eclipse Requirements n You will need a Java runtime environment (JRE) to use Eclipse.

Eclipse Requirements n You will need a Java runtime environment (JRE) to use Eclipse.

Getting Eclipse n download the Eclipse SDK q http: //www. eclipse. org/downloads/index. php

Getting Eclipse n download the Eclipse SDK q http: //www. eclipse. org/downloads/index. php

Installing Eclipse n Extract eclipse-SDK-3. 1 -win 32. zip to the folder of your

Installing Eclipse n Extract eclipse-SDK-3. 1 -win 32. zip to the folder of your choice

Running Eclipse n Run eclipse. exe

Running Eclipse n Run eclipse. exe

Running Eclipse. Workspace n Set arbitrary folder for the workspace

Running Eclipse. Workspace n Set arbitrary folder for the workspace

Running Eclipse. Close Welcome view n Close the welcome view

Running Eclipse. Close Welcome view n Close the welcome view

Running Eclipse n If you see this then you are ready

Running Eclipse n If you see this then you are ready

Creating a project in Eclipse

Creating a project in Eclipse

Creating a java project in Eclipse

Creating a java project in Eclipse

Adding JADE libs to the project

Adding JADE libs to the project

Adding JADE libs to the project

Adding JADE libs to the project

Development Environment is ready

Development Environment is ready

Testing Settings. Create Test. Agent

Testing Settings. Create Test. Agent

Create Test. Agent

Create Test. Agent

Test. Agent is created

Test. Agent is created

Method setup() to run Test. Agent n The setup() method is intended to include

Method setup() to run Test. Agent n The setup() method is intended to include agent initializations.

Override method setup()

Override method setup()

Result of overriding

Result of overriding

Print Hello message

Print Hello message

Running JADE and Test. Agent

Running JADE and Test. Agent

Create JADE configuration to run

Create JADE configuration to run

Create JADE configuration to run

Create JADE configuration to run

Create JADE configuration to run

Create JADE configuration to run

Create JADE configuration to run

Create JADE configuration to run

Create JADE configuration to run

Create JADE configuration to run

Result of Test

Result of Test

Book Trading Example n n It shows how to create simple JADE agents and

Book Trading Example n n It shows how to create simple JADE agents and how to make them executing tasks and communicate between each other. Some agents selling books and other agents buying books on behalf of their users.

Buyer Agent n n n Receives the title of the book to buy (the

Buyer Agent n n n Receives the title of the book to buy (the “target book”) as a command line argument Periodically requests all known seller agents to provide an offer As soon as an offer is received the buyer agent accepts it and issues a purchase order If more than one seller agent provides an offer the buyer agent accepts the best one (lowest price) Having bought the target book the buyer agent terminates

Seller Agent n n Has a minimal GUI by means of which the user

Seller Agent n n Has a minimal GUI by means of which the user can insert new titles Continuously wait for requests from buyer agents When asked to provide an offer for a book they check if the requested book is in their catalogue and in this case reply with the price. Otherwise they refuse. When they receive a purchase order they serve it and remove the requested book from their catalogue

setup() method The setup() method is intended to include agent initializations.

setup() method The setup() method is intended to include agent initializations.

Agent identifiers n n Each agent is identified by an “agent identifier” represented as

Agent identifiers n n Each agent is identified by an “agent identifier” represented as an instance of the jade. core. AID class. The get. AID() method of the Agent class allows retrieving the agent identifier. An AID object includes a globally unique name plus a number of addresses. <nickname>@<platform-name>

Assigning name String nickname = “Peter”; AID id = new AID(nickname, AID. ISLOCALNAME); n

Assigning name String nickname = “Peter”; AID id = new AID(nickname, AID. ISLOCALNAME); n The ISLOCALNAME constant indicates that the first parameter represents the nickname (local to the platform) and not the globally unique name of the agent.

Running JADE The first part of the above output is the JADE disclaimer The

Running JADE The first part of the above output is the JADE disclaimer The indication that a container called “Main-Container” is ready completes the JADE runtime startup. When the JADE runtime is up our agent is started and prints its welcome message The platform name “arman: 1099/JADE” is automatically assigned on the basis of the host and port we are running JADE on

Agent termination n Even if it does not have anything else to do after

Agent termination n Even if it does not have anything else to do after printing the welcome message, our agent is still running. In order to make it terminate its do. Delete() method must be called. The take. Down() method is invoked just before an agent terminates and is intended to include agent clean-up operations.

do. Delete() and take. Down()

do. Delete() and take. Down()

Passing arguments to an agent n n n Agents may get start-up arguments specified

Passing arguments to an agent n n n Agents may get start-up arguments specified on the command line. These arguments can be retrieved, as an array of Object, by means of the get. Arguments() method of the Agent class. We want our Book. Buyer. Agent to get the title of the book to buy as a command line argument.

Taking target book as argument

Taking target book as argument

Passing book title

Passing book title

Trying to buy …

Trying to buy …

Agent Tasks n n The actual job an agent has to do is typically

Agent Tasks n n The actual job an agent has to do is typically carried out within “behaviours” A behaviour is implemented as an object of a class that extends jade. core. behaviours. Behaviour.

Behaviour n n In order to make an agent execute the task implemented by

Behaviour n n In order to make an agent execute the task implemented by a behaviour object it is sufficient to add the behaviour to the agent by means of the add. Behaviour() method of the Agent class. Behaviours can be added at any time: q q when an agent starts (in the setup() method) or from within other behaviours.

action() and done() methods n Each class extending Behaviour must implement q q the

action() and done() methods n Each class extending Behaviour must implement q q the action() method actually defines the operations to be performed when the behaviour is in execution the done() method (returns a boolean value), that specifies whether or not a behaviour has completed and have to be removed from the pool of behaviours of an agent

Behaviours scheduling and execution n n An agent can execute several behaviours concurrently. It

Behaviours scheduling and execution n n An agent can execute several behaviours concurrently. It is important to notice that scheduling of behaviours in an agent is not pre-emptive (as for Java threads) but cooperative. This means that when a behaviour is scheduled for execution its action() method is called and runs until it returns. Therefore it is the programmer who defines when an agent switches from the execution of a behaviour to the execution of the next one.

Overbearing Behaviour n A behaviour prevents any other behaviour to be executed since its

Overbearing Behaviour n A behaviour prevents any other behaviour to be executed since its action() method never returns.

No Behaviours n n When there are no behaviours available for execution the agent’s

No Behaviours n n When there are no behaviours available for execution the agent’s thread goes to sleep in order not to consume CPU time. It is waken up as soon as there is again a behaviour available for execution.

One-shot behaviours n n Complete immediately and action() method is executed only once. The

One-shot behaviours n n Complete immediately and action() method is executed only once. The jade. core. behaviours. One. Shot. Behaviour already implements the done() method by returning true and can be conveniently extended to implement one-shot behaviours.

Cyclic behaviours n n Never complete and action() method executes each time it is

Cyclic behaviours n n Never complete and action() method executes each time it is called. The jade. core. behaviours. Cyclic. Behaviour already implements the done() method by returning false and can be conveniently extended to implement cyclic behaviours.

Generic behaviours n Generic behaviours that embeds a status and execute different operations depending

Generic behaviours n Generic behaviours that embeds a status and execute different operations depending on that status. They complete when a given condition is met.

Complex Behaviours n n n JADE provides the possibility of combining simple behaviours together

Complex Behaviours n n n JADE provides the possibility of combining simple behaviours together to create complex behaviours. This feature is outside the scope. Refer to the Javadoc of the Sequential. Behaviour, Parallel. Behaviour and FSMBehaviour for the details.

Scheduling operations n JADE provides two ready-made classes (in the jade. core. behaviours package)

Scheduling operations n JADE provides two ready-made classes (in the jade. core. behaviours package) by means of which it is possible to easily implement behaviours that execute certain operations at given points in time.

Waker Behaviour n n action() and done() methods are already implemented in such a

Waker Behaviour n n action() and done() methods are already implemented in such a way to execute the handle. Elapsed. Timeout() abstract method after a given timeout (specified in the constructor) expires. After the execution of the handle. Elapsed. Timeout() method the behaviour completes.

Waker Behaviour

Waker Behaviour

Ticker Behaviour n n action() and done() methods are already implemented in such a

Ticker Behaviour n n action() and done() methods are already implemented in such a way to execute the on. Tick() abstract method repetitively waiting a given period (specified in the constructor) after each execution. A Ticker. Behaviour never completes.

Book-buyer agent behaviours n n Book-buyer agent periodically requests seller agents the book it

Book-buyer agent behaviours n n Book-buyer agent periodically requests seller agents the book it was instructed to buy. We can easily achieve that by using a Ticker. Behaviour that, on each tick, adds another behaviour that actually deals with the request to seller agents.

Ticker. Behaviour

Ticker. Behaviour

Book-seller agent behaviours n Book-seller agent waits for requests from buyer agents and serves

Book-seller agent behaviours n Book-seller agent waits for requests from buyer agents and serves them q q n requests to provide an offer for a book purchase orders two cyclic behaviours: q q q one dedicated to serve requests for offer the other dedicated to serve purchase orders a one-shot behaviour updating the catalogue of books

Result

Result

Agent Communication n n The communication paradigm adopted is the asynchronous message passing. Each

Agent Communication n n The communication paradigm adopted is the asynchronous message passing. Each agent has a sort of mailbox (the agent message queue) where the JADE runtime posts messages. Whenever a message is posted in the message queue the receiving agent is notified. If and when the agent actually picks up the message from the message queue is completely up to the programmer.

Messaging

Messaging

ACL language n Messages exchanged by JADE agents have a format specified by the

ACL language n Messages exchanged by JADE agents have a format specified by the ACL language defined by the FIPA (http: //www. fipa. org) international standard for agent interoperability.

Message Fields n n n n The sender of the message The list of

Message Fields n n n n The sender of the message The list of receivers The performative The content language i. e. the syntax used to express the content The ontology i. e. the vocabulary of the symbols used in the content and their meaning Some fields used to control several concurrent conversations and to specify timeouts for receiving a reply such as conversation-id, reply-with, in-replyto, reply-by.

Performative n n The communicative intention (also called “performative”) indicating what the sender intends

Performative n n The communicative intention (also called “performative”) indicating what the sender intends to achieve by sending the message. The performative can be q REQUEST, if the sender wants the receiver to perform an action, q INFORM, if the sender wants the receiver to be aware a fact, q QUERY_IF, if the sender wants to know whether or not a given condition holds, q CFP (call for proposal), q PROPOSE, q ACCEPT_PROPOSAL, q REJECT_PROPOSAL, q and more.

Message in JADE n n A message in JADE is implemented as an object

Message in JADE n n A message in JADE is implemented as an object of the jade. lang. acl. ACLMessage class It provides get and set methods for handling all fields of a message After filling the fields of an ACLMessage object Call the send() method of the Agent class

Message Example

Message Example

Messages of scenario n n CFP to request an offer for a book PROPOSE

Messages of scenario n n CFP to request an offer for a book PROPOSE for messages carrying seller offers ACCEPT_PROPOSAL for messages carrying offer acceptance REFUSE for messages sent by seller agents when the requested book is not in their catalogue

Messages of scenario n n In both types of messages sent by buyer agents

Messages of scenario n n In both types of messages sent by buyer agents we assume that the message content is the title of the book. The content of PROPOSE messages will be the price of the book.

CFP message

CFP message

Receiving messages n n An agent can pick up messages by means of the

Receiving messages n n An agent can pick up messages by means of the receive() method. This method returns q q the first message in the message queue (removing it) null if the message queue is empty

Seller behaviours n n n Behaviours that process messages received by other agents the

Seller behaviours n n n Behaviours that process messages received by other agents the case for the Offer. Requests. Server and Purchase. Orders. Server behaviours behaviour must be continuously running must check if a message has been received and process it The two behaviours are very similar

Offer. Requests. Server behaviour n n The behaviour used by Book-seller agents to serve

Offer. Requests. Server behaviour n n The behaviour used by Book-seller agents to serve incoming requests for offer from buyer agents If the requested book is in the local catalogue q n the seller agent replies with a PROPOSE message specifying the price otherwise q a REFUSE message is sent back

create. Reply() method n The create. Reply() method of the ACLMessage class automatically creates

create. Reply() method n The create. Reply() method of the ACLMessage class automatically creates a new ACLMessage properly setting the receivers and all the fields used to control the conversation q q q conversation-id reply-with in-reply-to

Blocking of behaviour n n n When we add the above behaviour, the agent’s

Blocking of behaviour n n n When we add the above behaviour, the agent’s thread starts a continuous loop To avoid that we would like to execute the action() method only when a new message is received We can use the block() method of the Behaviour class

Right approach n The code is the typical (and strongly suggested) pattern for receiving

Right approach n The code is the typical (and strongly suggested) pattern for receiving messages inside a behaviour

Selecting messages with given characteristics from the message queue n n How can we

Selecting messages with given characteristics from the message queue n n How can we be sure that the Offer. Requests. Server behaviour picks up from the agent’s message queue only messages carrying requests for offer and the Purchase. Orders. Server behaviour only messages carrying purchase orders? Specifying proper “templates” when we call the receive() method we solve the problem

Message Template n Such templates are implemented as instances of the jade. lang. acl.

Message Template n Such templates are implemented as instances of the jade. lang. acl. Message. Template class that provides a number of factory methods to create templates in a very simple and flexible way

Adding templates n We modify the action() method of the Offer. Requests. Server so

Adding templates n We modify the action() method of the Offer. Requests. Server so that the call to my. Agent. receive() ignores all messages except those whose performative is CFP

Complex conversations n n The Request. Performer behaviour represents an example of a behaviour

Complex conversations n n The Request. Performer behaviour represents an example of a behaviour carrying out a “complex” conversation A conversation is a sequence of messages exchanged by two or more agents with well defined causal and temporal relations

Request. Performer behaviour n Buyer has to q q send a CFP message to

Request. Performer behaviour n Buyer has to q q send a CFP message to several agents get back all the replies in case at least one PROPOSE reply is received, send a further ACCEPT_PROPOSAL message get back the response

Yellow Pages Service n n n We have assumed that there is a fixed

Yellow Pages Service n n n We have assumed that there is a fixed set of seller agents (seller 1 and seller 2) Each buyer agent already knows them How to get rid of this assumption? Exploit the yellow pages service provided by the JADE platform Make buyer agents dynamically discover seller agents available at a given point in time

The DF agent n n A “yellow pages” service allows agents to publish one

The DF agent n n A “yellow pages” service allows agents to publish one or more services they provide so that other agents can find and successively exploit them The yellow pages service in JADE is an agent called DF (Directory Facilitator) Each FIPA compliant platform hosts a default DF agent Other DF agents can be activated and several DF agents can be federated so that to provide a single distributed yellow pages catalogue

Example

Example

Interacting with the DF n n it is possible to interact with DF as

Interacting with the DF n n it is possible to interact with DF as usual by exchanging ACL messages using a proper content language (the SL 0 language) and a proper ontology (the FIPA-agentmanagement ontology) In order to simplify these interactions, however JADE provides the jade. domain. DFService class by means of which it is possible to publish and search for services through method calls

Publishing services n n n An agent must provide the DF with a description

Publishing services n n n An agent must provide the DF with a description including its AID, possibly the list of languages and ontologies that other agents need to know to interact with it and the list of published services For each published service a description is provided including the service type, the service name, the languages and ontologies required to exploit that service and a number of servicespecific properties The DFAgent. Description, Service. Description and Property classes, included in the jade. domain. FIPAAgent. Management package, represent the three mentioned abstractions

To publish a book selling service n An agent must create a proper description

To publish a book selling service n An agent must create a proper description and call the DFService. register()

De-registration n When an agent terminates it is a good practice to de-register published

De-registration n When an agent terminates it is a good practice to de-register published services

Searching for services n n An agent wishing to search for services must provide

Searching for services n n An agent wishing to search for services must provide the DF with a template description The result of the search is the list of all the descriptions that match the provided template A description matches the template if all the fields specified in the template are present in the description with the same values The search() static method of the DFService class can be used

Update the list of seller agents

Update the list of seller agents

DF subscription n n Note that the update of the list of known seller

DF subscription n n Note that the update of the list of known seller agents is done before each attempt to buy the target book since seller agents may dynamically appear and disappear in the system The DFService class also provides support for subscribing to the DF to be notified as soon as an agent publishes a given service (see the search. Until. Found() and create. Subscription. Message() methods