📄 caragent.java
字号:
/*
* CarAgent.java
*
* Created on 2007年4月17日, 下午4:16
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
/**
*
* @author aying&mimi
*/
package mybag1;
import jade.core.Agent;
import jade.core.AID;
import java.util.*;
import jade.core.behaviours.*;
import jade.lang.acl.ACLMessage;
import jade.lang.acl.MessageTemplate;
import jade.domain.DFService;
import jade.domain.FIPAException;
import jade.domain.FIPAAgentManagement.DFAgentDescription;
import jade.domain.FIPAAgentManagement.ServiceDescription;
//car agent
public class CarAgent extends Agent{
//
private MinPath s;
long linkLongth;
String arg1;
String arg2;
String regLink;
int startNode;
int endNode;
double curV;
int curK;
int curQ;
int time;
int tempNode;
int numofCar;
int curLength;
int tempLength=0;
double curVf;
int curKj;
boolean delFlag=false;
// private AID[] numOfThisLink;
protected void setup()
{
s=new MinPath(this);
Object[] args = getArguments();
if (args != null && args.length > 0) {
arg1 = (String) args[0];
arg2 = (String) args[1];
startNode=Integer.parseInt(arg1);
endNode=Integer.parseInt(arg2);
}
else
System.out.println("the car "+this.getAID()+" ,it's arguments are missing!!!");
//zhuce dao car current link
//the first reg dao the first link(startNode-tempNode)
addBehaviour(new MyOneShotBehaviour());
addBehaviour(new TickerBehaviour(this, 1000) {
protected void onTick() {
tempLength=(int)((curV*1000)/3600)+tempLength;
// System.out.println("this agent"+myAgent.getName()+"tTHE TEMPLENGTH is: "+tempLength);
if(tempLength>=curLength&&delFlag==true) doDelete();
if(tempLength>=curLength){
try {
DFService.deregister(myAgent);
}
catch (FIPAException fe) {
fe.printStackTrace();
}
startNode=tempNode;
s.MinPathCal(startNode,endNode);//get the length between
// the startNode and the tempNode
tempNode=s.getNextNode();
if(tempNode==endNode) delFlag=true;//delete this agent flag
curLength=s.getLength(startNode,tempNode);
//zhuce dao xin d link
DFAgentDescription dfd = new DFAgentDescription();//the first reg to the current link
dfd.setName(getAID());
ServiceDescription sd = new ServiceDescription();
regLink=String.valueOf(startNode)+"-"+String.valueOf(tempNode);
sd.setType(regLink);
// System.out.println("the link "+regLink);
sd.setName("JADE-car-agent");
dfd.addServices(sd);
try {
DFService.register(myAgent, dfd);
}
catch (FIPAException fe) {
fe.printStackTrace();
}
tempLength=0;
}
}
} );
addBehaviour(new TickerBehaviour(this, 10000) {
protected void onTick() {
curVf=s.getVf(startNode,tempNode);
curKj=s.getKj(startNode,tempNode);
DFAgentDescription template1 = new DFAgentDescription();
ServiceDescription sd1 = new ServiceDescription();
sd1.setType(regLink);
template1.addServices(sd1);
try {
DFAgentDescription[] result1 = DFService.search(myAgent, template1);
curQ=result1.length;
// System.out.println("the LINK(V) OF "+startNode+"--"+tempNode+"is "+curQ);
}
catch (FIPAException fe) {
fe.printStackTrace();
}
curK=1000*curQ/curLength; //carnum/km
if(curK<=15) {// jisuan bu qong qingkuang xia d car v
double m=1-Math.exp(-2);
curV=curVf*m;
}
else if(curK>15&&curK<=80) {
curV=curVf*(1-curK/curKj);
}
else curV=0.5*curVf*Math.log10(curK/curKj);
// System.out.println("tTHE TEMPLENGTH "+curVf);
}
} );
}
// Put agent clean-up operations here
protected void takeDown() {
// Printout a dismissal message
System.out.println("Buyer-agent "+getAID().getName()+" terminating.");
}
public class MyOneShotBehaviour extends OneShotBehaviour {
public void action() {
s.MinPathCal(startNode,endNode);//get the length between
// the startNode and the tempNode
tempNode=s.getNextNode();
curLength=s.getLength(startNode,tempNode);//get the nextnode of the current startnode
// System.out.println("xxxxxxxxxxxxxxxxxxxthe link"+startNode+" and "+tempNode+"is" +curLength);
//第一次先注册第一个搜索的路段上
DFAgentDescription dfd = new DFAgentDescription();//the first reg to the current link
dfd.setName(getAID());
ServiceDescription sd = new ServiceDescription();
regLink=String.valueOf(startNode)+"-"+String.valueOf(tempNode);
sd.setType(regLink);
// System.out.println("the link "+regLink);
sd.setName("JADE-car-agent");
dfd.addServices(sd);
try {
DFService.register(myAgent, dfd);
}
catch (FIPAException fe) {
fe.printStackTrace();
}
//*得到当前已经搜索路段上的交通量并第一次计算这个路段上的速度值
curVf=s.getVf(startNode,tempNode);
curKj=s.getKj(startNode,tempNode);
DFAgentDescription template1 = new DFAgentDescription();
ServiceDescription sd1 = new ServiceDescription();
sd1.setType(regLink);
template1.addServices(sd1);
try {
DFAgentDescription[] result1 = DFService.search(myAgent, template1);
curQ=result1.length;
System.out.println("the LINK(the firstreg) OF "+startNode+"--"+tempNode+"is "+curQ);
}
catch (FIPAException fe) {
fe.printStackTrace();
}
curK=1000*curQ/curLength; //carnum/km
if(curK<=15) {// jisuan bu qong qingkuang xia d car v
double m=1-Math.exp(-2);
curV=curVf*m;
}
else if(curK>15&&curK<=80) {
curV=curVf*(1-curK/curKj);
}
else curV=0.5*curVf*Math.log10(curK/curKj);
// startNode=tempNode;//the first ba startNode bian wei xia yi ge node
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -