《智能Agent设计与开发》开放实验讲义_第1页
《智能Agent设计与开发》开放实验讲义_第2页
《智能Agent设计与开发》开放实验讲义_第3页
《智能Agent设计与开发》开放实验讲义_第4页
《智能Agent设计与开发》开放实验讲义_第5页
已阅读5页,还剩41页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

智能Agent设计与开发开放实验讲义 智能Agent设计与开发开放实验讲义实验1 JADE智能开发平台安装、配置及使用实验目的:掌握JADE开发平台的安装方法及简单使用。实验要求:提交JADE 平台安装和使用的实验报告。实验内容:JADE开发平台的安装、配置、基本使用过程。1、配置环境:(1)在官方网站/下载最新版本的jade3.6压缩包。压缩包中包含四个部分:JADEbin3.6(存放jade的核心部件,它是jade框架平台的支持文件);JADE-doc3.6(存放jade的说明文档,其中包括各种程序说明与教程资料);JADE-examples3.6(存放jade的实例源代码);JADE-src3.6(存放jade框架平台的源代码,jade为开源项目)。(2)classpath设置:在JADE-BIN-3.6jadelib目录下包含:http.jar、iiop.jar、jade.jar、jadeTools.jar和commons-codec-1.3.jar五个文件。可以使用两种方法设置这些文件的系统路径:在命令行下cmd,输入:Set CLASSPATH=C:JADE-bin-3.6jadelibjade.jar; C:JADE-bin-3.6jadelibiiop.jar; C:JADE-bin-3.6jadelibhttp.jar; C:JADE-bin-3.6jadelibjadeTools.jar; C:JADE-bin-3.6jadelibcomons-codeccommons-codec-1.3.jar注意:这种方式每次使用时都需要重新输入。设置系统环境变量:单击我的电脑图标,在快捷菜单中选择属性,在属性对话框中选择高级标签,单击环境变量按钮,设置环境变量。在classpath变量中设置:.;%JAVA_HOME%lib;%JAVA_HOME%libtools.jar; C:JADE-bin-3.6jadelibjade.jar; C:JADE-bin-3.6jadelibiiop.jar; C:JADE-bin-3.6jadelibhttp.jar; C:JADE-bin-3.6jadelibjadeTools.jar; C:JADE-bin-3.6jadelibcomons-codeccommons-codec-1.3.jar 注意:.;%JAVA_HOME%lib;%JAVA_HOME%libtools.jar 前面部分是java jdk的变量。 (3)环境正确性测试:在CMD命令行里输入:java jade.Boot -gui (注意大小写),如果配置正确会跳出一个jade GUI窗口。2、运行第一个示例:HelloWorldAgent在JADE-examples-3.6jadesrcexampleshello里面找到HelloWorldAgent.java示例程序。(1)运行HelloWorldAgent:首先,用记事本打开HelloWorldAgent.java ,将package examples.hello;这句注释掉(先去掉路径问题,后面会详细介绍)并保存修改。然后,打开控制台,首先输入:Cd E:agentJADE-bin-3.6JADE-examples-3.6jadesrcexampleshello (让这个目录作为默认路径),然后,编译agent文件,输入:javac HelloWorldAgent.java编译好了以后,输入:java jade.Boot -gui myAgent:HelloWorldAgent注意:myAgent是这个agent的名字,可以自由指定。HelloWorldAgent代表HelloWorldAgent类名。输入上面的命令后,会跳出一个窗口,然后控制台会输出:Hello World! My name is myAgent。而那个窗口的左边的树结构下的叶节点上没有显示我们的myAgent,因为这个agent的代码中输入Hello World! My name is myAgent后,Agent自行销毁了自己。如果想要在jade GUI中看到自己所建的agent,可以改HelloWorldAgent.java文件,将doDelete();注释掉,前面加/,保存重编译。再次使用上面的命令运行就可以在jade GUI中看到 myAgent name1099/JADE的agent3、利用Netbeans 和eclipse IDE平台调试编辑Agent程序。(1)运行netbeans平台,添加库,添加库德步骤如下运行菜单 工具->库管理器,点新建库,然后输入库名,不妨设为jade,然后点 添加jar文件,将classpath中的几个jar文件都添加进来。在netbeans中新建一个常规项目,程序内容如下:import jade.core.*;public class HelloWorld extends Agent public void setup() System.out.println(Agent Started: Hello World!); System.out.println(-About Me:-); System.out.println(My local name is:+getLocalName(); System.out.println(My globally unique name is:+getName() ); System.out.println(-About Here:-); Location l = here(); System.out.println(I am running in a location called:+l.getName(); System.out.println(Which is identified uniquely as:+l.getID(); System.out.println(And is contactable at:+l.getAddress(); System.out.println(Using the protocol:+l.getProtocol(); 注意:这时有错误提示,说明jade.core库并未找到,需要在工程窗口中,单击库 的右键,将我们前面加入的jade库添加近来。然后设置运行命令,打开工程属性窗口,单击运行,在主类编辑框中输入jade.Boot,在运行参数编辑框中输入 -gui hello:jadetest.HelloWorld。确定返回。这时直接点netbeans工具条上的运行按钮,程序运行,会弹出jade rma运行窗口,并在netbeans下面的输出框中打印出结果信息。(2)在eclipse中进行配置与netbeans稍有不同。不过步骤一样,首先要在eclipse中添加库。在窗口-受选项中,选择java,展开其子项,在构建路径->用户库中,新建一个库,名称不妨定为jade,然后,添加jar,将我们前面所列出的jar文件都导入。第二步就是在新建的工程中添加库和设置运行参数。比如新建了一个项目,在导航器中右键单击其属性,在属性对话框中选择java构建路径,在库选项卡中单击按钮,添加库,选择用户库,然后下一步把jade添加进来。设置运行参数的时候,可以按如下步骤进行:选择编写好的agent类,右键单击,运行方式->运行,弹出运行对话框,在java application中选择新建,不妨起名为run in jade,设置main类为jade.Boot,在自变量中输入参数:在上例中则为, -gui hello:jadetest.HelloWorldAgent。然后运行之,则会在工作区下面的控制台上输出结果,表示配置成功。注意:其实在项目中不一定非要完全运行,而且我们一般要创建的是多个Agent。一般我们也可以把它编译为类之后,使用命令行启动图形界面的管理器,使用start new agent添加Agent即可。实验2 Jade行为类实验目的:掌握Jade行为类的使用方法。实验要求:在简单智能Agent中添加行为类。实验内容:编写一个简单的智能Agent,给智能Agent添加基本行为类。1、Jade行为类简介agent所作的工作都定义在了setup中,实际上它具有的行为和执行的动作都应该定义在behavious类中,我们可以对生成behavios类的实例,然后将任务或者动作代码放在对behavious类中的action方法中,action方法是必须要有的。Behavious类还有很多子类,分别对应着不同类型的behavious,包括SimpleBehaviour,sequences behaviors ,parallel behaviors,cyclic behaviors等。一个agent的行为表示它能够执行的任务,通过继承jade.core.behaviours.Behaviour来实现。然后在agent类中通过addBehaviour()方法将行为加入进来。当一个agent启动(通过setup()方法)后,行为可以在任何时间加入进来。要定义Behaviour必须实现其action()方法,它定义了agent的执行时的实际动作,而done()方法指名了一个行为是否已执行完毕,是否要从行为池中删除。 一个agent可以并发执行多个behaviour。每个agent线程启动后执行的过程如下:(1)SimpleBehaviour简单行为下面的例子中我们不在setup()中打印信息,而是把它放在一个简单行为中:import jade.core.*;import jade.core.behaviours.SimpleBehaviour;public class HelloWorldBehaviours extends Agentpublic void setup()SimpleBehaviour hello_behaviour = new SimpleBehaviour(this)boolean finished = false;/ 覆盖 Behaviour 类的action这一抽象方法public void action()System.out.println(Hello World Behaviour run: Hello World!);System.out.println(-About Me:-);System.out.println(My local name is:+getLocalName();System.out.println(My globally unique name is:+getName() );System.out.println(-About Here:-);Location l = here();System.out.println(I am running in a location called:+l.getName();System.out.println(Which is identified uniquely as:+l.getID();System.out.println(And is contactable at:+l.getAddress();System.out.println(Using the protocol:+l.getProtocol();finished = true;/ done()在父类中也是一个抽象方法public boolean done()return finished;addBehaviour(hello_behaviour);/ java jade.Boot helloBehaviours:HelloWorldBehaviours(2)CyclicBehaviour循环行为一个Agent中可以加入各种Behaviour构成composite behaviour。在netbeans工程中编写下列程序,过程如前所描述。package examplesbehaviours;import jade.core.Agent;import jade.core.behaviours.Behaviour;import jade.core.behaviours.CyclicBehaviour;import jade.core.behaviours.OneShotBehaviour;public class SimpleAgent extends Agent private class FourStepBehaviour extends Behaviour private int step = 1; public void action() switch (step) case 1: System.out.println(Operation 1); break; case 2: System.out.println(Operation 2. Adding one-shot behaviour); myAgent.addBehaviour(new OneShotBehaviour(myAgent) public void action() System.out.println(One-shot); ); break; case 3: System.out.println(Operation 3); break; case 4: System.out.println(Operation 4); break; step+; public boolean done() return step = 5; public int onEnd() myAgent.doDelete(); System.out.println(Finished!); return super.onEnd(); /* Creates a new instance of SimpleAgent */ protected void setup() System.out.println(Agent +getLocalName()+ started.); addBehaviour(new CyclicBehaviour(this) public void action() System.out.println(Cycling); ); / Add the generic behaviour addBehaviour(new FourStepBehaviour(); 运行参数为 jade.Boot gui guojie: examplesbehaviours.SimpleAgent输出结果:Agent guojie started.CyclingOperation 1CyclingOperation 2. Adding one-shot behaviourCyclingOperation 3One-shotCyclingOperation 4Finished!注意:每个Agent内部都有一个parallel behaviour,我们如果加入多个behaviour到Agent中,他们会并行执行。Behaviours加入到队列的顺序就是他们执行的次序。最后,behaviours可以动态的加入到agent以及composite behaviours。实验3 agent通讯-ACL(agent communication language)实验目的:掌握agent通讯原理及ACL消息规范。实验要求:在简单智能Agent中利用ACL进行消息通讯。实验内容:编写两个简单的智能Agent,并进行消息通讯。Jade的agent之间进行通信使用的acl语言遵循fipa acl规范。一个acl消息通常包含这些参数:sende:消息的发送者,用agent标志AID表示; receivers,接受agent消息的agent可以是多个;Reply-to,应受到回应的接受者;Performative:标志发送消息的目的,即发送者想要通过发送消息干什么,通常有这样一些常值:REQUEST, INFORM, ACCEPT_PROPOSAL, REJECT_PROPOSAL, PROPOSE;Content,消息的内容;内容语言,比如内容的编码格式;ontology,双方都能够理解的消息内容的概念说明和语义描述。1、简单例子发送者:import java.io.InterruptedIOException;import java.io.IOException;import jade.core.*;import jade.core.behaviours.*;import jade.lang.acl.*;public class SimpleSender extends Agent protected void setup() addBehaviour(new SimpleBehaviour(this) private boolean finished = false;public void action() System.out.println(getLocalName() +: about to inform bob hello);/we sleep here to give bob a chance to start.doWait(5000);AID r = new AID();r.setLocalName(bob);ACLMessage msg = new ACLMessage(ACLMessage.INFORM); / set performativemsg.setSender(getAID();msg.addReceiver(r);msg.setContent(Hello_BOB);send(msg);System.out.println(getLocalName() +: Send hello to bob);finished = true;doWait(5000);doDelete();public boolean done()return finished;);这段代码的主要执行过程为:构建一个AID,以此来指出该消息的目的Agent。这里我们指定目的为一个本地的Agent,名字为bob。建立一个ACL消息标志其performative为INFORM。设定Sender为自身,指定接收者为bob。然后发送消息内容。打印相关信息。接收者:他的名字必须为bobimport jade.core.*;import jade.core.behaviours.*;import jade.lang.acl.ACLMessage;public class SimpleReceiver extends Agent class DoSimpleReceiveBehaviour extends SimpleBehaviour private boolean finished = false;public DoSimpleReceiveBehaviour(Agent agent)super(agent);public void action() ACLMessage msg = receive();if (msg!= null)System.out.println(getLocalName() + : received the following message : );System.out.println(msg.toString();finished = true;myAgent.doDelete();elseSystem.out.println(getLocalName() + :No message received, Blocking the behaviour till one is);block();public boolean done() return finished;protected void setup() DoSimpleReceiveBehaviour behaviour = new DoSimpleReceiveBehaviour(this);addBehaviour(behaviour);接收者的代码流程为:添加一个简单行为,这一行为检查现在是否有受到消息,若没有,则执行block()方法组织目前的behaviour执行,直到有新的消息到达。2、复杂例子FIPA定义了一组交互协议,包括FIPA-request, FIPA-query, FIPA-request-when, FIPA-contract-net, FIPA-Iterater-net, FIPA-Auction-English, FIPA-Auction-Dutch.其中: REQUEST-INFORM:A请求B做一些工作,B可以同意或拒绝。如果B同意,则会去完成并告诉A该工作已经完成。等等。Query:A想知道一些事情,B可以同意或不同意,并将B的回应告诉A。Propose:在给定一些precondition的条件下,提出一个proposal去执行某些动作。在netbeans中创建常规项目:其代码文件有两个,分别为package ips;import java.io.InterruptedIOException;import java.io.IOException;import jade.core.*;import jade.core.behaviours.*;import jade.lang.acl.*;import jade.domain.FIPANames.InteractionProtocol;import to.SimpleAchieveREInitiator;import java.util.Vector;import java.util.Enumeration;public class SimpleRequestInitiator extends Agent static class MarriageProposer extends SimpleAchieveREInitiator protected MarriageProposer(Agent agent, ACLMessage msg) super(agent, msg); protected void handleAgree(ACLMessage msg) System.out.println(myAgent.getLocalName() + : 吼吼! + msg.getSender().getLocalName() + 已经同意嫁给我了, Im so excited!); protected void handleRefuse(ACLMessage msg) System.out.println(myAgent.getLocalName() + : Oh no! + msg.getSender().getLocalName() + 拒绝了我, i feel sad.); protected void handleInform(ACLMessage msg) System.out.println(myAgent.getLocalName() + : + msg.getSender().getLocalName() + has informed me of the status of my request. + They said : + msg.getContent(); protected void handleNotUnderstood(ACLMessage msg) System.out.println(myAgent.getLocalName() + : + msg.getSender().getLocalName() + has indicated that they didnt understand.); protected void handleOutOfSequence(ACLMessage msg) System.out.println(myAgent.getLocalName() + : + msg.getSender().getLocalName() + has sent me a message which I wasnt + expecting in this conversation); protected void setup() System.out.println(getLocalName() +: about to propose marriage to bob ); doWait(5000); /wait for bob to be started. 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); 还有:package ips;import java.io.InterruptedIOException;import java.io.IOException;import jade.core.*;import jade.core.behaviours.*;import jade.lang.acl.*;import jade.domain.FIPANames.InteractionProtocol;import to.SimpleAchieveREResponder;import java.util.Vector;import java.util.Enumeration;public class SimpleRequestResponder extends Agent static class MarriageResponder extends SimpleAchieveREResponder public MarriageResponder(Agent agent) super(agent,createMessageTemplate(InteractionProtocol.FIPA_REQUEST); protected ACLMessage prepareResponse(ACLMessage msg) ACLMessage response = msg.createReply(); if(msg.getContent()!=null && msg.getContent().equals(Marry Me!) System.out.println(myAgent.getLocalName() + : + msg.getSender().getLocalName() + has asked me to marry him!); AID sender; sender = msg.getSender(); if(sender.getLocalName().equals(baz) response.setPerformative(ACLMessage.AGREE); System.out.println(myAgent.getLocalName() + :Im going to agree.); else response.setPerformative(ACLMessage.REFUSE); System.out.println(myAgent.getLocalName() + :Im going to turn him down.); else response.setPerformative(ACLMessage.NOT_UNDERSTOOD); System.out.println(myAgent.getLocalName() + :I didnt understand what + msg.getSender().getLocalName() + just said to me.); return response; protected ACLMessage prepareResultNotification(ACLMessage inmsg, ACLMessage outmsg) /what they have asked is now complete (or if it failed) ACLMessage msg = inmsg.createReply(); msg.setPerformative(ACLMessage.INFORM); msg.setContent(I Do!); return msg; protected void setup() System.out.println(getLocalName() + : I wonder if anybody wants to marry me?); addBehaviour(new MarriageResponder(this); / 按照以前记载,在netbeans属性中加入库,然后再运行参数中,设置,主类依然为jade.Boot,参数为baz:ips.SimpleRequestInitiator bob:ips.SimpleRequestResponder,运行成功后,以下是输出结果:baz: about to propose marriage to bob bob: I wonder if anybody wants to marry me?bob:baz has asked me to marry him!bob:Im going to agree.baz: 吼吼! bob 已经同意嫁给我了, Im so excited!baz:bob has informed me of the status of my request. They said : I Do!上例中,应用了SimpleAchieveREInitiator和SimpleAchieveREResponder两个基类,适用于两个Agent之间的交互。可以看出发起者对于不同的回应有不同的执行动作。技巧:从AMS中获取所有Agent的AID。引用文件: import jade.domain.AMSService; import jade.domain.FIPAAgentManagement.*;调用示例:AMSAgentDescription agents = null; try SearchConstraints c = new SearchConstraints(); c.setMaxResults (new Long(-1);agents = AMSService.search( this, new AMSAgentDescription (), c );catch (Exception e) System.out.println( Problem searching AMS: + e ); e.printStackTrace();ACLMessage msg = new ACLMessage(ACLMessage.INFORM);msg.setContent( Ping );for (int i=0; i<agents.length;i+)if ( agentsi.getName().equals(getAID() )/如果不是自己则加入到接收者数组中continue;msg.addReceiver( agentsi.getName() );实验4 ACL高级特性实验目的:掌握agent通讯中ACL高级特性。实验要求:在智能Agent中利用ACL消息模版和对象序列化特性进行通讯。实验内容:编写多个智能Agent,并利用ACL高级特性进行通讯。1、利用ACL消息模版进行通讯MessageTemplate class 利用MessageTemplate可以针对ACLMessage的每个属性设置模式,以达到过滤消息的目的。为了可以构建更复杂的匹配规则,多个模式也可以进行and,or,not运算。最有用的一些规则或方法包括:通信行为匹配,发送者匹配,会话ID匹配。比如MatchPerformative( performative ) 是通信行为的匹配。这里 performative 可能是: ACLMessage.INFORM ACLMessage.PROPOSE ACLMessage.AGREE 还有发送者匹配MatchSender( AID ),会话匹配MatchConversationID( String ),通信协议匹配MatchProtocol( String ) ,本体匹配MatchOntology( String)。比如:MessageTemplate mt = MessageTemplate.and( MessageTemplate.MatchPerformative( ACLMessage.INFORM ), MessageTemplate.MatchSender( new AID( a1, AID.ISLOCALNAME) ;相当于建立了一个模板,表示消息规则为INFORM行为并且发送者为a1。接收过程如下:ACLMessage msg = receive( mt ); if (msg != null) . handle message block();示例:package jadePrime.acl;import jade.core.Agent;import jade.core.AID;import jade.core.behaviours.*;

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论