Index: /applications/doprava/Zlicingw.cfg
===================================================================
--- /applications/doprava/Zlicingw.cfg (revision 1074)
+++ /applications/doprava/Zlicingw.cfg (revision 1081)
@@ -54,5 +54,5 @@
 	// simulation in our case starts always at 00:00:00. Default simulation
 	// length is 24 hours (24:00:00).
-	stop_time = "01:00:00";
+	stop_time = "00:15:00";
 	
 };
Index: /applications/doprava/traffic_agent_offset.h
===================================================================
--- /applications/doprava/traffic_agent_offset.h (revision 1071)
+++ /applications/doprava/traffic_agent_offset.h (revision 1081)
@@ -28,8 +28,10 @@
 	string accepted_from;
 
+	//TODO currently useless list of neighbours asking for expectations
 	list<string> seznam;
 	
 	double car_leaving; //s; how long is 1 car leaving queue
 
+	// some state variables
 	bool need_exps;
 	bool new_stable_state;
@@ -37,4 +39,11 @@
 	bool final_state;
 	//bool reset_negot_offset;
+
+	//! sum of final planned_offset values since last reach of cycle_count
+	int total_offset;
+	//! number of finished cycles since last reach of cycle_count
+	int negot_cycle;
+	//! after cycle_count cycles, we count avarege planned_offseta send it to Aimsun
+	int cycle_count;
 
 	//! counts all expected cars going from each lane, saves to outputs and rv_outputs
@@ -95,5 +104,4 @@
 		RV rv_vector;
 		vec vector;
