/** * Calculates how every traffic light should be switched Per node, per sign the waiting roadusers * are passed and per each roaduser the gain is calculated. * * @param The TLDecision is a tuple consisting of a traffic light and a reward (Q) value, for it * to be green * @see gld.algo.tlc.TLDecision */ public TLDecision[][] decideTLs() { int num_dec; int num_tld = tld.length; // Determine wheter it should be random or not boolean do_this_random = false; if (random_number.nextFloat() < random_chance) do_this_random = true; for (int i = 0; i < num_tld; i++) { num_dec = tld[i].length; for (int j = 0; j < num_dec; j++) { Sign currenttl = tld[i][j].getTL(); float gain = 0; Drivelane currentlane = currenttl.getLane(); int waitingsize = currentlane.getNumRoadusersWaiting(); ListIterator queue = currentlane.getQueue().listIterator(); if (!do_this_random) { for (; waitingsize > 0; waitingsize--) { Roaduser ru = (Roaduser) queue.next(); int pos = ru.getPosition(); Node destination = ru.getDestNode(); gain += q_table[currenttl.getId()][pos][destination.getId()][1] - q_table[currenttl.getId()][pos][destination.getId()][0]; // red - green } float q = gain; } else gain = random_number.nextFloat(); tld[i][j].setGain(gain); } } return tld; }
public void updateRoaduserMove( Roaduser ru, Drivelane prevlane, Sign prevsign, int prevpos, Drivelane dlanenow, Sign signnow, int posnow, PosMov[] posMovs, Drivelane desired) { // When a roaduser leaves the city; this will if (dlanenow == null || signnow == null) { dlanenow = prevlane; signnow = prevsign; posnow = -1; return; // ?? is recalculation is not necessary ?? } // This ordening is important for the execution of the algorithm! if (prevsign.getType() == Sign.TRAFFICLIGHT && (signnow.getType() == Sign.TRAFFICLIGHT || signnow.getType() == Sign.NO_SIGN)) { Node dest = ru.getDestNode(); recalcQ( prevsign, prevpos, dest, prevsign.mayDrive(), signnow, posnow, signnow.mayDrive(), posMovs); } }
/** * Calculates how every traffic light should be switched * Per node, per sign the waiting roadusers are passed and per each roaduser the gain is calculated. * @param The TLDecision is a tuple consisting of a traffic light and a reward (Q) value, for it to be green * @see gld.algo.tlc.TLDecision */ public TLDecision[][] decideTLs() { int num_dec, currenttlID, waitingsize, pos, destID; float gain =0, passenger_factor; Sign currenttl; Drivelane currentlane; ListIterator queue; Roaduser ru; //Determine wheter it should be random or not boolean randomrun = false; if (random_number.nextFloat() < random_chance) randomrun = true; for (int i=0;i<num_nodes;i++) { num_dec = tld[i].length; for(int j=0;j<num_dec;j++) { currenttl = tld[i][j].getTL(); currenttlID = currenttl.getId(); currentlane = currenttl.getLane(); waitingsize = currentlane.getNumRoadusersWaiting(); queue = currentlane.getCompleteQueue().listIterator(); gain = 0; for(; waitingsize>0; waitingsize--) { ru = (Roaduser) queue.next(); pos = ru.getPosition(); destID = ru.getDestNode().getId(); passenger_factor = ru.getNumPassengers(); gain += passenger_factor * (q_table[currenttlID][pos][destID][red_index] - q_table[currenttlID][pos][destID][green_index]); //red - green } if(trackNode!=-1 && i==trackNode) { Drivelane currentlane2 = tld[i][j].getTL().getLane(); boolean[] targets = currentlane2.getTargets(); System.out.println("node: "+i+" light: "+j+" gain: "+gain+" "+targets[0]+" "+targets[1]+" "+targets[2]+" "+currentlane2.getNumRoadusersWaiting()); } if(randomrun) gain = random_number.nextFloat(); tld[i][j].setGain(gain); } } return tld; }
protected int calcDelay(Roaduser ru, int stop, int distance) { // calculate the delay for the drivelane leading to this Junction int start = ru.getDrivelaneStartTime(); int speed = ru.getSpeed(); int min_steps = distance / speed; int num_steps = stop - start; int delay = num_steps - min_steps; ru.addDelay(delay); // System.out.println("Just crossed, and waited:"+delay+" start:"+start+" stop:"+stop+" // dist:"+distance); ru.setDrivelaneStartTime(stop); // then return the delay return delay; }
public void updateRoaduserMove(Roaduser ru, Drivelane prevlane, Sign prevsign, int prevpos, Drivelane dlanenow, Sign signnow, int posnow, PosMov[] posMovs, Drivelane desired) { // Roaduser has just left the building if(dlanenow == null || signnow == null) { return; } //This ordening is important for the execution of the algorithm! int Ktl = dlanenow.getNumRoadusersWaiting(); if(prevsign.getType()==Sign.TRAFFICLIGHT && (signnow.getType()==Sign.TRAFFICLIGHT || signnow.getType()==Sign.NO_SIGN)) { boolean light = prevsign.mayDrive(); Node dest = ru.getDestNode(); recalcP(prevsign, prevpos, dest, light, signnow, posnow, Ktl); recalcVa(prevsign, prevpos, dest); recalcV(prevsign, prevpos, dest, light, Ktl); recalcQa(prevsign, prevpos, dest, light, signnow, posnow, posMovs); recalcQ(prevsign, prevpos, dest, light, signnow, posnow, posMovs, Ktl); } }