Index: /applications/doprava/traffic_agent_cycle_time.h
===================================================================
--- /applications/doprava/traffic_agent_cycle_time.h (revision 1129)
+++ /applications/doprava/traffic_agent_cycle_time.h (revision 1139)
@@ -3,77 +3,32 @@
 const double measurement_cycle_time = 90; // s
 const double saturated_stream = 0.5; // car/s
-const int sample_cycles = 5;
-
-
-class SignalGroup {
-public:
-	string name;
-	Array <Lane*> lanes;
-	double green_time_ratio;
-	int average_queue_length;
-	string queue_name;
-	int cycle_counter;
-
-	SignalGroup () {
-		average_queue_length = 0;
-	}
-
-	void adapt() {
-		if ( (cycle_counter +1)% sample_cycles == 0 )
-			average_queue_length = 0;
-	}
-
-	
-	int getSumCarsWaitingTime ( double time_cycle ) {
-		int q = average_queue_length/sample_cycles; // pomocna delka fronty - UPRAVIT - pocitat s vetsi nez prumernou?
-		int cars_per_green = time_cycle * green_time_ratio * lanes.length() * saturated_stream; // pocet aut ktere projedou na zelenou
-		int sum_cars_waiting_time = 0; // suma aut * cekacich casu
-		int i = 0; // iterator
-		while ( q > cars_per_green ) {
-			q -= cars_per_green;
-			sum_cars_waiting_time += q*time_cycle;
-			i ++;
-		}
-		// + zbytek fronty + prvni nezapocitana vlna
-		if ( i > 0 ) {
-			sum_cars_waiting_time += q*time_cycle + cars_per_green*(1.0-green_time_ratio)*time_cycle*0,5;
-			//cout << "delsi fronta " << q << " ";
-		}
-
-		// pocet aut * stredni hodnota cekaci doby pri volne krizovatces
-		else {
-			sum_cars_waiting_time += (0.5*(1.0-green_time_ratio))*(time_cycle*q);
-			//cout << "kratsi fronta " << q << " ";
-		}
-
-		//cout << "signal group " << name << " tc " << time_cycle << " cekani " << sum_cars_waiting_time << " fronta " << average_queue_length << endl;
-		
-		return sum_cars_waiting_time;
-	}
-
-	void addQueue( int q ) {
-		
-		if ( q > 0 )
-			average_queue_length += q;
-	}	
-
-	
-};
+const int sample_cycles = 3;
+
+
+
+
+
 
 class TrafficAgentCycleTime : public BaseTrafficAgent {
 public:
 	int Tc;
-	int minTc;
-	int maxTc;
-	int stepTc;
+	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	
 	int idealTc;
-	static const int d_Tc = 3; // pocet navrhovanych delek cyklu	
 	int cycle_counter;
-	int max_profit;
+	double max_profit;
 	unsigned int n_of_broadcast;
 	Array <int> lane_sum;
-	Array <SignalGroup*> signal_groups;
-	Array <int> received_Tcs;
-	Array <int> received_profit_sum;
+	//Array <SignalGroup*> signal_groups;
+	Array <double> received_Tcs;
+	Array <double> received_profit_sum;
+	Array <double> received_queue_diffs;
+	// pomocne logovaci soubory
+	ofstream Tcs;
+	ofstream Ros;
+	ofstream Qs;
+
 
 // POMOCNE FUNKCE
@@ -93,4 +48,10 @@
 	}
 