-		datalink exps2vector;
 
 		for (int i=0;i<lanes.length();i++) {
@@ -105,10 +113,12 @@
 				int l=0;		
 				for (int j=0;j<rv_recieved_exps.length();j++) {
-					t_cars_begin.set_size(l+1,true);
-					t_cars_end.set_size(l+1,true);
-					cars_count.set_size(l+1,true);
 				
 					int result=rv_recieved_exps.name(j).find(lanes(i).inputs(k)+"-");
 					if (result>=0) {
+
+						t_cars_begin.set_size(l+1,true);
+						t_cars_end.set_size(l+1,true);
+						cars_count.set_size(l+1,true);
+
 
 						rv_vector = RV(rv_recieved_exps.name(j),3);
@@ -124,4 +134,5 @@
 				}
 				if (found) {			
+
 					//counting rating
 					string group = name+"_"+lanes(i).sg;		//e.g. 495_VA
@@ -138,5 +149,5 @@
 					virtual_queue=lanehs(i)->queue;
 
-					//cycle goes through all stopping points and counts queue lenght at these points
+					//cycle goes through all "stopping" points and counts queue lenght at these points
 					do {
 						k=min_i(t_cars_begin);
@@ -151,5 +162,5 @@
 								if (t_cars_end(k)<=t_act) {
 									virtual_queue+=cars_count(k);
-									//jen prodlouzime, t_act zustava, odstranime k-ty predpoklad (mozna hned, mozna az na konci
+									
 									cars_count.del(k);
 									t_cars_begin.del(k);
@@ -158,6 +169,4 @@
 								}
 								else {
-									//prodlouzujeme do casu t_green_begin, 
-									//pak bud green_begin posuneme do t_act
 									double pomer=(t_cars_begin(k)-t_act)/(t_cars_end(k)-t_cars_begin(k));
 									virtual_queue+=cars_count(k)*pomer;
@@ -166,6 +175,4 @@
 							}
 							else if (t_cars_begin(k)==t_act) {
-								//mixujeme az do t_cars_end nebo t_green_end
-								//posuneme t_act na jeden z tech casu
 								if (t_cars_end(k)<t_green_end) {
 									virtual_queue+=cars_count(k)-(t_cars_end(k)-t_act)/car_leaving;
@@ -176,5 +183,5 @@
 									t_cars_end.del(k);
 								}
-								//t_cars_end>=t_green_end
+								//if t_cars_end>=t_green_end
 								else {
 									virtual_queue+=cars_count(k)-(t_green_end-t_act)/car_leaving;
@@ -188,7 +195,6 @@
 
 							}
-							//t_cars_begin(k)>=t_act
+							//if t_cars_begin(k)>=t_act
 							else {
-								//zkracujeme frontu az do t_cars_end(k) nebo t_green_end a posuneme t_act
 								if (t_cars_end(k)<t_green_end) {
 									virtual_queue-=(t_cars_end(k)-t_act)/car_leaving;
@@ -201,4 +207,5 @@
 							}
 						}
+						//if no other expectations found
 						else {
 							virtual_queue-=(t_green_end-t_act)/car_leaving;
@@ -219,5 +226,5 @@
 	//! finds best offset using recieved_exps. Returns found offset
 	int find_best_offset(const int center, int interval) {
-		//! rating if offset is rised
+		//! rating if offset is rised 
 		double rating_p;
 		//! rating if offset is unchaged (=center)
@@ -368,18 +375,37 @@
 	}
 	
-	//! returns offset value shifted to fit interval <0;cycle_length>
-	int normalize_offset(int offset) {
-		while (offset<0 || offset>cycle_length) {
-			if (offset<0) {
-				offset+=cycle_length;
-			}
-			else {
-				offset-=cycle_length;
-			}
-		}
-		return offset;
-	}
+	/*! 
+	returns offset value shifted to fit interval <-cycle_length/2;cycle_length/2> 
+	or (when second parameter is false)) <0;cycle_length>
+	*/
+	int normalize_offset(int offset, bool zero=true) {
+		if (zero) {
+			while ((offset<(-cycle_length/2)) || (offset>(cycle_length/2))) {
+				if (offset<0) {
+					offset+=cycle_length;
+				}
+				else {
+					offset-=cycle_length;
+				}
+			}
+			return offset;
+		}
+		else {
+			while (offset<0 || offset>cycle_length) {
+				if (offset<0) {
+					offset+=cycle_length;
+				}
+				else {
+					offset-=cycle_length;
+				}
+			}
+			return offset;
+		}
+	}
+
+
 	
 	//! returns value shifted to fit interval <0;cycle_length>
+	//currently not in use
 	template<class T> T normalize(T time) {
 		while (time<0 && time<cycle_length) {
@@ -394,4 +420,5 @@
 	}
 
+	//! converts t to string
 	template <class T> inline string to_string (const T& t)
 	{
@@ -414,5 +441,5 @@
 	}
 
-	//! returns index of smallest element in vectorsx
+	//! returns index of smallest element in vector
 	int min_i(vec vector) {
 		if (vector.length()>0) {
@@ -440,6 +467,5 @@
 	double planned_rating;
 	//! rating of planned next offset
-	double planned_next_rating;
-	
+	double planned_next_rating;	
 	//! avarage speed of cars
 	int VP;
@@ -653,11 +679,11 @@
 		RV rv_exp;
 
-		std::ofstream ofile;
-		ofile.open("greentimes.txt");
-		ofile.close();
-
 		car_leaving=2;
 		VP=45;
 		actual_time=0;
+		
+		negot_cycle=1;
+		cycle_count=5;
+		total_offset=0;
 
 		negot_offset=8;
@@ -681,15 +707,28 @@
 
 	void act(vec &glob_ut){
-		vec action;
-		action.set_size(rv_action._dsize());
-		
-		ivec index = rv_action.dataind(RV(name+"_offset",1));
-
-		action(index(0))=planned_offset;
+		if (negot_cycle==cycle_count) {
+		
+			vec action;
+			action.set_size(rv_action._dsize());
+		
+			ivec index = rv_action.dataind(RV(name+"_offset",1));
+
+			action(index(0))=normalize_offset(total_offset/cycle_count, false);
+			action2ds.filldown(action,glob_ut);
+
+			total_offset=0;
+			negot_cycle=1;
+
+		}
+		else {
+			total_offset+=planned_offset;
+
+			negot_cycle++;
+		}
+
 		last_offset=planned_offset;
-		action2ds.filldown(action,glob_ut);
-
 		actual_time+=step_length;
 	}
+
 
 };
