770 likes | 1.09k Views
Benodigdheden. JADE – environment 3.0 JADE API (jadedocapi) JADE docs (jadedoc): programming tutorial for beginners (JPT), administrators guide (JAG), … JADE primer: http://www.iro.umontreal.ca/~vaucher/Agents/Jade/JadePrimer.html (PRI)
E N D
Benodigdheden • JADE – environment 3.0 • JADE API (jade\doc\api) • JADE docs (jade\doc): programming tutorial for beginners (JPT), administrators guide (JAG), … • JADE primer: http://www.iro.umontreal.ca/~vaucher/Agents/Jade/JadePrimer.html (PRI) • Basic aspects: http://www.cs.bath.ac.uk/~occ/agents_ecommerce/jade/ (BAS)
Verwijzingen • API: jade\doc\api • JPT: JADE Programming Tutorial for Beginners • JAG: JADE Administrator’s Guide • JPG: JADE Programmer’s Guide • CL&O: JADE Application-defined Content Languages and Ontologies Tutorial • PRI: JADE primer • BAS: Basic aspects (Bath)
Deel 1 JADE basisprincipes, graphical tools, Agents en Behaviours
JADE overview (JPT1) • Java Agent DEvelopment framework • JADE is a middleware that facilitates the development of multi-agent systems. It includes • 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. • A library of classes that programmers have to/can use (directly or by specializing them) to develop their agents. • A suite of graphical tools that allows administrating and monitoring the activity of running agents.
Containers, platforms, AMS en DF (JPT1) Remote container Remote platform
Running JADE (JAG2.3/JPT3.2) • Classpath • set_CLASSPATH=.;c:\jade\lib\jade.jar;c:\jade\lib\jadeTools.jar;c:\jade\lib\Base64.jar;c:\jade\lib\iiop.jar; • Agent compileren (PingAgent.java) • Bekijk_C:\jade\PingAgent.java en Logger.java • javac *.java
PingAgent.java • /** • This agent implements a simple Ping Agent for the AgentCities project. • First of all the agent registers itself with the DF of the platform and • then waits for ACLMessages. • If a QUERY_REF message arrives that contains the string "ping" within the content • then it replies with an INFORM message whose content will be the string "alive". • If it receives a NOT_UNDERSTOOD message no reply is sent. • For any other message received it replies with a NOT_UNDERSTOOD message. • The exchanged message are written in a log file whose name is the local name of the agent. • @author Tiziana Trucco - CSELT S.p.A. • @version $Date: 2002/08/02 08:10:01 $ $Revision: 1.5 $ • */
Running JADE (JAG2.3/JPT3.2) • Agentenplatform starten met RMA • java jade.Boot -gui • Agentenplatform + RMA starten met PingAgent (nickname fonske) in main container • java jade.Boot –gui fonske:PingAgent • Een nieuwe container inpluggen • java jade.Boot –container –host dezePC marcel:PingAgent
Agent class (JPT3) • We programmeren agents door overerving van klasse jade.core.Agent • Override void setup(), void takeDown() • Agent methods: AID getAID(), Location here(), Object[] getArguments(), void doDelete(), … • Voorbeeld JPT3: BookBuyerAgent (nog geen eigenlijke activiteit)
Agent life cycle (JPG3.2.1) doActivate() doWait() doSuspend() doWake() doDelete() doMove(*,*) doStart()
Behaviour class (JPT4.1) • Eigenlijke activiteit • We programmeren behaviours door overerving van subklasse van klasse jade.core.behaviours.Behaviour • Behaviour starten in Agent class m.b.v. addBehaviour() method (vaak in setup()) • “Behaviours are scheduled in an execution pool” • Override void action() en boolean done()
Agent 1 B1.action() B2.action() B1.action() B2.action() B1.action() Agent 2 B8.action() B9.action() B9.action() B9.action() B9.action() Agent 3 B1.action() B6.action() B7.action() B1.action() B6.action() B7.action() tijd
Behaviour class • Implementeer als inner class van uw agent (soms praktischer) of als aparte klasse (mooier). • action() atomische eenheid die steeds wordt uitgevoerd • Wanneer done() true wordt, wordt behaviour uit execution pool verwijderd • Override eventueel int onEnd() en void onStart() • Datamember Agent myAgent
Behaviour class • Method block(): roep enkel op op het einde van action()! • De behaviour wordt pas gerescheduled als • A time of millis milliseconds has passed since the call to block(). • An ACL message is received by the agent this behaviour belongs to. • Method restart() is called explicitly on this behaviour object. • Hou er rekening mee dat hij desondanks eens kan unblocken! • “block() blokt niet maar stelt gewoon een delay in die ingaat NA de action() method”
class BlockTwice extends SimpleBehaviour { static long t0 = System.currentTimeMillis(); public void action() { System.out.println( "Start: " + (System.currentTimeMillis()-t0) ); block(250); System.out.println( " after block(250): " + (System.currentTimeMillis()-t0) ); block(1000); System.out.println( " after block(1000): " + (System.currentTimeMillis()-t0) ); System.out.println(); } private int n = 0; public boolean done() { return ++n > 3; } } Start: 1 after block(250): 7 after block(1000): 9 Start: 258 after block(250): 261 after block(1000): 263 Start: 512 after block(250): 515 after block(1000): 517 Start: 767 after block(250): 769 after block(1000): 771 Voorbeeld PRI3
class TwoSteps extends SimpleBehaviour { int state = 1; public void action() { switch( state ) { case 1: block( 200 ); break; case 2: System.out.println( "--- Message 1 --- " ); block( 800 ); break; case 3: System.out.println( " -- message 2 --" ); finished = true; doDelete(); // applies to the Agent } state++; } private boolean finished = false; public boolean done() { return finished; } } FSM patroon Er wordt algemeen aangeraden om de CPU tijd tijdens action() kort te houden omwille van de preemptive multitasking; de andere behaviours van dezelfde agent krijgen zo ook een kans!!!! andere SimpleBehaviour Looper unblockt om de 300 ms!!!!!!!! 0: alice --- Message 1 --- 340: alice 640: alice 950: alice -- message 2 -- Voorbeeld Agent3.java (PRI3)
Oefening • Schrijf een agent met • Een datamember int aantalBranden die het aantal branden in een dorp aangeeft • Een behaviour Brandstichter die op random (Math.random()) tijdstippen brand sticht (aantalBranden++). • Een SimpleBehaviour Brandweer die zich in verschillende states kan bevinden • State 1: om de 500ms wordt er gecheckt of er brand is • State 2: de brandweer wordt bij elkaar geroepen (block 1 sec) • State 3: er wordt naar de brand gereden (block 1 sec) • State 4: de brand wordt geblust (block 1 sec) • State 5: er wordt terug naar de kazerne gereden (block 1 sec) - (aantalBranden--).
Deel 2 Agent communication, Message Templates, Directory Facilitator, Interaction Protocols en Message Content
Agent Communication (Helin) commun. Act + ontologies syntax (ACLcodec) + semantiek
ACLMessage fields (JPT5.1) • The sender of the message • The list of receivers • The communicative intention (also called “performative”) indicating what the sender intends to achieve by sending the message. REQUEST, INFORM, CFP, … • The content i.e. the actual information included in the message (i.e. the action to be performed in a REQUEST message, the fact that the sender wants to disclose in an INFORM message …). • The content language i.e. the syntax used to express the content (both the sender and the receiver must be able to encode/parse expressions compliant to this syntax for the communication to be effective). • The ontology i.e. the vocabulary of the symbols used in the content and their meaning (both the sender and the receiver must ascribe the same meaning to symbols for the communication to be effective). • Some fields used to control several concurrent conversations and to specify timeouts for receiving a reply such as conversation-id, reply-with, in-reply-to, reply-by.
Zenden en ontvangen (JPT5) STERK AANBEVOLEN – PROBEER HET NIET ANDERS!
Waarschuwing (JPT5.8) • Er bestaat ook zoiets als BlockingReceive() • Gebruik het beter niet: het blokkeert de complete agent-thread tot wanneer er een message arriveert ….
Message Templates (PRI4) • Behaviour met “selective reception” • Vermijd overlappende actieve templates! • MatchPerformative( performative ) • MatchSender( AID ) • MatchConversationID( String ): this is really useful • and( Template1, Template2 ) • or ( Template1, Template2 ) • not( Template ) • and to a lesser extent: • MatchOntology( String ) • MatchProtocol( String ) • MatchOntology( String )
Message Templates (PRI4) MessageTemplate mt = MessageTemplate.and( MessageTemplate.MatchPerformative( ACLMessage.INFORM ), MessageTemplate.MatchSender( new AID( "a1", AID.ISLOCALNAME))) ; ACLMessage msg = receive( mt ); if (msg != null) { // handle message } block(); • Er zullen meer nutteloze unblocks voorkomen
Hoe bekom ik AID’s van collega-agenten? (PRI4) • Nickname opnemen in de code: new AID( "store", AID.ISLOCALNAME) • Via gebruikersinterfaces, bijvoorbeeld JADE GUI • Via de DF (conventioneel) • Via de AMS (inconventioneel)
DFAgentDescription (PRI5) DFAgentDescription Name: AID // Required for registration Protocols: set of Strings Ontologies: set of Strings Languages: set of Strings Services: set of { { Name: String // Required for each service specified Type: String // Required ... Owner: String Protocols: set of Strings Ontologies: set of Strings Languages: set of Strings Properties: set of { Name: String Value: String } }
DF Registratie (PRI5) DFAgentDescription dfd = new DFAgentDescription(); dfd.setName( getAID() ); ServiceDescription sd = new ServiceDescription(); sd.setType( "buyer" ); sd.setName( getLocalName() ); dfd.addServices(sd); try { DFService.register(this, dfd ); } catch (FIPAException fe) { fe.printStackTrace(); } • Registratie doorgaans in setup() • Probeer dit eens te sniffen! • Slechts 1 registratie per agent
DF Deregistratie (PRI5) protected void takeDown() { try { DFService.deregister(this); } catch (Exception e) {} } • Deregistratie doorgaans in takeDown() • Probeer dit eens te sniffen!
DF Search (PRI5) DFAgentDescription dfd = new DFAgentDescription(); ServiceDescription sd = new ServiceDescription(); sd.setType( "buyer" ); dfd.addServices(sd); DFAgentDescription[] result = DFService.search(this, dfd); System.out.println(result.length + " results" ); if (result.length>0) System.out.println(" " + result[0].getName() ); • Ook vaak in setup() • Met search, deregistratie en registratie is het mogelijk om je eigen description dynamisch bij te werken
DF Subscription Service (PRI5) DFAgentDescription dfd = new DFAgentDescription(); ServiceDescription sd = new ServiceDescription(); sd.setType(....); dfd.addServices(sd); SearchConstraints sc = new SearchConstraints(); sc.setMaxResults(new Long(1)); send(DFService.createSubscriptionMessage(this, getDefaultDF(), dfd, sc)); • Launch een ontvangende behaviour (template?) met DFAgentDescription dfds =DFService.decodeNotification(msg.getContent());
DF Subscription Service (PRI5) • All in one: DFAgentDescription template = // fill the template addBehaviour( new SubscriptionInitiator( this, DFService.createSubscriptionMessage( this, getDefaultDF(), template, null)) { protected void handleInform(ACLMessage inform) { try { DFAgentDescription[] dfds = DFService.decodeNotification(inform.getContent()); // do something with dfds } catch (FIPAException fe) {fe.printStackTrace(); } } });
Interaction Protocols • Communicative act = performative = “message type” • Foundation for Intelligent and Physical Agents (FIPA) definieert bibliotheek van formele modellen: http://www.fipa.org/specs/fipa00037/ • Performatives in JADE: zie API (fields van klasse ACLMessage) • FIPA beschrijft protocollen bijv. FIPA Request (/fipa00026/) en FIPA Query (/fipa00027/)
SL formalisme i: zender j: ontvanger Φ: proposition FP: feasible precondition RE: rational effect operatoren B: beliefs that … is true U: is uncertain about … but thinks that it is more likely to be true than to be false C: desires that … currently holds BifjΦ = BjΦ v Bj┐Φ UifjΦ = UjΦ v Uj┐Φ
SL formalisme operatoren I: intends that … Done(a): a has just taken place Agent (j, a): j is the only agent that (will) perform(s) action a …
Interaction Protocols (BAS5) • Er bestaat een hele reeks gelijkaardige protocols (FIPA-Request, FIPA-query, FIPA-propose, FIPA-Request-When, FIPA-recruiting, FIPA-brokering, FIPA-subscribe, …) • “Rational Effect” behaviours in JADE: SimpleAchieveREInitiator en SimpleAchieveREResponder voor 1-to-1 communicatie • “Werd het rationeel effect van het verzenden van een eenvoudige communicative act bereikt?” • Voorbeeld BAS5: MarriageProposer en MarriageResponder
MarriageProposer (BAS5) class MarriageProposer extends SimpleAchieveREInitiator{ protected MarriageProposer(Agent agent,ACLMessage msg){ super(agent,msg); } protected void handleAgree(ACLMessage msg) { System.out.println(myAgent.getLocalName() + ": OOH! " + msg.getSender().getLocalName() +" Has agreed to marry me, I'm so excited!"); } protected void handleRefuse(ACLMessage msg) { // ... ACLMessage msg = new ACLMessage(ACLMessage.REQUEST); AID to = new AID(); to.setLocalName("bob"); msg.setSender(getAID()); msg.addReceiver(to); msg.setContent("Marry Me!"); msg.setProtocol(InteractionProtocol.FIPA_REQUEST); addBehaviour(new MarriageProposer(this,msg));