Ejemplo n.º 1
0
	public float getColearnValue(Sign sign_new, Sign sign, Node destination, int pos)
	{
		int Ktl = sign.getLane().getNumRoadusersWaiting();
		int tlId = sign.getId();
		int desId = destination.getId();
	
		// Calculate the colearning value
		float newCovalue=0;
		int size = sign.getLane().getCompleteLength()-1;

		for(; size>=0; size--) {
			float V;
			PKtlEntry P = new PKtlEntry(sign, 0, destination, green, sign_new, size, Ktl);
			int p_index = pKtl_table[tlId][pos][desId].indexOf(P);
			
			if(p_index>=0) {
				try {
					P = (PKtlEntry) pKtl_table[tlId][pos][desId].elementAt(p_index);
					V = v_table[tlId][size][desId];
					newCovalue += P.getValue() * V;
				}
				catch (Exception e) {
					System.out.println("Error");
				}
			}
		}
		return newCovalue;
	}
Ejemplo n.º 2
0
	protected void recalcQa(Sign tl, int pos, Node destination, boolean light, Sign tl_new, int pos_new, PosMov[] posMovs)
	{
		float newQvalue=0;
		int size = tl.getLane().getCompleteLength()-1;
		int R;
		int tlId = tl.getId();
		int desId = destination.getId();
		float Va;

		for(; size>=0; size--) {
			PEntry P = new PEntry(tl, pos, destination, light, tl, size);

			int p_index = p_table[tlId][pos][desId].indexOf(P);
			if(p_index>=0) {
				try {
					P = (PEntry) p_table[tlId][pos][desId].elementAt(p_index);
					Va = va_table[tlId][size][desId];	
					R = rewardFunction(tl_new, pos_new, posMovs);
					newQvalue += P.getValue() *(((float)R) + gamma * Va);
				}
				catch (Exception e) {
					System.out.println("Error in recalc Q'");
				}
			}
		}

		try {
			qa_table[tl.getId()][pos][destination.getId()][light?green_index:red_index] = newQvalue;
		}
		catch (Exception e) {
			System.out.println("ERROR, Zwaluw is not found");
		}
	}
Ejemplo n.º 3
0
  /**
   * 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;
  }
Ejemplo n.º 4
0
	/**
	* 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;
	}