Index: /applications/doprava/traffic_agent_cycle_time.h
===================================================================
--- /applications/doprava/traffic_agent_cycle_time.h (revision 1139)
+++ /applications/doprava/traffic_agent_cycle_time.h (revision 1141)
@@ -4,4 +4,5 @@
 const double saturated_stream = 0.5; // car/s
 const int sample_cycles = 3;
+const double min_profit = 30.0;
 
 
@@ -13,8 +14,8 @@
 public:
 	int Tc;
-	static const int minTc = 60;  // nepouziva se
-	static const int maxTc = 300; // nepouziva se
-	static const int stepTc = 8;	
-	static const int d_Tc = 2; // pocet navrhovanych delek cyklu	
+	static const int minTc = 40;  // nepouziva se
+	static const int maxTc = 200; // nepouziva se
+	static const int stepTc = 2;	
+	static const int d_Tc = 3; // pocet navrhovanych delek cyklu	
 	int idealTc;
 	int cycle_counter;
@@ -51,5 +52,5 @@
 		arr.set_length( arr.length()+1, true );
 		arr(arr.length()-1) = item;
-		cout << endl << "addin to array value " << item << endl;
+		//cout << endl << "addin to array value " << item << endl;
 	}
 
@@ -86,5 +87,8 @@
 
 	double getProfit ( double tc ) {
-		return getWT(Tc) - getWT(tc);
+		if ( tc != Tc )
+			return (getWT(Tc) - getWT(tc))*( 1/ (Tc-tc)*(Tc-tc) );
+		else
+			return 0;
 	}
 
@@ -97,15 +101,14 @@
 	}
 
