Index: /applications/robust/robustlib.h
===================================================================
--- /applications/robust/robustlib.h (revision 1204)
+++ /applications/robust/robustlib.h (revision 1207)
@@ -130,12 +130,131 @@
 	int number_of_children()
 	{
-		return children.size()+positivechildren.size()+negativechildren.size()+neutralchildren.size();
-	}
-
-	void send_state_message(bool shouldsplit, bool shouldmerge)
+		return children.size();
+	}
+
+	
+};
+
+/// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse
+/// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space.
+class vertex : public polyhedron
+{
+	/// A dynamic array representing coordinates of the vertex
+	vec coordinates;	
+
+	enum actions {MERGE, SPLIT};
+
+public:
+
+
+
+	/// Default constructor
+	vertex();
+
+	/// Constructor of a vertex from a set of coordinates
+	vertex(vec coordinates)
+	{
+		this->coordinates = coordinates;
+	}
+
+	/// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter
+	/// space of certain dimension is established, but the dimension is not known when the vertex is created.
+	void push_coordinate(double coordinate)
+	{
+		coordinates = concat(coordinates,coordinate);
+	}
+
+	/// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer 
+	/// (not given by reference), but a new copy is created (they are given by value).
+	vec get_coordinates()
+	{		
+		return coordinates;
+	}
+
+		
+};
+
+/// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates
+/// it from polyhedrons in other rows. 
+class toprow : public polyhedron
+{
+	
+public:
+	/// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation
+	vec condition;
+
+	/// Default constructor
+	toprow();
+
+	/// Constructor creating a toprow from the condition
+	toprow(vec condition)
+	{
+		this->condition = condition;
+	}
+
+};
+
+class condition
+{	
+public:
+	vec value;	
+
+	int multiplicity;
+
+	condition(vec value)
+	{
+		this->value = value;
+		multiplicity = 1;
+	}
+}
+
+
+//! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density
+class emlig // : eEF
+{
+
+	/// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result
+	/// of data update from Bayesian estimation or set by the user if this emlig is a prior density
+	vector<vector<polyhedron*>> statistic;
+
+	vector<vector<polyhedron*>> for_splitting;
+		
+	vector<vector<polyhedron*>> for_merging;
+
+	vector<condition*> conditions;
+
+	double normalization_factor;
+
+	void alter_toprow_conditions(vec condition, bool should_be_added)
+	{
+		for(vector<polyhedron*>::iterator horiz_ref = statistic[statistic.size()-1].begin();horiz_ref<statistic[statistic.size()-1].end();horiz_ref++)
+		{
+			double product = 0;
+
+			vector<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin();
+
+			do
+			{
+				product = (*vertex_ref)->coordinates*condition;
+			}
+			while(product == 0)
+
+			if((product>0 && should_be_added)||(product<0 && !should_be_added))
+			{
+				((toprow*) (*horiz_ref))->condition += condition;
+			}
+			else
+			{
+				((toprow*) (*horiz_ref))->condition -= condition;
+			}							
+		}
+	}
+
+
+	void send_state_message(polyhedron* sender, bool shouldsplit, bool shouldmerge, int level)
 	{
 		if(shouldsplit||shouldmerge)
 		{
-			for(vector<polyhedron*>::iterator parent_iterator = parents.begin();parent_iterator<parents.end();parent_iterator++)
+			for(vector<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator<sender->parents.end();parent_iterator++)
 			{
 				polyhedron* current_parent = *parent_iterator;
@@ -147,8 +266,8 @@
 				if(shouldmerge)
 				{
-					int child_state  = get_state(MERGE);
+					int child_state  = sender->get_state(MERGE);
 					int parent_state = current_parent->get_state(MERGE);
 
-					if(parent_state == NULL || parent_state == 0)
+					if(parent_state == 0)
 					{
 						current_parent->set_state(child_state, MERGE);
@@ -156,5 +275,5 @@
 						if(child_state == 0)
 						{
-							current_parent->mergechildren.push_back(this);
+							current_parent->mergechildren.push_back(sender);
 						}
 					}
@@ -165,9 +284,9 @@
 							if(parent_state > 0)
 							{
-								positiveparent = current_parent;
+								sender->positiveparent = current_parent;
 							}
 							else
 							{
-								negativeparent = current_parent;
+								sender->negativeparent = current_parent;
 							}
 						}
@@ -192,124 +311,70 @@
 						}
 
+						if(parent_state == 0)
+						{
+							for_merging[level+1].push_back(current_parent);
+						}
+
 						current_parent->mergechildren.clear();
 					}
 
-
-				}
+					if(shouldsplit)
+					{
+						switch(sender->get_state(SPLIT))
+						{
+						case 1:
+							current_parent->positivechildren.push_back(sender);	
+						break;
+						case 0:
+							current_parent->neutralchildren.push_back(sender);
+						break;
+						case -1:
+							current_parent->negativechildren.push_back(sender);
+						break;
+						}
+
+						if(is_last)
+						{
+							if(current_parent->negativechildren.size()>0) 
+							{								
+								if(current_parent->positivechildren.size()>0)
+								{
+									for_splitting[level+1].push_back(current_parent);
+								
+									current_parent->set_state(0, SPLIT);
+								}
+								else 
+								{
+									current_parent->set_state(-1, SPLIT);
+
+									current_parent->negativechildren.clear();
+									current_parent->neutralchildren.clear();
+								}
+							}
+							else if(current_parent->positivechildren.size()>0)
+							{
+								current_parent->set_state(1, SPLIT);
+
+								current_parent->positivechildren.clear();
+								current_parent->neutralchildren.clear();
+							}
+							else
+							{
+								current_parent->raise_multiplicity();
+
+								current_parent->neutralchildren.clear();
+							}
+						}
+					}
+
+					if(is_last)
+					{
+						send_state_message(current_parent,shouldsplit,shouldmerge,level+1);
+					}
+				}			
 			
 			}
