CoPark-WS

package park2;

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.core.behaviours.WakerBehaviour;

import jade.domain.DFService;

import jade.domain.FIPAAgentManagement.DFAgentDescription;

import jade.domain.FIPAAgentManagement.ServiceDescription;

import jade.domain.FIPAException;

import jade.lang.acl.ACLMessage;

import jade.lang.acl.MessageTemplate;

import jade.lang.acl.UnreadableException;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Arrays;

import java.util.Timer;

import java.util.TimerTask;

import java.util.logging.Level;

import java.util.logging.Logger;

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;

double dis1,dis2;

int DIM=120;

int pointeR=0;

Destination privat;

double prefX,prefY;

private int id;

public int delay,end,begin;

public static int numDrivers;

public boolean arrived=false;

boolean EN=true;

int FnumR,Fnumch;

ArrayList <Integer> Sum;

int m;

String Sid;

sortDestination mylist;

Destination[] FlistD;

Destination[] listD;

utility mynew;

Destination myDes;

String origin;

SumoLane enter;

Vehicle vehicle;

double partpointer;

evaluate eval;

char currentchar;

ArrayList<Destination> currentlist;

ArrayList<Integer> NH;

int map=0;

int mn;

ArrayList<Character>Visted,loser;

Character[] UTSORT1;

Character[] partfav1;

evaluate[] database1;

double compet,tgchange;

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

super();

Sum= new ArrayList <Integer>();

NH= new ArrayList <Integer>();

FlistD= new Destination[DIM];

Visted= new ArrayList<Character>();

loser= new ArrayList<Character>();

listD=new Destination[DIM];

compet=tgchange=1;

UTSORT1=new Character[20];

System.arraycopy(listDD,0,listD,0,DIM);

mn=0;

myDes=listD[0];

if(idDriver%2==0){

prefX= 0.0;

prefY=250.0;

}

else {

prefX= 250.0;

prefY=0.0;

}

partpointer=0;

try {

this.id= idDriver;

if(idDriver%3==0){

origin="vw";

}else {

if(idDriver%3==1){

origin="sn";

}

else

origin="di";

//di

}

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(myDes.getDestination());

} 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;

@Override

