Index: applications/robust/main.cpp
===================================================================
--- applications/robust/main.cpp (revision 1299)
+++ applications/robust/main.cpp (revision 1300)
@@ -20,5 +20,5 @@
 int main ( int argc, char* argv[] ) {
 	
-	
+	/*
 	vector<vector<vector<string>>> string_lists;
 	string_lists.push_back(vector<vector<string>>());
@@ -55,5 +55,5 @@
 	for(int j = 0;j<string_lists.size();j++)
 	{ 
-		
+		/*
 		for(int i = 0;i<string_lists[j].size()-1;i++)
 		{
@@ -118,8 +118,9 @@
 		myfile.close();
 	}
-    
-
-	// emlig* emlig1 = new emlig(emlig_size);
-	// emlig* emlig2 = new emlig(emlig_size);
+    */
+	
+	
+	emlig* emlig1 = new emlig(emlig_size);
+	emlig* emlig2 = new emlig(emlig_size);
 	
 	/*
@@ -141,32 +142,37 @@
 	}*/
 	
-	/*
-    vec condition5 = "-0.3 1.7 1.5";
+	
+    vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5";
+
 	emlig1->add_condition(condition5);
 	
-	vec condition1 = "1.0 1.0 1.01";
-	emlig1->add_condition(condition1);
-	emlig2->add_condition(condition1);
-
-	vec condition2 = "-1.0 1.0 1.0";
-	emlig1->add_condition(condition2);
-	emlig2->add_condition(condition2);
-
-	vec condition3 = "0.5 -1.01 1.0";
-	emlig1->add_condition(condition3);
-	emlig2->add_condition(condition3);
-	
-
-	vec condition4 = "-0.5 -1.0 1.0";
-	emlig1->add_condition(condition4);
-	cout << "************************************************" << endl;
-	emlig2->add_condition(condition4);
-	cout << "************************************************" << endl;
-
+	//vec condition1a = "1.0 1.0 1.01";
+	//vec condition1b = "1.0 1.0 1.01";
+	//emlig1->add_condition(condition1a);
+	//emlig2->add_condition(condition1b);
+
+	//vec condition2a = "-1.0 1.0 1.0";
+	//vec condition2b = "-1.0 1.0 1.0";
+	//emlig1->add_condition(condition2a);
+	//emlig2->add_condition(condition2b);
+
+	//vec condition3a = "0.5 -1.01 1.0";
+	//vec condition3b = "0.5 -1.01 1.0";
+
+	//emlig1->add_condition(condition3a);
+	//emlig2->add_condition(condition3b);	
+
+	//vec condition4a = "-0.5 -1.0 1.0";
+	//vec condition4b = "-0.5 -1.0 1.0";	
+
+	//emlig1->add_condition(condition4a);
+	//cout << "************************************************" << endl;
+	//emlig2->add_condition(condition4b);
+	//cout << "************************************************" << endl;
+	
 	//cout << emlig1->minimal_vertex->get_coordinates();
-
+	
 	emlig1->remove_condition(condition5);
-	cout << "************************************************" << endl;
-	
+
 	//emlig1->step_me(0);
 	//emlig2->step_me(0);
Index: applications/robust/robustlib.cpp
===================================================================
--- applications/robust/robustlib.cpp (revision 1299)
+++ applications/robust/robustlib.cpp (revision 1300)
@@ -66,5 +66,5 @@
 			toprow* as_toprow = (toprow*)this;
 
-			vec cur_condition = as_toprow->condition.get(1,as_toprow->condition.size()-1);
+			vec cur_condition = as_toprow->condition_sum.get(1,as_toprow->condition_sum.size()-1);
 
 			// cout << as_toprow->condition << endl;			
@@ -91,5 +91,5 @@
 				cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl;
 
-				a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition[0];
+				a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition_sum[0];
 				
 
@@ -127,5 +127,5 @@
 						}
 
-						double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition[0];
+						double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition_sum[0];
 						if(a_m<current_emlig->likelihood_value)
 						{
Index: applications/robust/robustlib.h
===================================================================
--- applications/robust/robustlib.h (revision 1299)
+++ applications/robust/robustlib.h (revision 1300)
@@ -48,4 +48,17 @@
 class polyhedron;
 
+class condition
+{	
+public:
+	vec value;	
+
+	int multiplicity;
+
+	condition(vec value)
+	{
+		this->value = value;
+		multiplicity = 1;
+	}
+};
 
 
@@ -77,4 +90,6 @@
 	set<vertex*> vertices;
 
+	set<condition*> parentconditions;
+
 	/// A list used for storing children that lie in the positive region related to a certain condition
 	list<polyhedron*> positivechildren;
@@ -124,4 +139,6 @@
 
 		totally_neutral = NULL;
+
+		mergechild = NULL;		
 	}
 	
@@ -157,14 +174,14 @@
 	
 
-	void set_state(double state_indicator, actions action)
+	int set_state(double state_indicator, actions action)
 	{
 		switch(action)
 		{
 			case MERGE:
-				merge_state = (int)sign(state_indicator);			
-			break;
+				merge_state = (int)sign(state_indicator);
+				return merge_state;			
 			case SPLIT:
 				split_state = (int)sign(state_indicator);
-			break;
+				return split_state;		
 		}
 	}
@@ -210,5 +227,5 @@
 	vertex(vec coordinates)
 	{
-		this->coordinates = coordinates;
+		this->coordinates   = coordinates;
 
 		vertices.insert(this);
@@ -225,5 +242,5 @@
 	void push_coordinate(double coordinate)
 	{
-		coordinates = concat(coordinates,coordinate);
+		coordinates  = concat(coordinates,coordinate);		
 	}
 
@@ -231,8 +248,7 @@
 	/// (not given by reference), but a new copy is created (they are given by value).
 	vec get_coordinates()
-	{		
+	{
 		return coordinates;
 	}
-
 		
 };
@@ -250,5 +266,5 @@
 
 	/// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation
-	vec condition;
+	vec condition_sum;
 
 	int condition_order;
@@ -258,10 +274,15 @@
 
 	/// Constructor creating a toprow from the condition
-	toprow(vec condition, int condition_order)
-	{
-		this->condition = condition;
+	toprow(condition *condition, int condition_order)
+	{
+		this->condition_sum   = condition->value;
 		this->condition_order = condition_order;
 	}
 
+	toprow(vec condition_sum, int condition_order)
+	{
+		this->condition_sum = condition_sum;
+	}
+
 	double integrate_simplex(set<vertex*> simplex, char c);
 
@@ -269,17 +290,4 @@
 
 
-class condition
-{	
-public:
-	vec value;	
-
-	int multiplicity;
-
-	condition(vec value)
-	{
-		this->value = value;
-		multiplicity = 1;
-	}
-};
 
 
@@ -603,25 +611,25 @@
 	
 
-	void alter_toprow_conditions(vec condition, bool should_be_added)
+	void alter_toprow_conditions(condition *condition, bool should_be_added)
 	{
 		for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly)
 		{
-			double product = 0;
-
 			set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();
 
 			do
 			{
-				product = (*vertex_ref)->get_coordinates()*condition;
-			}
-			while(product == 0);
+				vertex_ref++;
+			}
+			while((*vertex_ref)->parentconditions.find(condition)==(*vertex_ref)->parentconditions.end());
+
+			double product = (*vertex_ref)->get_coordinates()*condition->value;
 
 			if((product>0 && should_be_added)||(product<0 && !should_be_added))
 			{
-				((toprow*) horiz_ref)->condition += condition;
+				((toprow*) horiz_ref)->condition_sum += condition->value;
 			}
 			else
 			{
-				((toprow*) horiz_ref)->condition -= condition;
+				((toprow*) horiz_ref)->condition_sum -= condition->value;
 			}				
 		}
@@ -630,9 +638,9 @@
 
 
-	void send_state_message(polyhedron* sender, vec toadd, vec toremove, int level)
+	void send_state_message(polyhedron* sender, condition *toadd, condition *toremove, int level)
 	{			
 
-		bool shouldmerge = (toremove.size() != 0);
-		bool shouldsplit    = (toadd.size() != 0);
+		bool shouldmerge    = (toremove == NULL);
+		bool shouldsplit    = (toadd == NULL);
 		
 		if(shouldsplit||shouldmerge)
@@ -644,5 +652,6 @@
 				current_parent->message_counter++;
 
-				bool is_last = (current_parent->message_counter == current_parent->number_of_children());
+				bool is_last  = (current_parent->message_counter == current_parent->number_of_children());
+				bool is_first = (current_parent->message_counter == 1);
 
 				if(shouldmerge)
@@ -651,8 +660,8 @@
 					int parent_state = current_parent->get_state(MERGE);
 
-					if(parent_state == 0)
-					{
-						current_parent->set_state(child_state, MERGE);						
-					}
+					if(parent_state == 0||is_first)
+					{
+						parent_state = current_parent->set_state(child_state, MERGE);						
+					}					
 
 					if(child_state == 0)
@@ -686,4 +695,8 @@
 						{
 							for_merging[level+1].push_back(current_parent);							
+						}
+						else
+						{
+							current_parent->set_state(0,MERGE);							
 						}
 
@@ -751,5 +764,5 @@
 									current_parent->set_state(-1, SPLIT);
 
-									((toprow*)current_parent)->condition-=toadd;
+									((toprow*)current_parent)->condition_sum-=toadd->value;
 
 									
@@ -759,5 +772,5 @@
 									current_parent->set_state(1, SPLIT);
 
-									((toprow*)current_parent)->condition+=toadd;									
+									((toprow*)current_parent)->condition_sum+=toadd->value;									
 								}
 								else
@@ -921,5 +934,4 @@
 	}
 
-
 	void add_and_remove_condition(vec toadd, vec toremove)
 	{
@@ -954,5 +966,5 @@
 						(*ref)->multiplicity--;
 
-						alter_toprow_conditions(toremove,false);
+						alter_toprow_conditions(*ref,false);
 
 						should_remove = false;
@@ -971,5 +983,5 @@
 					(*ref)->multiplicity++;
 
-					alter_toprow_conditions(toadd,true);
+					alter_toprow_conditions(*ref,true);
 
 					should_add = false;
@@ -982,8 +994,13 @@
 		}
 
+		condition* condition_to_remove = NULL;
+
 		if(toremove_ref!=conditions.end())
 		{
 			conditions.erase(toremove_ref);
-		}
+			condition_to_remove = *toremove_ref;
+		}
+
+		condition* condition_to_add = NULL;
 
 		if(condition_should_be_added)
@@ -1000,13 +1017,16 @@
 			if(should_add||should_remove)
 			{
-				vec appended_vec = current_vertex->get_coordinates();
-				appended_vec.ins(0,-1.0);
+				vec appended_coords = current_vertex->get_coordinates();
+				appended_coords.ins(0,-1.0);				
 
 				if(should_add)
 				{
-					double local_condition = toadd*appended_vec;
+					double local_condition = 0;// = toadd*(appended_coords.first/=appended_coords.second);
+
+					local_condition = appended_coords*toadd;
 
 					current_vertex->set_state(local_condition,SPLIT);
 
+					/// \TODO There should be a rounding error tolerance used here to insure we are not having too many points because of rounding error.
 					if(local_condition == 0)
 					{
@@ -1021,22 +1041,47 @@
 			
 				if(should_remove)
+				{					
+					set<condition*>::iterator cond_ref;
+					
+					for(cond_ref = current_vertex->parentconditions.begin();cond_ref!=current_vertex->parentconditions.end();cond_ref++)
+					{
+						if(*cond_ref == condition_to_remove)
+						{
+							break;
+						}
+					}
+
+					if(cond_ref!=current_vertex->parentconditions.end())
+					{
+						current_vertex->parentconditions.erase(cond_ref);
+						current_vertex->set_state(0,MERGE);
+						for_merging[0].push_back(current_vertex);
+					}
+					else
+					{
+						double local_condition = toremove*appended_coords;
+						current_vertex->set_state(local_condition,MERGE);
+					}
+				}				
+			}
+
+			send_state_message(current_vertex, condition_to_add, condition_to_remove, 0);		
+			
+		}
+
+		
+		
+		if(should_remove)
+		{
+			for(int i = 0;i<for_merging.size();i++)
+			{
+				for(list<polyhedron*>::iterator merge_ref = for_merging[i].begin();merge_ref!=for_merging[i].end();merge_ref++)
 				{
-					double local_condition = toremove*appended_vec;
-
-					current_vertex->set_state(local_condition,MERGE);
-
-					if(local_condition == 0)
-					{
-						for_merging[0].push_back(current_vertex);
-					}
-				}				
-			}
-
-			send_state_message(current_vertex, toadd, toremove, 0);		
-			
-		}
-
-		if(should_remove)
-		{
+					cout << (*merge_ref)->get_state(MERGE) << ",";
+				}
+
+				cout << endl;
+			}
+
 			set<vertex*> vertices_to_be_reduced;			
 			
@@ -1063,5 +1108,5 @@
 						toprow* current_negative = (toprow*)(*merge_ref)->negativeparent;
 
-						current_positive->condition -= toremove;
+						current_positive->condition_sum -= toremove;
 						current_positive->condition_order--;
 						
@@ -1225,4 +1270,5 @@
 			}			
 		}
+		
 
 		if(should_add)
@@ -1247,9 +1293,9 @@
 						
 						vec extended_coord2 = coordinates2;
-						extended_coord2.ins(0,-1.0);
-
-						double t = (-toadd*extended_coord2)/((toadd(1,toadd.size()-1)*(coordinates1-coordinates2)));
-
-						vec new_coordinates = coordinates2+t*(coordinates1-coordinates2);					
+						extended_coord2.ins(0,-1.0);						
+
+						double t = (-toadd*extended_coord2)/(toadd(1,toadd.size()-1)*(coordinates1-coordinates2));						
+
+						vec new_coordinates = (1-t)*coordinates2+t*coordinates1;						
 
 						// cout << "c1:" << coordinates1 << endl << "c2:" << coordinates2 << endl << "nc:" << new_coordinates << endl;
@@ -1263,5 +1309,5 @@
 						toprow* neutral_toprow = new toprow();
 						
-						neutral_toprow->condition       = ((toprow*)current_polyhedron)->condition; // tohle tu bylo driv: zeros(number_of_parameters+1);
+						neutral_toprow->condition_sum   = ((toprow*)current_polyhedron)->condition_sum; // tohle tu bylo driv: zeros(number_of_parameters+1);
 						neutral_toprow->condition_order = ((toprow*)current_polyhedron)->condition_order+1;
 
@@ -1279,6 +1325,6 @@
 					// cout << ((toprow*)current_polyhedron)->condition << endl << toadd << endl;
 
-					toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition+toadd, ((toprow*)current_polyhedron)->condition_order+1);
-					toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition-toadd, ((toprow*)current_polyhedron)->condition_order+1);
+					toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition_sum+toadd, ((toprow*)current_polyhedron)->condition_order+1);
+					toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition_sum-toadd, ((toprow*)current_polyhedron)->condition_order+1);
 
 					positive_poly->my_emlig = this;
@@ -1738,24 +1784,29 @@
 };
 
-/*
+
 
 //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density
-class RARX : public BM
+class RARX //: public BM
 {
 private:
 
-	emlig posterior;
+	emlig* posterior;
+
+	int window_size;
 
 public:
-	RARX():BM()
-	{
+	RARX(int number_of_parameters, const int window_size)//:BM()
+	{
+		posterior = new emlig(number_of_parameters);
+
+		this->window_size = window_size;
 	};
 
-	void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec)
+	void bayes(const itpp::vec &yt, const itpp::vec &cond = "")
 	{
 		
 	}
 
-};*/
+};
 
 