-		}
-	}
-};
-
-/// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse
-/// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space.
-class vertex : public polyhedron
-{
-	/// A dynamic array representing coordinates of the vertex
-	vec coordinates;	
-
-	enum actions {MERGE, SPLIT};
-
-public:
-
-
-
-	/// Default constructor
-	vertex();
-
-	/// Constructor of a vertex from a set of coordinates
-	vertex(vec coordinates)
-	{
-		this->coordinates = coordinates;
-	}
-
-	/// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter
-	/// space of certain dimension is established, but the dimension is not known when the vertex is created.
-	void push_coordinate(double coordinate)
-	{
-		coordinates = concat(coordinates,coordinate);
-	}
-
-	/// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer 
-	/// (not given by reference), but a new copy is created (they are given by value).
-	vec get_coordinates()
-	{		
-		return coordinates;
-	}
-
-		
-};
-
-/// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates
-/// it from polyhedrons in other rows. 
-class toprow : public polyhedron
-{
-	
-public:
-	/// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation
-	vec condition;
-
-	/// Default constructor
-	toprow();
-
-	/// Constructor creating a toprow from the condition
-	toprow(vec condition)
-	{
-		this->condition = condition;
-	}
-
-};
-
-class condition
-{	
-public:
-	vec value;	
-
-	int multiplicity;
-
-	condition(vec value)
-	{
-		this->value = value;
-		multiplicity = 1;
-	}
-}
-
-
-//! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density
-class emlig // : eEF
-{
-
-	/// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result
-	/// of data update from Bayesian estimation or set by the user if this emlig is a prior density
-	vector<vector<polyhedron*>> statistic;
-
-	vector<condition*> conditions;
-
-	double normalization_factor;
-
-	void alter_toprow_conditions(vec condition, bool should_be_added)
-	{
-		for(vector<polyhedron*>::iterator horiz_ref = statistic[statistic.size()-1].begin();horiz_ref<statistic[statistic.size()-1].end();horiz_ref++)
-		{
-			double product = 0;
-
-			vector<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin();
-
-			do
-			{
-				product = (*vertex_ref)->coordinates*condition;
-			}
-			while(product == 0)
-
-			if((product>0 && should_be_added)||(product<0 && !should_be_added))
-			{
-				((toprow*) (*horiz_ref))->condition += condition;
-			}
-			else
-			{
-				((toprow*) (*horiz_ref))->condition -= condition;
-			}							
-		}
+			
+		}		
 	}
 	
@@ -321,4 +386,13 @@
 	{
 		create_statistic(number_of_parameters);
+
+		for(int i = 0;i<statistic.size();i++)
+		{
+			vector<polyhedron*> empty_split;
+			vector<polyhedron*> empty_merge;
+
+			for_splitting.push_back(empty_split);
+			for_merging.push_back(empty_merge);
+		}
 	}
 
@@ -383,6 +457,5 @@
 		}
 
-		vector<vector<polyhedron*>> for_splitting;
-		vector<vector<polyhedron*>> for_merging;
+		
 
 		for(vector<polyhedron*>::iterator horizontal_position = statistic[0].begin();horizontal_position<statistic[0].end();horizontal_position++)
@@ -398,7 +471,14 @@
 			{
 				current_vertex->set_state(toremove*current_vertex->coordinates,MERGE);
-			}
-
-			current_vertex->send_state_message(toadd != NULL, toremove != NULL);
+
+				if(current_vertex->get_state(MERGE) == 0)
+				{
+					for_merging[0].push_back(current_vertex);
+				}
+			}
+
+			send_state_message(current_vertex, toadd != NULL, toremove != NULL, 0);
+
+			
 		}
 	}
