/* Returns whether or not the tails of the lanes not being accesible by the given array of Signs are free */ public boolean areOtherTailsFree(Sign[] mayUse) { boolean[] pos = new boolean[4]; Road[] roads = getAllRoads(); Drivelane lane; int thisRoad; boolean[] targets; for (int i = 0; i < 4; i++) pos[i] = true; int num_mayuse = mayUse.length; for (int i = 0; i < num_mayuse; i++) { lane = mayUse[i].getLane(); try { thisRoad = isConnectedAt(lane.getRoad()); } catch (InfraException e) { thisRoad = 0; System.out.println("Something went wrong in areOtherTailsFree()"); } targets = lane.getTargets(); if (targets[0]) { pos[(thisRoad + 1) % 4] = false; } else if (targets[1]) { pos[(thisRoad + 2) % 4] = false; } else if (targets[2]) { pos[(thisRoad + 3) % 4] = false; } } int num_check = 0; for (int i = 0; i < 4; i++) { if (pos[i] && roads[i] != null) { Drivelane[] check = new Drivelane[0]; try { check = roads[i].getOutboundLanes(this); } catch (Exception e) { check = new Drivelane[0]; System.out.println("Something went wrong in areOtherTailsFree() 2"); } num_check = check.length; for (int j = 0; j < num_check; j++) { if (!check[j].isTailFree()) { return false; } } } } return true; }
/* Needs Testing! */ public Drivelane[] getLanesLeadingFrom(Drivelane lane, int ruType) throws InfraException { Road road = lane.getRoad(); // Road[] which will contain the Roads of this Node in a sorted fashion: // [0] == the drivelanes on this Road will have to turn left to get to 'road', .. Road[] srt_rarr = new Road[3]; if (allRoads[0] == road) { srt_rarr[0] = allRoads[1]; // Must turn left srt_rarr[1] = allRoads[2]; // Must go straight on srt_rarr[2] = allRoads[3]; // Must turn right } else if (allRoads[1] == road) { srt_rarr[0] = allRoads[2]; srt_rarr[1] = allRoads[3]; srt_rarr[2] = allRoads[0]; } else if (allRoads[2] == road) { srt_rarr[0] = allRoads[3]; srt_rarr[1] = allRoads[0]; srt_rarr[2] = allRoads[1]; } else { srt_rarr[0] = allRoads[0]; srt_rarr[1] = allRoads[1]; srt_rarr[2] = allRoads[2]; } // System.out.println("Junction getLanesLeadingFrom "+nodeId); Vector v = new Vector(); Drivelane[] lanes; int num_lanes; int cnt_lanes = 0; boolean[] targets = lane.getTargets(); for (int i = 0; i < 3; i++) { if (srt_rarr[i] != null && targets[i] == true) { // System.out.println("Road at target:"+i+" isnt null, getting Outboundlanes"); lanes = srt_rarr[i].getOutboundLanes(this); num_lanes = lanes.length; // System.out.println("Num lanes :"+num_lanes); for (int j = 0; j < num_lanes; j++) { Drivelane l = lanes[j]; // System.out.println("Lane"+j+" being checked now. Has type:"+l.getType()); if (l.mayUse(ruType)) { v.addElement(l); cnt_lanes++; } } } } return (Drivelane[]) v.toArray(new Drivelane[cnt_lanes]); }
/** * 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; }
public Drivelane[] getLanesLeadingTo(Drivelane lane, int ruType) throws InfraException { Road road = lane.getRoad(); // Road[] which will contain the Roads of this Node in a sorted fashion: // [0] == the drivelanes on this Road will have to turn left to get to 'road', .. Road[] srt_rarr = new Road[3]; if (allRoads[0] == road) { srt_rarr[0] = allRoads[3]; // Must turn left srt_rarr[1] = allRoads[2]; // Must go straight on srt_rarr[2] = allRoads[1]; // Must turn right } else if (allRoads[1] == road) { srt_rarr[0] = allRoads[0]; srt_rarr[1] = allRoads[3]; srt_rarr[2] = allRoads[2]; } else if (allRoads[2] == road) { srt_rarr[0] = allRoads[1]; srt_rarr[1] = allRoads[0]; srt_rarr[2] = allRoads[3]; } else { srt_rarr[0] = allRoads[2]; srt_rarr[1] = allRoads[1]; srt_rarr[2] = allRoads[0]; } Vector v = new Vector(); Drivelane[] lanes; int num_lanes; int cnt_lanes = 0; boolean[] targets; for (int i = 0; i < 3; i++) { if (srt_rarr[i] != null) { lanes = srt_rarr[i].getInboundLanes(this); num_lanes = lanes.length; for (int j = 0; j < num_lanes; j++) { Drivelane l = lanes[j]; targets = l.getTargets(); if (targets[i] == true && l.mayUse(ruType)) { v.addElement(l); cnt_lanes++; } } } } return (Drivelane[]) v.toArray(new Drivelane[cnt_lanes]); }