gt: 						gain += dec[k].getGain();
context: 
  /*-----------------------------------------------------------------------
   * Copyright (C) 2001 Green Light District Team, Utrecht University
   *
   * This program (Green Light District) is free software.
   * You may redistribute it and/or modify it under the terms
   * of the GNU General Public License as published by
   * the Free Software Foundation (version 2 or later).
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty
   * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
   * See the GNU General Public License for more details.
   * See the documentation of Green Light District for further information.
   *------------------------------------------------------------------------*/
  
  package com.github.cc007.trafficlights.algo.tlc;
  
  import com.github.cc007.trafficlights.algo.tlc.*;
  import com.github.cc007.trafficlights.infra.*;
  import com.github.cc007.trafficlights.xml.*;
  import com.github.cc007.trafficlights.utils.*;
  import com.github.cc007.trafficlights.GLDSim;
  
  import java.io.IOException;
  import java.util.Random;
  
  /**
   *
   * This is the class for the sign controller. Here is decided how each sign should work.
   * Reward values are gathered for each sign, when set on 'Green' asking the applied TLController.
   * Non-TrafficLight signs can also be implemented when a Q-value algorithm is implemented for the appropiate
   * type of sign. After those values are gathered, this class will select the most rewarding
   * trafficlightconfiguration for each Node and will set the Signs accordingly.
   * @see gld.infra.Sign
   *
   * @author Group Algorithms
   * @version 1.0
   */
  public class SignController implements XMLSerializable
  {
  	/** The TLcontroller is used to gather Q-values and use them to set all the TLs */
  	protected TLController tlcontroller;
  	/**
  	 * Indicates if trafficlights must be switched safely or not.
  	 * Safely means no 2 trafficlights may be turned to green at the same
  	 * time, if roadusers moving from the drivelanes behind those signs
  	 * would collide on the node.
  	 */
  	public static boolean CrossNodesSafely = true;
  	protected static final String shortXMLName = "signcontroller";
  	protected Infrastructure infra;
  	protected int num_nodes;
  	//protected int[] chosenConfigs;
  	protected Sign[][] currentSC;
  	Random generator;
  
  
  	public SignController(TLController t, Infrastructure i) {
  		tlcontroller = t;
  		infra = i;
  		num_nodes = i.getNumNodes();
  		generator = new Random(GLDSim.seriesSeed[GLDSim.seriesSeedIndex]);
  		/*chosenConfigs = new int[i.getAllNodes().length];
  		for (int j=0; j<chosenConfigs.length; j++) chosenConfigs[j]=-1; // This should be done, otherwise all chosenConfigs are 0. This leads to problems, when config 0 is the first time the best config.*/
  		currentSC = new Sign[num_nodes][0];
  	}
  
  	public Infrastructure getInfrastructure() { return infra; }
  
  	public void setInfrastructure(Infrastructure i)
  	{
  		infra = i;
  		num_nodes = i.getNumNodes();
  		/*chosenConfigs = new int[i.getAllNodes().length];
  		for (int j=0; j<chosenConfigs.length; j++) chosenConfigs[j]=-1;*/
  		currentSC = new Sign[num_nodes][0];
  	}
  	public TLController getTLC() { return tlcontroller; }
  	public void setTLC(TLController t) { tlcontroller = t; }
  
  	/**
  	 * Switch all the signs to their appropiate value.
  	 */
  	public void switchSigns()
  	{
  		TLDecision[][] decisions = tlcontroller.decideTLs();
  		Node node = null;
  		Node[] nodes = infra.getAllNodes();
  		if (num_nodes>decisions.length) {
              System.out.println("SignController switchSigns WARNING : "+"Less decisions than nodes !!!");
          }
  		for (int i=0; i < num_nodes; i++) {
  			node = nodes[i];
  			if (node.getType() == Node.JUNCTION) {
  		 		if (decisions[i].length > 0) {
  					switchTrafficLights((Junction)node, decisions[i]);
  		 		}
  				else
  				if (node.getType() == Node.NON_TL )
  				{
  					switchNonTrafficLights((NonTLJunction)node, decisions[i]);
  				}
  			}
  		}
  	}
  
  	/**
  	 * Switch the the non-TLsigns to their appropiate value according to normal traffic rules
  	 * On a normal junction traffic from the right gets priority
  	 * @param node The node involved.
  	 */
  	public void switchNonTrafficLights(NonTLJunction node, TLDecision[] dec)
  	{
  		System.out.println("NonTLSwitch");
  		//Sign[][] signConfigs = node.getSignConfigs() ;
  		Sign[] signs = node.getSigns() ;
  		boolean[] mayDrive = { false, false, false, false } ;
  		//Check for traffic from the right
  		Road[] roads = node.getAllRoads(); //In clockwise order I hope/think ;)
  		Road right = roads[3] ;//Start with last road as being right
  		DriveLane[] lanes ;
  		boolean alpha = false ;
  
  		for ( int i=0 ; i<roads.length ; i++ )//length is always 4
  		{
  			alpha = right.getAlphaNode() == node ;//Check which are the incoming lanes
  			lanes = alpha ? right.getAlphaLanes() : right.getBetaLanes() ;
  			for ( int  j=0; j<lanes.length ; j++ ) {
  				if ( lanes[j].getNumRoadusersWaiting() > 0 ) {
  					mayDrive[i] = false ;//Traffic is coming from the right
  				}
  				else {
  					mayDrive[i] = true ;
  				}
  			}
  		}
  
  		Random random = new Random(GLDSim.seriesSeed[GLDSim.seriesSeedIndex]) ;
  		boolean deadlock = true ;//All roads are waiting for eachother
  		int choosenRoad = -1 ;
  		//length is always 4
  		for (int i=0; i<roads.length; i++)	{
  			if(deadlock && mayDrive[i])	{
  				deadlock = false ;
  				choosenRoad = i ;
  			}
  		}
  		if (deadlock)	{
  			choosenRoad = (int)Math.floor(random.nextFloat()*4) ;
  		}
  		//Now switch the lights
  		lanes = null ;
  		try {
  			lanes = node.getInboundLanes() ;
  		}
  		catch (InfraException e) {
  			System.out.println("An error occured while setting non-Traffic lights") ;
  		}
  
  		for ( int i=0 ; i<lanes.length ; i++ ) {
  			if ( lanes[i].getRoad() == roads[i] ) {
  				signs[i].setState( true ) ;
  			}
  			else {
  				signs[i].setState( false ) ;
  			}
  		}
  	}
  
  	/**
  	 * Switch the the TLsigns to their appropiate value
  	 * The config with highest total gain is chosen
  	 * @param dec The decision array consists of the generated values.
  	 */
  	public void switchTrafficLights(Junction node, TLDecision[] dec)
  	{	//System.out.println("Switching TL's on junction "+node.getId());
  		Sign[][] signConfs = node.getSignConfigs();
  		Sign[] signs = node.getSigns();
  
  		int num_sc = signConfs.length;
  
  		Sign[][] possibleSC = new Sign[num_sc][];
  		int p_index = 0;
  
  		float maxGain = Float.MIN_VALUE;
  		float gain;
  
  		Sign[] thisSC;
  		int num_thissc;
  
  		int num_dec = dec.length;
  
  		for (int i=0; i < num_sc; i++) {
  			gain = 0;
  			thisSC = signConfs[i];
  			num_thissc = thisSC.length;
  
  			// Summation of all gains in this SignConfig
  			for (int j=0; j < num_thissc; j++) {
  				for (int k=0; k < num_dec; k++) {
  					if (dec[k].getTL() == thisSC[j]) {
  