-	
-	double getRelativeQueueDiff() {
-		double dq = 0;
-		for ( int i=0; i< lanehs.length(); i ++ ) {
-			dq += lanehs(i)->getRelativeQueueDiff();
-		}
-		return dq;
-	}
-
-	
-
+	double getAverageRo () {
+		double Ro_sum = 0;
+		for ( int i = 0; i <lanehs.length(); i ++ ) {
+			Ro_sum += lanehs(i)->getRo();
+		}
+		return Ro_sum/lanehs.length();
+	}
+
+	
+	
 
 	void send2neighbour( Setting &set, int i, string messageName, double messageValue ) {		
@@ -144,6 +147,7 @@
 		idealTc = Tc;
 		max_profit = 0;
-		received_profit_sum.set_length( 2*d_Tc +1 );
-		received_Tcs.set_length( 2*d_Tc +1 );
+		// prijate Tc a profity
+		received_profit_sum.set_length( (maxTc - minTc)/stepTc +1 );
+		received_Tcs.set_length( (maxTc - minTc)/stepTc +1 );
 		cycle_counter = 0;
 
@@ -191,14 +195,16 @@
 		Qs << cycle_counter;
 		for ( int i=0; i<lanehs.length(); i ++ ) {
+			lanehs(i)->inputs( 0 ) = inputs( 2*find_index(rv_inputs, lanehs(i)->rv_inputs.name(0) ) );
+			lanehs(i)->inputs( 1 ) = inputs( 2*find_index(rv_inputs, lanehs(i)->rv_inputs.name(0) ) + 1 );
 			lanehs(i)->Tc = Tc;
 			lanehs(i)->addQueueLength( queues( find_index( rv_queues, lanehs(i)->getQueueName() ) ) );
 			
-			lanehs(i)->echo();
+			//lanehs(i)->echo();
 			// pomocne logovaci soubory
-			Ros << "\t" << lanehs(i)->getRo();
-			Qs << "\t" << lanehs(i)->getAverageQueueLength();
-		}
-		Ros << endl;
-		Qs << endl;
+			Ros << ";" << lanehs(i)->getRo();
+			Qs << ";" << queues( find_index( rv_queues, lanehs(i)->getQueueName() ) )<<";"<< lanehs(i)->getAverageQueueLength()<< ";";
+		}
+		Ros <<";;"<<getAverageRo() << endl;
+		Qs <<";;"<<getQueue() << endl;
 
 		if ( cycle_counter % sample_cycles == 0 ) {
@@ -227,22 +233,23 @@
 		// sends all tcs in range +- d_tc and expected profits according to tc
 		// format: tc_index, profit_index
-		if ( n_of_broadcast == 0 && ( cycle_counter % sample_cycles == 0 ) ) {
-			cout << endl << name+" PROFITS:";
-			for ( int i = -d_Tc; i <= d_Tc; i++ ) {				
-				int time_cycle = Tc + i*stepTc;
+		if ( n_of_broadcast == 0  ) {
+			//cout << endl << name+" PROFITS:";
+			int broadcast_width = (maxTc - minTc)/stepTc + 1;
+			for ( int i = 0; i < broadcast_width; i++ ) {				
+				int time_cycle = minTc + i*stepTc;
 				double profit = getProfit(time_cycle);
-				cout << "\t" << time_cycle << " " <<profit;
+				//cout << "\t" << time_cycle << " " <<profit;
 				// send tc
 				stringstream time_cycle_stream;
-				time_cycle_stream << "tc_" << (i+d_Tc);
+				time_cycle_stream << "tc_" << (i);
 				send2allNeighbours( set, time_cycle_stream.str(), time_cycle);
 				
 				// send profit
 				stringstream profit_stream;
-				profit_stream << "profit_" << (i+d_Tc);
+				profit_stream << "profit_" << (i);
 				send2allNeighbours( set, profit_stream.str(), profit );				
 			}
 			
-			cout << endl;
+			//cout << endl;
 		}
 		
@@ -286,26 +293,37 @@
 	void act (vec &glob_ut){
 		
-		if ( cycle_counter % sample_cycles == 0 ) {
+		if ( cycle_counter >= 10  && (cycle_counter %sample_cycles) == 0 ) {
 			// choose tc with max profit		
-			cout << endl << name << " received + my profits "<<endl;;
+			//cout << name << " soucet front " << getQueue()<< endl;
+			//cout << endl << name << " received + my profits "<<endl;;
+			idealTc = Tc;
+			max_profit = 0;
 			for ( int i = 0; i < received_profit_sum.length(); i ++ ) {
 				int time_cycle = received_Tcs(i);
 				double profit = received_profit_sum(i) + getProfit(time_cycle);
-				cout << time_cycle << " " << profit << endl;
-				if ( profit > max_profit ) {
+				//cout << time_cycle << " " << profit << endl;
+				if ( profit > max_profit && profit > min_profit ) {
 					max_profit = profit;
 					idealTc = time_cycle;
 				}
 			}
-			cout << endl << name << "Tc: " << idealTc << " s celkovym ziskem " << max_profit << endl;
-			// reseting lanehs
-			for ( int i = 0; i < lanehs.length(); i ++ ) { 
-				lanehs(i)->resetQueue();
-			}
-
-			Tc = idealTc;
-			Tcs << cycle_counter << "\t" << Tc << "\t" << max_profit <<  endl;
-
-
+			
+			
+			
+			// NASTAVENI Tc
+			//Tc = idealTc;
+			//Tc = 40 + (cycle_counter%4)*80;
+			int dTc = 0;
+			if ( idealTc - Tc >= 2 )
+				dTc = 2;
+			if ( Tc - idealTc >=2 )
+				dTc = -2;
+			if ( idealTc - Tc >= 4 )
+				dTc = 4;
+			if ( Tc - idealTc >=4 )
+				dTc = -4;
+			Tc += dTc;
+			//Tc = 84;
+			cout << endl << name << "Tc: " << Tc << " idalni Tc " << idealTc << " se ziskem " << max_profit << endl;
 			// nastaveni delky cyklu na Tc
 			vec action;
@@ -316,5 +334,5 @@
 			for ( int i =0; i < stage_names.length(); i ++) {			
 				if ( (i+1) < stage_names.length() ) {
-					st = (stage_times(i)/80)*Tc;
+					st = round(((double)(stage_times(i)*Tc))/80.0);
 					stage_time_sum += st;
 					action(find_index(rv_action, stage_names(i))) = st;
@@ -325,5 +343,7 @@
 			action2ds.filldown(action,glob_ut);
 		}
-			cycle_counter ++;
+		Tcs << cycle_counter << ";" << Tc<< ";" << idealTc << ";" << max_profit <<  endl;
+
+		cycle_counter ++;
 	}
 	
Index: /applications/doprava/traffic_agent.h
===================================================================
--- /applications/doprava/traffic_agent.h (revision 1137)
+++ /applications/doprava/traffic_agent.h (revision 1141)
@@ -95,12 +95,6 @@
 * KOD PRO TrafficAgentCycleTime *
 *********************************/
-	double last_queue;
-	double sum_queue_length;
-	double last_sum_queue_length;
-	double queue_diff_sum;
-	double queue_variance_sum;
-	int n_of_queues_in_length;
-	int last_n_of_queues_in_length;
-	double last_variance;
+	double queue_last;	
+	double queue_avg_last;
 	double green_time_ratio;
 	double Tc;
@@ -108,16 +102,15 @@
 	double saturated_stream;
 	double delta; // ztratovy cas pri prechodu na zelenou
-	// udaje opro kalmana
+	double delta_w;
+	// udaje opro filtr
 	double queue_avg; //  pruner
 	double queue_w;   //  vaha
-	double queue_d;   //  rozptyl
-	double queue_dd;  //  snizeni verohodnosti odhadu 
-	double queue_r;   //  neduveryhodnost mereni = 1
 
 	double Ro_avg;	//  prumer
 	double Ro_w;	//  vaha K
-	double Ro_d;		//  rozptyl P
-	double Ro_dd;		//  snizeni verohodnosti odhadu Q
-	double Ro_r;		//  neduveryhodnost mereni = 1 R
+
+	double cars_in_avg;
+	double cars_in_w;
+	
 
 	double last_queue_avg;
@@ -131,25 +124,18 @@
 		saturated_stream = 0.5;
 		queue = 0;
-		sum_queue_length = 0;
-		last_sum_queue_length = 0;
-		n_of_queues_in_length = 0;
-		last_n_of_queues_in_length = 0;
-		queue_diff_sum = 0;
-		queue_variance_sum = 0;
-		last_variance = 0;
-		delta = 5;
-		// KALMAN
+		queue_last = 0;
+		// filtr
+		delta = 0;
+		delta_w = 0.2;
+		
 		queue_avg = 0;	//  prumer
-		queue_w = 1;	//  vaha K
-		queue_d = 0.9;		//  rozptyl P
-		queue_dd =0.1;		//  snizeni verohodnosti odhadu Q
-		queue_r = 9;		//  neduveryhodnost mereni = 1 R
+		queue_w = 0.4;	//  vaha		
 
 		Ro_avg = 0;	//  prumer
-		Ro_w = 1;	//  vaha K
-		Ro_d = 0.9;		//  rozptyl P
-		Ro_dd =0.1;		//  snizeni verohodnosti odhadu Q
-		Ro_r = 9;		//  neduveryhodnost mereni = 1 R
-
+		Ro_w = 0.1;	//  vaha
+
+		cars_in_avg = 0;
+		cars_in_w = 0.4;
+		
 		last_queue_avg = 0;
 	}
@@ -161,18 +147,7 @@
 
 	void echo () {
-		//cout << "n:" << n_of_queues_in_length << " " << getQueueName() << " q:" << getQueue() << " aq:" << getAverageQueueLength() << " diff:" << getAverageQueueGrow() << " D:" << getQueueVariance() << endl;
-		/*cout << "n:" << n_of_queues_in_length 
-		     << " " << getQueueName() 
-			 << "LAST aq:" << last_sum_queue_length/last_n_of_queues_in_length 
-			 << " aq:" << getAverageQueueLength()
-			 << " diff" <<  getAverageQueueLength() - last_sum_queue_length/last_n_of_queues_in_length 
-			 << endl;*/
+		
 		cout << getQueueName();
-		/*if ( last_n_of_queues_in_length> 0 && n_of_queues_in_length > 0 )
-			cout << " DIFF: " << getAverageQueueLength() - last_sum_queue_length/last_n_of_queues_in_length;
-		else
-			cout << " no diff available";*/
-		//cout << "\tn " << n_of_queues_in_length;
-		//cout << " prijizdejicich aut za 90: " << getRo()*90;
+		
 		cout << "\tgtr: " << green_time_ratio;
 		cout << "\tRo: " << getRo();
@@ -188,9 +163,5 @@
 			cout << (int)(100*getWT(tc)) << "\t";
 		}
-		//cout << " diff: " << getRo()*Tc - saturated_stream*getGreenTime();
-		/*if ( saturated_stream*getGreenTime() >= getAverageQueueLength() )
-			cout << " projede vsechno ";
-		else
-			cout << " zustanou auta ";*/
+		
 		cout << endl << endl;
 	}
@@ -216,54 +187,49 @@
 		// pricita pouze namerene udaje
 		if ( ql >= 0 ) {
-			last_queue = queue;
+			queue_last = queue;
 			queue = ql;
-			queue_diff_sum += queue - last_queue;
-			//queue_variance_sum += abs(queue - last_queue);
+			queue_avg_last = queue_avg;
+			
+			// KALMAN			
+			//cout << endl << "pridani fronty\t" << queue_avg << "\t" << ql << endl;
+			queue_avg += queue_w*( ql - queue_avg );			
+			/*cout << getQueueName() << " inputs";
+			int k = 0;
+			for ( int i = 0; i < rv_inputs.length(); i ++ ) {
+			cout <<"\t"<< rv_inputs.name(i) << ": ";
+			for ( int j = 0; j < rv_inputs.size(i); j ++ ) {				
+				cout << inputs(k) << " ";
+				k ++;
+			}
+			cout << endl;*/
+		}
+		if ( inputs(0) >= 0 ) {
+			cars_in_avg += cars_in_w*( inputs(0) - cars_in_avg );
 		
-			sum_queue_length += ql;
-			n_of_queues_in_length ++;
-			// KALMAN			
-			cout << endl << "pridani fronty\t" << queue_avg << "\t" << ql << endl;
-			queue_avg += queue_w*( ql - queue_avg );
-			queue_d += queue_dd;
-			queue_w = queue_d/(queue_d+1);
-			queue_d = (1-queue_w)*queue_d;
-	
-			double Ro = getActualRo();
-			Ro_avg += Ro_w*( Ro - Ro_avg );
-			Ro_d += Ro_dd;
-			Ro_w = Ro_d/(Ro_d+1);
-			Ro_d = (1-Ro_w)*Ro_d;
-		}
-	}
-
-	void resetQueue () {
-		last_Tc = Tc;
-		//last_variance = getQueueVariance();
-		last_n_of_queues_in_length = n_of_queues_in_length;
-		last_sum_queue_length = sum_queue_length;
-		n_of_queues_in_length = 0;
-		sum_queue_length = 0;
-		queue_variance_sum = 0;
-		queue_diff_sum = 0;
-		// kalman
-		last_queue_avg = getAverageQueueLength();
-	}
+		}
+		if ( cars_in_avg > 0 && queue_avg_last > saturated_stream*last_Tc*green_time_ratio ) {
+			//double delta_d = queue_avg - queue_avg_last + last_Tc * saturated_stream - cars_in_avg*(double)last_Tc/90;			
+			double delta_d = ( last_Tc/(90*saturated_stream)) * (cars_in_avg - queue_avg + queue_avg_last) - green_time_ratio;
+			delta += delta_w * ( delta_d - delta );
+			if ( delta < 0 )
+				delta = 0;
+		}
+
+		cout << getQueueName() << " cars in: " << inputs(0) << " " << cars_in_avg <<
+			" q_avd: " << queue_avg <<
+			" delta: " << delta <<
+			endl;
+	}
+
+
+	
 
 	double getAverageQueueLength () {
 		// kalmam
 		return queue_avg;
-
-		if( n_of_queues_in_length > 0 )
-			return sum_queue_length / n_of_queues_in_length;
-		else
-			return 0.0;
 	}
 
 	double getLastAverageQueueLength () {
-		if( last_n_of_queues_in_length > 0 )
-			return last_sum_queue_length / last_n_of_queues_in_length;
-		else
-			return 0.0;
+		return queue_avg_last;
 	}
 
@@ -282,60 +248,73 @@
 	double getRedTime () { return getRedTime( Tc ); }	
 
-	double getRelativeQueueDiff () {
-		if ( last_n_of_queues_in_length > 0 && n_of_queues_in_length > 0 ) {
-			
-			return getAverageQueueLength()/(Tc*(1-green_time_ratio)) - (last_sum_queue_length/last_n_of_queues_in_length)/(last_Tc*(1-green_time_ratio));
-		}
-		else
-			return 0;
-	}
+	
 
 	// hodnotici funkce - suma cekaciho casu aut za 10h
-	double getWT ( double  Tc ) {
-	  double T = 36000; // celkovy cas 10h
-	  double Ro = getRo();
-	  double Gr = green_time_ratio;
-	  double ss = saturated_stream;
-	  double WT = 0;
-	  double q = 0;  // zacina s nulovou frontou ?
-	  double ti = 0;
-	  double sumq = q;
-	  while ( ti < T ) {
-		// ve fronte stoji vic aut nez je schopno odjet za zelenou
-		if ( q > 0.5*ss*Tc ) {
-		  WT += 0.5*ss*(Tc*Gr - delta)*(Tc*Gr - delta);
-		}
-		else {
-
-		  WT += 0.5*q*(Tc*Gr - delta);
-		}
-
-		if ( (Tc*Gr - delta)*ss < q )
-		  q -= (Tc*Gr - delta)*ss;
-		else
-		  q = 0;
-
-		WT += q * Tc; // zbytek fronty ceka cely cyklus
-		// cekani vozidel, ktera prijela za pocitany cyklus
-		if ( q > 0 ) {
-		  //pokud je fronta > 0, auta cekaji prumerne polovinu delky cyklu
-		  WT += Ro*Tc*0.5*Tc;
-		}
-		else {
-		  // pokud je fronta = 0 cekaji pouze auta, ktera prijela na cervenou (p=(1-Gr)) 0.5Tc(1-Gr)
-		  WT += Ro*(1-Gr)*Tc*0.5*(1-Gr)*Tc;
-		}
-		// fronta se zvetsi o Ro*Tc
-		q += Ro*Tc;
-		sumq += Ro*Tc;
-		ti += Tc;
-	  }
-
-	  //return WT;
-	  if ( sumq > 0 )
-		return 100*(WT/(0.5*T*sumq));
-	  else
-	  return 0;
-	}
+	//double getWT_old ( double  Tc ) {
+	//  double T = 36000; // celkovy cas 10h
+	//  double Ro = getRo();
+	//  double Gr = green_time_ratio;
+	//  double ss = saturated_stream;
+	//  double WT = 0;
+	//  double q = 0;  // zacina s nulovou frontou ?
+	//  double ti = 0;
+	//  double sumq = q;
+	//  while ( ti < T ) {
+	//	// ve fronte stoji vic aut nez je schopno odjet za zelenou
+	//	if ( q > 0.5*ss*Tc ) {
+	//	  WT += 0.5*ss*(Tc*Gr - delta)*(Tc*Gr - delta);
+	//	}
+	//	else {
+
+	//	  WT += 0.5*q*(Tc*Gr - delta);
+	//	}
+
+	//	if ( (Tc*Gr - delta)*ss < q )
+	//	  q -= (Tc*Gr - delta)*ss;
+	//	else
+	//	  q = 0;
+
+	//	WT += q * Tc; // zbytek fronty ceka cely cyklus
+	//	// cekani vozidel, ktera prijela za pocitany cyklus
+	//	if ( q > 0 ) {
+	//	  //pokud je fronta > 0, auta cekaji prumerne polovinu delky cyklu
+	//	  WT += Ro*Tc*0.5*Tc;
+	//	}
+	//	else {
+	//	  // pokud je fronta = 0 cekaji pouze auta, ktera prijela na cervenou (p=(1-Gr)) 0.5Tc(1-Gr)
+	//	  WT += Ro*(1-Gr)*Tc*0.5*(1-Gr)*Tc;
+	//	}
+	//	// fronta se zvetsi o Ro*Tc
+	//	q += Ro*Tc;
+	//	sumq += Ro*Tc;
+	//	ti += Tc;
+	//  }
+
+	//  //return WT;
+	//  if ( sumq > 0 )
+	//	return 100*(WT/(0.5*T*sumq));
+	//  else
+	//  return 0;
+	//}
+	// stredni doba prujezdu n-teho auta ve fronte
+	double getEcarWT ( const double tc, const int n ) {
+		double cpg = (tc * green_time_ratio - delta) * saturated_stream; // cars per green time
+		double wc = floor( n / cpg ); // number of waiting cycles		
+		double ET_last_cycle = ( 1/ (2*tc) ) * ( tc*(1-green_time_ratio) + (n-wc*cpg)/saturated_stream )*( tc*(1-green_time_ratio) + (n-wc*cpg)/saturated_stream );
+		double ET = wc*tc + ET_last_cycle;
+		return ET;
+	}
+
+	// suma strednich hodnot cekacich casu pres auta ve fronte
+	double getWT ( const double tc ) {
+		double sumEWT = 0;
+		//int n = round(getAverageQueueLength());
+		int n = round(cars_in_avg + getAverageQueueLength());
+		for ( int i = 0; i <= n; i ++ ) {
+			sumEWT += getEcarWT( tc, i );
+		}
+		return sumEWT;
+	}
+
 
 	//double getWT ( double  tc ) {
