Example #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;
	}
Example #2
0
	protected void recalcQ(Sign tl, int pos, Node destination, boolean light, Sign tl_new, int pos_new, PosMov[] posMovs, int Ktl)
	{
		/* The calculation of the Q values in TC-3 */
		float newQvalue = qa_table[tl.getId()][pos][destination.getId()][light?green_index:red_index];
		float V=0;

// Waarom splitst TC2 wel op rood/groen, en TC3 niet??
		CountEntry currentsituation = new CountEntry (tl, pos, destination, light, tl_new, pos_new, Ktl);		
		Enumeration e = pKtl_table[tl.getId()][pos][destination.getId()].elements();
		
		while(e.hasMoreElements()) {
			PKtlEntry P = (PKtlEntry) e.nextElement();
			if(P.sameSourceKtl(currentsituation) != -1.0f) {
				try {
					V = v_table[P.tl_new.getId()][P.pos_new][destination.getId()];
				}
				catch (Exception excep) {
					System.out.println("ERROR in q");
				}
// Moet er hier geen reward functie??				
				newQvalue += P.getValue() *gamma * V;
			}
		}
		
		q_table[tl.getId()][pos][destination.getId()][light?green_index:red_index] = newQvalue; //sign, pos, des, color (red=0, green=1)
	}
Example #3
0
	protected void recalcV(Sign tl, int pos, Node destination, boolean light, int Ktl)
	{
		/* The calculation of the V values in TC-3 */
		float newVvalue;
		float tempSumGreen=0, tempSumRed=0;
		float V;
		int[] amount = count(tl, pos, destination);
		int tlId = tl.getId();
		int desId = destination.getId();
		float total = (float) amount[green_index] + (float) amount[red_index];

		newVvalue = va_table[tl.getId()][pos][destination.getId()];
		
		CountEntry currentsituation_green = new CountEntry (tl, pos, destination, green, tl, pos, Ktl);
		CountEntry currentsituation_red = new CountEntry (tl, pos, destination, red, tl, pos, Ktl);
		
		Enumeration e = pKtl_table[tlId][pos][desId].elements();
		
		while(e.hasMoreElements()) {
			//Green part
			PKtlEntry P = (PKtlEntry) e.nextElement();
			
			if(P.sameSourceKtl(currentsituation_green) != -1) {
				try {				
					V = v_table[P.tl_new.getId()][P.pos_new][destination.getId()];
					tempSumGreen += P.getValue() *gamma * V;
				}
				catch (Exception excep) {
					System.out.println(excep+"");
					excep.printStackTrace();
				}
			}
			//Red Part
			if(P.sameSourceKtl(currentsituation_red) != -1) {
				try {				
					V = v_table[P.tl_new.getId()][P.pos_new][destination.getId()];
					tempSumRed += P.getValue() *gamma * V;
				}
				catch (Exception excep) {
					System.out.println("ERROR in recalc V2");
					System.out.println(excep+"");
					excep.printStackTrace();
				}
			}
		}
		
		newVvalue += ((float)amount[green_index]/ (float)total) * tempSumGreen + ((float)amount[red_index]/ (float)total) * tempSumRed;
		try {
			v_table[tl.getId()][pos][destination.getId()] = newVvalue;
		}
		catch (Exception excep) {
			System.out.println("Error in v");
		}
	}
Example #4
0
	protected void recalcP(Sign tl, int pos, Node destination, boolean light, Sign tl_new, int pos_new, int Ktl)
	{
		int tlId = tl.getId();
		int desId = destination.getId();
		//Update the count table
		CountEntry currentsituation = new  CountEntry(tl, pos, destination, light, tl_new, pos_new, Ktl);
		int count_index = count[tlId][pos][desId].indexOf(currentsituation);
		if (count_index>=0) {
			currentsituation = (CountEntry) count[tlId][pos][desId].elementAt(count_index);
			currentsituation.incrementValue();
		}
		else {
			count[tlId][pos][desId].add(currentsituation);
		}
		//Update the p_table
		PEntry currentchance = new PEntry(tl, pos, destination, light, tl_new, pos_new);		
		
		int dest=0, source=0;
		
		Enumeration enum = count[tlId][pos][desId].elements();
		while(enum.hasMoreElements()) {
			CountEntry current = (CountEntry) enum.nextElement();
			dest += current.sameSourceDifferentKtl(currentsituation);
			source += current.sameSource(currentsituation);
		}
		
		if(source == 0) currentchance.setValue(0);
		else currentchance.setValue((float)dest/(float)source);
		
		int p_index = p_table[tlId][pos][desId].indexOf(currentchance);
		if(p_index>=0) p_table[tlId][pos][desId].setElementAt(currentchance, p_index);
		else { 
			p_table[tlId][pos][desId].add(currentchance);
			p_index = p_table[tlId][pos][desId].indexOf(currentchance);
		}
		
		// Change the rest of the p_table, Also check the other chances for updates
		int size = p_table[tlId][pos][desId].size()-1;
		for(; size>=0; size--) {
			PEntry P = (PEntry) p_table[tlId][pos][desId].elementAt(size);
			float pvalue = P.sameSource(currentsituation);
			if(pvalue > -1.0f) {
				if(size != p_index)
					P.setValue(pvalue * (float)(source-1) / (float)source);
			}
		}

		//update the p'_table ......		
		PKtlEntry currentchance2 = new PKtlEntry(tl, pos, destination, light, tl_new, pos_new, Ktl);
		source=0;

		enum = count[tlId][pos][desId].elements();
		while(enum.hasMoreElements()) {
			source += ((CountEntry) enum.nextElement()).sameSourceWithKtl(currentsituation);
		}
		
		dest = currentsituation.getValue();
		if(source == 0) currentchance2.setValue(0);
		else currentchance2.setValue((float)dest/(float)source);
	
		p_index = pKtl_table[tlId][pos][desId].indexOf(currentchance2);
		if(p_index>=0) pKtl_table[tlId][pos][desId].setElementAt(currentchance2, p_index);
		else {
			pKtl_table[tlId][pos][desId].add(currentchance2);
			p_index = pKtl_table[tlId][pos][desId].indexOf(currentchance2);
		}
		
		// Change the rest of the pKtl_table, Also check the other chances for updates
		size = pKtl_table[tlId][pos][desId].size()-1;
		for(; size>=0; size--) {
			PKtlEntry P = (PKtlEntry) pKtl_table[tlId][pos][desId].elementAt(size);
			float pvalue = P.sameSource(currentsituation);
			if(pvalue > -1) {
				if(size != p_index) {
					P.setValue(pvalue * (float)(source-1) / (float)source);
				}
			}
		}

		if(currentchance.getValue() >1  ||currentchance2.getValue() >1 || currentchance.getValue() <0  ||currentchance2.getValue() <0 )	System.out.println("Serious error !!!!!!!!!1");
	}