Index: applications/doprava/traffic_agent_offset.h
===================================================================
--- applications/doprava/traffic_agent_offset.h (revision 1120)
+++ applications/doprava/traffic_agent_offset.h (revision 1127)
@@ -24,12 +24,10 @@
 	vec next_exps;
 
+	//! expectations recieved from neighbours
 	RV rv_recieved_changes;
 	vec recieved_changes;
 
-	//! name of agent, whose change we have accepted
-	string accepted_from;
-
-	//TODO currently useless list of neighbours asking for expectations
-	list<string> seznam;
+	//! list of agents, who request expected cars
+	list<string> requesters;
 	
 	double car_leaving; //s; how long is 1 car leaving queue
@@ -40,6 +38,6 @@
 	bool send_requests;
 	bool final_state;
-	//bool reset_negot_offset;
-
+	
+	//! determines wheteher agent actively communicates
 	int passive;
 
@@ -58,21 +56,17 @@
 		datalink exp2outputs;
 		vec exp;
+		string group_name;
+		double green_time;
+		int index;
 
 		for (int i=0;i<lanes.length();i++) {
 			for (int j=0;j<lanes(i).outputs.length();j++) {
 				if (lanes(i).outputs(j)!="DUMMY_DET") {
-					string group = name+"_"+lanes(i).sg;		//e.g. 495_VA
+					group_name = name+"_"+lanes(i).sg;		//e.g. 495_VA
 					
-					int index=group_index(group);
-					
-					double green_time=green_times(index)*cycle_length;
-
-					//ivec green_index=rv_inputs.dataind(RV(group,1));
-					//vec green_time=inputs(green_index);
-
-					//cout << "Green time "<<green_time<<endl;
+					index=group_index(group_name);
+					green_time=green_times(index)*cycle_length;
 
 					rv_exp=RV(lanes(i).outputs(j)+"-"+name+"_"+to_string(i),3);
-
 					exp.set_size(rv_exp._dsize());
 					exp2outputs.set_connection(rv_exp,rv_outputs);
@@ -88,5 +82,4 @@
 					//TODO pushup az na konec
 					exp2outputs.pushup(outputs,exp);
-
 				}			
 			}
@@ -95,5 +88,5 @@
 
 	//! counts planned rating using offset and recieved_exps
-	double count_rating(const int offset) {
+	double count_rating(const int offset, const vec recieved_exps, const RV rv_recieved_exps) {
 		double virtual_queue;
 		double t_green_begin;
@@ -103,9 +96,8 @@
 		vec t_cars_end;
 		bool found;
+		ivec ind;
+		string group_name;
 
 		double rating=0.0;
-
-		RV rv_vector;
-		vec vector;
 
 		for (int i=0;i<lanes.length();i++) {
@@ -113,5 +105,4 @@
 			//Finding, if we have some expectations
 			found=false;
-			
 			for (int k=0;k<lanes(i).inputs.length();k++) { 
 				int l=0;		
@@ -125,7 +116,5 @@
 						cars_count.set_size(l+1,true);
 
-
-						rv_vector = RV(rv_recieved_exps.name(j),3);
-						ivec ind = rv_vector.dataind(rv_recieved_exps);
+						ind = RV(rv_recieved_exps.name(j),3).dataind(rv_recieved_exps);
 
 						cars_count(l)=recieved_exps(ind(0));
@@ -137,15 +126,13 @@
 					}
 				}
-
 			}
 			if (found) {			
 
 				//counting rating
-				string group = name+"_"+lanes(i).sg;		//e.g. 495_VA
-				int index=group_index(group);
+				group_name = name+"_"+lanes(i).sg;		//e.g. 495_VA
+				int index=group_index(group_name);
 
 				t_green_begin=green_starts(index) + offset;
-				double green_time=green_times(index)*cycle_length;
-				t_green_end=t_green_begin+green_time;
+				t_green_end=t_green_begin + green_times(index)*cycle_length;
 
 				/************** counting with all exps ****************/
@@ -235,7 +222,7 @@
 		int new_center;
 
-		rating_p=count_rating(center+interval);
-		rating_c=count_rating(center);
-		rating_n=count_rating(center-interval);
+		rating_p=count_rating(center+interval, recieved_exps, rv_recieved_exps);
+		rating_c=count_rating(center, recieved_exps, rv_recieved_exps);
+		rating_n=count_rating(center-interval, recieved_exps, rv_recieved_exps);
 
 		new_center=center;
@@ -260,5 +247,5 @@
 	}
 
-	//! finds if changing neighbour's offset could have positive effect, returns found offset change
+	//! finds if changing neighbour's offset could have positive effect, returns found offset change and stores chage of rating to rating_change
 	int find_best_exps(const int offset_change, const string neighbour, double &rating_change) {
 		//! expectations recieved from neighbour
@@ -268,5 +255,5 @@
 		//! expactations after negative change of neighbour's offset
 		vec negative_exps;
-		//! rating if offset is rised
+		//! rating if offset is raised
 		double rating_p;
 		//! rating if offset is unchaged
@@ -285,6 +272,4 @@
 				ivec ind = RV(rv_recieved_exps.name(j),3).dataind(rv_recieved_exps);
 
-				rating_n=count_rating(planned_offset);
-
 				positive_exps(ind(1))+=offset_change;
 				positive_exps(ind(2))+=offset_change;
@@ -295,13 +280,7 @@
 		}
 
-		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;
+		rating_c=count_rating(planned_offset, recieved_exps, rv_recieved_exps);
+		rating_p=count_rating(planned_offset, positive_exps,  rv_recieved_exps);
+		rating_n=count_rating(planned_offset, negative_exps,  rv_recieved_exps);
 
 		int max_index=max_i_of_three(rating_p,rating_c,rating_n);
@@ -324,46 +303,5 @@
 	}
 
-	// pravdepodobne s chybou, momentalne se nepouziva
-	void split_exps() {
-		ivec ind;
-		RV rv_exp;
-		datalink recieved2next;
-
-		rv_next_exps=RV();
-		for (int i=0;i<rv_recieved_exps.length();i++) {
-			if (rv_recieved_exps.size(i)==3) {		
-				rv_exp=RV(rv_recieved_exps.name(i),3);
-				ind = rv_exp.dataind(rv_recieved_exps);
-				//cout << "ind " << ind << endl;
-
-				int next_cycle_end=2 * cycle_length - (actual_time % cycle_length);
-				if (recieved_exps(ind(1))>next_cycle_end) {
-					rv_next_exps.add(rv_exp);
-					next_exps.set_size(rv_next_exps._dsize());
-					
-					if (actual_time==630) {
-						cout << "rv_next_exps " << rv_next_exps.to_string() << endl;
-						cout << "rv_recived_exps " << rv_recieved_exps.to_string() << endl;
-					}
-
-					recieved2next.set_connection(rv_next_exps,rv_recieved_exps);
-					recieved2next.filldown(recieved_exps,next_exps);
-					rv_recieved_exps=rv_recieved_exps.subt(rv_exp);
-				}
-				else if (recieved_exps(ind(2))>next_cycle_end) {
-					rv_next_exps.add(rv_exp);
-					next_exps.set_size(rv_next_exps._dsize());
-
-					ivec ind2=rv_exp.dataind(rv_next_exps);
-					next_exps(ind2(0))=recieved_exps(ind(0)); //TODO to neni spravne
-					next_exps(ind2(1))=next_cycle_end; 
-					next_exps(ind2(2))=recieved_exps(ind(2));
-					recieved_exps(ind(2))=next_cycle_end;
-				}
-			}
-		}
-	}
-
-	//! returns index of signal group group
+	//! returns index of signal group "group"
 	int group_index(const string group) {
 		for (int i=0;i<green_names.length();i++) {
@@ -404,20 +342,4 @@
 	}
 
-
-	
-	//! 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) {
-			if (time<0) {
-				time+=cycle_length;
-			}
-			else {
-				time-=cycle_length;
-			}
-		}
-		return time;
-	}
-
 	//! converts t to string
 	template <class T> inline string to_string (const T& t)