+	void add2array( Array <double> &arr, double item  ) {
+		arr.set_length( arr.length()+1, true );
+		arr(arr.length()-1) = item;
+		cout << endl << "addin to array value " << item << endl;
+	}
+
 	void echo ( string message ) {
 		cout << name << " hlasi: " << message << endl;		
@@ -98,5 +59,5 @@
 
 	unsigned int find_index ( RV rv_vector, string index_string ) {
-		for ( unsigned int i = 0; i < (int) rv_vector.length(); i ++) {
+		for ( unsigned int i = 0; i < rv_vector.length(); i ++) {
 			if ( rv_vector.name(i) == index_string )
 				return i;
@@ -106,5 +67,5 @@
 
 	unsigned int find_index ( Array <string> arr, string index_string ) {
-		for ( unsigned int i = 0; i < (int) arr.length(); i ++) {
+		for ( unsigned int i = 0; i < arr.length(); i ++) {
 			if ( arr(i) == index_string )
 				return i;
@@ -115,45 +76,35 @@
 // VYPOCETNI FUNKCE
 
-	int getSumCarsWaitingTime ( double time_cycle ) {
-		int sum = 0;
-		SignalGroup * sg;
-		for ( int i = 0; i < signal_groups.length(); i ++ ) {
-			sg = signal_groups(i);
-			sum += sg->getSumCarsWaitingTime( time_cycle );
-		}
+	// soucet cekacich autocasu
+	double getWT ( double time_cycle ) {
+		double sum = 0;		
+		for ( int i = 0; i <lanehs.length(); i ++ ) {
+			sum += lanehs(i)->getWT( time_cycle );
+		}		
 		return sum;
 	}
 
-	int getQueue () {
-		int queue = 0;
-		SignalGroup *sg;
-		for ( int i = 0; i < signal_groups.length(); i ++ ) {
-			sg = signal_groups(i);
-			queue += sg->average_queue_length;
+	double getProfit ( double tc ) {
+		return getWT(Tc) - getWT(tc);
+	}
+
+	double getQueue () {
+		double queue = 0;
+		for ( int i = 0; i <lanehs.length(); i ++ ) {
+			queue += lanehs(i)->getAverageQueueLength();
 		}
 		return queue;
 	}
 
-	// NEPOUZIVAT?
-	//int getIdealTc() {
-	//	int min_waiting_time = getSumCarsWaitingTime(Tc);
-	//	int waiting_time;
-	//	int idealTc = Tc;
-	//	cout << name << " Tc wt" << endl;
-	//	for ( int t_c = Tc - 2*stepTc; t_c <= Tc + 2*stepTc; t_c += stepTc ) {
-	//		waiting_time = getSumCarsWaitingTime(t_c);
-	//		cout << t_c << " " << waiting_time << endl;
-	//			if ( waiting_time < min_waiting_time ) {
-	//				min_waiting_time = waiting_time;
-	//				idealTc = t_c;
-	//			}
-	//	}
-	//	cout << "IDEAL " << idealTc << endl;
-	//	return idealTc;
-	//}
-
-	int getIdealTc() {
-			
-	}
+	
+	double getRelativeQueueDiff() {
+		double dq = 0;
+		for ( int i=0; i< lanehs.length(); i ++ ) {
+			dq += lanehs(i)->getRelativeQueueDiff();
+		}
+		return dq;
+	}
+
+	
 
 
@@ -182,25 +133,14 @@
 	}
 
-	int getProfit( int time_cycle ) {
-		if ( Tc != time_cycle )
-			return getSumCarsWaitingTime( Tc ) - getSumCarsWaitingTime ( time_cycle );
-		else
-			return 0;
-	}
+	
 
 	
 // FUNKCE VOLANE V main_loop NA ZACATKU
-	void from_setting( Setting &set ) {
-		BaseTrafficAgent::from_setting(set);
-		//TrafficAgentCycleTime::Tc = 80;
-	}
-
+	
 	void validate (){
 		rv_action = RV("Tc",1);
 		rv_action.add( RV( stage_names, ones_i(stage_names.length()) ) );
-		Tc = 80;
-		minTc = 60;
-		maxTc = 120;
-		stepTc = 5;
+		Tc = 80;		
+		idealTc = Tc;
 		max_profit = 0;
 		received_profit_sum.set_length( 2*d_Tc +1 );
@@ -209,28 +149,30 @@
 
 		BaseTrafficAgent::validate();
-		// inicializace signalnich skupin
-		for ( int i = 0; i < lanes.length(); i ++ ) {
-			Lane * l = & lanes(i);
-			int sg_index = -1;
-			for ( int j = 0; j < signal_groups.length(); j ++ ) {
-				SignalGroup * sg;
-				sg = signal_groups(j);
-				if ( sg->name == l->sg )
-					sg_index = j;
-			}
-			SignalGroup * sg;
-			if ( sg_index >= 0 ) {				
-				sg = signal_groups(sg_index);
-			}
-			else {
-				sg = new SignalGroup();
-				sg->name = l->sg;
-				sg->average_queue_length = 0;
-				signal_groups.set_size( signal_groups.length()+1, true );
-				signal_groups(signal_groups.length()-1) = sg;
-			}			
-			sg->lanes.set_size(sg->lanes.length()+1, true);
-			sg->lanes(sg->lanes.length()-1) = l;
-		}
+
+		for ( int i=0; i<lanehs.length(); i ++ ) {
+			unsigned int index = find_index( green_names, name + "_" + lanehs(i)->getSG() );
+				lanehs(i)->green_time_ratio = green_times( index );
+		}
+
+		// pomocne logovaci soubory
+		Tcs.open("Tcs.dat");
+		Tcs << "cyklus\tTc\tprofit\n";
+		stringstream Ro_file_name;
+		Ro_file_name << "Ro_" << name << ".dat";
+		if ( name == "495" ) {
+			Ros.open("Ros_495.dat");
+			Qs.open("Qs_495.dat");
+		}
+		else { 
+			Ros.open("Ros_601.dat");
+			Qs.open("Qs_601.dat");
+		}
+		Ros << "Hustoty provozu ve frontach" << endl << "n";
+		Qs << "Prumerna delka fronty" << endl << "n";
+		for ( int i = 0; i < lanehs.length(); i++ ) {
+			Ros << "\t" << lanehs(i)->getQueueName();
+			Qs << "\t" << lanehs(i)->getAverageQueueLength();
+		}
+		Ros << endl;
 	}
 
@@ -238,33 +180,42 @@
 
 	void adapt (const vec &glob_dt) {
+		// vynulovani pole prijatych rozdilu fronty
+		received_queue_diffs.set_length(0);
+
 		// inicializes vars of cycle of broadcast
 		n_of_broadcast = 0;
 		max_profit = 0;
-		idealTc = Tc;
-		
-		for ( int i = 0; i < signal_groups.length(); i ++ ) {
-			signal_groups(i)->adapt();
-		}
-
-		for ( int i = 0; i < received_profit_sum.length(); i++ ) {
+		//cout << endl << name << ":" << endl << endl;
+		// predani aktualnich dat do LaneHandler lanehs
+		Ros << cycle_counter;
+		Qs << cycle_counter;
+		for ( int i=0; i<lanehs.length(); i ++ ) {
+			lanehs(i)->Tc = Tc;
+			lanehs(i)->addQueueLength( queues( find_index( rv_queues, lanehs(i)->getQueueName() ) ) );
+			
+			lanehs(i)->echo();
+			// pomocne logovaci soubory
+			Ros << "\t" << lanehs(i)->getRo();
+			Qs << "\t" << lanehs(i)->getAverageQueueLength();
+		}
+		Ros << endl;
+		Qs << endl;
+
+		if ( cycle_counter % sample_cycles == 0 ) {
+			//cout << name << " queue DIFF " << getRelativeQueueDiff() << endl;
+			
+			for ( int i=0; i<lanehs.length(); i ++ ) {
+			
+			
+				//lanehs(i)->echo();
+				
+			
+			//cout << name << " " << i << " "  << lanes(i).sg << " " << lanes(i).queue  << " input " << queues(i) << endl;
+			}
+		}
+
+		for ( int i = 0; i < received_profit_sum.length(); i++ )
 			received_profit_sum(i) = 0;
-		}
-
-		// nacteni dat do signalnich skupin
-		cout << endl << cycle_counter << " SG " << (cycle_counter%sample_cycles +1) << endl;
-		for ( int i = 0; i < rv_queues.length(); i ++ ) {
-			SignalGroup * sg;
-			sg = signal_groups(i);
-			sg->queue_name = rv_queues.name(i);
-			sg->addQueue(queues(i));
-			sg->green_time_ratio = green_times(i);
-			sg->cycle_counter = cycle_counter;
-			//cout << sg->name << " " << sg->getSumCarsWaitingTime(Tc) << " " << sg->average_queue_length << " " << sg->green_time_ratio*Tc << " " << sg->getTime2zeroQue() << endl;
-			cout << sg->name << " " << queues(i) << " " << sg->average_queue_length << " " << sg->average_queue_length / (cycle_counter%sample_cycles +1) << endl;
-		}
-
-		//suggestedTc = idealTc = getIdealTc();		
-		
-		//cout << endl << "CELKOVA FRONTA " << name << " " << getQueue() << endl;
+
 
 		BaseTrafficAgent::adapt(glob_dt);
@@ -277,8 +228,9 @@
 		// 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;
-				int profit = getProfit(time_cycle);
-
+				double profit = getProfit(time_cycle);
+				cout << "\t" << time_cycle << " " <<profit;
 				// send tc
 				stringstream time_cycle_stream;
@@ -289,9 +241,8 @@
 				stringstream profit_stream;
 				profit_stream << "profit_" << (i+d_Tc);
-				send2allNeighbours( set, profit_stream.str(), profit );
-
-				//cout << name << " tc " << time_cycle << " profit " << profit << " cekani " << getSumCarsWaitingTime(time_cycle) << endl;
-			}
-			//cout << endl;
+				send2allNeighbours( set, profit_stream.str(), profit );				
+			}
+			
+			cout << endl;
 		}
 		
@@ -306,22 +257,23 @@
 		UI::get(from, msg, "from", UI::compulsory);
 		UI::get(val, msg, "value");
-		
+
+				
 		//cout << name << " receiving from " << from << " " << what << " : " << val<<endl;
-		if ( n_of_broadcast == 1 ) {
-			if ( what.substr(0,2) == "tc" ) {			
-				istringstream tc_i( what.substr(3, what.length()-3) );
-				int index;
-				tc_i >> index;
-				received_Tcs( index ) = val;
-				//cout << "tc " << val << endl;
-			}
-			if ( what.substr(0,6) == "profit" ) {				
-				istringstream profiti( what.substr(7, what.length()-7) );
-				int index;
-				profiti >> index;
-				received_profit_sum( index ) = received_profit_sum( index ) + val;
-				//cout << "profit " << val << endl;			
-			}
-		}
+		
+		if ( what.substr(0,2) == "tc" ) {			
+			istringstream tc_i( what.substr(3, what.length()-3) );
+			int index;
+			tc_i >> index;
+			received_Tcs( index ) = val;
+			//cout << "tc " << val << endl;
+		}
+		if ( what.substr(0,6) == "profit" ) {				
+			istringstream profit_i( what.substr(7, what.length()-7) );
+			int index;
+			profit_i >> index;
+			received_profit_sum( index ) = received_profit_sum( index ) + val;
+			//cout << "pridavam profit " << val << endl;			
+		}
+		
 		
 		if ( what == "new_stable_state" ) {
@@ -333,17 +285,25 @@
 
 	void act (vec &glob_ut){
+		
 		if ( cycle_counter % sample_cycles == 0 ) {
 			// choose tc with max profit		
+			cout << endl << name << " received + my profits "<<endl;;
 			for ( int i = 0; i < received_profit_sum.length(); i ++ ) {
 				int time_cycle = received_Tcs(i);
-				int profit = received_profit_sum(i) + getProfit(time_cycle);
+				double profit = received_profit_sum(i) + getProfit(time_cycle);
+				cout << time_cycle << " " << profit << endl;
 				if ( profit > max_profit ) {
 					max_profit = profit;
 					idealTc = time_cycle;
-					//cout << name << " idealni Tc " << time_cycle << " s celkovym ziskem " << profit << endl;
 				}
 			}
-			//cout << endl << name << " nastevuje TC na " << idealTc << endl;
+			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;
 
 
@@ -365,7 +325,13 @@
 			action2ds.filldown(action,glob_ut);
 		}
-		cycle_counter ++;
-	}
-	
+			cycle_counter ++;
+	}
+	
+	// KONEC
+	void finalize() {
+		Tcs.close();
+	}
+
+
 
 };
