Changeset 1300 for applications/robust

Show
Ignore:
Timestamp:
03/18/11 17:59:56 (13 years ago)
Author:
sindj
Message:

Dalsi update merge polyhedronu. Rozhodovani o prislusnosti k podmince predelano z urcovani podle souradnic na urcovani podle materskych podminek (nedodelano - je treba dodelat progresovani podminek).JS

Location:
applications/robust
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r1299 r1300  
    2020int main ( int argc, char* argv[] ) { 
    2121         
    22          
     22        /* 
    2323        vector<vector<vector<string>>> string_lists; 
    2424        string_lists.push_back(vector<vector<string>>()); 
     
    5555        for(int j = 0;j<string_lists.size();j++) 
    5656        {  
    57                  
     57                /* 
    5858                for(int i = 0;i<string_lists[j].size()-1;i++) 
    5959                { 
     
    118118                myfile.close(); 
    119119        } 
    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); 
    124125         
    125126        /* 
     
    141142        }*/ 
    142143         
    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 
    145147        emlig1->add_condition(condition5); 
    146148         
    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         
    166173        //cout << emlig1->minimal_vertex->get_coordinates(); 
    167  
     174         
    168175        emlig1->remove_condition(condition5); 
    169         cout << "************************************************" << endl; 
    170          
     176 
    171177        //emlig1->step_me(0); 
    172178        //emlig2->step_me(0); 
  • applications/robust/robustlib.cpp

    r1299 r1300  
    6666                        toprow* as_toprow = (toprow*)this; 
    6767 
    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); 
    6969 
    7070                        // cout << as_toprow->condition << endl;                         
     
    9191                                cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl; 
    9292 
    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]; 
    9494                                 
    9595 
     
    127127                                                } 
    128128 
    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]; 
    130130                                                if(a_m<current_emlig->likelihood_value) 
    131131                                                { 
  • applications/robust/robustlib.h

    r1299 r1300  
    4848class polyhedron; 
    4949 
     50class condition 
     51{        
     52public: 
     53        vec value;       
     54 
     55        int multiplicity; 
     56 
     57        condition(vec value) 
     58        { 
     59                this->value = value; 
     60                multiplicity = 1; 
     61        } 
     62}; 
    5063 
    5164 
     
    7790        set<vertex*> vertices; 
    7891 
     92        set<condition*> parentconditions; 
     93 
    7994        /// A list used for storing children that lie in the positive region related to a certain condition 
    8095        list<polyhedron*> positivechildren; 
     
    124139 
    125140                totally_neutral = NULL; 
     141 
     142                mergechild = NULL;               
    126143        } 
    127144         
     
    157174         
    158175 
    159         void set_state(double state_indicator, actions action) 
     176        int set_state(double state_indicator, actions action) 
    160177        { 
    161178                switch(action) 
    162179                { 
    163180                        case MERGE: 
    164                                 merge_state = (int)sign(state_indicator);                        
    165                         break; 
     181                                merge_state = (int)sign(state_indicator); 
     182                                return merge_state;                      
    166183                        case SPLIT: 
    167184                                split_state = (int)sign(state_indicator); 
    168                         break; 
     185                                return split_state;              
    169186                } 
    170187        } 
     
    210227        vertex(vec coordinates) 
    211228        { 
    212                 this->coordinates = coordinates; 
     229                this->coordinates   = coordinates; 
    213230 
    214231                vertices.insert(this); 
     
    225242        void push_coordinate(double coordinate) 
    226243        { 
    227                 coordinates = concat(coordinates,coordinate); 
     244                coordinates  = concat(coordinates,coordinate);           
    228245        } 
    229246 
     
    231248        /// (not given by reference), but a new copy is created (they are given by value). 
    232249        vec get_coordinates() 
    233         {                
     250        { 
    234251                return coordinates; 
    235252        } 
    236  
    237253                 
    238254}; 
     
    250266 
    251267        /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation 
    252         vec condition; 
     268        vec condition_sum; 
    253269 
    254270        int condition_order; 
     
    258274 
    259275        /// Constructor creating a toprow from the condition 
    260         toprow(vec condition, int condition_order) 
    261         { 
    262                 this->condition = condition; 
     276        toprow(condition *condition, int condition_order) 
     277        { 
     278                this->condition_sum   = condition->value; 
    263279                this->condition_order = condition_order; 
    264280        } 
    265281 
     282        toprow(vec condition_sum, int condition_order) 
     283        { 
     284                this->condition_sum = condition_sum; 
     285        } 
     286 
    266287        double integrate_simplex(set<vertex*> simplex, char c); 
    267288 
     
    269290 
    270291 
    271 class condition 
    272 {        
    273 public: 
    274         vec value;       
    275  
    276         int multiplicity; 
    277  
    278         condition(vec value) 
    279         { 
    280                 this->value = value; 
    281                 multiplicity = 1; 
    282         } 
    283 }; 
    284292 
    285293 
     
    603611         
    604612 
    605         void alter_toprow_conditions(vec condition, bool should_be_added) 
     613        void alter_toprow_conditions(condition *condition, bool should_be_added) 
    606614        { 
    607615                for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) 
    608616                { 
    609                         double product = 0; 
    610  
    611617                        set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); 
    612618 
    613619                        do 
    614620                        { 
    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; 
    618626 
    619627                        if((product>0 && should_be_added)||(product<0 && !should_be_added)) 
    620628                        { 
    621                                 ((toprow*) horiz_ref)->condition += condition; 
     629                                ((toprow*) horiz_ref)->condition_sum += condition->value; 
    622630                        } 
    623631                        else 
    624632                        { 
    625                                 ((toprow*) horiz_ref)->condition -= condition; 
     633                                ((toprow*) horiz_ref)->condition_sum -= condition->value; 
    626634                        }                                
    627635                } 
     
    630638 
    631639 
    632         void send_state_message(polyhedron* sender, vec toadd, vec toremove, int level) 
     640        void send_state_message(polyhedron* sender, condition *toadd, condition *toremove, int level) 
    633641        {                        
    634642 
    635                 bool shouldmerge = (toremove.size() != 0); 
    636                 bool shouldsplit    = (toadd.size() != 0); 
     643                bool shouldmerge    = (toremove == NULL); 
     644                bool shouldsplit    = (toadd == NULL); 
    637645                 
    638646                if(shouldsplit||shouldmerge) 
     
    644652                                current_parent->message_counter++; 
    645653 
    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); 
    647656 
    648657                                if(shouldmerge) 
     
    651660                                        int parent_state = current_parent->get_state(MERGE); 
    652661 
    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                                        }                                        
    657666 
    658667                                        if(child_state == 0) 
     
    686695                                                { 
    687696                                                        for_merging[level+1].push_back(current_parent);                                                  
     697                                                } 
     698                                                else 
     699                                                { 
     700                                                        current_parent->set_state(0,MERGE);                                                      
    688701                                                } 
    689702 
     
    751764                                                                        current_parent->set_state(-1, SPLIT); 
    752765 
    753                                                                         ((toprow*)current_parent)->condition-=toadd; 
     766                                                                        ((toprow*)current_parent)->condition_sum-=toadd->value; 
    754767 
    755768                                                                         
     
    759772                                                                        current_parent->set_state(1, SPLIT); 
    760773 
    761                                                                         ((toprow*)current_parent)->condition+=toadd;                                                                     
     774                                                                        ((toprow*)current_parent)->condition_sum+=toadd->value;                                                                  
    762775                                                                } 
    763776                                                                else 
     
    921934        } 
    922935 
    923  
    924936        void add_and_remove_condition(vec toadd, vec toremove) 
    925937        { 
     
    954966                                                (*ref)->multiplicity--; 
    955967 
    956                                                 alter_toprow_conditions(toremove,false); 
     968                                                alter_toprow_conditions(*ref,false); 
    957969 
    958970                                                should_remove = false; 
     
    971983                                        (*ref)->multiplicity++; 
    972984 
    973                                         alter_toprow_conditions(toadd,true); 
     985                                        alter_toprow_conditions(*ref,true); 
    974986 
    975987                                        should_add = false; 
     
    982994                } 
    983995 
     996                condition* condition_to_remove = NULL; 
     997 
    984998                if(toremove_ref!=conditions.end()) 
    985999                { 
    9861000                        conditions.erase(toremove_ref); 
    987                 } 
     1001                        condition_to_remove = *toremove_ref; 
     1002                } 
     1003 
     1004                condition* condition_to_add = NULL; 
    9881005 
    9891006                if(condition_should_be_added) 
     
    10001017                        if(should_add||should_remove) 
    10011018                        { 
    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);                             
    10041021 
    10051022                                if(should_add) 
    10061023                                { 
    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; 
    10081027 
    10091028                                        current_vertex->set_state(local_condition,SPLIT); 
    10101029 
     1030                                        /// \TODO There should be a rounding error tolerance used here to insure we are not having too many points because of rounding error. 
    10111031                                        if(local_condition == 0) 
    10121032                                        { 
     
    10211041                         
    10221042                                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++) 
    10231079                                { 
    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 
    10411086                        set<vertex*> vertices_to_be_reduced;                     
    10421087                         
     
    10631108                                                toprow* current_negative = (toprow*)(*merge_ref)->negativeparent; 
    10641109 
    1065                                                 current_positive->condition -= toremove; 
     1110                                                current_positive->condition_sum -= toremove; 
    10661111                                                current_positive->condition_order--; 
    10671112                                                 
     
    12251270                        }                        
    12261271                } 
     1272                 
    12271273 
    12281274                if(should_add) 
     
    12471293                                                 
    12481294                                                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;                                                 
    12541300 
    12551301                                                // cout << "c1:" << coordinates1 << endl << "c2:" << coordinates2 << endl << "nc:" << new_coordinates << endl; 
     
    12631309                                                toprow* neutral_toprow = new toprow(); 
    12641310                                                 
    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); 
    12661312                                                neutral_toprow->condition_order = ((toprow*)current_polyhedron)->condition_order+1; 
    12671313 
     
    12791325                                        // cout << ((toprow*)current_polyhedron)->condition << endl << toadd << endl; 
    12801326 
    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); 
    12831329 
    12841330                                        positive_poly->my_emlig = this; 
     
    17381784}; 
    17391785 
    1740 /* 
     1786 
    17411787 
    17421788//! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density 
    1743 class RARX : public BM 
     1789class RARX //: public BM 
    17441790{ 
    17451791private: 
    17461792 
    1747         emlig posterior; 
     1793        emlig* posterior; 
     1794 
     1795        int window_size; 
    17481796 
    17491797public: 
    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; 
    17521803        }; 
    17531804 
    1754         void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec) 
     1805        void bayes(const itpp::vec &yt, const itpp::vec &cond = "") 
    17551806        { 
    17561807                 
    17571808        } 
    17581809 
    1759 };*/ 
     1810}; 
    17601811 
    17611812