@@ -462,15 +384,11 @@
 	//! actual planned offset to set for next simulation step
 	int planned_offset;
-	//! planned offset for cycle after next cycle
-	int planned_next_offset;
 	//! rating of actual planned offset
-	double planned_rating;
-	//! rating of planned next offset
-	double planned_next_rating;	
+	double planned_rating;	
 	//! avarage speed of cars
 	int VP;
 
 	void validate() {
-		rv_action = RV(name+"_offset", 1); // <======= example
+		rv_action = RV(name+"_offset", 1);
 
 		for (int i=0; i<green_names.length();i++) {
@@ -510,5 +428,4 @@
 			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");
@@ -519,18 +436,17 @@
 
 		// broadcast expected cars
-		if (!seznam.empty()) {
+		if (!requesters.empty()) {
 			double a;
 			expected_cars();
 			do {
 				Setting &msg =set.add(Setting::TypeGroup);
-				UI::save ( seznam.back(), msg, "to");
+				UI::save ( requesters.back(), msg, "to");
 				UI::save ( name, msg, "from");
 				UI::save ( (string)"new_expected_cars", msg, "what");
 				UI::save ( &(rv_outputs), msg, "rv");
 				UI::save ( outputs, msg, "value");
-				seznam.pop_back();
+				requesters.pop_back();
 				a=outputs (10);
-				
-			} while (!seznam.empty());			
+			} while (!requesters.empty());			
 		}
 
@@ -542,5 +458,5 @@
 				UI::save ( neighbours(i), msg, "to");
 				UI::save ( name, msg, "from");
-				UI::save ( (string)"new_stable_state2", msg, "what");
+				UI::save ( (string)"stable_state", msg, "what");
 				UI::save ( &(rv_outputs), msg, "rv");
 				UI::save ( outputs, msg, "value");
@@ -561,19 +477,7 @@
 			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?
+	
+		// reached final offset.
 		if (final_state) {
-			cout << "Jmenuji se "<<name<< " a skoncil jsem na offsetu " << planned_offset << " s hodnocenim " << planned_rating <<endl;
 			final_state=false;
 		}
@@ -583,5 +487,4 @@
 		string what;
 		string to;
-
 		string from;
 		vec value;
@@ -595,39 +498,32 @@
 		
 		if (what=="expected_times_request"){ 
-			seznam.push_back(from);
+			requesters.push_back(from);
 		} 
 		else if (what=="new_expected_cars") {
 			rv_recieved_exps=*rv;
 			recieved_exps=value;
-			//split_exps();
-			
 			
 			if (!passive) {
-				last_offset=planned_offset;
 				planned_offset=find_best_offset(planned_offset,8);
 				planned_offset=normalize_offset(planned_offset);
 			}
+
+			planned_rating=count_rating(planned_offset, recieved_exps, rv_recieved_exps);
 			
-
-			/*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") {
+		else if (what=="stable_state") {
 			rv_recieved_exps=*rv;
 			recieved_exps=value;
-			//split_exps();
-			planned_rating=count_rating(planned_offset);
+
+			planned_rating=count_rating(planned_offset, recieved_exps, rv_recieved_exps);
 
 			if (!passive) {
-
 				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(neighbours(i)+"_change",2).dataind(rv_change_request);
+					
 					// offset change
 					change_request(ind(0))=find_best_exps(negot_offset,neighbours(i),rating_change);
@@ -636,5 +532,5 @@
 				}
 
-				if (negot_offset>negot_limit) { 
+				if (negot_offset>=negot_limit) { 
 					negot_offset/=2;
 					send_requests=true;
@@ -642,5 +538,4 @@
 				else {
 					final_state=true;
-
 				}
 			}
@@ -659,18 +554,14 @@
 				ivec ind=RV(rv_recieved_changes.name(i),2).dataind(rv_recieved_changes);
 
-				final_rating_diff=-planned_rating+count_rating(planned_offset+(int)recieved_changes(ind(0)))+recieved_changes(ind(1));
+				final_rating_diff=-planned_rating+count_rating(planned_offset+(int)recieved_changes(ind(0)), recieved_exps, rv_recieved_exps)+recieved_changes(ind(1));
 				if (final_rating_diff>=0) {
 					planned_offset+=(int)recieved_changes(ind(0));
 					planned_offset=normalize_offset(planned_offset);
 					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);
@@ -684,7 +575,7 @@
 
 	void from_setting(const Setting &set) {
+		RV rv_exp;
+
 		BaseTrafficAgent::from_setting(set);
-
-		RV rv_exp;
 
 		car_leaving=2;
@@ -701,8 +592,4 @@
 		passive=0;
 		
-		// load from file
-		//UI::get(sgs, set, "sgs", UI::compulsory);	
-
-		//UI::get(green_starts, set, "green_starts", UI::compulsory);
 		UI::get(last_offset, set, "offset", UI::compulsory);
 		UI::get(passive, set, "passive", UI::optional);
@@ -723,9 +610,8 @@
 	void act(vec &glob_ut){
 		if (negot_cycle==cycle_count) {
-		
 			vec action;
 			action.set_size(rv_action._dsize());
 		
-			ivec index = rv_action.dataind(RV(name+"_offset",1));
+			ivec index = RV(name+"_offset",1).dataind(rv_action);
 
 			action(index(0))=normalize_offset(total_offset/cycle_count, false);
@@ -734,13 +620,13 @@
 			total_offset=0;
 			negot_cycle=1;
-
 		}
 		else {
 			total_offset+=planned_offset;
-
 			negot_cycle++;
 		}
-
 		last_offset=planned_offset;
+	}
+
+	void step() {
 		actual_time+=step_length;
 	}
@@ -748,15 +634,15 @@
 	void log_register(logger &l, const string &prefix){
 		if ( log_level[logoffset]){
-			l.add_vector ( log_level, logoffset, RV("mujoffset",1), prefix );	//TODO 
+			l.add_vector ( log_level, logoffset, RV("1",2), "x"+prefix );	//TODO 
 		}
 	}
 	void log_write() const {
 		if (log_level[logoffset]){
-			vec offset_vec(1);
-			offset_vec(0)=(double)planned_offset;
-			log_level.store(logoffset, offset_vec);
-			//log_level.store(logoffset, planned_rating);
-		}
-	}
+			vec offset_vec(2);
+			offset_vec(0)=planned_offset;
+			offset_vec(1)=planned_rating;
+			log_level.store(logoffset, offset_vec);			
+		}
+	} 
 };
 UIREGISTER(GreenWaveTrafficAgent);
