Exemplo n.º 1
0
  /**
   * The constructor for TL controllers
   *
   * @param The model being used.
   */
  public SL1TLC(Infrastructure infra) throws InfraException {
    super(infra);
    Node[] nodes =
        infra.getAllNodes(); // Moet Edge zijn eigenlijk, alleen testSimModel knalt er dan op
    int num_nodes = nodes.length;

    count = new Vector();

    int numSigns = infra.getAllInboundLanes().size();
    q_table = new float[numSigns + 1][][][];
    int num_specialnodes = infra.getNumSpecialNodes();
    for (int i = 0; i < nodes.length; i++) {
      Node n = nodes[i];
      Drivelane[] dls = n.getInboundLanes();
      for (int j = 0; j < dls.length; j++) {
        Drivelane d = dls[j];
        Sign s = d.getSign();
        int id = s.getId();
        int num_pos_on_dl = d.getCompleteLength();
        q_table[id] = new float[num_pos_on_dl][][];
        for (int k = 0; k < num_pos_on_dl; k++) {
          q_table[id][k] = new float[num_specialnodes][];
          for (int l = 0; l < q_table[id][k].length; l++) {
            q_table[id][k][l] = new float[2];
            q_table[id][k][l][0] = 0.0f;
            q_table[id][k][l][1] = 0.0f;
          }
        }
      }
    }
    System.out.println("Startet med Alpha = " + alpha);

    random_number = new Random();
  }
Exemplo n.º 2
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;
  }
Exemplo 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, 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;
	}
Exemplo n.º 4
0
	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);
		}
	}
Exemplo n.º 5
0
	public void setInfrastructure( Infrastructure infra )
	{	super.setInfrastructure(infra);
	
		Node[] nodes = infra.getAllNodes();
		num_nodes = nodes.length;
	
		try{
			int numSigns = infra.getAllInboundLanes().size();
		
			q_table 	= new float [numSigns][][][];
			qa_table	= new float [numSigns][][][];
			v_table 	= new float [numSigns][][];
			va_table	= new float [numSigns][][];
			count		= new Vector[numSigns][][];
			p_table 	= new Vector[numSigns][][];
			pKtl_table	= new Vector[numSigns][][];
			
			int num_specialnodes = infra.getNumSpecialNodes();

			for (int i=0; i<num_nodes; i++) {
				Node n = nodes[i];
				Drivelane[] dls = dls = n.getInboundLanes();
				int num_dls = num_dls = dls.length;
				// huh?
				Drivelane [] lanes = new Drivelane[numSigns];
				infra.getAllInboundLanes().copyInto(lanes);
				
				for (int j=0; j<num_dls; j++) {
					Drivelane d = dls[j];
					Sign s = d.getSign();
					int id = d.getId();
					int num_pos_on_dl = d.getCompleteLength();

					q_table[id]		= new float [num_pos_on_dl][][];
					qa_table[id]	= new float [num_pos_on_dl][][];
					v_table[id]		= new float [num_pos_on_dl][];
					va_table[id]	= new float [num_pos_on_dl][];
					count[id]		= new Vector[num_pos_on_dl][];
					p_table[id]		= new Vector[num_pos_on_dl][];
					pKtl_table[id]	= new Vector[num_pos_on_dl][];
					
					for (int k=0; k<num_pos_on_dl; k++) {
						q_table[id][k]	= new float[num_specialnodes][2];
						qa_table[id][k]	= new float[num_specialnodes][2];
						v_table[id][k]	= new float[num_specialnodes];
						va_table[id][k]	= new float[num_specialnodes];
						count[id][k]	= new Vector[num_specialnodes];
						p_table[id][k]	= new Vector[num_specialnodes];
						pKtl_table[id][k] =  new Vector[num_specialnodes];
						
						for (int l=0; l<num_specialnodes;l++) {
							q_table[id][k][l][0]	= 0.0f;
							q_table[id][k][l][1]	= 0.0f;
							qa_table[id][k][l][0]	= 0.0f;
							qa_table[id][k][l][1]	= 0.0f;
							v_table[id][k][l]		= 0.0f;
							va_table[id][k][l]		= 0.0f;
							count[id][k][l]			= new Vector();
							p_table[id][k][l]		= new Vector();
							pKtl_table[id][k][l]	= new Vector();
						}
					}
				}
			} 
			random_number = new Random();
		}
		catch(Exception e) { System.out.println("Error."); }
	}