public void action(){

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

while(F){

try {

if(vehicle.getSpeed()>0.0){

begin=SumoCom.getCurrentSimStep();

F=false;

mylist= new sortDestination(listD,prefX,prefY,(int)partpointer);

FlistD= mylist.MinSpanningTree();

int i=0;

int j,z,max;

max=0;

partfav1=new Character[20];

database1=new evaluate[20];

while(i<DIM-1){

char char1=FlistD[i].destination.charAt(0);

eval=new evaluate();

j=i;

z=0;

char char2=FlistD[j].destination.charAt(0);

do{

Destination newslot=new Destination();

newslot=FlistD[j];

eval.consist[z]=newslot;

eval.size++;

z++;

j++;

char2=FlistD[j].destination.charAt(0);

}while(char2==char1&&j<DIM-1);

if(j==DIM-1){

Destination newslot=new Destination();

newslot=FlistD[j];

eval.consist[z]=newslot;

eval.size++;

}

eval.selfx=((eval.consist[z-1].x-eval.consist[0].x)/2)+eval.consist[0].x;

eval.selfy=eval.consist[0].y;

char newcar=char1;

eval.part=char1;

database1[max]=eval;

partfav1[max]=newcar;

max++;

i=j;

}

currentchar=partfav1[0];

// currentchar=ut3.getutiliy();

System.out.println("the value om my currnet char is "+currentchar);

myDes=database1[0].consist[0];

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

try {

vehicle.changeTarget(myDes.getDestination());


} 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 IntentionSend(this,3500));

addBehaviour(new IamPark(this,1000));

addBehaviour(new ReceiveIntention());

addBehaviour(new RecognizePark());

addBehaviour(new ReceiveAdvice());

addBehaviour(new AdviseeNot());

addBehaviour(new LocatePark());

}

@Override

protected void takeDown() {

try {

DFService.deregister(this);

}

catch (FIPAException fe) {

}

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

}

class IntentionSend extends TickerBehaviour{


private static final long serialVersionUID = -2919540587709502841L;

MessageTemplate tmp;

Position2D pos1;

int time1;

Vehicle ve1;

Position2D posi;

Destination sendingDes;

double desi,myx,myy;

ArrayList<Integer> cov;

public IntentionSend(Agent a,long t){

super(a,t);

}

@Override

public void onTick() {

ve1=SumoCom.vehicles.get(id);

try {

if(!ve1.getEdgeId().equals(myDes.destination)&&partfav1.length-partpointer>0){

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<=300.0){

cov.add(ij);

}

}

}

if(!cov.isEmpty()){

time1=SumoCom.getCurrentSimStep();

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

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{

//System.out.println(" STOP STOP STOP");

stop();

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

private class RecognizePark extends CyclicBehaviour {

/**

*

*/

private static final long serialVersionUID = -7078137609028646000L;

MessageTemplate mx2;

Character char3;

utility ut1;

boolean found;

int map2;

double localdistance,localdistance2;

SumoVehicle ve5;

public void action() {

ve5=SumoCom.vehicles.get(id);

//ve5.speed>0.0

if(!ve5.getEdgeId().equals(myDes.destination)&&partfav1.length-partpointer>0){

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);

}

// int imdex =-1;

char3=privat2.destination.charAt(0);

found=false;

for(int i=0;i<partfav1.length-partpointer;i++){

if(partfav1[i].equals(char3)){

found=true;

map2=i;

}

}

if(found){


double bound=database1[map2].size-database1[map2].pointer;

int imdex =-1;

for(int z=0;z<bound;z++)

{

if(privat2.destination.equals(database1[map2].consist[z].destination)){

imdex=z;

}

}

if (imdex>-1){

database1[map2].pointer++;

bound--;

// if(bound>0){

Character mychar=myDes.destination.charAt(0);

if(char3.equals(mychar)){

if(bound<=0){

System.out.println(" ZZZZZZZZZZZZZZZZZZZZZZZZZZZ OCCUPIED STSTUS CHANGE IN "+char3+" iam "+myAgent.getLocalName());


Visted.add(currentchar);

partpointer++;

if(partfav1.length-partpointer>0){

for(int j=map2;j<partfav1.length-partpointer;j++){

database1[j]=database1[j+1];

partfav1[j]=partfav1[j+1];;

}

ut1=new utility(partfav1,partpointer,database1,ve5.position.x,ve5.position.y,compet,tgchange);

UTSORT1=ut1.getutiliy();

System.out.println(" iam "+myAgent.getLocalName()+" my list is "+Arrays.toString(UTSORT1)+" my position is "+ve5.position.x);

currentchar=UTSORT1[0];

//map=partfav.indexOf(currentchar);

for(int i=0;i<partfav1.length-partpointer;i++){

if(partfav1[i].equals(currentchar))

map=i;

}

double bound2=database1[map].size-database1[map].pointer;

if(bound2>0){

localdistance=CalDis(database1[map].consist[0].x,ve5.position.x,database1[map].consist[0].y,ve5.position.y);

mn=0;

for(int k=1;k<bound2;k++){

localdistance2=CalDis(database1[map].consist[k].x,ve5.position.x,database1[map].consist[k].y,ve5.position.y);

if(localdistance2<localdistance){

localdistance=localdistance2;

mn=k;

}

}

myDes=database1[map].consist[mn];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

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

}

}

}

else{

//myDes=database1[map].consist[mn];

try {

vehicle.changeTarget(origin);

} catch (UnimplementedMethod ex) {

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

}

}

}

else{

for(int z=imdex;z<bound;z++)

database1[map2].consist[z]= database1[map2].consist[z+1];

// System.out.println(" WWWWWWWWWWWWW OCCUPIED STSTUS CHANGE IN size is "+bound+" iam "+myAgent.getLocalName()+" imdex is "+imdex+" "+char3);

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

// if(bound>0){

localdistance=CalDis(database1[map2].consist[0].x,ve5.position.x,database1[map2].consist[0].y,ve5.position.y);

mn=0;

for(int k=1;k<bound;k++){

localdistance2=CalDis(database1[map2].consist[k].x,ve5.position.x,database1[map2].consist[k].y,ve5.position.y);

if(localdistance2<localdistance){

localdistance=localdistance2;

mn=k;

}

}

myDes=database1[map2].consist[mn];


try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

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

}

}

}

}

if (!mychar.equals(char3)) {

if(bound<=0)

{

Visted.add(char3);

// allocator++;

partpointer++;

// nme++;

for(int j=map2;j<partfav1.length-partpointer;j++){

database1[j]=database1[j+1];

partfav1[j]=partfav1[j+1];;

}

}

else{

for(int z=imdex;z<bound;z++)

database1[map2].consist[z]= database1[map2].consist[z+1];

// System.out.println(" WWWWWWWWWWWWW OCCUPIED STSTUS CHANGE IN size is "+bound+" iam "+myAgent.getLocalName()+" imdex is "+imdex+" "+char3);

}

}

// }

}

}

}


else

block();

}

else{

myAgent.removeBehaviour(this);

}

}

}


