Changeset 1207 for applications/robust

Show
Ignore:
Timestamp:
10/03/10 12:32:08 (14 years ago)
Author:
sindj
Message:

Zase kousek dal. JS

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/robustlib.h

    r1204 r1207  
    130130        int number_of_children() 
    131131        { 
    132                 return children.size()+positivechildren.size()+negativechildren.size()+neutralchildren.size(); 
    133         } 
    134  
    135         void send_state_message(bool shouldsplit, bool shouldmerge) 
     132                return children.size(); 
     133        } 
     134 
     135         
     136}; 
     137 
     138/// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse 
     139/// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. 
     140class vertex : public polyhedron 
     141{ 
     142        /// A dynamic array representing coordinates of the vertex 
     143        vec coordinates;         
     144 
     145        enum actions {MERGE, SPLIT}; 
     146 
     147public: 
     148 
     149 
     150 
     151        /// Default constructor 
     152        vertex(); 
     153 
     154        /// Constructor of a vertex from a set of coordinates 
     155        vertex(vec coordinates) 
     156        { 
     157                this->coordinates = coordinates; 
     158        } 
     159 
     160        /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter 
     161        /// space of certain dimension is established, but the dimension is not known when the vertex is created. 
     162        void push_coordinate(double coordinate) 
     163        { 
     164                coordinates = concat(coordinates,coordinate); 
     165        } 
     166 
     167        /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer  
     168        /// (not given by reference), but a new copy is created (they are given by value). 
     169        vec get_coordinates() 
     170        {                
     171                return coordinates; 
     172        } 
     173 
     174                 
     175}; 
     176 
     177/// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates 
     178/// it from polyhedrons in other rows.  
     179class toprow : public polyhedron 
     180{ 
     181         
     182public: 
     183        /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation 
     184        vec condition; 
     185 
     186        /// Default constructor 
     187        toprow(); 
     188 
     189        /// Constructor creating a toprow from the condition 
     190        toprow(vec condition) 
     191        { 
     192                this->condition = condition; 
     193        } 
     194 
     195}; 
     196 
     197class condition 
     198{        
     199public: 
     200        vec value;       
     201 
     202        int multiplicity; 
     203 
     204        condition(vec value) 
     205        { 
     206                this->value = value; 
     207                multiplicity = 1; 
     208        } 
     209} 
     210 
     211 
     212//! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density 
     213class emlig // : eEF 
     214{ 
     215 
     216        /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result 
     217        /// of data update from Bayesian estimation or set by the user if this emlig is a prior density 
     218        vector<vector<polyhedron*>> statistic; 
     219 
     220        vector<vector<polyhedron*>> for_splitting; 
     221                 
     222        vector<vector<polyhedron*>> for_merging; 
     223 
     224        vector<condition*> conditions; 
     225 
     226        double normalization_factor; 
     227 
     228        void alter_toprow_conditions(vec condition, bool should_be_added) 
     229        { 
     230                for(vector<polyhedron*>::iterator horiz_ref = statistic[statistic.size()-1].begin();horiz_ref<statistic[statistic.size()-1].end();horiz_ref++) 
     231                { 
     232                        double product = 0; 
     233 
     234                        vector<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin(); 
     235 
     236                        do 
     237                        { 
     238                                product = (*vertex_ref)->coordinates*condition; 
     239                        } 
     240                        while(product == 0) 
     241 
     242                        if((product>0 && should_be_added)||(product<0 && !should_be_added)) 
     243                        { 
     244                                ((toprow*) (*horiz_ref))->condition += condition; 
     245                        } 
     246                        else 
     247                        { 
     248                                ((toprow*) (*horiz_ref))->condition -= condition; 
     249                        }                                                        
     250                } 
     251        } 
     252 
     253 
     254        void send_state_message(polyhedron* sender, bool shouldsplit, bool shouldmerge, int level) 
    136255        { 
    137256                if(shouldsplit||shouldmerge) 
    138257                { 
    139                         for(vector<polyhedron*>::iterator parent_iterator = parents.begin();parent_iterator<parents.end();parent_iterator++) 
     258                        for(vector<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator<sender->parents.end();parent_iterator++) 
    140259                        { 
    141260                                polyhedron* current_parent = *parent_iterator; 
     
    147266                                if(shouldmerge) 
    148267                                { 
    149                                         int child_state  = get_state(MERGE); 
     268                                        int child_state  = sender->get_state(MERGE); 
    150269                                        int parent_state = current_parent->get_state(MERGE); 
    151270 
    152                                         if(parent_state == NULL || parent_state == 0) 
     271                                        if(parent_state == 0) 
    153272                                        { 
    154273                                                current_parent->set_state(child_state, MERGE); 
     
    156275                                                if(child_state == 0) 
    157276                                                { 
    158                                                         current_parent->mergechildren.push_back(this); 
     277                                                        current_parent->mergechildren.push_back(sender); 
    159278                                                } 
    160279                                        } 
     
    165284                                                        if(parent_state > 0) 
    166285                                                        { 
    167                                                                 positiveparent = current_parent; 
     286                                                                sender->positiveparent = current_parent; 
    168287                                                        } 
    169288                                                        else 
    170289                                                        { 
    171                                                                 negativeparent = current_parent; 
     290                                                                sender->negativeparent = current_parent; 
    172291                                                        } 
    173292                                                } 
     
    192311                                                } 
    193312 
     313                                                if(parent_state == 0) 
     314                                                { 
     315                                                        for_merging[level+1].push_back(current_parent); 
     316                                                } 
     317 
    194318                                                current_parent->mergechildren.clear(); 
    195319                                        } 
    196320 
    197  
    198                                 } 
     321                                        if(shouldsplit) 
     322                                        { 
     323                                                switch(sender->get_state(SPLIT)) 
     324                                                { 
     325                                                case 1: 
     326                                                        current_parent->positivechildren.push_back(sender);      
     327                                                break; 
     328                                                case 0: 
     329                                                        current_parent->neutralchildren.push_back(sender); 
     330                                                break; 
     331                                                case -1: 
     332                                                        current_parent->negativechildren.push_back(sender); 
     333                                                break; 
     334                                                } 
     335 
     336                                                if(is_last) 
     337                                                { 
     338                                                        if(current_parent->negativechildren.size()>0)  
     339                                                        {                                                                
     340                                                                if(current_parent->positivechildren.size()>0) 
     341                                                                { 
     342                                                                        for_splitting[level+1].push_back(current_parent); 
     343                                                                 
     344                                                                        current_parent->set_state(0, SPLIT); 
     345                                                                } 
     346                                                                else  
     347                                                                { 
     348                                                                        current_parent->set_state(-1, SPLIT); 
     349 
     350                                                                        current_parent->negativechildren.clear(); 
     351                                                                        current_parent->neutralchildren.clear(); 
     352                                                                } 
     353                                                        } 
     354                                                        else if(current_parent->positivechildren.size()>0) 
     355                                                        { 
     356                                                                current_parent->set_state(1, SPLIT); 
     357 
     358                                                                current_parent->positivechildren.clear(); 
     359                                                                current_parent->neutralchildren.clear(); 
     360                                                        } 
     361                                                        else 
     362                                                        { 
     363                                                                current_parent->raise_multiplicity(); 
     364 
     365                                                                current_parent->neutralchildren.clear(); 
     366                                                        } 
     367                                                } 
     368                                        } 
     369 
     370                                        if(is_last) 
     371                                        { 
     372                                                send_state_message(current_parent,shouldsplit,shouldmerge,level+1); 
     373                                        } 
     374                                }                        
    199375                         
    200376                        } 
    201                 } 
    202         } 
    203 }; 
    204  
    205 /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse 
    206 /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. 
    207 class vertex : public polyhedron 
    208 { 
    209         /// A dynamic array representing coordinates of the vertex 
    210         vec coordinates;         
    211  
    212         enum actions {MERGE, SPLIT}; 
    213  
    214 public: 
    215  
    216  
    217  
    218         /// Default constructor 
    219         vertex(); 
    220  
    221         /// Constructor of a vertex from a set of coordinates 
    222         vertex(vec coordinates) 
    223         { 
    224                 this->coordinates = coordinates; 
    225         } 
    226  
    227         /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter 
    228         /// space of certain dimension is established, but the dimension is not known when the vertex is created. 
    229         void push_coordinate(double coordinate) 
    230         { 
    231                 coordinates = concat(coordinates,coordinate); 
    232         } 
    233  
    234         /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer  
    235         /// (not given by reference), but a new copy is created (they are given by value). 
    236         vec get_coordinates() 
    237         {                
    238                 return coordinates; 
    239         } 
    240  
    241                  
    242 }; 
    243  
    244 /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates 
    245 /// it from polyhedrons in other rows.  
    246 class toprow : public polyhedron 
    247 { 
    248          
    249 public: 
    250         /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation 
    251         vec condition; 
    252  
    253         /// Default constructor 
    254         toprow(); 
    255  
    256         /// Constructor creating a toprow from the condition 
    257         toprow(vec condition) 
    258         { 
    259                 this->condition = condition; 
    260         } 
    261  
    262 }; 
    263  
    264 class condition 
    265 {        
    266 public: 
    267         vec value;       
    268  
    269         int multiplicity; 
    270  
    271         condition(vec value) 
    272         { 
    273                 this->value = value; 
    274                 multiplicity = 1; 
    275         } 
    276 } 
    277  
    278  
    279 //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density 
    280 class emlig // : eEF 
    281 { 
    282  
    283         /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result 
    284         /// of data update from Bayesian estimation or set by the user if this emlig is a prior density 
    285         vector<vector<polyhedron*>> statistic; 
    286  
    287         vector<condition*> conditions; 
    288  
    289         double normalization_factor; 
    290  
    291         void alter_toprow_conditions(vec condition, bool should_be_added) 
    292         { 
    293                 for(vector<polyhedron*>::iterator horiz_ref = statistic[statistic.size()-1].begin();horiz_ref<statistic[statistic.size()-1].end();horiz_ref++) 
    294                 { 
    295                         double product = 0; 
    296  
    297                         vector<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin(); 
    298  
    299                         do 
    300                         { 
    301                                 product = (*vertex_ref)->coordinates*condition; 
    302                         } 
    303                         while(product == 0) 
    304  
    305                         if((product>0 && should_be_added)||(product<0 && !should_be_added)) 
    306                         { 
    307                                 ((toprow*) (*horiz_ref))->condition += condition; 
    308                         } 
    309                         else 
    310                         { 
    311                                 ((toprow*) (*horiz_ref))->condition -= condition; 
    312                         }                                                        
    313                 } 
     377                         
     378                }                
    314379        } 
    315380         
     
    321386        { 
    322387                create_statistic(number_of_parameters); 
     388 
     389                for(int i = 0;i<statistic.size();i++) 
     390                { 
     391                        vector<polyhedron*> empty_split; 
     392                        vector<polyhedron*> empty_merge; 
     393 
     394                        for_splitting.push_back(empty_split); 
     395                        for_merging.push_back(empty_merge); 
     396                } 
    323397        } 
    324398 
     
    383457                } 
    384458 
    385                 vector<vector<polyhedron*>> for_splitting; 
    386                 vector<vector<polyhedron*>> for_merging; 
     459                 
    387460 
    388461                for(vector<polyhedron*>::iterator horizontal_position = statistic[0].begin();horizontal_position<statistic[0].end();horizontal_position++) 
     
    398471                        { 
    399472                                current_vertex->set_state(toremove*current_vertex->coordinates,MERGE); 
    400                         } 
    401  
    402                         current_vertex->send_state_message(toadd != NULL, toremove != NULL); 
     473 
     474                                if(current_vertex->get_state(MERGE) == 0) 
     475                                { 
     476                                        for_merging[0].push_back(current_vertex); 
     477                                } 
     478                        } 
     479 
     480                        send_state_message(current_vertex, toadd != NULL, toremove != NULL, 0); 
     481 
     482                         
    403483                } 
    404484        }