Changeset 1300
- Timestamp:
- 03/18/11 17:59:56 (14 years ago)
- Location:
- applications/robust
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
applications/robust/main.cpp
r1299 r1300 20 20 int main ( int argc, char* argv[] ) { 21 21 22 22 /* 23 23 vector<vector<vector<string>>> string_lists; 24 24 string_lists.push_back(vector<vector<string>>()); … … 55 55 for(int j = 0;j<string_lists.size();j++) 56 56 { 57 57 /* 58 58 for(int i = 0;i<string_lists[j].size()-1;i++) 59 59 { … … 118 118 myfile.close(); 119 119 } 120 121 122 // emlig* emlig1 = new emlig(emlig_size); 123 // emlig* emlig2 = new emlig(emlig_size); 120 */ 121 122 123 emlig* emlig1 = new emlig(emlig_size); 124 emlig* emlig2 = new emlig(emlig_size); 124 125 125 126 /* … … 141 142 }*/ 142 143 143 /* 144 vec condition5 = "-0.3 1.7 1.5"; 144 145 vec condition5 = "1.0 1.0 1.01";//"-0.3 1.7 1.5"; 146 145 147 emlig1->add_condition(condition5); 146 148 147 vec condition1 = "1.0 1.0 1.01"; 148 emlig1->add_condition(condition1); 149 emlig2->add_condition(condition1); 150 151 vec condition2 = "-1.0 1.0 1.0"; 152 emlig1->add_condition(condition2); 153 emlig2->add_condition(condition2); 154 155 vec condition3 = "0.5 -1.01 1.0"; 156 emlig1->add_condition(condition3); 157 emlig2->add_condition(condition3); 158 159 160 vec condition4 = "-0.5 -1.0 1.0"; 161 emlig1->add_condition(condition4); 162 cout << "************************************************" << endl; 163 emlig2->add_condition(condition4); 164 cout << "************************************************" << endl; 165 149 //vec condition1a = "1.0 1.0 1.01"; 150 //vec condition1b = "1.0 1.0 1.01"; 151 //emlig1->add_condition(condition1a); 152 //emlig2->add_condition(condition1b); 153 154 //vec condition2a = "-1.0 1.0 1.0"; 155 //vec condition2b = "-1.0 1.0 1.0"; 156 //emlig1->add_condition(condition2a); 157 //emlig2->add_condition(condition2b); 158 159 //vec condition3a = "0.5 -1.01 1.0"; 160 //vec condition3b = "0.5 -1.01 1.0"; 161 162 //emlig1->add_condition(condition3a); 163 //emlig2->add_condition(condition3b); 164 165 //vec condition4a = "-0.5 -1.0 1.0"; 166 //vec condition4b = "-0.5 -1.0 1.0"; 167 168 //emlig1->add_condition(condition4a); 169 //cout << "************************************************" << endl; 170 //emlig2->add_condition(condition4b); 171 //cout << "************************************************" << endl; 172 166 173 //cout << emlig1->minimal_vertex->get_coordinates(); 167 174 168 175 emlig1->remove_condition(condition5); 169 cout << "************************************************" << endl; 170 176 171 177 //emlig1->step_me(0); 172 178 //emlig2->step_me(0); -
applications/robust/robustlib.cpp
r1299 r1300 66 66 toprow* as_toprow = (toprow*)this; 67 67 68 vec cur_condition = as_toprow->condition .get(1,as_toprow->condition.size()-1);68 vec cur_condition = as_toprow->condition_sum.get(1,as_toprow->condition_sum.size()-1); 69 69 70 70 // cout << as_toprow->condition << endl; … … 91 91 cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl; 92 92 93 a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition [0];93 a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition_sum[0]; 94 94 95 95 … … 127 127 } 128 128 129 double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition [0];129 double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition_sum[0]; 130 130 if(a_m<current_emlig->likelihood_value) 131 131 { -
applications/robust/robustlib.h
r1299 r1300 48 48 class polyhedron; 49 49 50 class condition 51 { 52 public: 53 vec value; 54 55 int multiplicity; 56 57 condition(vec value) 58 { 59 this->value = value; 60 multiplicity = 1; 61 } 62 }; 50 63 51 64 … … 77 90 set<vertex*> vertices; 78 91 92 set<condition*> parentconditions; 93 79 94 /// A list used for storing children that lie in the positive region related to a certain condition 80 95 list<polyhedron*> positivechildren; … … 124 139 125 140 totally_neutral = NULL; 141 142 mergechild = NULL; 126 143 } 127 144 … … 157 174 158 175 159 voidset_state(double state_indicator, actions action)176 int set_state(double state_indicator, actions action) 160 177 { 161 178 switch(action) 162 179 { 163 180 case MERGE: 164 merge_state = (int)sign(state_indicator); 165 break;181 merge_state = (int)sign(state_indicator); 182 return merge_state; 166 183 case SPLIT: 167 184 split_state = (int)sign(state_indicator); 168 break;185 return split_state; 169 186 } 170 187 } … … 210 227 vertex(vec coordinates) 211 228 { 212 this->coordinates = coordinates;229 this->coordinates = coordinates; 213 230 214 231 vertices.insert(this); … … 225 242 void push_coordinate(double coordinate) 226 243 { 227 coordinates = concat(coordinates,coordinate);244 coordinates = concat(coordinates,coordinate); 228 245 } 229 246 … … 231 248 /// (not given by reference), but a new copy is created (they are given by value). 232 249 vec get_coordinates() 233 { 250 { 234 251 return coordinates; 235 252 } 236 237 253 238 254 }; … … 250 266 251 267 /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation 252 vec condition ;268 vec condition_sum; 253 269 254 270 int condition_order; … … 258 274 259 275 /// Constructor creating a toprow from the condition 260 toprow( veccondition, int condition_order)261 { 262 this->condition = condition;276 toprow(condition *condition, int condition_order) 277 { 278 this->condition_sum = condition->value; 263 279 this->condition_order = condition_order; 264 280 } 265 281 282 toprow(vec condition_sum, int condition_order) 283 { 284 this->condition_sum = condition_sum; 285 } 286 266 287 double integrate_simplex(set<vertex*> simplex, char c); 267 288 … … 269 290 270 291 271 class condition272 {273 public:274 vec value;275 276 int multiplicity;277 278 condition(vec value)279 {280 this->value = value;281 multiplicity = 1;282 }283 };284 292 285 293 … … 603 611 604 612 605 void alter_toprow_conditions( veccondition, bool should_be_added)613 void alter_toprow_conditions(condition *condition, bool should_be_added) 606 614 { 607 615 for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) 608 616 { 609 double product = 0;610 611 617 set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); 612 618 613 619 do 614 620 { 615 product = (*vertex_ref)->get_coordinates()*condition; 616 } 617 while(product == 0); 621 vertex_ref++; 622 } 623 while((*vertex_ref)->parentconditions.find(condition)==(*vertex_ref)->parentconditions.end()); 624 625 double product = (*vertex_ref)->get_coordinates()*condition->value; 618 626 619 627 if((product>0 && should_be_added)||(product<0 && !should_be_added)) 620 628 { 621 ((toprow*) horiz_ref)->condition += condition;629 ((toprow*) horiz_ref)->condition_sum += condition->value; 622 630 } 623 631 else 624 632 { 625 ((toprow*) horiz_ref)->condition -= condition;633 ((toprow*) horiz_ref)->condition_sum -= condition->value; 626 634 } 627 635 } … … 630 638 631 639 632 void send_state_message(polyhedron* sender, vec toadd, vectoremove, int level)640 void send_state_message(polyhedron* sender, condition *toadd, condition *toremove, int level) 633 641 { 634 642 635 bool shouldmerge = (toremove.size() != 0);636 bool shouldsplit = (toadd .size() != 0);643 bool shouldmerge = (toremove == NULL); 644 bool shouldsplit = (toadd == NULL); 637 645 638 646 if(shouldsplit||shouldmerge) … … 644 652 current_parent->message_counter++; 645 653 646 bool is_last = (current_parent->message_counter == current_parent->number_of_children()); 654 bool is_last = (current_parent->message_counter == current_parent->number_of_children()); 655 bool is_first = (current_parent->message_counter == 1); 647 656 648 657 if(shouldmerge) … … 651 660 int parent_state = current_parent->get_state(MERGE); 652 661 653 if(parent_state == 0 )654 { 655 current_parent->set_state(child_state, MERGE);656 } 662 if(parent_state == 0||is_first) 663 { 664 parent_state = current_parent->set_state(child_state, MERGE); 665 } 657 666 658 667 if(child_state == 0) … … 686 695 { 687 696 for_merging[level+1].push_back(current_parent); 697 } 698 else 699 { 700 current_parent->set_state(0,MERGE); 688 701 } 689 702 … … 751 764 current_parent->set_state(-1, SPLIT); 752 765 753 ((toprow*)current_parent)->condition -=toadd;766 ((toprow*)current_parent)->condition_sum-=toadd->value; 754 767 755 768 … … 759 772 current_parent->set_state(1, SPLIT); 760 773 761 ((toprow*)current_parent)->condition +=toadd;774 ((toprow*)current_parent)->condition_sum+=toadd->value; 762 775 } 763 776 else … … 921 934 } 922 935 923 924 936 void add_and_remove_condition(vec toadd, vec toremove) 925 937 { … … 954 966 (*ref)->multiplicity--; 955 967 956 alter_toprow_conditions( toremove,false);968 alter_toprow_conditions(*ref,false); 957 969 958 970 should_remove = false; … … 971 983 (*ref)->multiplicity++; 972 984 973 alter_toprow_conditions( toadd,true);985 alter_toprow_conditions(*ref,true); 974 986 975 987 should_add = false; … … 982 994 } 983 995 996 condition* condition_to_remove = NULL; 997 984 998 if(toremove_ref!=conditions.end()) 985 999 { 986 1000 conditions.erase(toremove_ref); 987 } 1001 condition_to_remove = *toremove_ref; 1002 } 1003 1004 condition* condition_to_add = NULL; 988 1005 989 1006 if(condition_should_be_added) … … 1000 1017 if(should_add||should_remove) 1001 1018 { 1002 vec appended_ vec= current_vertex->get_coordinates();1003 appended_ vec.ins(0,-1.0);1019 vec appended_coords = current_vertex->get_coordinates(); 1020 appended_coords.ins(0,-1.0); 1004 1021 1005 1022 if(should_add) 1006 1023 { 1007 double local_condition = toadd*appended_vec; 1024 double local_condition = 0;// = toadd*(appended_coords.first/=appended_coords.second); 1025 1026 local_condition = appended_coords*toadd; 1008 1027 1009 1028 current_vertex->set_state(local_condition,SPLIT); 1010 1029 1030 /// \TODO There should be a rounding error tolerance used here to insure we are not having too many points because of rounding error. 1011 1031 if(local_condition == 0) 1012 1032 { … … 1021 1041 1022 1042 if(should_remove) 1043 { 1044 set<condition*>::iterator cond_ref; 1045 1046 for(cond_ref = current_vertex->parentconditions.begin();cond_ref!=current_vertex->parentconditions.end();cond_ref++) 1047 { 1048 if(*cond_ref == condition_to_remove) 1049 { 1050 break; 1051 } 1052 } 1053 1054 if(cond_ref!=current_vertex->parentconditions.end()) 1055 { 1056 current_vertex->parentconditions.erase(cond_ref); 1057 current_vertex->set_state(0,MERGE); 1058 for_merging[0].push_back(current_vertex); 1059 } 1060 else 1061 { 1062 double local_condition = toremove*appended_coords; 1063 current_vertex->set_state(local_condition,MERGE); 1064 } 1065 } 1066 } 1067 1068 send_state_message(current_vertex, condition_to_add, condition_to_remove, 0); 1069 1070 } 1071 1072 1073 1074 if(should_remove) 1075 { 1076 for(int i = 0;i<for_merging.size();i++) 1077 { 1078 for(list<polyhedron*>::iterator merge_ref = for_merging[i].begin();merge_ref!=for_merging[i].end();merge_ref++) 1023 1079 { 1024 double local_condition = toremove*appended_vec; 1025 1026 current_vertex->set_state(local_condition,MERGE); 1027 1028 if(local_condition == 0) 1029 { 1030 for_merging[0].push_back(current_vertex); 1031 } 1032 } 1033 } 1034 1035 send_state_message(current_vertex, toadd, toremove, 0); 1036 1037 } 1038 1039 if(should_remove) 1040 { 1080 cout << (*merge_ref)->get_state(MERGE) << ","; 1081 } 1082 1083 cout << endl; 1084 } 1085 1041 1086 set<vertex*> vertices_to_be_reduced; 1042 1087 … … 1063 1108 toprow* current_negative = (toprow*)(*merge_ref)->negativeparent; 1064 1109 1065 current_positive->condition -= toremove;1110 current_positive->condition_sum -= toremove; 1066 1111 current_positive->condition_order--; 1067 1112 … … 1225 1270 } 1226 1271 } 1272 1227 1273 1228 1274 if(should_add) … … 1247 1293 1248 1294 vec extended_coord2 = coordinates2; 1249 extended_coord2.ins(0,-1.0); 1250 1251 double t = (-toadd*extended_coord2)/( (toadd(1,toadd.size()-1)*(coordinates1-coordinates2)));1252 1253 vec new_coordinates = coordinates2+t*(coordinates1-coordinates2);1295 extended_coord2.ins(0,-1.0); 1296 1297 double t = (-toadd*extended_coord2)/(toadd(1,toadd.size()-1)*(coordinates1-coordinates2)); 1298 1299 vec new_coordinates = (1-t)*coordinates2+t*coordinates1; 1254 1300 1255 1301 // cout << "c1:" << coordinates1 << endl << "c2:" << coordinates2 << endl << "nc:" << new_coordinates << endl; … … 1263 1309 toprow* neutral_toprow = new toprow(); 1264 1310 1265 neutral_toprow->condition = ((toprow*)current_polyhedron)->condition; // tohle tu bylo driv: zeros(number_of_parameters+1);1311 neutral_toprow->condition_sum = ((toprow*)current_polyhedron)->condition_sum; // tohle tu bylo driv: zeros(number_of_parameters+1); 1266 1312 neutral_toprow->condition_order = ((toprow*)current_polyhedron)->condition_order+1; 1267 1313 … … 1279 1325 // cout << ((toprow*)current_polyhedron)->condition << endl << toadd << endl; 1280 1326 1281 toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition +toadd, ((toprow*)current_polyhedron)->condition_order+1);1282 toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition -toadd, ((toprow*)current_polyhedron)->condition_order+1);1327 toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition_sum+toadd, ((toprow*)current_polyhedron)->condition_order+1); 1328 toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition_sum-toadd, ((toprow*)current_polyhedron)->condition_order+1); 1283 1329 1284 1330 positive_poly->my_emlig = this; … … 1738 1784 }; 1739 1785 1740 /* 1786 1741 1787 1742 1788 //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density 1743 class RARX : public BM1789 class RARX //: public BM 1744 1790 { 1745 1791 private: 1746 1792 1747 emlig posterior; 1793 emlig* posterior; 1794 1795 int window_size; 1748 1796 1749 1797 public: 1750 RARX():BM() 1751 { 1798 RARX(int number_of_parameters, const int window_size)//:BM() 1799 { 1800 posterior = new emlig(number_of_parameters); 1801 1802 this->window_size = window_size; 1752 1803 }; 1753 1804 1754 void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec)1805 void bayes(const itpp::vec &yt, const itpp::vec &cond = "") 1755 1806 { 1756 1807 1757 1808 } 1758 1809 1759 }; */1810 }; 1760 1811 1761 1812