private class ReceiveIntention extends CyclicBehaviour {

/**

*

*/

private static final long serialVersionUID = -1167565660520566700L;

MessageTemplate mx;

Position2D pos;

int time2;

double myx1,myy1;

double dis11,dis22;

int parkN;

double localdistance3,localdistance4;

// int mno=0;

utility ut2;

//boolean change,change1;

temp T1;

Vehicle ve2;

@Override

public void action() {

ve2=SumoCom.vehicles.get(id);

try {

try {

if(!ve2.getEdgeId().equals(myDes.destination)&&partfav1.length-partpointer>0){

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);

}

String[] SENDER1,token2;

SENDER1=new String[2];

SENDER1 = msgg.getSender().getLocalName().trim().split("#");

String Id = SENDER1[1].trim();

boolean exist=false;

Integer SENDERId = Integer.parseInt(Id);

if(!NH.contains(SENDERId)){

exist =true;

NH.add(SENDERId);

}

String recived=privat.getDestination();

String park=new String(),lr=new String();

token2=new String[2];

if(recived.charAt(0)=='p'){

lr="p";


park=recived.substring(2);

}


else{

token2=recived.trim().split("p");

park=token2[1].trim();

lr=token2[0].trim();

}

int N9;

switch (lr) {

case "a": N9=1;

break;

case "b": N9=2;

break;

case "z": N9=3;

break;

case "d": N9=4;

break;

case "e": N9=5;

break;

case "f": N9=6;

break;

case "g": N9=7;

break;

case "y": N9=8;

break;

case "i": N9=9;

break;

case "j": N9=10;

break;

case "k": N9=11;

break;

case "l": N9=12;

break;

case "m": N9=13;

break;

case "n": N9=14;

break;

case "o": N9=15;

break;

case "p": N9=16;

break;

case "q": N9=17;

break;

case "r": N9=18;

break;

case "s": N9=19;

break;

case "t": N9=20;

break;

case "u": N9=21;

break;

case "v": N9=22;

break;

case "w": N9=23;

break;

case "x": N9=24;

break;

default: N9=0;

break;

}

parkN = Integer.parseInt(park);

Integer KEY;

KEY=(SENDERId)*100+N9;


boolean found2=false;

int map3=-1;

Character c = lr.charAt(0);


if(!Sum.contains(KEY)){

Sum.add(KEY);

for(int i=0;i<partfav1.length-partpointer;i++){

if(partfav1[i].equals(c)){

found2=true;

map3=i;

}

}

if(found2){

database1[map3].demand++;


}


if(Visted.contains(c)&&partfav1.length-partpointer>23) {

String CON=new String();

char coma=',';

StringBuilder collect=new StringBuilder();

collect.append(c);

collect.append(coma);

if(partfav1.length-partpointer>=3){

collect.append(UTSORT1[1]);

collect.append(coma);

collect.append(UTSORT1[2]);

collect.append(coma);

}

else{

collect.append(UTSORT1[1]);

collect.append(coma);

collect.append('-');

collect.append(coma);

}

collect.append('-');

//collect.append(partfav.get(3));

CON=collect.toString();

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

MM.setContent(CON);

MM.setConversationId("OFFER");

MM.addReceiver(msgg.getSender());

myAgent.send(MM);

}

}

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

time2=privat.time;

T1=ve2.cache.get(time2);

// if(T1==null)

pos=T1.Position;

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

pos=ve2.position;

myx1=pos.x;

myy1=pos.y;

int mp6=-1;

for(int i=0;i<partfav1.length-partpointer;i++)

if(partfav1[i].equals(myDes.destination.charAt(0)))

mp6=i;

{

database1[mp6].cmp++;

compet++;

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

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

// System.out.println(" the value of my distance is "+dis11+" the value of recived distance is "+dis22+" iam "+getLocalName());


if(dis11>=dis22)

{

mn=-1;

double bound3=database1[mp6].size-database1[mp6].pointer;

for(int z=0;z<bound3;z++)

if(database1[mp6].consist[z].destination.equals(privat.destination))

mn=z;

// System.out.println(" the destination is changed to becuase of "+msgg.getSender().getLocalName()+" ian "+myAgent.getLocalName());

database1[mp6].pointer++;

tgchange++;

bound3--;


for(int zi=mn;zi<bound3;zi++)

database1[mp6].consist[zi]= database1[mp6].consist[zi+1];

//database1[map3].consist.remove(mn);

if(bound3>0){

mn=0;

localdistance3=CalDis(database1[mp6].consist[0].x,ve2.position.x,database1[mp6].consist[0].y,ve2.position.y);

for(int k=1;k<bound3;k++){

localdistance4=CalDis(database1[mp6].consist[k].x,ve2.position.x,database1[mp6].consist[k].y,ve2.position.y);

if(localdistance4<localdistance3){

localdistance3=localdistance4;

mn=k;

}

}

myDes=database1[mp6].consist[mn];


try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

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

}

}

else{

partpointer++;

if(partfav1.length-partpointer>0){

loser.add(partfav1[mp6]);

for(int j=mp6;j<partfav1.length-partpointer;j++){

database1[j]=database1[j+1];

partfav1[j]=partfav1[j+1];;

}

System.out.print(" Hi "+myAgent.getLocalName()+" my demand is ");

for(evaluate fg:database1)

System.out.print(fg.demand+" "+fg.part+" + ");

System.out.println("==========="+myDes.destination.charAt(0));

ut2=new utility(partfav1,partpointer,database1,ve2.position.x,ve2.position.y,compet,tgchange);

UTSORT1=ut2.getutiliy();

System.out.println(" iam "+myAgent.getLocalName()+" my list is "+Arrays.toString(UTSORT1)+" my position is "+ve2.position.x);

currentchar=UTSORT1[0];

for(int i=0;i<partfav1.length-partpointer;i++){

if(partfav1[i].equals(currentchar))

map=i;

}

//map=partfav.indexOf(currentchar);

double bound4=database1[map].size-database1[map].pointer;

if(bound4>0){

localdistance3=CalDis(database1[map].consist[0].x,ve2.position.x,database1[map].consist[0].y,ve2.position.y);

mn=0;

for(int k=1;k<bound4;k++){

localdistance4=CalDis(database1[map].consist[k].x,ve2.position.x,database1[map].consist[k].y,ve2.position.y);

if(localdistance4<localdistance3){

localdistance3=localdistance4;

mn=k;

}

}

// System.out.println("next cluster destination is "+database.get(currentchar).consist.get(mno).destination+" with current size is a "+database.get(currentchar).consist.size()+" the current char is "+currentchar+" iam "+ myAgent.getLocalName());

myDes=database1[map].consist[mn];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

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

}

}

}

else{

//myDes=database1[map].consist[mn];

try {

vehicle.changeTarget(origin);

} catch (UnimplementedMethod ex) {

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

}

}

}

}

}

}

