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