Changeset 1214
- Timestamp:
- 10/13/10 20:15:15 (14 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/robustlib.h
r1213 r1214 71 71 polyhedron* negativeparent; 72 72 73 polyhedron* next_poly; 74 75 polyhedron* prev_poly; 76 73 77 int message_counter; 74 78 … … 222 226 }; 223 227 228 class c_statistic 229 { 230 polyhedron* end_poly; 231 polyhedron* start_poly; 232 233 public: 234 vector<polyhedron*> rows; 235 236 vector<polyhedron*> row_ends; 237 238 c_statistic() 239 { 240 end_poly = new polyhedron(); 241 start_poly = new polyhedron(); 242 }; 243 244 void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end) 245 { 246 if(row>((int)rows.size())-1) 247 { 248 if(row>rows.size()) 249 { 250 throw new exception("You are trying to append a polyhedron whose children are not in the statistic yet!"); 251 return; 252 } 253 254 rows.push_back(end_poly); 255 row_ends.push_back(end_poly); 256 } 257 258 // POSSIBLE FAILURE: the function is not checking if start and end are connected 259 260 if(rows[row] != end_poly) 261 { 262 appended_start->prev_poly = row_ends[row]; 263 row_ends[row]->next_poly = appended_start; 264 265 } 266 else if((row>0 && rows[row-1]!=end_poly)||row==0) 267 { 268 appended_start->prev_poly = start_poly; 269 rows[row]= appended_start; 270 } 271 else 272 { 273 throw new exception("Wrong polyhedron insertion into statistic: missing intermediary polyhedron!"); 274 } 275 276 appended_end->next_poly = end_poly; 277 row_ends[row] = appended_end; 278 } 279 280 void append_polyhedron(int row, polyhedron* appended_poly) 281 { 282 append_polyhedron(row,appended_poly,appended_poly); 283 } 284 285 void insert_polyhedron(int row, polyhedron* inserted_poly, polyhedron* following_poly) 286 { 287 if(following_poly != end_poly) 288 { 289 inserted_poly->next_poly = following_poly; 290 inserted_poly->prev_poly = following_poly->prev_poly; 291 292 if(following_poly->prev_poly == start_poly) 293 { 294 rows[row] = inserted_poly; 295 } 296 else 297 { 298 inserted_poly->prev_poly->next_poly = inserted_poly; 299 } 300 301 following_poly->prev_poly = inserted_poly; 302 } 303 else 304 { 305 this->append_polyhedron(row, inserted_poly); 306 } 307 308 } 309 310 311 312 313 void delete_polyhedron(int row, polyhedron* deleted_poly) 314 { 315 if(deleted_poly->prev_poly != start_poly) 316 { 317 deleted_poly->prev_poly->next_poly = deleted_poly->next_poly; 318 } 319 else 320 { 321 rows[row] = deleted_poly->next_poly; 322 } 323 324 if(deleted_poly->next_poly!=end_poly) 325 { 326 deleted_poly->next_poly->prev_poly = deleted_poly->prev_poly; 327 } 328 else 329 { 330 row_ends[row] = deleted_poly->prev_poly; 331 } 332 333 334 335 deleted_poly->next_poly = NULL; 336 deleted_poly->prev_poly = NULL; 337 } 338 339 int size() 340 { 341 return rows.size(); 342 } 343 344 polyhedron* get_end() 345 { 346 return end_poly; 347 } 348 349 polyhedron* get_start() 350 { 351 return start_poly; 352 } 353 354 int row_size(int row) 355 { 356 if(this->size()>row && row>=0) 357 { 358 int row_size = 0; 359 360 for(polyhedron* row_poly = rows[row]; row_poly!=end_poly; row_poly=row_poly->next_poly) 361 { 362 row_size++; 363 } 364 365 return row_size; 366 } 367 else 368 { 369 throw new exception("There is no row to obtain size from!"); 370 } 371 } 372 }; 373 224 374 225 375 //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density … … 229 379 /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result 230 380 /// of data update from Bayesian estimation or set by the user if this emlig is a prior density 231 vector<list<polyhedron*>>statistic;381 c_statistic statistic; 232 382 233 383 vector<list<polyhedron*>> for_splitting; … … 241 391 void alter_toprow_conditions(vec condition, bool should_be_added) 242 392 { 243 for( list<polyhedron*>::iterator horiz_ref = (statistic.end()--)->begin();horiz_ref!=(statistic.end()--)->end();horiz_ref++)393 for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) 244 394 { 245 395 double product = 0; 246 396 247 list<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin();397 list<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); 248 398 249 399 do … … 255 405 if((product>0 && should_be_added)||(product<0 && !should_be_added)) 256 406 { 257 ((toprow*) (*horiz_ref))->condition += condition;407 ((toprow*) horiz_ref)->condition += condition; 258 408 } 259 409 else 260 410 { 261 ((toprow*) (*horiz_ref))->condition -= condition;262 } 411 ((toprow*) horiz_ref)->condition -= condition; 412 } 263 413 } 264 414 } … … 427 577 /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. 428 578 emlig(int number_of_parameters) 429 { 579 { 580 430 581 create_statistic(number_of_parameters); 431 582 432 for( vector<list<polyhedron*>>::iterator local_iter = statistic.begin();local_iter<statistic.end();local_iter++)583 for(int i = 0;i<statistic.size();i++) 433 584 { 434 585 list<polyhedron*> empty_split; … … 442 593 /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a 443 594 /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. 444 emlig( vector<list<polyhedron*>>statistic)595 emlig(c_statistic statistic) 445 596 { 446 597 this->statistic = statistic; … … 519 670 520 671 521 522 for( list<polyhedron*>::iterator horizontal_position = statistic.begin()->begin();horizontal_position!=statistic.begin()->end();horizontal_position++)672 673 for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly) 523 674 { 524 vertex* current_vertex = (vertex*) (*horizontal_position);675 vertex* current_vertex = (vertex*)horizontal_position; 525 676 526 677 if(should_add||should_remove) … … 556 707 } 557 708 558 send_state_message(current_vertex, toadd, toremove, 0); 709 send_state_message(current_vertex, toadd, toremove, 0); 710 559 711 } 560 712 561 713 if(should_add) 562 714 { 563 for(vector<list<polyhedron*>>::iterator vert_ref = for_splitting.begin();vert_ref<for_splitting.end();vert_ref++) 715 int k = 1; 716 717 vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin(); 718 719 for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++) 564 720 { 565 721 … … 570 726 polyhedron* current_polyhedron = (*split_ref); 571 727 572 if(vert_ref == for_splitting.begin())728 if(vert_ref == beginning_ref) 573 729 { 574 730 vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates(); … … 617 773 current_polyhedron->negativechildren.end()); 618 774 775 statistic.append_polyhedron(k-1, new_totally_neutral_child); 619 776 620 621 622 623 777 statistic.insert_polyhedron(k, positive_poly, current_polyhedron); 778 statistic.insert_polyhedron(k, negative_poly, current_polyhedron); 779 780 statistic.delete_polyhedron(k, current_polyhedron); 624 781 } 625 } 626 } 627 628 782 783 k++; 784 } 785 } 786 787 vector<int> sizevector; 788 for(int s = 0;s<statistic.size();s++) 789 { 790 sizevector.push_back(statistic.row_size(s)); 791 } 629 792 } 630 793 … … 644 807 origin->vertices.push_back(origin); 645 808 809 /* 646 810 // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse 647 811 // diagram. First we create a vector of polyhedrons.. … … 653 817 // ..and we fill the statistic with the created vector. 654 818 statistic.push_back(origin_vec); 819 */ 820 821 statistic = *(new c_statistic()); 822 823 statistic.append_polyhedron(0, origin); 655 824 656 825 // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to … … 680 849 681 850 851 /* 682 852 // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram 683 853 vector<vector<polyhedron*>> new_statistic1; 684 854 vector<vector<polyhedron*>> new_statistic2; 855 */ 856 857 c_statistic* new_statistic1 = new c_statistic(); 858 c_statistic* new_statistic2 = new c_statistic(); 685 859 686 860 … … 693 867 int element_number = 0; 694 868 869 /* 695 870 vector<polyhedron*> supportnew_1; 696 871 vector<polyhedron*> supportnew_2; … … 698 873 new_statistic1.push_back(supportnew_1); 699 874 new_statistic2.push_back(supportnew_2); 875 */ 700 876 701 877 // for each polyhedron in the given row 702 for( list<polyhedron*>::iterator horiz_ref = (statistic.begin()+j)->begin();horiz_ref!=(statistic.begin()+j)->end();horiz_ref++)878 for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) 703 879 { 704 880 // Append an extra zero coordinate to each of the vertices for the new dimension … … 707 883 { 708 884 // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates 709 ((vertex*) (*horiz_ref))->push_coordinate(0);885 ((vertex*) horiz_ref)->push_coordinate(0); 710 886 } 711 887 /* … … 716 892 717 893 // if it has parents 718 if(! (*horiz_ref)->parents.empty())894 if(!horiz_ref->parents.empty()) 719 895 { 720 896 // save the relative address of this child in a vector kids_rel_addresses of all its parents. 721 897 // This information will later be used for copying the whole Hasse diagram with each of the 722 898 // relations contained within. 723 for(list<polyhedron*>::iterator parent_ref = (*horiz_ref)->parents.begin();parent_ref != (*horiz_ref)->parents.end();parent_ref++)899 for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++) 724 900 { 725 901 (*parent_ref)->kids_rel_addresses.push_back(element_number); … … 749 925 // between each polyhedron and its offspring (comming from the copy) and a parent has all the 750 926 // vertices of its child plus more. 751 for(list<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin();vertex_ref!=(*horiz_ref)->vertices.end();vertex_ref++)927 for(list<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++) 752 928 { 753 929 current_copy1->vertices.push_back(*vertex_ref); … … 768 944 // kids and when we do that and know the child, in the child we will remember the parent we came from. 769 945 // This way all the parents/children relations are saved in both the parent and the child. 770 if(! (*horiz_ref)->kids_rel_addresses.empty())771 { 772 for(list<int>::iterator kid_ref = (*horiz_ref)->kids_rel_addresses.begin();kid_ref!=(*horiz_ref)->kids_rel_addresses.end();kid_ref++)946 if(!horiz_ref->kids_rel_addresses.empty()) 947 { 948 for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++) 773 949 { 950 polyhedron* new_kid1 = new_statistic1->rows[j-1]; 951 polyhedron* new_kid2 = new_statistic2->rows[j-1]; 952 953 // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but 954 // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline. 955 if(*kid_ref) 956 { 957 for(int k = 1;k<=(*kid_ref);k++) 958 { 959 new_kid1=new_kid1->next_poly; 960 new_kid2=new_kid2->next_poly; 961 } 962 } 963 774 964 // find the child and save the relation to the parent 775 current_copy1->children.push_back(new_ statistic1[j-1][(*kid_ref)]);776 current_copy2->children.push_back(new_ statistic2[j-1][(*kid_ref)]);965 current_copy1->children.push_back(new_kid1); 966 current_copy2->children.push_back(new_kid2); 777 967 778 968 // in the child save the parents' address 779 new_ statistic1[j-1][(*kid_ref)]->parents.push_back(current_copy1);780 new_ statistic2[j-1][(*kid_ref)]->parents.push_back(current_copy2);969 new_kid1->parents.push_back(current_copy1); 970 new_kid2->parents.push_back(current_copy2); 781 971 } 782 972 783 973 // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the 784 974 // Hasse diagram again) 785 (*horiz_ref)->kids_rel_addresses.clear();975 horiz_ref->kids_rel_addresses.clear(); 786 976 } 787 977 // If there were no children previously, we are copying a polyhedron that has been a vertex before. … … 800 990 801 991 // Save the mother in its offspring 802 current_copy1->children.push_back( *horiz_ref);803 current_copy2->children.push_back( *horiz_ref);992 current_copy1->children.push_back(horiz_ref); 993 current_copy2->children.push_back(horiz_ref); 804 994 805 995 // Save the offspring in its mother 806 (*horiz_ref)->parents.push_back(current_copy1);807 (*horiz_ref)->parents.push_back(current_copy2);996 horiz_ref->parents.push_back(current_copy1); 997 horiz_ref->parents.push_back(current_copy2); 808 998 809 999 810 1000 // Add the copies into the relevant statistic. The statistic will later be appended to the previous 811 1001 // Hasse diagram 812 new_statistic1 [j].push_back(current_copy1);813 new_statistic2 [j].push_back(current_copy2);1002 new_statistic1->append_polyhedron(j,current_copy1); 1003 new_statistic2->append_polyhedron(j,current_copy2); 814 1004 815 1005 // Raise the count in the vector of polyhedrons 816 element_number++; 1006 element_number++; 817 1007 818 1008 } … … 820 1010 } 821 1011 1012 /* 822 1013 statistic.begin()->push_back(new_point1); 823 1014 statistic.begin()->push_back(new_point2); 1015 */ 1016 1017 statistic.append_polyhedron(0, new_point1); 1018 statistic.append_polyhedron(0, new_point2); 824 1019 825 1020 // Merge the new statistics into the old one. This will either be the final statistic or we will 826 1021 // reenter the widening loop. 827 for(int j=0;j<new_statistic1.size();j++) 828 { 1022 for(int j=0;j<new_statistic1->size();j++) 1023 { 1024 /* 829 1025 if(j+1==statistic.size()) 830 1026 { … … 835 1031 (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); 836 1032 (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end()); 1033 */ 1034 statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]); 1035 statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]); 837 1036 } 838 1037 … … 840 1039 } 841 1040 1041 /* 842 1042 vector<list<toprow*>> toprow_statistic; 843 1043 int line_count = 0; … … 856 1056 857 1057 line_count++; 858 } 1058 }*/ 1059 1060 1061 vector<int> sizevector; 1062 for(int s = 0;s<statistic.size();s++) 1063 { 1064 sizevector.push_back(statistic.row_size(s)); 1065 } 1066 859 1067 } 860 1068