Index: /applications/doprava/traffic_agent.h
===================================================================
--- /applications/doprava/traffic_agent.h (revision 986)
+++ /applications/doprava/traffic_agent.h (revision 1026)
@@ -135,8 +135,8 @@
 
 		//! simulator's step length in seconds
-		int step_time;
+		int step_length;
 
 		//! lenght of cycle in seconds
-		int cycle_time;
+		int cycle_length;
 		
 	public:
Index: /applications/doprava/Zlicingw.cfg
===================================================================
--- /applications/doprava/Zlicingw.cfg (revision 986)
+++ /applications/doprava/Zlicingw.cfg (revision 1026)
@@ -4,6 +4,10 @@
 	name = "495";
 	lanes = (
-			{ sg="VA"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVA"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q1";}, 
-			{ sg="VB"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVB"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q2";}
+			{ sg="VA"; inputs = ("495_DVA1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q1";}, 
+			{ sg="VB"; inputs = ("495_DVB1");  outputs= ("601_DVA","601_DVAa"); input_distances=[30]; output_distances=[200,200]; alpha= [0.5,0.5 ]; queue="495_Q2";},
+			{ sg="VC"; inputs = ("495_DVC");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q1";}, 
+			{ sg="VD"; inputs = ("495_DVD");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q2";},
+			{ sg="VE"; inputs = ("495_DVE");  outputs= ("601_DVA","601_DVAa"); input_distances=[30]; output_distances=[200,200]; alpha= [0.5,0.5 ]; queue="495_Q1";}, 
+			{ sg="VF"; inputs = ("495_DVF1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q2";}
 	);
 	neighbours = ("601");
@@ -17,6 +21,12 @@
 	name = "601";
 	lanes = (
-			{ sg="VA"; inputs = ("601_DVA","601_DVB");  outputs= ("495_DVA"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q1";}, 
-			{ sg="VB"; inputs = ("601_DVC","601_DVD");  outputs= ("495_DVB"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";}
+			{ sg="VA"; inputs = ("601_DVA");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q1";}, 
+			{ sg="VA"; inputs = ("601_DVAa");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";},
+			{ sg="VB"; inputs = ("601_DVB1");  outputs= ("495_DVC","495_DVD"); input_distances=[30]; output_distances=[200,200]; alpha= [0.5,0.5 ]; queue="601_Q1";}, 
+			{ sg="VB"; inputs = ("601_DVB1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";},
+			{ sg="VC"; inputs = ("601_DVC");  outputs= ("495_DVC","495_DVD"); input_distances=[30]; output_distances=[200,200]; alpha= [1.0 ]; queue="601_Q1";}, 
+			{ sg="VD"; inputs = ("601_DVD1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";},
+			{ sg="SE"; inputs = ("601_DSE1");  outputs= (""); input_distances=[30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q1";}, 
+			{ sg="VE"; inputs = ("601_DVE1");  outputs= ("495_DVC","495_DVD"); input_distances=[30]; output_distances=[200,200]; alpha= [0.5,0.5 ]; queue="601_Q2";}
 	);  
 	neighbours = ("495");
@@ -28,5 +38,5 @@
 );
 
-logger = {class="memlog"; filename = "zlicin"; maxlen = 1000;};
+logger = {class="memlog"; filename = "mujzlicin"; maxlen = 1000;};
 
 
@@ -44,8 +54,8 @@
 	// simulation in our case starts always at 00:00:00. Default simulation
 	// length is 24 hours (24:00:00).
-	stop_time = "00:05:00";
+	stop_time = "00:30:00";
 	
 };
 
 //Number of recieve/broadcast calls before end of negotiation
-negotiation_cycles = 5;
+negotiation_cycles = 15;
Index: /applications/doprava/Zlicin.cfg
===================================================================
--- /applications/doprava/Zlicin.cfg (revision 903)
+++ /applications/doprava/Zlicin.cfg (revision 1026)
@@ -2,61 +2,31 @@
 	{ // agent 1
 	class = "BaseTrafficAgent";
-	  name = "495";
-	  sg = (
-	    { name = "VA";
-	      detectors = (
-			{ name = "DVA1";  distance = 10;}, 
-	      {name = "DVA";  distance = 20; }
-	      );
-	    }, 
-	    { name = "VB";
-	      detectors = ();
-	      input = {};
-	      output = {};
-	    });
-  input = ( // remote detectors 
-    {
-      name = "601_DVAa";
-		distance = 100;
-      to_sg = ("VC","VD");
-    },
-    {
-      name = "601_DVA";
-		distance = 100;
-      to_sg = ("VC","VD");
-    }
-  );
-  output = ( // 
-     {
-		detectors = ("495_DVC");
-      to = "601";
-      }
-  );
-
-  offset = 40; //s
-  moje_special=14;
+	name = "495";
+	lanes = (
+			{ sg="VA"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVA"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q1";}, 
+			{ sg="VB"; inputs = ("495_DVA1","495_DVB");  outputs= ("601_DVB"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="495_Q2";}
+	);
+	neighbours = ("601");
+  	offset = 10; //s
+  	moje_special=14;
 },
 { //agent 2
 	class = "BaseTrafficAgent";
-  name = "601";
-  sg = (
-    { name = "VA";
-      detectors = (
-		{ name = "DVA"; distance = 10; }, 
-		 {name = "DVAa"; distance = 20; }
-      );
-    }, 
-    { name = "VB";
-      detectors = ();
-    }
-  );
-  
-  offset = 60; //s
-	input = ();
-	output = ({to="495"; detectors=("601_DVAa","601_DVA");});
+	name = "601";
+	lanes = (
+			{ sg="VA"; inputs = ("601_DVA","601_DVB");  outputs= ("495_DVA"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q1";}, 
+			{ sg="VB"; inputs = ("601_DVC","601_DVD");  outputs= ("495_DVB"); input_distances=[30,30]; output_distances=[200]; alpha= [1.0 ]; queue="601_Q2";}
+	);  
+	neighbours = ("495");
+	offset = 40;
+
 }
+
 );
 
-logger = {class="memlog"; filename = "zlicin"; maxlen = 1000;};
+logger = {class="memlog"; filename = "mujlog"; maxlen = 1000;};
+
+
+
 
 system = {
@@ -72,3 +42,7 @@
 	// length is 24 hours (24:00:00).
 	stop_time = "01:00:00";
+	
 };
+
+//Number of recieve/broadcast calls before end of negotiation
+negotiation_cycles = 5;
Index: /applications/doprava/traffic_agent_offset.h
===================================================================
--- /applications/doprava/traffic_agent_offset.h (revision 986)
+++ /applications/doprava/traffic_agent_offset.h (revision 1026)
@@ -4,18 +4,30 @@
 class GreenWaveTrafficAgent : public BaseTrafficAgent {
 protected:
-	double stable_state_loss;
-	double best_offset;
+	double rating_change;
+	int negot_offset;
+
+	RV rv_change_request;
+	vec change_request;
 
 	RV rv_recieved_exps;
 	vec recieved_exps;
-	
+
+	RV rv_recieved_changes;
+	vec recieved_changes;
+
+	//! name of agent, whose change we have accepted
+	string accepted_from;
+
 	list<string> seznam;
 	
-
 	double car_leaving; //s; how long is 1 car leaving queue
 
-	bool recieved_exp_request;
-			
-	//counts all expected cars going from each lane
+	bool need_exps;
+	bool new_stable_state;
+	bool send_requests;
+	bool final_state;
+	//bool reset_negot_offset;
+
+	//! counts all expected cars going from each lane, saves to outputs and rv_outputs
 	void expected_cars(/*const string dest*/) {
 		double start_time;
@@ -23,76 +35,27 @@
 		for (int i=0;i<lanes.length();i++) {
 			for (int j=0;j<lanes(i).outputs.length();j++) {
-				//		if (lanes(i).outputs(j).substr(0,3)==dest) {
-				//
 				string group = name+"_"+lanes(i).sg;		//e.g. 495_VA
 				ivec index=rv_inputs.dataind(RV(group,1));
 				vec green_time=inputs(index);
 
-
 				rv_outputs.add(RV(lanes(i).outputs(j)+"-"+group,3));
 				outputs.set_size(rv_outputs._dsize());
 
-				ivec indexes = rv_outputs.dataind(RV(lanes(i).outputs(j)+"-"+group,3));
+				ivec ind = rv_outputs.dataind(RV(lanes(i).outputs(j)+"-"+group,3));
 
 				//Number of cars
-				outputs(indexes(0))=lanehs(i)->expected_output(green_time(0));	
+				outputs(ind(0))=lanehs(i)->expected_output(green_time(0));	
 
 				start_time = green_starts(sg_index(lanes(i).sg)) + lanes(i).output_distances(j)/VP + planned_offset;
 				//first car arrive time
-				outputs(indexes(1))=start_time;									
+				outputs(ind(1))=start_time;									
 				//last car arrive time
-				outputs(indexes(2))= start_time + green_time(0);
-
-				//}
-			}
-		}
-	}
-
-public:
-	int last_offset;
-	int planned_offset;
-	double actual_rating;
-	
-	//! array of existing signal groups
-	Array<string> sgs;
-	//! relative starts of green for signal groups
-	ivec green_starts;
-
-	//! avarage speed of cars
-	int VP;
-
-	void validate() {
-		rv_action = RV(name+"_offset", 1); // <======= example
-
-		for (int i=0; i<sgs.length();i++) {
-			rv_inputs.add(RV(name+"_"+sgs(i),1));
-			
-			//place for expected inputs
-			//rv_expected_traffic.add(RV(name+"_"+sgs(i)+"_exp",3));
-		}
-
-		inputs.set_size(rv_inputs._dsize());
-		//expected_traffic.set_size(rv_expected_traffic._dsize());
-		
-		BaseTrafficAgent::validate();
-	}
-
-	//! returns index of signal group sg
-	int sg_index(string sg) {
-		for (int i=0;i<sgs.length();i++) {
-			if (sgs(i)==sg) {
-				return i;
-			}
-		}
-		return -1;
-	}
-
-	void adapt(const vec &glob_dt) {
-		BaseTrafficAgent::adapt(glob_dt);
-
-		planned_offset=last_offset;
-	}
-	//! counts actual rating using planned_offset and recieved_exps
-	void count_rating() {
+				outputs(ind(2))= start_time + green_time(0);
+			}
+		}
+	};
+
+	//! counts planned rating using offset and recieved_exps
+	double count_rating(const int offset) {
 		double virtual_queue;
 		double t_emptiyng;
@@ -101,55 +64,251 @@
 		double t_cars_begin;
 		double t_cars_end;
+//		vec t_cars_begin;
+//		vec t_cars_end;
 		double t_mixing;
 		bool found;
 
-		actual_rating=0.0;
-
-		for (int i=0;i<lanehs.length();i++) {
+		double rating=0.0;
+
+		for (int i=0;i<lanes.length();i++) {
 
 			//Finding, if we have some expectations
 			found=false;
-			for (int j=0;j<rv_recieved_exps.length();j++) {
-				for (int k=0;k<lanes(i).inputs.length();k++) {
+			for (int k=0;k<lanes(i).inputs.length();k++) { 
+//				t_cars_begin.set_size(lanes(i).inputs.length());
+//				t_cars_end.set_size(lanes(i).inputs.length());
+
+				for (int j=0;j<rv_recieved_exps.length();j++) {
 					int result=rv_recieved_exps.name(j).find(lanes(i).inputs(k)+"-");
 					if (result>=0) {
-						ivec indexes = rv_recieved_exps.dataind(RV(rv_recieved_exps.name(j),3));
-
-						t_cars_begin=recieved_exps(indexes(1));
-						t_cars_end=recieved_exps(indexes(2));
+						ivec ind = rv_recieved_exps.dataind(RV(rv_recieved_exps.name(j),3));
+
+						t_cars_begin=recieved_exps(ind(1));		//TODO now using only last found exp.
+						t_cars_end=recieved_exps(ind(2));
 						found=true;
 					}
 				}
 			}
+
 			if (!found) {
 				t_cars_begin=0;
-				t_cars_end=cycle_time;
-			}
-
+				t_cars_end=cycle_length;
+			}
 			//counting rating
-			t_green_begin=green_starts(sg_index(lanes(i).sg)) + planned_offset;
-			ivec indexes = rv_inputs.dataind(RV(name+"_"+lanes(i).sg,1));
-			t_green_end=t_green_begin+inputs(indexes(0));
-
+			t_green_begin=green_starts(sg_index(lanes(i).sg)) + offset;
+			ivec ind = rv_inputs.dataind(RV(name+"_"+lanes(i).sg,1));
+			t_green_end=t_green_begin+inputs(ind(0));
+
+			//TODO use real queues lenghts
 			if (t_green_begin<t_cars_begin) {
-				virtual_queue=lanehs(i)->queue-(t_cars_begin-t_green_begin)/car_leaving;
+				//virtual_queue=lanehs(i)->queue-(t_cars_begin-t_green_begin)/car_leaving;
+				//virtual_queue=(rand()%8)-(t_cars_begin-t_green_begin)/car_leaving;
+				virtual_queue=5-(t_cars_begin-t_green_begin)/car_leaving;
 			} 
 			else if (t_green_begin>t_cars_begin) {
-				virtual_queue=lanehs(i)->queue+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density();
-			}
-			//TODO lanehs(i)->queue je zatim prazdne => nesmyslne vysledky
+				//virtual_queue=lanehs(i)->queue+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density();
+				//virtual_queue=(rand()%9)+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density();
+				virtual_queue=4+(t_green_begin-t_cars_begin)*lanehs(i)->expected_density();
+			}
 
 			t_emptiyng=virtual_queue/(1/car_leaving - lanehs(i)->expected_density());
 			t_mixing=min(t_green_end,t_cars_end)-max(t_green_begin,t_cars_begin);
 
-			actual_rating+=max((t_mixing-t_emptiyng)*lanehs(i)->expected_density(),0.0);  
-		}
-	}
-
+			rating+=max((t_mixing-t_emptiyng)*lanehs(i)->expected_density(),0.0);  
+		}
+		return rating;
+	}
+
+	//! finds best offset using recieved_exps. Returns found offset
+	int find_best_offset(const int center, int interval) {
+		//! rating if offset is rised
+		double rating_p;
+		//! rating if offset is unchaged (=center)
+		double rating_c;
+		//! rating if offset is lowered
+		double rating_n;
+
+		int new_center;
+
+		rating_p=count_rating(center+interval);
+		rating_c=count_rating(center);
+		rating_n=count_rating(center-interval);
+
+		new_center=center;
+		int max_index=max_of_three(rating_p,rating_c,rating_n);
+		switch (max_index) {
+			case 0:
+				new_center+=interval;
+				break;
+			case 1:
+				break;
+			case 2:
+				new_center-=interval;
+				break;
+		}
+
+		if (interval>2) {
+			interval/=2;
+			new_center=find_best_offset(new_center,interval);
+		}
+
+		return new_center;
+	}
+
+	//! finds if changing neighbour's offset could have positive effect, returns found offset change
+	int find_best_exps(const int offset_change, const string neighbour, double &rating_change) {
+		//! expectations recieved from neighbour
+		vec original_exps;
+		//! expactations after positve change of neighbour's offset
+		vec positive_exps;
+		//! expactations after negative change of neighbour's offset
+		vec negative_exps;
+		//! rating if offset is rised
+		double rating_p;
+		//! rating if offset is unchaged
+		double rating_c;
+		//! rating if offset is lowered
+		double rating_n;		
+		original_exps.set_size(recieved_exps.length());
+		
+		original_exps=recieved_exps;
+		positive_exps=recieved_exps;
+		negative_exps=recieved_exps;
+
+		for (int j=0;j<rv_recieved_exps.length();j++) {
+			int res = rv_recieved_exps.name(j).find("-"+neighbour);
+			if (res>0) {
+				ivec ind = rv_recieved_exps.dataind(RV(rv_recieved_exps.name(j),3));
+				rating_n=count_rating(planned_offset);
+
+				positive_exps(ind(1))+=offset_change;
+				positive_exps(ind(2))+=offset_change;
+
+				negative_exps(ind(1))-=offset_change;
+				negative_exps(ind(2))-=offset_change;
+			}
+		}
+
+		rating_c=count_rating(planned_offset);
+
+		recieved_exps=positive_exps;
+		rating_p=count_rating(planned_offset);
+
+		recieved_exps=negative_exps;
+		rating_n=count_rating(planned_offset);
+
+		recieved_exps=original_exps;
+
+		int max_index=max_of_three(rating_p,rating_c,rating_n);
+		switch (max_index) {
+			case 0:
+				rating_change=rating_p-rating_c;
+				return offset_change;
+				break;
+			case 1:
+				rating_change=0;
+				return 0;
+				break;
+			case 2:
+				rating_change=rating_n-rating_c;
+				return -offset_change;
+				break;
+		}
+		rating_change=NULL;
+		return NULL;
+	}
+
+	//! returns index of signal group sg
+	int sg_index(const string sg) {
+		for (int i=0;i<sgs.length();i++) {
+			if (sgs(i)==sg) {
+				return i;
+			}
+		}
+		return -1;
+	}
+	
+	//! 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 index of maximum of entered values
+	int max_of_three(const double a, const double b, const double c) {
+		int index = a > b ? 0 : 1;
+
+		if (index == 0) {
+			index = a > c ? 0 : 2;
+		} 
+		else {
+			index = b > c ? 1 : 2;
+		}
+		return index;
+	}
+
+public:
+	//! offset set in last simulation step
+	int last_offset;
+	//! actual planned offset to set for next simulation step
+	int planned_offset;
+	//! rating of actual planned offset
+	double planned_rating;
+	
+	//! array of existing signal groups
+	Array<string> sgs;
+	//! relative starts of green for signal groups
+	ivec green_starts;
+
+	//! avarage speed of cars
+	int VP;
+
+	void validate() {
+		rv_action = RV(name+"_offset", 1); // <======= example
+
+		for (int i=0; i<sgs.length();i++) {
+			rv_inputs.add(RV(name+"_"+sgs(i),1));
+		}
+		inputs.set_size(rv_inputs._dsize());
+		
+		BaseTrafficAgent::validate();
+
+		for (int i=0;i<lanehs.length();i++) {
+			ivec index = rv_queues.dataind(RV(lanes(i).queue,1));
+			lanehs(i)->queue_index=index(0);
+		}
+
+
+	}
+
+	void adapt(const vec &glob_dt) {
+		BaseTrafficAgent::adapt(glob_dt);
+			
+		for (int i=0;i<lanehs.length();i++) {
+			lanehs(i)->queue=queues(lanehs(i)->queue_index);		
+		}
+
+		planned_offset=last_offset;
+		
+		//set state variables to default values
+		final_state=false;
+		new_stable_state=false;
+		send_requests=false;
+		need_exps=true;
+		negot_offset=8;
+	}
 
 	void broadcast(Setting& set){
-			
+
 		//ask neighbours for exptected arrive times
-		if (true) {
+		if (need_exps) {
 			for (int i=0; i<neighbours.length(); i++){
 				Setting &msg =set.add(Setting::TypeGroup);
@@ -159,13 +318,11 @@
 				UI::save ( (string)"expected_times_request", msg, "what");
 			}
-		}
-
+			need_exps=false;
+		}
 
 		// broadcast expected cars
 		if (!seznam.empty()) {
+			expected_cars();
 			do {
-				vec value;
-				
-				expected_cars();
 				Setting &msg =set.add(Setting::TypeGroup);
 				UI::save ( seznam.back(), msg, "to");
@@ -175,9 +332,51 @@
 				UI::save ( outputs, msg, "value");
 				seznam.pop_back();
-
-			} while (!seznam.empty());
+			} while (!seznam.empty());			
+		}
+
+		// broadcast new stable state (new stable expectations)
+		if (new_stable_state) {
+			expected_cars();
+			for (int i=0;i<neighbours.length();i++) {
+				Setting &msg = set.add(Setting::TypeGroup);
+				UI::save ( neighbours(i), msg, "to");
+				UI::save ( name, msg, "from");
+				UI::save ( (string)"new_stable_state2", msg, "what");
+				UI::save ( &(rv_outputs), msg, "rv");
+				UI::save ( outputs, msg, "value");
+			}
+			new_stable_state=false;
+		}
+
+		// broadcast requests to change offset(s)
+		if (send_requests) {
+			for (int i=0;i<neighbours.length();i++) {
+				Setting &msg = set.add(Setting::TypeGroup);
+				UI::save ( neighbours(i), msg, "to");
+				UI::save ( name, msg, "from");
+				UI::save ( (string)"offset_change_request", msg, "what");
+				UI::save ( &(rv_change_request), msg, "rv");
+				UI::save ( change_request, msg, "value");
+			}
+			send_requests=false;
+		}
+
+		/*if (reset_negot_offset) {
+			for (int i=0;i<neighbours.length();i++) {
+				Setting &msg = set.add(Setting::TypeGroup);
+				UI::save ( neighbours(i), msg, "to");
+				UI::save ( name, msg, "from");
+				UI::save ( (string)"reset_negot_offset", msg, "what");
+			}
+		}*/
+
+
+		
+		// reached final offset. Log value?
+		if (final_state) {
 			
-		}
-
+			//cout << "Jmenuji se "<<name<< " a skoncil jsem na offsetu " << planned_offset << " s hodnocenim " << planned_rating <<endl;
+			final_state=false;
+		}
 	}
 
@@ -185,4 +384,5 @@
 		string what;
 		string to;
+
 		string from;
 		vec value;
@@ -194,15 +394,71 @@
 		UI::get(rv, msg, "rv");
 		UI::get(value, msg, "value");
+		
 		if (what=="expected_times_request"){ 
-			recieved_exp_request=true;
 			seznam.push_back(from);
-
 		} 
 		else if (what=="new_expected_cars") {
 			rv_recieved_exps=*rv;
 			recieved_exps=value;
-			count_rating();
-			//TODO skutecne vyjednavani
-		}
+			
+			last_offset=planned_offset;
+
+			planned_offset=find_best_offset(planned_offset,8);
+			planned_offset=normalize_offset(planned_offset);
+			
+
+			/*if (planned_offset!=last_offset) {
+				reset_negot_offset=true;
+			}*/
+			planned_rating=count_rating(planned_offset);
+			// we have new stable state to broadcast
+			new_stable_state=true;
+		}
+		else if (what=="new_stable_state2") {
+			rv_recieved_exps=*rv;
+			recieved_exps=value;
+			planned_rating=count_rating(planned_offset);
+
+			for (int i=0;i<neighbours.length();i++) {
+				rv_change_request.add(RV(neighbours(i)+"_change",2));
+				change_request.set_size(rv_change_request._dsize()); 
+				ivec ind=rv_change_request.dataind(RV(neighbours(i)+"_change",2));
+				// offset change
+				change_request(ind(0))=find_best_exps(negot_offset,neighbours(i),rating_change);
+				// rating change
+				change_request(ind(1))=rating_change;
+			}
+
+			if (negot_offset>2) { 
+				negot_offset/=2;
+					send_requests=true;
+			}
+			else {
+				final_state=true;
+			}
+		}
+		else if (what=="offset_change_request") {
+			double final_rating_diff;
+
+			rv_recieved_changes=*rv;
+			recieved_changes=value;
+
+			for (int i=0;i<rv_recieved_changes.length();i++) {
+
+				ivec ind=rv_recieved_changes.dataind(RV(rv_recieved_changes.name(i),2));
+
+				final_rating_diff=-planned_rating+count_rating(planned_offset+(int)recieved_changes(ind(0)))-recieved_changes(ind(0));
+				if (final_rating_diff>0) {
+					planned_offset+=(int)recieved_changes(ind(0));
+					planned_rating+=final_rating_diff;
+					accepted_from=from;
+				}
+			}
+			//need_exps=true;s
+			new_stable_state=true;
+		}
+		/*else if (what=="reset_negot_offset") {
+			negot_offset=8;
+		}*/
 		else {
 			BaseTrafficAgent::receive(msg);
@@ -213,5 +469,4 @@
 		BaseTrafficAgent::ds_register(ds);
 		action2ds.set_connection( ds._urv(), rv_action);
-
 	}
 
@@ -219,7 +474,10 @@
 		BaseTrafficAgent::from_setting(set);
 		
+		srand(time(NULL));
+
 		car_leaving=2;
 		VP=45;
-		recieved_exp_request=false;
+
+		negot_offset=8;
 		
 		// load from file
@@ -232,7 +490,8 @@
 		vec action;
 		action.set_size(rv_action._dsize());
-		
+
 		ivec index = rv_action.dataind(RV(name+"_offset",1));
 		action(index(0))=planned_offset;
+		last_offset=planned_offset;
 
 		action2ds.filldown(action,glob_ut);
Index: /applications/doprava/traffic_agent.cpp
===================================================================
--- /applications/doprava/traffic_agent.cpp (revision 986)
+++ /applications/doprava/traffic_agent.cpp (revision 1026)
@@ -31,5 +31,5 @@
 	}
 
-	exp_output=last_inputs*(green_time/agent->step_time);
+	exp_output=last_inputs*(green_time/agent->step_length);
 
 	////////
@@ -43,5 +43,5 @@
 	
 	for (int i=0;i<inputs.length();i+=2) {
-		density+=inputs(i)/agent->step_time;
+		density+=inputs(i)/agent->step_length;
 	}
 
@@ -51,6 +51,6 @@
 void BaseTrafficAgent::from_setting(const Setting& set)
 {
-	step_time=90;
-	cycle_time=80;
+	step_length=90;
+	cycle_length=80;
 
 	bdm::Participant::from_setting(set);
Index: /applications/doprava/main_loop.cpp
===================================================================
--- /applications/doprava/main_loop.cpp (revision 932)
+++ /applications/doprava/main_loop.cpp (revision 1026)
@@ -36,5 +36,7 @@
 	// LOGGER
 	shared_ptr<logger> L = UI::build <logger>( Cfg, "logger" );
-	if (!L) {L=new stdlog();} // DEFAULT LOGGER <== poor choice, use better ones
+	if (!L) {
+		L=new stdlog(); // DEFAULT LOGGER <== poor choice, use better ones
+	} 
 
 
@@ -105,4 +107,7 @@
 		} 
 		while ((Queue.getLength()>0) && (cycle<max_cycles));
+		if (cycle==max_cycles) {
+			bdm_warning("Reached maximum number of cycles");
+		}
 
 		for ( int i=0; i<Ags.length(); i++ ) {