else{

block();

}

}

else{

// System.out.println(" TERMINATE RECEIVING BEHAVIOUR ");

myAgent.removeBehaviour(this);

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (NumberFormatException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

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;

}


private class LocatePark extends SimpleBehaviour {

/**

*

*/

private static final long serialVersionUID = -2639002170028439905L;

result output;

boolean C=false;

int counter=0;

Vehicle ve4;

// SumoVehicle ve44;

@Override

public void action(){

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

ve4=vehicle;

try {

if(ve4.getEdgeId().equals(myDes.getDestination())&&partfav1.length-partpointer>0){

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);

}

// }

counter++;

if(counter==1)

{

end=SumoCom.getCurrentSimStep();

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

{

if(ve4.getEdgeId().equals(origin)&&partfav1.length-partpointer<=0){

// }

counter++;

if(counter==1)

{


end=SumoCom.getCurrentSimStep();

delay=end-begin;

try {

output=new result();

} catch (IOException ex) {

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

}

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

myAgent.doDelete();

}

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

//myAgent.doDelete();

}

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

@Override

public boolean done(){

return C;

}

}

private class IamPark extends TickerBehaviour {

/**

*

*/

private static final long serialVersionUID = 1855878952337101620L;

SumoVehicle ve3;

Position2D posi2;

ArrayList<Integer> cov2;

double desi2,xvalue,yvalue;

Position2D pp;

Destination sendingDes2;

public IamPark(Agent a,long t){

super(a,t);

}

protected void onTick() {

ve3=SumoCom.vehicles.get(id);

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<=60.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 ReceiveAdvice extends CyclicBehaviour {


private static final long serialVersionUID = 3843825647082669218L;

Vehicle ve10;

double localdistance5,localdistance6;

char partprop;

int offerid;

int number;

utility ut5;

Character mypart;

proposal prop1;

int mp7=0;

//ArrayList<proposal> PROP=new ArrayList<proposal>();

//int fav,i;

String[] token=new String[4];

public void action() {

ve10=SumoCom.vehicles.get(id);

try {

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

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

MessageTemplate.MatchPerformative(ACLMessage.INFORM));

//MessageTemplate.MatchPerformative(ACLMessage.CFP);

ACLMessage msg = myAgent.receive(mt);

if (msg != null) {

//PROP=new ArrayList<proposal>();

String title = msg.getContent();

token=title.trim().split(",");

String park=token[0].trim();

mypart=park.charAt(0);

for(int i=0;i<partfav1.length-partpointer;i++)

if(partfav1[i].equals(myDes.destination.charAt(0)))

mp7=i;

if(mypart.equals(currentchar)){

//if(database1[mp7].cmp==0){

Visted.add(partfav1[mp7]);

partpointer++;

for(int j=mp7;j<partfav1.length-partpointer;j++){

database1[j]=database1[j+1];

partfav1[j]=partfav1[j+1];;

}

prop1=new proposal();

int z=1;

partprop=token[z].trim().charAt(0);

while(partprop!='-'){

number=0;

while(number<partfav1.length-partpointer&&!partfav1[number].equals(partprop)){

number++;

}

if(number<partfav1.length-partpointer){

prop1.offer.add(partprop);

}

else{

System.out.println("HI HI HI THI INFORCHANT "+z+"*******************************"+msg.getSender().getLocalName());

ACLMessage MM=new ACLMessage(ACLMessage.INFORM);

String CON="thank but-"+partprop+"-Occupied";

MM.setContent(CON);

MM.setConversationId("cancel");

MM.addReceiver(msg.getSender());

myAgent.send(MM);

}

z++;

partprop=token[z].trim().charAt(0);

}

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

if(!prop1.offer.isEmpty()){

currentchar=prop1.offer.get(0);

offerid=-1;

System.out.println("HI HI HI THANK THANK THANK*******************************");

for(int i=0;i<partfav1.length-partpointer;i++)

if(partfav1[i].equals(currentchar)){

offerid=i;

}

map=offerid;

//PROP.clear();

double bound6=database1[map].size-database1[map].pointer;

localdistance5=CalDis(database1[map].consist[0].x,ve10.position.x,database1[map].consist[0].y,ve10.position.y);

mn=0;

for(int k=1;k<bound6;k++){

localdistance6=CalDis(database1[map].consist[k].x,ve10.position.x,database1[map].consist[k].y,ve10.position.y);

if(localdistance6<localdistance5){

localdistance5=localdistance6;

mn=k;

}

}

// System.out.println("next cluster destination is "+database.get(currentchar).consist.get(mno).destination+" with current size is a "+database.get(currentchar).consist.size()+" the current char is "+currentchar+" iam "+ myAgent.getLocalName());

myDes=database1[map].consist[mn];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

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

}

}

else{

System.out.println(" ALREADY IS REMOVED "+myAgent.getLocalName());

ut5=new utility(partfav1,partpointer,database1,ve10.position.x,ve10.position.y,compet,tgchange);

UTSORT1=ut5.getutiliy();

currentchar=UTSORT1[0];

// int mp7=0;

for(int i=0;i<partfav1.length-partpointer;i++)

if(partfav1[i].equals(currentchar))

mp7=i;

double bound6=database1[mp7].size-database1[mp7].pointer;

localdistance5=CalDis(database1[mp7].consist[0].x,ve10.position.x,database1[mp7].consist[0].y,ve10.position.y);

mn=0;

for(int k=1;k<bound6;k++){

localdistance6=CalDis(database1[mp7].consist[k].x,ve10.position.x,database1[mp7].consist[k].y,ve10.position.y);

if(localdistance6<localdistance5){

localdistance5=localdistance6;

mn=k;

}

}

// System.out.println("next cluster destination is "+database.get(currentchar).consist.get(mno).destination+" with current size is a "+database.get(currentchar).consist.size()+" the current char is "+currentchar+" iam "+ myAgent.getLocalName());

myDes=database1[mp7].consist[mn];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

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

}

}

// }

}

}

else

block();

}

else

myAgent.removeBehaviour(this);

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


private class AdviseeNot extends CyclicBehaviour {


/**

*

*/

private static final long serialVersionUID = 2965006172552026401L;

Vehicle ve11;

//double localdistance7,localdistance8;

String[] mody=new String[3];

public void action() {

ve11=SumoCom.vehicles.get(id);

try {

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

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

MessageTemplate.MatchPerformative(ACLMessage.INFORM));

ACLMessage msg = myAgent.receive(mt);

if (msg != null) {

//PROP=new ArrayList<proposal>();

String title2 = msg.getContent();

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

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

int mp10=0;

boolean right=false;

for(int i=0;i<partfav1.length-partpointer;i++)

if(partfav1[i].equals(park)){

mp10=i;

right=true;

}

if(right){

System.out.println(" ###################+++++++++++++++++++++++++++TROUBLE____%%%%%%%%%%%%%%%%%%%%%%%%***********************");

partpointer++;

for(int j=mp10;j<partfav1.length-partpointer;j++){

database1[j]=database1[j+1];

partfav1[j]=partfav1[j+1];;

}


}

}

else

block();

}

else

myAgent.removeBehaviour(this);

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

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

private class Intialization extends CyclicBehaviour {

private static final long serialVersionUID = 1301400729516438184L;

// Position2D pos7;

//SumoVehicle ve7;

boolean F=true;

@Override

public void action(){

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

while(F){

try {

if(vehicle.getSpeed()>0.0){

begin=SumoCom.getCurrentSimStep();

F=false;

mylist= new sortDestination(listD,prefX,prefY,(int)partpointer);

FlistD= mylist.MinSpanningTree();

// for(int i=0;i<80;i++)

// System.out.println(FlistD[i].destination);

int i=0;

int j,z,max;

max=0;

partfav1=new Character[16];

database1=new evaluate[16];

while(i<DIM-1){

char char1=FlistD[i].destination.charAt(0);

eval=new evaluate();

j=i;

z=0;

char char2=FlistD[j].destination.charAt(0);

// System.out.println(" the value of char1 is "+char1);

//eval.size++;

do{

Destination newslot=new Destination();

newslot=FlistD[j];

eval.consist[z]=newslot;

eval.size++;

z++;

j++;

char2=FlistD[j].destination.charAt(0);

}while(char2==char1&&j<DIM-1);

if(j==DIM-1){

Destination newslot=new Destination();

newslot=FlistD[j];

eval.consist[z]=newslot;

eval.size++;

}

eval.selfx=((eval.consist[z-1].x-eval.consist[0].x)/2)+eval.consist[0].x;

eval.selfy=eval.consist[0].y;

char newcar=char1;

eval.part=char1;

database1[max]=eval;

partfav1[max]=newcar;

max++;

i=j;

}

System.out.println("the last slot is"+database1[15].consist[7].destination);


//ut3=new utility(partfav,partpointer,database,ve7.position.x,ve7.position.y,allocator);

currentchar=partfav1[0];

// currentchar=ut3.getutiliy();

System.out.println("the value om my currnet char is "+currentchar);

myDes=database1[0].consist[0];

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

try {

vehicle.changeTarget(myDes.getDestination());


} catch (UnimplementedMethod ex) {

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

}

}

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}

}

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


private class RESPOND extends CyclicBehaviour {

/**

*

*/

private static final long serialVersionUID = -1094850519731729557L;

Vehicle ve13;

// double XC,YC;

double localdistance77,localdistance88;

String[] mody=new String[2];

public void action() {

ve13=SumoCom.vehicles.get(id);

try {

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

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

MessageTemplate.MatchPerformative(ACLMessage.INFORM));

ACLMessage msg = myAgent.receive(mt);

if (msg != null) {

//localdistance77=CalDis(ve12.position.x,myDes.x,ve12.position.y,myDes.y);

//PROP=new ArrayList<proposal>();

String title2 = msg.getContent();

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

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

Character pk=park.charAt(0);

if(!loser.contains(pk)||!Visted.contains(pk)){

int mp10=0;

boolean right=false;

for(int i=0;i<partfav1.length-partpointer;i++)

if(partfav1[i].equals(pk)){

mp10=i;

right=true;

}

if(right){

loser.add(pk);

partpointer++;

for(int j=mp10;j<partfav1.length-partpointer;j++){

database1[j]=database1[j+1];

partfav1[j]=partfav1[j+1];;

}

utility ut8;

ut8=new utility(partfav1,partpointer,database1,ve13.position.x,ve13.position.y,compet,tgchange);

UTSORT1=ut8.getutiliy();

currentchar=UTSORT1[0];

int map3o =-1;

for(int i=0;i<partfav1.length-partpointer;i++)

if(partfav1[i].equals(currentchar))

map3o=i;

double bound6=database1[map3o].size-database1[map3o].pointer;

localdistance77=CalDis(database1[map3o].consist[0].x,ve13.position.x,database1[map3o].consist[0].y,ve13.position.y);

mn=0;

for(int k=1;k<bound6;k++){

localdistance88=CalDis(database1[map3o].consist[k].x,ve13.position.x,database1[map3o].consist[k].y,ve13.position.y);

if(localdistance88<localdistance77){

localdistance77=localdistance88;

mn=k;

}

}

System.out.println("??????????????????????????????????? THANK????????????????????????????????????????????????????");

myDes=database1[map3o].consist[mn];

try {

vehicle.changeTarget(myDes.getDestination());

} catch (UnimplementedMethod ex) {

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

}


}

}




}

else

block();

}

else

myAgent.removeBehaviour(this);

} catch (UnimplementedMethod e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}


}