Cooperative Drop-off (CDO)

Drive Agent

package park2;

import java.io.IOException;

import java.util.ArrayList;

import java.util.logging.Level;

import java.util.logging.Logger;

import jade.core.AID;

import jade.core.Agent;

import jade.core.behaviours.CyclicBehaviour;

import jade.core.behaviours.OneShotBehaviour;

import jade.core.behaviours.SimpleBehaviour;

import jade.core.behaviours.TickerBehaviour;

import jade.domain.DFService;

import jade.domain.FIPAException;

import jade.domain.FIPAAgentManagement.DFAgentDescription;

import jade.domain.FIPAAgentManagement.ServiceDescription;

import jade.lang.acl.ACLMessage;

import jade.lang.acl.MessageTemplate;

import jade.lang.acl.UnreadableException;

import trasmapi.genAPI.Vehicle;

import trasmapi.genAPI.temp;

import trasmapi.genAPI.exceptions.UnimplementedMethod;

import trasmapi.sumo.SumoCom;

import trasmapi.sumo.SumoLane;

//import trasmapi.sumo.SumoLane;

import trasmapi.sumo.SumoVehicle;

import trasmapi.sumo.vartypes.Position2D;

/**

*

* @author 17525014

*/

public class DriverAgent extends Agent{

private static final long serialVersionUID = 6095960260125307076L;

int DIM=50;

int DIM2=30;

int pointeR=0;

Destination privat;

private int id;

public long delay,end,begin;

public static int numDrivers;

int m;

String Sid;

boolean GETC,PK,SSecond,CK1,CK2,ELG;

sortDestination mylist;

Destination[] FlistD;

Destination[] listD;

Destination[] FlistD2;

Destination[] listD2;

qparm []qprocess;

qparm myque;

// utility mynew;

public Destination myDes;

String origin;

SumoLane enter;

Vehicle vehicle;

double partpointer,Globalspeed;

public int pointer,allocator;

long du1,du2,dut,parktime;

int map=0;

int TKK=0;

int poi=0;

int letgo=1;

int mn,myorder=0;

double arrivalrate;

String MyGroupID;

int GM, Dsize;

public Destination [] drop,drop2;

ArrayList<EQueue>HeadQ2;

ArrayList<EQueue>HeadF2;

public DriverAgent(int idDriver, Destination[] listDD,Destination[] listDD2) {

super();

ELG=GETC=SSecond=CK1=false;

CK2=false;

MyGroupID=new String();

MyGroupID="NO";

GM=100;

//Dsize=25;

Dsize=10;

Globalspeed=28;

myDes=new Destination();

drop=new Destination[Dsize];

drop2=new Destination[Dsize];

PK= false;

delay=0;

arrivalrate=0; ;

// drop2=new String[10];

FlistD= new Destination[DIM];

FlistD2= new Destination[DIM2];

listD=new Destination[DIM];

listD2= new Destination[DIM2];

qprocess=new qparm[Dsize];

myque=new qparm();

HeadQ2=new ArrayList<EQueue>();

HeadF2=new ArrayList<EQueue>();

for(int o=0;o<DIM;o++)

listD[o]=listDD[o];

for(int o=0;o<DIM2;o++)

listD2[o]=listDD2[o];

myDes=new Destination();

du1=du2=dut=parktime=0;

// myDes.destination="DA1";

partpointer=1;

pointer= allocator=0;

try {

this.id= idDriver;

origin="source1";

System.out.println("the start point is"+origin);

String vehicleType = SumoCom.vehicleTypesIDs.get(0);

String routeId = SumoCom.getRouteId(origin, null);

int departureTime;

departureTime =100;

double departPosition =5.0;

double departSpeed = 0.0;

byte departLane = 0;

vehicle = new SumoVehicle(id, vehicleType, routeId, departureTime, departPosition, departSpeed, departLane);

SumoCom.addVehicle((SumoVehicle)vehicle);

SumoCom.addVehicleToSimulation((SumoVehicle)vehicle);

vehicle.changeTarget(listD[0].destination);

} catch (UnimplementedMethod e) {

}

}

/* (non-Javadoc)

* @see jade.core.Agent#setup()

*/

@Override

protected void setup() {

DFAgentDescription ad = new DFAgentDescription();

ad.setName(getAID()); //agentID

System.out.println("AID: "+ad.getName());

ServiceDescription sd = new ServiceDescription();

sd.setName(getName()); //nome do agente

System.out.println("Nome: "+sd.getName());

sd.setType("Driver");

System.out.println("Tipo: "+sd.getType()+"\n\n\n");

ad.addServices(sd);

Sid=id+"";

SumoCom.subscribeVehicle(Sid);

try {

DFService.register(this, ad);

} catch(FIPAException e) {

}

addBehaviour(new OneShotBehaviour() {

/**

*

*/

private static final long serialVersionUID = 1301400729516438184L;

// Position2D pos7;

//SumoVehicle ve7;

boolean F=true;

int hk=0;

@Override

public void action(){

// ve7=SumoCom.vehicles.get(id);

while(F){

try {

if(vehicle.getSpeed()>0.0){

// begin=System.currentTimeMillis();

begin=SumoCom.currentSimStep;

for(int j=0;j<Dsize;j++){

Destination DD=new Destination();

Destination DD2=new Destination();

hk=j+1;

String elment=new String();

elment="DA"+hk;

DD.destination=elment;

DD.x=0;

DD.y=410-j*20;

DD2=DD;

drop[j]=DD;

drop2[j]=DD2;

}

F=false;

mylist= new sortDestination(listD,0,250,pointer,allocator);

FlistD= mylist.MinSpanningTree();

// myDes=FlistD[pointer];

//FlistD=listD;

FlistD2=listD2;

myDes=drop[0];

// System.out.println(" iam "+getLocalName()+"my destination is "+myDes.getDestination());

try {

vehicle.changeTarget(myDes.destination);


} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

);

addBehaviour(new PeriodicSending(this,2000));

addBehaviour(new REC(this,3000));

addBehaviour(new ANC(this,2200));

addBehaviour(new receivingMessage());

addBehaviour(new receivingMessage2());

addBehaviour(new QINFO());

addBehaviour(new GroupRequest());

addBehaviour(new Correction());

addBehaviour(new check());

addBehaviour(new VERFY());

addBehaviour(new ChangeHeader());

addBehaviour(new ELECT());

}

@Override

protected void takeDown() {

try {

DFService.deregister(this);

}

catch (FIPAException fe) {

}

System.out.println("terminating.");

}

public class PeriodicSending extends TickerBehaviour{


private static final long serialVersionUID = -2919540587709502841L;

MessageTemplate tmp;

Position2D pos1;

int or,time1;

long time2;

Vehicle ve1;

Position2D posi;

Destination sendingDes;

sendQ sendingDes2;

teDes sendingDes3;

double desi,myx,myy;

boolean sendOk;

ArrayList<Integer> cov;

public PeriodicSending(Agent a,long t){

super(a,t);

}

@Override

public void onTick() {

ve1=SumoCom.vehicles.get(id);

String firstchar=new String();

firstchar=String.valueOf(myDes.destination.charAt(0));

//" FIRST char is "+firstchar+

// System.out.println(" FIRST char is "+firstchar+" full destination is "+FlistD[pointer].destination);

//&&myDes.destination.charAt(0)=='m'

try {

if(!ve1.getEdgeId().equals(myDes.destination)){

pos1=ve1.position;

myx=pos1.x;

myy=pos1.y;

sendOk=true;

if(myDes.destination.charAt(0)=='D'&&myy<305)

sendOk=false;

if(sendOk){

cov=new ArrayList<>();

for(int ij=0;ij<numDrivers;ij++){

if(ij!=id){

posi=SumoCom.vehicles.get(ij).position;

desi=CalDis(myx,posi.x,myy,posi.y);

if(desi<=500.0){

cov.add(ij);

// System.out.println("this is inside my range "+ij+" i am "+getLocalName()+" with distance is "+desi);

}

}

}

if(!cov.isEmpty()){

time1=SumoCom.getCurrentSimStep();

if(myDes.destination.charAt(0)!='D'){

sendingDes=new Destination(myDes.getDestination(),myx,myy,time1);

//sendingDes.flag=myflag;

}

else{

sendingDes=new Destination(drop[0].destination,myx,myy,time1);

// sendingDes.flag=myflag;

}

ACLMessage msg = new ACLMessage( ACLMessage.INFORM );

try {

msg.setContentObject(sendingDes);

} catch (IOException ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

msg.setConversationId("park searching message");


addReceivers(msg,cov);

// System.out.println("the time for sending this message is "+System.currentTimeMillis()+" and this is number "+i+" iam "+getLocalName());

myAgent.send(msg);

}

}

}

else{ try {

if(ve1.getEdgeId().equals(myDes.destination)&&firstchar.equals("D")){

pos1=ve1.position;

myx=pos1.x;

myy=pos1.y;

cov=new ArrayList<>();

for(int ij=0;ij<numDrivers;ij++){

if(ij!=id){

posi=SumoCom.vehicles.get(ij).position;

desi=CalDis(myx,posi.x,myy,posi.y);

if(desi<=500.0){

cov.add(ij);

// System.out.println("this is inside my range "+ij+" i am "+getLocalName()+" with distance is "+desi);

}

}

}

if(!cov.isEmpty()){

int x,y;

if(myDes.getDestination().length()==3){

x=Character.getNumericValue(myDes.getDestination().charAt(2));

}

else{

x=Character.getNumericValue(myDes.getDestination().charAt(2));

y=Character.getNumericValue(myDes.getDestination().charAt(3));

x=x*10+y;

}

//System.out.println("the time for sending this message is "+du2+" iam "+getLocalName()+" x "+x);

if(qprocess[x-1]==null){

qparm new1=new qparm();

new1.idp=id;

new1.ttp=(int)du2;

qprocess[x-1]=new1;

}

int rightsend=0;

for(int j=0;j<Dsize;j++)

if(qprocess[j]!=null)

rightsend++;

// System.out.println("the time for sending this message is with length "+rightsend+" iam "+getLocalName()+" x ");

sendingDes2=new sendQ(qprocess,rightsend);

ACLMessage msg = new ACLMessage( ACLMessage.INFORM );

try {

msg.setContentObject(sendingDes2);

} catch (IOException ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

msg.setConversationId("time to leave");


addReceivers(msg,cov);

myAgent.send(msg);

}

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

public class receivingMessage2 extends CyclicBehaviour {

/**

*

*/

private static final long serialVersionUID = -7078137609028646000L;

MessageTemplate mx2;

int poi;

boolean myflag6=false;

SumoVehicle ve5;

public void action() {

ve5=SumoCom.vehicles.get(id);

//ve5.speed>0.0

if(!ve5.getEdgeId().equals(myDes.destination)){

mx2 = MessageTemplate.MatchConversationId("NO");

ACLMessage msgg2 = myAgent.receive(mx2);

if(msgg2 != null)

{

Destination privat2 = null;

try {

privat2 = (Destination) msgg2.getContentObject();

} catch (UnreadableException ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}


if(privat2.destination.charAt(0)=='D'){


poi=-1;

for(int i=0;i<Dsize;i++){

if(drop[i].destination.equals(privat2.destination)){

poi=i;

}

}

// System.out.println("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*******"+myAgent.getLocalName());

if(myDes.destination.equals(privat2.destination)&&poi<Dsize-2){


//myDes=drop[poi+1];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}



}

}

if(privat2.destination.charAt(0)!='D'){

if(privat2.getDestination().equals(myDes.getDestination())){

// System.out.println(" receiving new type message iam "+getLocalName()+" from "+msgg2.getSender().getLocalName());

pointer++;

if(FlistD.length-allocator>pointer){

mylist= new sortDestination(FlistD,ve5.position.x,ve5.position.y,pointer,allocator);

FlistD= mylist.MinSpanningTree();

myDes=FlistD[pointer];

// System.out.println(" I am "+myAgent.getLocalName()+" change the address to "+FlistD[pointer].destination.charAt(0)+" "+myDes.destination);

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

}

else{ if(!SSecond){

SSecond=true;

FlistD=FlistD2;

pointer=0;

allocator=0;

myDes=FlistD[pointer];

try {

vehicle.changeTarget(myDes.getDestination());

// myAgent.doDelete();

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

}

else{

int bn=-1;

int anz=pointer;

while(bn==-1&&anz<(FlistD.length-allocator))

{

if(privat2.destination.equals(FlistD[anz].destination)){

bn=anz;

allocator++;

}

anz++;

}


for(int n=anz-1;n<FlistD.length-allocator-1;n++){

FlistD[n]=FlistD[n+1];

}

}

}

}


else

block();

}

}

}


public class receivingMessage extends CyclicBehaviour {

/**

*

*/

private static final long serialVersionUID = -1167565660520566700L;

MessageTemplate mx;

Position2D pos;

int ti=0,time2;

long TT1=0,TT2=0;

int number=0;

double myx1,myy1;

double dis11,dis22;

Integer Head,Head2;

//boolean change,change1;

temp T1;

Vehicle ve2;

@Override

public void action() {

ve2=SumoCom.vehicles.get(id);

try {

if(!ve2.getEdgeId().equals(myDes.destination)){

mx = MessageTemplate.MatchConversationId("park searching message");

ACLMessage msgg = myAgent.receive(mx);

if(msgg != null)

{

// if("park searching message".equals(msgg.getConversationId())){

Destination privat = null;

try {

privat = (Destination) msgg.getContentObject();

} catch (UnreadableException ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

time2=privat.time;

T1=ve2.cache.get(time2);

pos=T1.Position;

if(ve2.position.x==pos.x&&ve2.position.y==pos.y)

pos=ve2.position;

myx1=pos.x;

myy1=pos.y;

if(privat.getDestination().charAt(0)!='D'){

if (privat.getDestination().equals(myDes.getDestination())){

dis11=CalDis(myx1,myDes.getX(),myy1,myDes.getY());

dis22=CalDis(privat.getX(),myDes.getX(),privat.getY(),myDes.getY());

if(dis11>=dis22)

{

pointer++;

if(FlistD.length-allocator>pointer){

//NeW= Arrays.copyOfRange(FlistD, pointer, FlistD.length);

mylist= new sortDestination(FlistD,ve2.position.x,ve2.position.y,pointer,allocator);

FlistD= mylist.MinSpanningTree();

myDes=FlistD[pointer];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

}

else {

FlistD=FlistD2;

pointer=0;

allocator=0;

myDes=FlistD[pointer];

try {

vehicle.changeTarget(myDes.getDestination());

// myAgent.doDelete();

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

}

}

else{

dis11=CalDis(myx1,drop[0].x,myy1,drop[0].y);

dis22=CalDis(privat.getX(),drop[0].x,privat.getY(),drop[0].y);

if(msgg.getSender().getLocalName().length()==8){

Head=Character.getNumericValue(msgg.getSender().getLocalName().charAt(7));

}

else{

Head=Character.getNumericValue(msgg.getSender().getLocalName().charAt(7));

Head2=Character.getNumericValue(msgg.getSender().getLocalName().charAt(8));

Head=Head*10+Head2;

}

if(myy1>405){

ti=0;

while(ti<HeadQ2.size()&&HeadQ2.get(ti).ID!=Head){

ti++;

}

if(ti==HeadQ2.size()){

EQueue QW=new EQueue();

QW.ID=Head;

// if(dis11<=dis22)

QW.FAR=dis11-dis22;

// else

// QW.FAR=dis22;

HeadQ2.add(QW);

int mv;

EQueue MUSS=new EQueue();

EQueue MUSS2=new EQueue();

for(int i=0;i<HeadQ2.size();i++) {

MUSS=HeadQ2.get(i);

for(mv=i-1;mv>=0 && MUSS.FAR<HeadQ2.get(mv).FAR;mv--){

MUSS2=HeadQ2.get(mv);

HeadQ2.set(mv+1,MUSS2);

}

HeadQ2.set(mv+1,MUSS);

}


//System.out.println(" meeeeeeeeeeeee "+myAgent.getLocalName()+" list is "+HeadQ2.get(0).ID+" and the last one is "+HeadQ2.get(ti).ID);

}

}

}


}

else{

block();

}

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


public class QINFO extends CyclicBehaviour {

/**

*

*/

private static final long serialVersionUID = -1167565660520566700L;

MessageTemplate mx3;

Position2D pos3,pp;

Position2D posi2;

int resp2,Headt,Head2t,time3,check1=0;

double myx3,myy3,myspeed;

double dis113,dis223;

qparm tempq;

double estimate,current,prevte,current2;

qparm MUS=new qparm();

qparm insertel;

ArrayList<Integer> cov2;

double desi2,xvalue,yvalue;

//boolean change,change1;

temp T13;

Vehicle ve23;

@Override

public void action() {

ve23=SumoCom.vehicles.get(id);

try {

if(letgo==1){

//&&MyGroupID.equals("NO")

if(myDes.destination.charAt(0)=='D'){

mx3 = MessageTemplate.MatchConversationId("time to leave");

ACLMessage msgg = myAgent.receive(mx3);

if(msgg != null)

{

sendQ privat = null;

try {

privat = (sendQ) msgg.getContentObject();

} catch (UnreadableException ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

int all=privat.SZ;

arrivalrate=0;

for(int i=0;i<Dsize;i++){

if(privat.send[i]!=null){

// all++;

if(qprocess[i]==null){

insertel=new qparm();

insertel=privat.send[i];

qprocess[i]=insertel;

}

else if(qprocess[i].ttp<privat.send[i].ttp){

qprocess[i]=privat.send[i];

}

}

}

if(ve23.position.y>410&&check1!=1&&!GETC&&MyGroupID.equals("NO")){

if(msgg.getSender().getLocalName().length()==8){

Headt=Character.getNumericValue(msgg.getSender().getLocalName().charAt(7));

}

else{

Headt=Character.getNumericValue(msgg.getSender().getLocalName().charAt(7));

Head2t=Character.getNumericValue(msgg.getSender().getLocalName().charAt(8));

Headt=Headt*10+Head2t;

}

boolean myflag3=false;

int resp=resp2=100;

for(int i=0;i<HeadQ2.size();i++){

if(HeadQ2.get(i).ID==Headt){

resp2=i;

}

if(HeadQ2.get(i).FAR<0)

resp=i;

}

if(resp2<100&&HeadQ2.get(resp2).FAR>0){

int resp3=resp2-resp;

resp2=all+resp2-resp-1;

if(resp2>=0&&resp2<Dsize){

check1=1;

myflag3=true;

MyGroupID=msgg.getSender().getLocalName();

GM=resp2;

myDes=drop[resp2];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

System.out.println(" ^^^^^^^^"+all+"^^^^^^^^^^^^^^^^^^"+msgg.getSender().getLocalName()+"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ my des is "+resp2+" iam "+myAgent.getLocalName());

}else{ if(resp2==Dsize){

MyGroupID=msgg.getSender().getLocalName();

//myAgent.getLocalName();

GM=resp3;

check1=1;

myspeed=ve23.getSpeed();

pos3=ve23.position;

myDes=drop[0];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

//67=(1000/15)*distance= the travelling time to the first slot 67

current= (SumoCom.currentSimStep+(arrivingDis(pos3,1))*(1000/myspeed));

current2= arrivingDis(pos3,1);

letgo=1;

myflag3=true;

//myDes=drop[qprocess[0].idp];

double Rtime=(qprocess[0].ttp-SumoCom.currentSimStep);

double ss=((double)current2/Rtime)*1000-1;

ve23.setSpeed(ss);

System.out.println(" ***XXXXXxxXXXXX***"+all+"****BBBBBBB***"+current+"****"+ss+"*** my des is "+resp2+" iam "+myAgent.getLocalName());

CK1=true;

}

if(GM==100){

System.out.println(" ***XXXXXxxXXXXX***"+all+"****AAAAAAAAA***"+current+"****"+GM+"*** my des is "+resp2+" iam "+myAgent.getLocalName());

GM=0;

while(GM<HeadQ2.size()&&HeadQ2.get(GM).FAR<0)

GM++;

GM=(HeadQ2.size()-GM-1)%Dsize;

myDes=drop[GM];


// System.out.println(" I am "+myAgent.getLocalName()+" change the address to "+FlistD[pointer].destination.charAt(0)+" "+myDes.destination);

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

}

}

if(myflag3){

pp=vehicle.position;

xvalue=pp.x;

yvalue=pp.y;

cov2=new ArrayList<>();

for(int ij=0;ij<numDrivers;ij++){

if(ij!=id){

posi2=SumoCom.vehicles.get(ij).position;

desi2=CalDis(xvalue,posi2.x,yvalue,posi2.y);

if(desi2<=700.0){

cov2.add(ij); }

}

}

//!cov2.isEmpty()

String CON=new String();

// GM=resp2;

CON=MyGroupID+"-"+GM;

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("GROUP");

for(int x=0; x< cov2.size();x++){

MM.addReceiver(new AID("DRIVER#"+cov2.get(x),AID.ISLOCALNAME));

}

myAgent.send(MM);

}

}

}


}

else{

block();

}

}

}

else{

myAgent.removeBehaviour(this);

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

private class ANC extends TickerBehaviour {

/**

*

*/

private static final long serialVersionUID = 1855878952337101620L;

SumoVehicle ve3;

Position2D posi2;

ArrayList<Integer> cov2;

double desi2,xvalue,yvalue;

Position2D pp;

public ANC(Agent a,long t){

super(a,t);

}

protected void onTick() {

ve3=SumoCom.vehicles.get(id);

if(CK1&&ve3.position.y>400){

pp=vehicle.position;

xvalue=pp.x;

yvalue=pp.y;

cov2=new ArrayList<>();

for(int ij=0;ij<numDrivers;ij++){

if(ij!=id){

posi2=SumoCom.vehicles.get(ij).position;

desi2=CalDis(xvalue,posi2.x,yvalue,posi2.y);

if(desi2<=500.0){

cov2.add(ij); }

}

}

//!cov2.isEmpty()

String CON=new String();

double Rtime=(qprocess[0].ttp);

Rtime=Rtime+50000;

int sss=(int) ve3.getSpeed();

CON=Rtime+"- "+sss+"-"+letgo;

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("Vpark");

for(int x=0; x< cov2.size();x++){

MM.addReceiver(new AID("DRIVER#"+cov2.get(x),AID.ISLOCALNAME));

}

myAgent.send(MM);

}

}

}



/////////////////////////////////////////////////////////

private void addReceivers(ACLMessage msg, ArrayList<Integer> cov)

{

for(int x=0; x< cov.size();x++){

msg.addReceiver(new AID("DRIVER#"+cov.get(x),AID.ISLOCALNAME));

}

}

public double CalDis(double x1,double x2,double y1,double y2)

{

double xDiffloc,yDiffloc,xSqrloc,ySqrloc,outputloc;

xDiffloc =x1-x2;

xSqrloc = Math.pow(xDiffloc, 2);

yDiffloc = y1-y2;

ySqrloc = Math.pow(yDiffloc, 2);

outputloc= Math.sqrt(xSqrloc + ySqrloc);

return outputloc;

}

public double arrivingDis(Position2D PS, int ch)

{

double mytime,time;

double x1,y1,x2,y2;

if(ch==1){

y1=490;

x1=450;

x2=0;

y2=405;

if(PS.y>=y1){

time=(int) ((PS.x-x2+y1-y2));

}

else{

time=(int) ((PS.y-y2));

}

mytime=time;

}

else{

mytime=180+Math.abs(PS.x-460);

}

return mytime;

}

private class check extends SimpleBehaviour {

/**

*

*/

private static final long serialVersionUID = -2639002170028439905L;

result output,output2;

// boolean A=false,C=false;

Position2D pos3;

int counter=0;

int counter2=0;

//Random ran;

Vehicle ve4;

// SumoVehicle ve44;

@Override

public void action(){

// ve44=SumoCom.vehicles.get(id);

ve4=vehicle;

try {

if(ve4.getEdgeId().equals(myDes.getDestination())){

//C=true;

// EN=false;

// if(!myDes.getDestination().equals("ma2,5")){

try {

vehicle.setSpeed(0.0);

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

// }

if(myDes.destination.charAt(0)!='D')

{

counter++;

if(counter==1){

end=SumoCom.currentSimStep;

delay=end-begin;

try {

output=new result();

} catch (IOException ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

output.INSERT(getLocalName(),myDes.destination,delay/100,end);

}

}

else{

counter2++;

if(counter2==1){

GETC=true;

end=SumoCom.currentSimStep;

delay=end-begin;

try {

output2=new result();

} catch (IOException ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

output2.INSERT(getLocalName(),myDes.destination,delay/100,end);

/// du1=System.currentTimeMillis();

du1=SumoCom.currentSimStep;

// ran=new Random();

// du2=du1+((ran.nextInt(100))*300+2000);

du2=du1+50000;

}

}

dut=SumoCom.currentSimStep;

if(dut>du2&&myDes.destination.charAt(0)=='D'&&!SSecond){

pos3=ve4.position;

xvalue=pos3.x;

yvalue=pos3.y;


myDes=FlistD[0];

// C=true;

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

//myAgent.doDelete();

vehicle.setSpeed(10.0);

}

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public boolean done(){

return false;

}

}

private class REC extends TickerBehaviour {

/**

*

*/

private static final long serialVersionUID = 1855878952337101620L;

SumoVehicle ve3;

Position2D posi2;

ArrayList<Integer> cov2;

double desi2,xvalue,yvalue;

Position2D pp;

Destination sendingDes2;

public REC(Agent a,long t){

super(a,t);

}

protected void onTick() {

ve3=SumoCom.vehicles.get(id);

//&&myDes.destination.charAt(0)!='D'

if(ve3.getEdgeId().equals(myDes.getDestination())){

pp=vehicle.position;

xvalue=pp.x;

yvalue=pp.y;

cov2=new ArrayList<>();

for(int ij=0;ij<numDrivers;ij++){

if(ij!=id){

posi2=SumoCom.vehicles.get(ij).position;

desi2=CalDis(xvalue,posi2.x,yvalue,posi2.y);

if(desi2<=100.0){

cov2.add(ij); }

}

}

//!cov2.isEmpty()

if(true){

sendingDes2=new Destination(myDes.getDestination(),xvalue,yvalue);

ACLMessage msg2 = new ACLMessage( ACLMessage.INFORM );

try {

msg2.setContentObject(sendingDes2);

} catch (IOException ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

msg2.setConversationId("NO");

for(int x=0; x< cov2.size();x++){

msg2.addReceiver(new AID("DRIVER#"+cov2.get(x),AID.ISLOCALNAME));

}


myAgent.send(msg2);

}

}

}

}

////////////////////////////

private class Correction extends CyclicBehaviour {


/**

*

*/

private static final long serialVersionUID = 2965006172552026401L;

Vehicle ve11;

Position2D pp,posi2;

double xvalue,yvalue,desi2;

boolean myflag1;

ArrayList<Integer> cov2;

ArrayList<String> repeat2=new ArrayList<String>();

String [] mody= new String[4];

int HH,NID,NCK=0,PGM;

public void action() {

ve11=SumoCom.vehicles.get(id);

try {

if(letgo==1){

if(myDes.destination.charAt(0)=='D'&&!GETC){

MessageTemplate mt =MessageTemplate.and(MessageTemplate.MatchConversationId("CHG"),

MessageTemplate.MatchPerformative(ACLMessage.INFORM));

ACLMessage msg = myAgent.receive(mt);

if (msg != null) {

String title2 = msg.getContent();

mody=title2.trim().split("-");

String SG=mody[0].trim();

String SM=mody[1].trim();

int prop=Integer.parseInt(SM);

String SM2=mody[2].trim();

int ptime1=Integer.parseInt(SM2);

//HH=Character.getNumericValue(privat2.destination.charAt(2));

myflag1=false;

if(SG.equals(MyGroupID)&&prop!=GM){

if(NCK==0){

NCK++;

NID=prop;

repeat2.add(msg.getSender().getLocalName());

}

else{

if(NCK<2&&NID==prop&&!repeat2.contains(msg.getSender().getLocalName())){

NCK++;

repeat2.add(msg.getSender().getLocalName());

}

if(NCK>=2){

ELG=true;

System.out.println("@@@@@@@ "+repeat2+" @@@@@ "+"##### i will change from "+GM+" to "+NID+" iam "+myAgent.getLocalName());

//repeat2.clear();

if(qprocess[0]==null||qprocess[0].ttp==0){

qparm ter=new qparm();

ter.ttp=ptime1;

qprocess[0]=ter;

}

NCK=0;

PGM=GM;

GM=prop;

if(NID<Dsize){

HH=NID;

myflag1=true;

}else{

if(NID==Dsize){

myflag1=true;

CK1=true;

HH=0;

double current2= arrivingDis(ve11.position,1);

double ss=((current2*1000)/(ptime1-SumoCom.currentSimStep));

ve11.setSpeed(ss);

}

else if(NID>Dsize){

myflag1=true;

HH=NID%Dsize;

GM=NID;

}

}

myDes=drop[HH];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

if(myflag1){

pp=vehicle.position;

xvalue=pp.x;

yvalue=pp.y;

cov2=new ArrayList<>();

for(int ij=0;ij<numDrivers;ij++){

if(ij!=id){

posi2=SumoCom.vehicles.get(ij).position;

desi2=CalDis(xvalue,posi2.x,yvalue,posi2.y);

if(desi2<=700.0){

cov2.add(ij); }

}

}

//!cov2.isEmpty()

String CON=new String();

GM=prop;

CON=MyGroupID+"-"+NID;

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("GROUP");

for(int x=0; x< cov2.size();x++){

MM.addReceiver(new AID("DRIVER#"+cov2.get(x),AID.ISLOCALNAME));

}

myAgent.send(MM);

}

if(GM==Dsize)

System.out.println(" ++++++++++ FROM "+PGM+" ### Change from cluster head to "+GM+" iam "+myAgent.getLocalName()+" new Group is "+MyGroupID);

if(PGM==Dsize){

//if(GM>10)

// MyGroupID=mody[3].trim();

CK1=false;

ve11.setSpeed(28);

System.out.println(" ++++++++++ FROM "+PGM+" ### Change from cluster head to "+GM+" iam "+myAgent.getLocalName()+" new Group is "+MyGroupID);

pp=vehicle.position;

xvalue=pp.x;

yvalue=pp.y;

cov2=new ArrayList<>();

for(int ij=0;ij<numDrivers;ij++){

if(ij!=id){

posi2=SumoCom.vehicles.get(ij).position;

desi2=CalDis(xvalue,posi2.x,yvalue,posi2.y);

if(desi2<=700.0){

cov2.add(ij); }

}

}

//!cov2.isEmpty()

String CON=new String();

int dif=GM-PGM;

if(dif>0)

CON="A "+"-"+dif+"-";

else{

dif=PGM-GM;

CON="B "+"-"+dif+"-";

}

System.out.println(" ++++++++++ FROM "+PGM+" ### Change from cluster head to "+GM+" iam "+myAgent.getLocalName()+" new Group is "+CON);

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("HeaderG");

for(int x=0; x< cov2.size();x++){

MM.addReceiver(new AID("DRIVER#"+cov2.get(x),AID.ISLOCALNAME));

}

myAgent.send(MM);

}

}

}

}

}

else

block();

}

}

else{

myAgent.removeBehaviour(this);

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

//else

// myAgent.removeBehaviour(this);

}

}


private class GroupRequest extends CyclicBehaviour {



private static final long serialVersionUID = 2965006172552026401L;

Vehicle ve11;

ArrayList<Integer> cov2;

int Headt,Head2t,Headt3,Head2t4;

double xvalue,yvalue,desi2;

Position2D pp,posi2;

Position2D pos3;

boolean myflag2=false;

boolean justO=false,right1=false;

String[] mody=new String[3];

public void action() {

ve11=SumoCom.vehicles.get(id);

pos3=ve11.position;

try {

if(letgo==1){

MessageTemplate mt =MessageTemplate.and(MessageTemplate.MatchConversationId("Vpark"),

MessageTemplate.MatchPerformative(ACLMessage.INFORM));

ACLMessage msg = myAgent.receive(mt);

if (msg != null) {

right1=false;

String title2 = msg.getContent();

mody=title2.trim().split("-");

String ptime=mody[0].trim();

//ptime2= the speed of sender

String ptime2=mody[1].trim();

//ptime1= the leaving time of sender

int ptime1=(int) Double.parseDouble(ptime);

//double Hspeed=Double.parseDouble(ptime2);

int lego= Integer.parseInt(mody[2].trim());

if(msg.getSender().getLocalName().length()==8){

Headt=Character.getNumericValue(msg.getSender().getLocalName().charAt(7));

}

else{

Headt=Character.getNumericValue(msg.getSender().getLocalName().charAt(7));

Head2t=Character.getNumericValue(msg.getSender().getLocalName().charAt(8));

Headt=Headt*10+Head2t;

}

if(!GETC&&pos3.y>400&&MyGroupID.equals("NO")&&pos3.x<1500){

int Hindex=-100;

int myin=0;

for(int i=0;i<HeadQ2.size();i++){

if(HeadQ2.get(i).ID==Headt){

Hindex=i;

if(HeadQ2.get(i).FAR<0)

right1=true;

}

if(HeadQ2.get(i).FAR<0)

myin=i;

}

Hindex-=myin;

if(qprocess[0]==null||qprocess[0].ttp<ptime1){

qparm ent=new qparm();

ent.idp=Headt;

ent.ttp=ptime1;

qprocess[0]=ent;

}

//if(MyGroupID.equals("NO")){

myflag2=false;

if(Hindex==Dsize){

if(letgo==1){

MyGroupID=msg.getSender().getLocalName();

GM=Hindex;

myflag2=CK1=true;

double current2= arrivingDis(pos3,1);

double parkarrival=arrivingDis(pos3,0);

double parktime=((parkarrival/ve11.speed))*1000+115000;

parktime+=SumoCom.currentSimStep;

double ss;

ss=((current2*1000)/(ptime1-SumoCom.currentSimStep));

if(ss>5&&ve11.speed>5){

// ss=((current2*1000)/(ptime1-SumoCom.currentSimStep));

letgo=1;

Globalspeed=ss;

ve11.setSpeed(ss);

myDes=drop[0];

}

else{

letgo=0;

myDes=FlistD[0];

ss=15;

}

System.out.println("@@@@@ "+ss+" @@@@@@@@@@@@@@@@ "+"##### my speed now "+(parktime-ptime1)+" iam "+myAgent.getLocalName());

}

}

else{if(Hindex>0&&Hindex<=Dsize-1){

if(lego==1){

double Hspeed=Double.parseDouble(ptime2);

myflag2=true;

MyGroupID=msg.getSender().getLocalName();

//////////////////////////////////////////////////////////////////////////////////////

Globalspeed=Hspeed;

// ve11.setSpeed(Hspeed+2);

GM=Hindex;

myDes=drop[Hindex];

System.out.println("+++++++++ "+msg.getSender().getLocalName()+" +### my destyination "+myDes.destination+" iam "+myAgent.getLocalName());

}

else{

letgo=0;

myDes=FlistD[pointer];

MyGroupID=msg.getSender().getLocalName();

}

}

else if(Hindex>Dsize){

myDes=drop[Hindex%Dsize];

double Hspeed=Double.parseDouble(ptime2);

//ve11.setSpeed(Hspeed);

Globalspeed=ve11.speed;

int propH=Hindex+myin-Dsize;

// System.out.println("+++++ "+Hindex+" +++++++ "+propH+" +++++"+"##### my Header is "+HeadQ2.get(propH).ID+" iam "+myAgent.getLocalName());

String CON=new String();

String SG1=new String();

SG1=msg.getSender().getLocalName();

CON=SG1+"-"+propH+"-"+qprocess[0].ttp;

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("elect");

//for(int x=0; x< cov2.size();x++){

//MM.addReceiver(msg.getSender());

MM.addReceiver(new AID("DRIVER#"+HeadQ2.get(propH).ID,AID.ISLOCALNAME));

// }

myAgent.send(MM);

}

}

////////////////////////////////////////////////////////////////////////////////////

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

if(myflag2){

pp=vehicle.position;

xvalue=pp.x;

yvalue=pp.y;

cov2=new ArrayList<>();

for(int ij=0;ij<numDrivers;ij++){

if(ij!=id){

posi2=SumoCom.vehicles.get(ij).position;

desi2=CalDis(xvalue,posi2.x,yvalue,posi2.y);

if(desi2<=700.0){

cov2.add(ij); }

}

}

//!cov2.isEmpty()

String CON=new String();

CON=MyGroupID+"-"+GM;

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("GROUP");

for(int x=0; x< cov2.size();x++){

MM.addReceiver(new AID("DRIVER#"+cov2.get(x),AID.ISLOCALNAME));

}

myAgent.send(MM);

}

//}


///////////////////////////////////////////////////////////////////////////////////

}

}

else

block();

}

else{

myAgent.removeBehaviour(this);

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

private class VERFY extends CyclicBehaviour {


private static final long serialVersionUID = 2965006172552026401L;

Vehicle ve11;

String [] mody=new String[3];

int Headt,Head2t,Headt3,Head2t4;

int x,y;

public void action() {

ve11=SumoCom.vehicles.get(id);

try {

if(letgo==1){

if(!ve11.getEdgeId().equals(myDes.destination)&&myDes.destination.charAt(0)=='D'&&!GETC&&!ELG){

MessageTemplate mt =MessageTemplate.and(MessageTemplate.MatchConversationId("GROUP"),

MessageTemplate.MatchPerformative(ACLMessage.INFORM));

ACLMessage msg = myAgent.receive(mt);

if (msg != null) {

String title2 = msg.getContent();

mody=title2.trim().split("-");

String SG=mody[0].trim();

String SM=mody[1].trim();

int ptime1=(int) Double.parseDouble(SM);


if(!MyGroupID.equals("NO")&&!SG.equals("NO")){

if(SG.length()==8){

//(MyGroupID

Headt=Character.getNumericValue(SG.charAt(7));

}

else{

Headt=Character.getNumericValue(SG.charAt(7));

Head2t=Character.getNumericValue(SG.charAt(8));

Headt=Headt*10+Head2t;

}

if(msg.getSender().getLocalName().length()==8){

Headt3=Character.getNumericValue(msg.getSender().getLocalName().charAt(7));

}

else{

Headt3=Character.getNumericValue(msg.getSender().getLocalName().charAt(7));

Head2t4=Character.getNumericValue(msg.getSender().getLocalName().charAt(8));

Headt3=Headt3*10+Head2t4;

}

x=-1000;

y=100;

int n=-1,z=-1;

int vi=-1;

// boolean behind=true;

for(int i=0;i<HeadQ2.size();i++){

if(HeadQ2.get(i).ID==Headt){

x=i;

n=i;

}

if(HeadQ2.get(i).FAR<0)

z=i;

if(HeadQ2.get(i).ID==Headt3){

if(HeadQ2.get(i).FAR<=0) {

vi=i;

y=i-1;

}else{

vi=y=i;

}

}

}

if(MyGroupID.equals(SG)&&GM==ptime1){

}

else{

if(x>0&&HeadQ2.get(x).FAR<0)

x=x-y-1;

else if(x>0)

x=x-y;

if(x>0&&x!=ptime1&&ptime1<HeadQ2.size()){

if(GM==100&&z!=-1&&myDes.destination.equals(drop[0].destination)){

myDes=drop[(n-z)%Dsize];

// GM=-1;

// System.out.println("(((((((( NO MEMBER "+(n-z)+" &&& Destination is "+drop[(n-z)%10].destination+" iam "+myAgent.getLocalName()+" and i is "+GM);

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

}

//!cov2.isEmpty()

System.out.println(HeadQ2.get(ptime1).ID+" ((("+ptime1+"((((( should bee "+HeadQ2.get(vi).ID+" "+x+"iam "+myAgent.getLocalName()+" he is "+msg.getSender().getLocalName());

String CON=new String();

if(qprocess[0]==null){

System.out.println("UUUUUUU "+myAgent.getLocalName()+" and i is "+GM);

// double current2= (arrivingDis(ve11.position)/ve11.speed)*1000;

qparm ter=new qparm();

//ter.idp=GM;

// ter.ttp=(int)current2;

qprocess[0]=ter;

}

CON=SG+"-"+x+"-"+qprocess[0].ttp+"-"+MyGroupID;

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("CHG");

MM.addReceiver(msg.getSender());


myAgent.send(MM);

}

}

}

else if(!MyGroupID.equals("NO")&&SG.equals("NO")){

if(msg.getSender().getLocalName().length()==8){

Headt3=Character.getNumericValue(msg.getSender().getLocalName().charAt(7));

}

else{

Headt3=Character.getNumericValue(msg.getSender().getLocalName().charAt(7));

Head2t4=Character.getNumericValue(msg.getSender().getLocalName().charAt(8));

Headt3=Headt3*10+Head2t4;

}

if(MyGroupID.length()==8){

//(MyGroupID

Headt=Character.getNumericValue(MyGroupID.charAt(7));

}

else{

Headt=Character.getNumericValue(MyGroupID.charAt(7));

Head2t=Character.getNumericValue(MyGroupID.charAt(8));

Headt=Headt*10+Head2t;

}

x=y=-1;

int z=-1;

// boolean behind=true;

for(int i=0;i<HeadQ2.size();i++){

if(HeadQ2.get(i).ID==Headt){

x=i;

}

if(HeadQ2.get(i).FAR<0)

z=i;

if(HeadQ2.get(i).ID==Headt3){

y=i;

}

}

if(y>-1&&HeadQ2.get(y).FAR<0)

y--;

y=x-y;

z=x-z;

int tD=y-z;

if(y>0){

String CON=new String();

if(qprocess[0]==null){

System.out.println("UUUUUUU "+myAgent.getLocalName()+" and i is "+GM);

qparm ter=new qparm();

qprocess[0]=ter;

}

if(!CK1){

CON=SG+"-"+y+"-"+qprocess[0].ttp+"-";

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("CHG");

MM.addReceiver(msg.getSender());


myAgent.send(MM);

// System.out.println(tD+" !!!!!! "+CON+" !!! "+CK1+" !!!!!! received no group i am"+myAgent.getLocalName()+" from "+msg.getSender().getLocalName());

}

else if(CK1&&tD>0&&tD<=Dsize-1){

CON=SG+"-"+tD+"-"+qprocess[0].ttp+"-";

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("CHG");

MM.addReceiver(msg.getSender());


myAgent.send(MM);

// System.out.println(tD+" !!!!!! "+CON+" !!! "+CK1+" !!!!!! received no group i am"+myAgent.getLocalName()+" from "+msg.getSender().getLocalName());

}

}

}

}

else

block();

}

}

else{

myAgent.removeBehaviour(this);

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


private class ChangeHeader extends CyclicBehaviour {



private static final long serialVersionUID = 2965006172552026401L;

Vehicle ve11;

//double localdistance7,localdistance8;

int Headt,NS;

Position2D pos3;

String[] mody=new String[4];

public void action() {

ve11=SumoCom.vehicles.get(id);

pos3=ve11.position;

if(letgo==1){

if(pos3.y>300&&!GETC){

MessageTemplate mt =MessageTemplate.and(MessageTemplate.MatchConversationId("HeaderG"),

MessageTemplate.MatchPerformative(ACLMessage.INFORM));

ACLMessage msg = myAgent.receive(mt);

if (msg != null) {

String title2 =new String();

title2=msg.getContent();

mody=title2.trim().split("-");

String Sign=mody[0].trim();

String ptime=new String();

ptime=mody[1].trim();

//System.out.println(msg.getSender().getLocalName()+" to "+ptime+" iam "+myAgent.getLocalName());

int ptime1=Integer.parseInt(ptime);

if(MyGroupID.equals(msg.getSender().getLocalName())){

ELG=true;

NS=GM;

if(Sign.equals("A"))

NS+=ptime1;

else

NS-=ptime1;

Headt=Math.abs(NS%Dsize);

try {

ve11.setSpeed(25);

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

myDes=drop[Headt];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

System.out.println(msg.getSender().getLocalName()+" to "+GM+" iam "+myAgent.getLocalName()+" "+NS);

GM=100;

MyGroupID="NO";

}

}

else

block();

}

}

else{

myAgent.removeBehaviour(this);

}

}

}



private class ELECT extends CyclicBehaviour {


private static final long serialVersionUID = 2965006172552026401L;

Vehicle ve11;

String [] mody=new String[3];

int Headt,Head2t,Headt3,Head2t4;

int conter=0;

ArrayList<Integer> repeat3=new ArrayList<>();

public void action() {

ve11=SumoCom.vehicles.get(id);

try {

if(letgo==1){

if(!GETC&&!CK1){

MessageTemplate mt =MessageTemplate.and(MessageTemplate.MatchConversationId("elect"),

MessageTemplate.MatchPerformative(ACLMessage.INFORM));

ACLMessage msg = myAgent.receive(mt);

if (msg != null) {

String title2 = msg.getContent();

mody=title2.trim().split("-");

String SG=mody[0].trim();

String SM=mody[1].trim();

int ptime1=(int) Double.parseDouble(SM);

if(SG.length()==8){

//(MyGroupID

Headt=Character.getNumericValue(SG.charAt(7));

}

else{

Headt=Character.getNumericValue(SG.charAt(7));

Head2t=Character.getNumericValue(SG.charAt(8));

Headt=Headt*10+Head2t;

}

if(qprocess[0]==null||qprocess[0].ttp<ptime1){

qparm ent=new qparm();

ent.idp=Headt;

ent.ttp=ptime1;

qprocess[0]=ent;

}

if(msg.getSender().getLocalName().length()==8){

Headt3=Character.getNumericValue(msg.getSender().getLocalName().charAt(7));

}

else{

Headt3=Character.getNumericValue(msg.getSender().getLocalName().charAt(7));

Head2t4=Character.getNumericValue(msg.getSender().getLocalName().charAt(8));

Headt3=Headt3*10+Head2t4;

}


if((MyGroupID.equals("NO")||MyGroupID.equals(SG))&&!CK1&&conter>=1){

ELG=true;

conter=0;

MyGroupID=SG;

GM=Dsize;

myDes=drop[0];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

Logger.getLogger(DriverAgent.class.getName()).log(Level.SEVERE, null, ex);

}

System.out.println(" Elected to Header "+MyGroupID+" iam "+myAgent.getLocalName()+" and i is "+GM);

CK1=true;

double current2= arrivingDis(ve11.position,1);

double Rtime=(ptime1-SumoCom.currentSimStep);

double ss=((double)current2/Rtime)*1000;

ve11.setSpeed(ss-1);

}

else if ((MyGroupID.equals("NO")||MyGroupID.equals(SG))&&!repeat3.contains(Headt3)){

conter++;

repeat3.add(Headt3);

}

}

else

block();

}

}

else{

myAgent.removeBehaviour(this);

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}

}





}