/*! \file \brief Robust Bayesian auto-regression model \author Jan Sindelar. */ #ifndef ROBUST_H #define ROBUST_H #include #include #include #include #include #include #include #include using namespace bdm; using namespace std; using namespace itpp; const double max_range = 1000.0;//numeric_limits::max()/10e-10; enum actions {MERGE, SPLIT}; class polyhedron; class vertex; class toprow; /* class t_simplex { public: set minima; set simplex; t_simplex(vertex* origin_vertex) { simplex.insert(origin_vertex); minima.insert(origin_vertex); } };*/ /// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram /// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created. class polyhedron { /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of /// more than just the necessary number of conditions. For example if a newly created line passes through an already /// existing point, the points multiplicity will rise by 1. int multiplicity; int split_state; int merge_state; public: /// A list of polyhedrons parents within the Hasse diagram. list parents; /// A list of polyhedrons children withing the Hasse diagram. list children; /// All the vertices of the given polyhedron set vertices; /// A list used for storing children that lie in the positive region related to a certain condition list positivechildren; /// A list used for storing children that lie in the negative region related to a certain condition list negativechildren; /// Children intersecting the condition list neutralchildren; list totallyneutralgrandchildren; list totallyneutralchildren; set positiveneutralvertices; set negativeneutralvertices; bool totally_neutral; list mergechildren; polyhedron* positiveparent; polyhedron* negativeparent; polyhedron* next_poly; polyhedron* prev_poly; int message_counter; /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. list> triangulation; /// A list of relative addresses serving for Hasse diagram construction. list kids_rel_addresses; /// Default constructor polyhedron() { multiplicity = 1; message_counter = 0; totally_neutral = NULL; } /// Setter for raising multiplicity void raise_multiplicity() { multiplicity++; } /// Setter for lowering multiplicity void lower_multiplicity() { multiplicity--; } /// An obligatory operator, when the class is used within a C++ STL structure like a vector int operator==(polyhedron polyhedron2) { return true; } /// An obligatory operator, when the class is used within a C++ STL structure like a vector int operator<(polyhedron polyhedron2) { return false; } void set_state(double state_indicator, actions action) { switch(action) { case MERGE: merge_state = (int)sign(state_indicator); break; case SPLIT: split_state = (int)sign(state_indicator); break; } } int get_state(actions action) { switch(action) { case MERGE: return merge_state; break; case SPLIT: return split_state; break; } } int number_of_children() { return children.size(); } void triangulate(bool should_integrate); }; /// 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; public: /// Default constructor vertex(); /// Constructor of a vertex from a set of coordinates vertex(vec coordinates) { this->coordinates = coordinates; vertices.insert(this); set vert_simplex; vert_simplex.insert(this); triangulation.push_back(vert_simplex); } /// 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: double probability; /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation vec condition; int condition_order; /// Default constructor toprow(){}; /// Constructor creating a toprow from the condition toprow(vec condition, int condition_order) { this->condition = condition; this->condition_order = condition_order; } }; class condition { public: vec value; int multiplicity; condition(vec value) { this->value = value; multiplicity = 1; } }; class c_statistic { polyhedron* end_poly; polyhedron* start_poly; public: vector rows; vector row_ends; c_statistic() { end_poly = new polyhedron(); start_poly = new polyhedron(); }; void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end) { if(row>((int)rows.size())-1) { if(row>rows.size()) { throw new exception("You are trying to append a polyhedron whose children are not in the statistic yet!"); return; } rows.push_back(end_poly); row_ends.push_back(end_poly); } // POSSIBLE FAILURE: the function is not checking if start and end are connected if(rows[row] != end_poly) { appended_start->prev_poly = row_ends[row]; row_ends[row]->next_poly = appended_start; } else if((row>0 && rows[row-1]!=end_poly)||row==0) { appended_start->prev_poly = start_poly; rows[row]= appended_start; } else { throw new exception("Wrong polyhedron insertion into statistic: missing intermediary polyhedron!"); } appended_end->next_poly = end_poly; row_ends[row] = appended_end; } void append_polyhedron(int row, polyhedron* appended_poly) { append_polyhedron(row,appended_poly,appended_poly); } void insert_polyhedron(int row, polyhedron* inserted_poly, polyhedron* following_poly) { if(following_poly != end_poly) { inserted_poly->next_poly = following_poly; inserted_poly->prev_poly = following_poly->prev_poly; if(following_poly->prev_poly == start_poly) { rows[row] = inserted_poly; } else { inserted_poly->prev_poly->next_poly = inserted_poly; } following_poly->prev_poly = inserted_poly; } else { this->append_polyhedron(row, inserted_poly); } } void delete_polyhedron(int row, polyhedron* deleted_poly) { if(deleted_poly->prev_poly != start_poly) { deleted_poly->prev_poly->next_poly = deleted_poly->next_poly; } else { rows[row] = deleted_poly->next_poly; } if(deleted_poly->next_poly!=end_poly) { deleted_poly->next_poly->prev_poly = deleted_poly->prev_poly; } else { row_ends[row] = deleted_poly->prev_poly; } deleted_poly->next_poly = NULL; deleted_poly->prev_poly = NULL; } int size() { return rows.size(); } polyhedron* get_end() { return end_poly; } polyhedron* get_start() { return start_poly; } int row_size(int row) { if(this->size()>row && row>=0) { int row_size = 0; for(polyhedron* row_poly = rows[row]; row_poly!=end_poly; row_poly=row_poly->next_poly) { row_size++; } return row_size; } else { throw new exception("There is no row to obtain size from!"); } } }; class my_ivec : public ivec { public: my_ivec():ivec(){}; my_ivec(ivec origin):ivec() { this->ins(0,origin); } bool operator>(const my_ivec &second) const { int size1 = this->size(); int size2 = second.size(); int counter1 = 0; while(0==0) { if((*this)[counter1]==0) { size1--; } if((*this)[counter1]!=0) break; counter1++; } int counter2 = 0; while(0==0) { if(second[counter2]==0) { size2--; } if(second[counter2]!=0) break; counter2++; } if(size1!=size2) { return size1>size2; } else { for(int i = 0;isecond[counter2+i]; } } return false; } } bool operator==(const my_ivec &second) const { int size1 = this->size(); int size2 = second.size(); int counter = 0; while(0==0) { if((*this)[counter]==0) { size1--; } if((*this)[counter]!=0) break; counter++; } counter = 0; while(0==0) { if(second[counter]==0) { size2--; } if(second[counter]!=0) break; counter++; } if(size1!=size2) { return false; } else { for(int i=0;isecond)||((*this)==second)); } bool operator!=(const my_ivec &second) const { return !((*this)==second); } bool operator<=(const my_ivec &second) const { return !((*this)>second); } bool operator>=(const my_ivec &second) const { return !((*this)> for_splitting; vector> for_merging; list conditions; double normalization_factor; void alter_toprow_conditions(vec 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::iterator vertex_ref = horiz_ref->vertices.begin(); do { product = (*vertex_ref)->get_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, vec toadd, vec toremove, int level) { bool shouldmerge = (toremove.size() != 0); bool shouldsplit = (toadd.size() != 0); if(shouldsplit||shouldmerge) { for(list::iterator parent_iterator = sender->parents.begin();parent_iterator!=sender->parents.end();parent_iterator++) { polyhedron* current_parent = *parent_iterator; current_parent->message_counter++; bool is_last = (current_parent->message_counter == current_parent->number_of_children()); if(shouldmerge) { int child_state = sender->get_state(MERGE); int parent_state = current_parent->get_state(MERGE); if(parent_state == 0) { current_parent->set_state(child_state, MERGE); if(child_state == 0) { current_parent->mergechildren.push_back(sender); } } else { if(child_state == 0) { if(parent_state > 0) { sender->positiveparent = current_parent; } else { sender->negativeparent = current_parent; } } } if(is_last) { if(parent_state > 0) { for(list::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) { (*merge_child)->positiveparent = current_parent; } } if(parent_state < 0) { for(list::iterator merge_child = current_parent->mergechildren.begin(); merge_child != current_parent->mergechildren.end();merge_child++) { (*merge_child)->negativeparent = current_parent; } } if(parent_state == 0) { for_merging[level+1].push_back(current_parent); } current_parent->mergechildren.clear(); } } if(shouldsplit) { current_parent->totallyneutralgrandchildren.insert(current_parent->totallyneutralgrandchildren.end(),sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); switch(sender->get_state(SPLIT)) { case 1: current_parent->positivechildren.push_back(sender); current_parent->positiveneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); break; case 0: current_parent->neutralchildren.push_back(sender); current_parent->positiveneutralvertices.insert(sender->positiveneutralvertices.begin(),sender->positiveneutralvertices.end()); current_parent->negativeneutralvertices.insert(sender->negativeneutralvertices.begin(),sender->negativeneutralvertices.end()); if(current_parent->totally_neutral == NULL) { current_parent->totally_neutral = sender->totally_neutral; } else { current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; } if(sender->totally_neutral) { current_parent->totallyneutralchildren.push_back(sender); } break; case -1: current_parent->negativechildren.push_back(sender); current_parent->negativeneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); break; } if(is_last) { unique(current_parent->totallyneutralgrandchildren.begin(),current_parent->totallyneutralgrandchildren.end()); if((current_parent->negativechildren.size()>0&¤t_parent->positivechildren.size()>0)|| (current_parent->neutralchildren.size()>0&¤t_parent->totally_neutral==false)) { for_splitting[level+1].push_back(current_parent); current_parent->set_state(0, SPLIT); } else { ((toprow*)current_parent)->condition_order++; if(current_parent->negativechildren.size()>0) { current_parent->set_state(-1, SPLIT); ((toprow*)current_parent)->condition-=toadd; } else if(current_parent->positivechildren.size()>0) { current_parent->set_state(1, SPLIT); ((toprow*)current_parent)->condition+=toadd; } else { current_parent->raise_multiplicity(); } current_parent->positivechildren.clear(); current_parent->negativechildren.clear(); current_parent->neutralchildren.clear(); current_parent->totallyneutralchildren.clear(); current_parent->totallyneutralgrandchildren.clear(); current_parent->positiveneutralvertices.clear(); current_parent->negativeneutralvertices.clear(); current_parent->totally_neutral = NULL; current_parent->kids_rel_addresses.clear(); current_parent->message_counter = 0; } } } if(is_last) { send_state_message(current_parent,toadd,toremove,level+1); } } } } public: vector> correction_factors; int number_of_parameters; /// A default constructor creates an emlig with predefined statistic representing only the range of the given /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. emlig(int number_of_parameters) { this->number_of_parameters = number_of_parameters; create_statistic(number_of_parameters); } /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. emlig(c_statistic statistic) { this->statistic = statistic; } void step_me(int marker) { for(int i = 0;inext_poly) { char* string = "Checkpoint"; } } } int statistic_rowsize(int row) { return statistic.row_size(row); } void add_condition(vec toadd) { vec null_vector = ""; add_and_remove_condition(toadd, null_vector); } void remove_condition(vec toremove) { vec null_vector = ""; add_and_remove_condition(null_vector, toremove); } void add_and_remove_condition(vec toadd, vec toremove) { bool should_remove = (toremove.size() != 0); bool should_add = (toadd.size() != 0); for_splitting.clear(); for_merging.clear(); for(int i = 0;i empty_split; list empty_merge; for_splitting.push_back(empty_split); for_merging.push_back(empty_merge); } list::iterator toremove_ref = conditions.end(); bool condition_should_be_added = false; for(list::iterator ref = conditions.begin();ref!=conditions.end();ref++) { if(should_remove) { if((*ref)->value == toremove) { if((*ref)->multiplicity>1) { (*ref)->multiplicity--; alter_toprow_conditions(toremove,false); should_remove = false; } else { toremove_ref = ref; } } } if(should_add) { if((*ref)->value == toadd) { (*ref)->multiplicity++; alter_toprow_conditions(toadd,true); should_add = false; } else { condition_should_be_added = true; } } } if(toremove_ref!=conditions.end()) { conditions.erase(toremove_ref); } if(condition_should_be_added) { conditions.push_back(new condition(toadd)); } for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly) { vertex* current_vertex = (vertex*)horizontal_position; if(should_add||should_remove) { vec appended_vec = current_vertex->get_coordinates(); appended_vec.ins(0,-1.0); if(should_add) { double local_condition = toadd*appended_vec; current_vertex->set_state(local_condition,SPLIT); if(local_condition == 0) { current_vertex->totally_neutral = true; current_vertex->raise_multiplicity(); current_vertex->negativeneutralvertices.insert(current_vertex); current_vertex->positiveneutralvertices.insert(current_vertex); } } if(should_remove) { 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_add) { int k = 1; vector>::iterator beginning_ref = ++for_splitting.begin(); for(vector>::iterator vert_ref = beginning_ref;vert_ref::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++) { polyhedron* new_totally_neutral_child; polyhedron* current_polyhedron = (*split_ref); if(vert_ref == beginning_ref) { vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates(); vec coordinates2 = ((vertex*)(*(current_polyhedron->children.begin()++)))->get_coordinates(); coordinates2.ins(0,-1.0); double t = (-toadd*coordinates2)/(toadd(1,toadd.size()-1)*coordinates1)+1; vec new_coordinates = coordinates1*t+(coordinates2(1,coordinates2.size()-1)-coordinates1); vertex* neutral_vertex = new vertex(new_coordinates); new_totally_neutral_child = neutral_vertex; } else { toprow* neutral_toprow = new toprow(); new_totally_neutral_child = neutral_toprow; } new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(), current_polyhedron->totallyneutralgrandchildren.begin(), current_polyhedron->totallyneutralgrandchildren.end()); for(list::iterator grand_ref = current_polyhedron->totallyneutralgrandchildren.begin(); grand_ref != current_polyhedron->totallyneutralgrandchildren.end();grand_ref++) { (*grand_ref)->parents.push_back(new_totally_neutral_child); new_totally_neutral_child->vertices.insert((*grand_ref)->vertices.begin(),(*grand_ref)->vertices.end()); } 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); for(list::iterator parent_ref = current_polyhedron->parents.begin();parent_ref!=current_polyhedron->parents.end();parent_ref++) { (*parent_ref)->totallyneutralgrandchildren.push_back(new_totally_neutral_child); (*parent_ref)->neutralchildren.remove(current_polyhedron); (*parent_ref)->children.remove(current_polyhedron); (*parent_ref)->children.push_back(positive_poly); (*parent_ref)->children.push_back(negative_poly); (*parent_ref)->positivechildren.push_back(positive_poly); (*parent_ref)->negativechildren.push_back(negative_poly); } positive_poly->parents.insert(positive_poly->parents.end(), current_polyhedron->parents.begin(), current_polyhedron->parents.end()); negative_poly->parents.insert(negative_poly->parents.end(), current_polyhedron->parents.begin(), current_polyhedron->parents.end()); positive_poly->children.push_back(new_totally_neutral_child); negative_poly->children.push_back(new_totally_neutral_child); new_totally_neutral_child->parents.push_back(positive_poly); new_totally_neutral_child->parents.push_back(negative_poly); for(list::iterator child_ref = current_polyhedron->positivechildren.begin();child_ref!=current_polyhedron->positivechildren.end();child_ref++) { (*child_ref)->parents.remove(current_polyhedron); (*child_ref)->parents.push_back(positive_poly); } positive_poly->children.insert(positive_poly->children.end(), current_polyhedron->positivechildren.begin(), current_polyhedron->positivechildren.end()); for(list::iterator child_ref = current_polyhedron->negativechildren.begin();child_ref!=current_polyhedron->negativechildren.end();child_ref++) { (*child_ref)->parents.remove(current_polyhedron); (*child_ref)->parents.push_back(negative_poly); } negative_poly->children.insert(negative_poly->children.end(), current_polyhedron->negativechildren.begin(), current_polyhedron->negativechildren.end()); positive_poly->vertices.insert(current_polyhedron->positiveneutralvertices.begin(),current_polyhedron->positiveneutralvertices.end()); positive_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); negative_poly->vertices.insert(current_polyhedron->negativeneutralvertices.begin(),current_polyhedron->negativeneutralvertices.end()); negative_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); new_totally_neutral_child->triangulate(false); positive_poly->triangulate(k==for_splitting.size()-1); negative_poly->triangulate(k==for_splitting.size()-1); statistic.append_polyhedron(k-1, new_totally_neutral_child); statistic.insert_polyhedron(k, positive_poly, current_polyhedron); statistic.insert_polyhedron(k, negative_poly, current_polyhedron); statistic.delete_polyhedron(k, current_polyhedron); delete current_polyhedron; } k++; } } /* vector sizevector; for(int s = 0;sorder);remaining_order++) { set factor_templates; set final_factors; for(int i = 1;i!=number_of_parameters-order+1;i++) { my_ivec new_template = my_ivec(); new_template.ins(0,1); new_template.ins(1,i); factor_templates.insert(new_template); for(int j = 1;j::iterator fac_ref = factor_templates.begin();fac_ref!=factor_templates.end();fac_ref++) { ivec current_template = (*fac_ref); current_template[0]+=1; current_template.ins(current_template.size(),i); if(current_template[0]==remaining_order) { final_factors.insert(current_template.right(current_template.size()-1)); } else { factor_templates.insert(current_template); } } } } correction_factors.push_back(final_factors); } } protected: /// A method for creating plain default statistic representing only the range of the parameter space. void create_statistic(int number_of_parameters) { for(int i = 0;i origin_vec; // ..we fill it with the origin.. origin_vec.push_back(origin); // ..and we fill the statistic with the created vector. statistic.push_back(origin_vec); */ statistic = *(new c_statistic()); statistic.append_polyhedron(0, origin); // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: for(int i=0;iget_coordinates(); // And we incorporate the nonzero coordinates into the new cooordinate vectors vec origin_coord1 = concat(origin_coord,-max_range); vec origin_coord2 = concat(origin_coord,max_range); // Now we create the points vertex* new_point1 = new vertex(origin_coord1); vertex* new_point2 = new vertex(origin_coord2); //********************************************************************************************************* // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is // connected to the first (second) newly created vertex by a parent-child relation. //********************************************************************************************************* /* // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram vector> new_statistic1; vector> new_statistic2; */ c_statistic* new_statistic1 = new c_statistic(); c_statistic* new_statistic2 = new c_statistic(); // Copy the statistic by rows for(int j=0;j supportnew_1; vector supportnew_2; new_statistic1.push_back(supportnew_1); new_statistic2.push_back(supportnew_2); */ // for each polyhedron in the given row for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) { // Append an extra zero coordinate to each of the vertices for the new dimension // If vert_ref is at the first index => we loop through vertices if(j == 0) { // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates ((vertex*) horiz_ref)->push_coordinate(0); } /* else { ((toprow*) (*horiz_ref))->condition.ins(0,0); }*/ // if it has parents if(!horiz_ref->parents.empty()) { // save the relative address of this child in a vector kids_rel_addresses of all its parents. // This information will later be used for copying the whole Hasse diagram with each of the // relations contained within. for(list::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++) { (*parent_ref)->kids_rel_addresses.push_back(element_number); } } // ************************************************************************************************** // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron // will be created as a toprow, but this information will be later forgotten and only the polyhedrons // in the top row of the Hasse diagram will be considered toprow for later use. // ************************************************************************************************** // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic // this condition will be a vector of zeros. There are two vectors, because we need two copies of // the original Hasse diagram. vec vec1(number_of_parameters+1); vec1.zeros(); vec vec2(number_of_parameters+1); vec2.zeros(); // We create a new toprow with the previously specified condition. toprow* current_copy1 = new toprow(vec1, 0); toprow* current_copy2 = new toprow(vec2, 0); // The vertices of the copies will be inherited, because there will be a parent/child relation // between each polyhedron and its offspring (comming from the copy) and a parent has all the // vertices of its child plus more. for(set::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++) { current_copy1->vertices.insert(*vertex_ref); current_copy2->vertices.insert(*vertex_ref); } // The only new vertex of the offspring should be the newly created point. current_copy1->vertices.insert(new_point1); current_copy2->vertices.insert(new_point2); // This method guarantees that each polyhedron is already triangulated, therefore its triangulation // is only one set of vertices and it is the set of all its vertices. set t_simplex1; set t_simplex2; t_simplex1.insert(current_copy1->vertices.begin(),current_copy1->vertices.end()); t_simplex2.insert(current_copy2->vertices.begin(),current_copy2->vertices.end()); current_copy1->triangulation.push_back(t_simplex1); current_copy2->triangulation.push_back(t_simplex2); // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the // kids and when we do that and know the child, in the child we will remember the parent we came from. // This way all the parents/children relations are saved in both the parent and the child. if(!horiz_ref->kids_rel_addresses.empty()) { for(list::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++) { polyhedron* new_kid1 = new_statistic1->rows[j-1]; polyhedron* new_kid2 = new_statistic2->rows[j-1]; // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline. if(*kid_ref) { for(int k = 1;k<=(*kid_ref);k++) { new_kid1=new_kid1->next_poly; new_kid2=new_kid2->next_poly; } } // find the child and save the relation to the parent current_copy1->children.push_back(new_kid1); current_copy2->children.push_back(new_kid2); // in the child save the parents' address new_kid1->parents.push_back(current_copy1); new_kid2->parents.push_back(current_copy2); } // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the // Hasse diagram again) horiz_ref->kids_rel_addresses.clear(); } // If there were no children previously, we are copying a polyhedron that has been a vertex before. // In this case it is a segment now and it will have a relation to its mother (copywise) and to the // newly created point. Here we create the connection to the new point, again from both sides. else { // Add the address of the new point in the former vertex current_copy1->children.push_back(new_point1); current_copy2->children.push_back(new_point2); // Add the address of the former vertex in the new point new_point1->parents.push_back(current_copy1); new_point2->parents.push_back(current_copy2); } // Save the mother in its offspring current_copy1->children.push_back(horiz_ref); current_copy2->children.push_back(horiz_ref); // Save the offspring in its mother horiz_ref->parents.push_back(current_copy1); horiz_ref->parents.push_back(current_copy2); // Add the copies into the relevant statistic. The statistic will later be appended to the previous // Hasse diagram new_statistic1->append_polyhedron(j,current_copy1); new_statistic2->append_polyhedron(j,current_copy2); // Raise the count in the vector of polyhedrons element_number++; } } /* statistic.begin()->push_back(new_point1); statistic.begin()->push_back(new_point2); */ statistic.append_polyhedron(0, new_point1); statistic.append_polyhedron(0, new_point2); // Merge the new statistics into the old one. This will either be the final statistic or we will // reenter the widening loop. for(int j=0;jsize();j++) { /* if(j+1==statistic.size()) { list support; statistic.push_back(support); } (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end()); */ statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]); statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]); } } /* vector> toprow_statistic; int line_count = 0; for(vector>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++) { list support_list; toprow_statistic.push_back(support_list); for(list::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++) { toprow* support_top = (toprow*)(*polyhedron_ref2); toprow_statistic[line_count].push_back(support_top); } line_count++; }*/ /* vector sizevector; for(int s = 0;s