Changeset 1186 for applications/robust

Show
Ignore:
Timestamp:
09/15/10 19:14:46 (14 years ago)
Author:
sindj
Message:

Dodelana a odzkousena metoda create_statistic() v emlig. Zatim nefunguje dedeni od eEF. JS

Location:
applications/robust
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/main.cpp

    r976 r1186  
    99#include "robustlib.h" 
    1010 
    11 using namespace bdm; 
     11//using namespace bdm; 
    1212 
    1313int main ( int argc, char* argv[] ) { 
    1414         
     15        emlig* emlig1 = new emlig(10); 
     16 
    1517        return 0; 
    1618} 
  • applications/robust/robustlib.h

    r1172 r1186  
    8686}; 
    8787 
    88 /// A 
     88/// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse 
     89/// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. 
    8990class vertex : public polyhedron 
    9091{ 
     92        /// A dynamic array representing coordinates of the vertex 
    9193        vector<double> coordinates; 
    9294 
    9395public: 
    9496 
     97        /// Default constructor 
    9598        vertex(); 
    9699 
     100        /// Constructor of a vertex from a set of coordinates 
    97101        vertex(vector<double> coordinates) 
    98102        { 
     
    100104        } 
    101105 
     106        /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter 
     107        /// space of certain dimension is established, but the dimension is not known when the vertex is created. 
    102108        void push_coordinate(double coordinate) 
    103109        { 
     
    105111        } 
    106112 
     113        /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer  
     114        /// (not given by reference), but a new copy is created (they are given by value). 
    107115        vector<double> get_coordinates() 
    108116        { 
    109117                vector<double> returned_vec; 
    110118 
    111                 copy(this->coordinates.begin(),this->coordinates.end(),returned_vec.begin()); 
     119                for(int i = 0;i<coordinates.size();i++) 
     120                { 
     121                        returned_vec.push_back(coordinates[i]); 
     122                } 
    112123 
    113124                return returned_vec; 
     
    115126}; 
    116127 
     128/// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates 
     129/// it from polyhedrons in other rows.  
    117130class toprow : public polyhedron 
    118131{ 
     132        /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation 
    119133        vector<double> condition; 
    120134 
    121135public: 
    122136 
     137        /// Default constructor 
    123138        toprow(); 
    124139 
     140        /// Constructor creating a toprow from the condition 
    125141        toprow(vector<double> condition) 
    126142        { 
     
    134150 
    135151//! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density 
    136 class emlig : public eEF 
     152class emlig // : eEF 
    137153{ 
    138154 
     155        /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result 
     156        /// of data update from Bayesian estimation or set by the user if this emlig is a prior density 
    139157        vector<vector<polyhedron*>> statistic; 
    140158         
    141159public:  
    142160 
     161        /// A default constructor creates an emlig with predefined statistic representing only the range of the given 
     162        /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. 
    143163        emlig(int number_of_parameters) 
    144164        { 
     
    146166        } 
    147167 
     168        /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a 
     169        /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. 
    148170        emlig(vector<vector<polyhedron*>> statistic) 
    149171        { 
     
    151173        } 
    152174 
    153  
    154  
    155175protected: 
    156176 
     177        /// A method for creating plain default statistic representing only the range of the parameter space. 
    157178    void create_statistic(int number_of_parameters) 
    158179        { 
     180                // An empty vector of coordinates. 
    159181                vector<double> origin_coord;     
    160182 
     183                // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. 
    161184                vertex *origin = new vertex(origin_coord); 
    162185 
     186                // It has itself as a vertex. There will be a nice use for this when the vertices of its parents are searched in 
     187                // the recursive creation procedure below. 
    163188                origin->vertices.push_back(origin); 
    164189 
     190                // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse 
     191                // diagram. First we create a vector of polyhedrons.. 
    165192                vector<polyhedron*> origin_vec; 
    166193 
     194                // ..we fill it with the origin.. 
    167195                origin_vec.push_back(origin); 
    168196 
     197                // ..and we fill the statistic with the created vector. 
    169198                statistic.push_back(origin_vec); 
    170199 
     200                // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to  
     201                // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: 
    171202                for(int i=0;i<number_of_parameters;i++) 
    172203                { 
     204                        // We first will create two new vertices. These will be the borders of the parameter space in the dimension 
     205                        // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the  
     206                        // right amount of zero cooridnates by reading them from the origin 
    173207                        vector<double> origin_coord1 = origin->get_coordinates(); 
    174                         vector<double> origin_coord2 = origin->get_coordinates(); 
    175  
    176                         origin->push_coordinate(0); 
    177  
     208                        vector<double> origin_coord2 = origin->get_coordinates();                        
     209 
     210                        // And we incorporate the nonzero coordinates into the new cooordinate vectors 
    178211                        origin_coord1.push_back(max_range); 
    179212                        origin_coord2.push_back(-max_range); 
    180213 
     214                        // Now we create the points 
    181215                        vertex *new_point1 = new vertex(origin_coord1); 
    182                         vertex *new_point2 = new vertex(origin_coord2);  
    183  
     216                        vertex *new_point2 = new vertex(origin_coord2); 
     217                         
     218                        //********************************************************************************************************* 
     219                        // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old 
     220                        // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points 
     221                        // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons 
     222                        // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is 
     223                        // connected to the first (second) newly created vertex by a parent-child relation. 
     224                        //********************************************************************************************************* 
     225 
     226 
     227                        // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram 
    184228                        vector<vector<polyhedron*>> new_statistic1; 
    185229                        vector<vector<polyhedron*>> new_statistic2; 
    186230 
    187                          
     231                        // Copy the statistic by rows 
    188232                        for(int j=0;j<statistic.size();j++) 
    189233                        { 
     234                                // an element counter 
    190235                                int element_number = 0; 
    191236 
     237                                vector<polyhedron*> supportnew_1; 
     238                                vector<polyhedron*> supportnew_2; 
     239 
     240                                new_statistic1.push_back(supportnew_1); 
     241                                new_statistic2.push_back(supportnew_2); 
     242 
     243                                // for each polyhedron in the given row 
    192244                                for(vector<polyhedron*>::iterator horiz_ref = statistic[j].begin();horiz_ref<statistic[j].end();horiz_ref++) 
    193245                                {        
     246                                        // Append an extra zero coordinate to each of the vertices for the new dimension 
     247                                        // If j==0 => we loop through vertices 
     248                                        if(j == 0) 
     249                                        { 
     250                                                // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates 
     251                                                ((vertex*) (*horiz_ref))->push_coordinate(0); 
     252                                        } 
     253 
     254                                        // if it has parents 
    194255                                        if(!(*horiz_ref)->parents.empty()) 
    195256                                        { 
     257                                                // save the relative address of this child in a vector kids_rel_addresses of all its parents. 
     258                                                // This information will later be used for copying the whole Hasse diagram with each of the 
     259                                                // relations contained within. 
    196260                                                for(vector<polyhedron*>::iterator parent_ref = (*horiz_ref)->parents.begin();parent_ref < (*horiz_ref)->parents.end();parent_ref++) 
    197261                                                { 
     
    200264                                        } 
    201265 
     266                                        // ************************************************************************************************** 
     267                                        // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron 
     268                                        // will be created as a toprow, but this information will be later forgotten and only the polyhedrons 
     269                                        // in the top row of the Hasse diagram will be considered toprow for later use. 
     270                                        // ************************************************************************************************** 
     271 
     272                                        // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic 
     273                                        // this condition will be a vector of zeros. There are two vectors, because we need two copies of  
     274                                        // the original Hasse diagram. 
    202275                                        vector<double> vec1(i+2,0); 
    203276                                        vector<double> vec2(i+2,0); 
    204277 
     278                                        // We create a new toprow with the previously specified condition. 
    205279                                        toprow *current_copy1 = new toprow(vec1); 
    206280                                        toprow *current_copy2 = new toprow(vec2);                                        
    207281 
     282                                        // The vertices of the copies will be inherited, because there will be a parent/child relation 
     283                                        // between each polyhedron and its offspring (comming from the copy) and a parent has all the 
     284                                        // vertices of its child plus more. 
    208285                                        for(vector<vertex*>::iterator vert_ref = (*horiz_ref)->vertices.begin();vert_ref<(*horiz_ref)->vertices.end();vert_ref++) 
    209286                                        { 
     
    212289                                        } 
    213290                                         
    214  
     291                                        // The only new vertex of the offspring should be the newly created point. 
    215292                                        current_copy1->vertices.push_back(new_point1); 
    216293                                        current_copy2->vertices.push_back(new_point2); 
    217  
     294                                         
     295                                        // This method guarantees that each polyhedron is already triangulated, therefore its triangulation 
     296                                        // is only one set of vertices and it is the set of all its vertices. 
    218297                                        current_copy1->triangulations.push_back(current_copy1->vertices); 
    219298                                        current_copy2->triangulations.push_back(current_copy2->vertices); 
    220  
    221                                          
    222  
     299                                         
     300                                        // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying 
     301                                        // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the 
     302                                        // kids and when we do that and know the child, in the child we will remember the parent we came from. 
     303                                        // This way all the parents/children relations are saved in both the parent and the child. 
    223304                                        if(!(*horiz_ref)->kids_rel_addresses.empty()) 
    224305                                        { 
    225306                                                for(vector<int>::iterator kid_ref = (*horiz_ref)->kids_rel_addresses.begin();kid_ref<(*horiz_ref)->kids_rel_addresses.end();kid_ref++) 
    226                                                 {                                                
    227                                                         current_copy1->children.push_back(new_statistic1[i][(*kid_ref)]); 
    228                                                         current_copy2->children.push_back(new_statistic2[i][(*kid_ref)]); 
    229  
    230                                                         new_statistic1[i][(*kid_ref)]->parents.push_back(current_copy1); 
    231                                                         new_statistic2[i][(*kid_ref)]->parents.push_back(current_copy2); 
     307                                                {        
     308                                                        // find the child and save the relation to the parent 
     309                                                        current_copy1->children.push_back(new_statistic1[j-1][(*kid_ref)]); 
     310                                                        current_copy2->children.push_back(new_statistic2[j-1][(*kid_ref)]); 
     311 
     312                                                        // in the child save the parents' address 
     313                                                        new_statistic1[j-1][(*kid_ref)]->parents.push_back(current_copy1); 
     314                                                        new_statistic2[j-1][(*kid_ref)]->parents.push_back(current_copy2); 
    232315                                                }                                                
    233316 
     317                                                // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the 
     318                                                // Hasse diagram again) 
    234319                                                (*horiz_ref)->kids_rel_addresses.clear(); 
    235320                                        } 
     321                                        // If there were no children previously, we are copying a polyhedron that has been a vertex before. 
     322                                        // In this case it is a segment now and it will have a relation to its mother (copywise) and to the 
     323                                        // newly created point. Here we create the connection to the new point, again from both sides. 
    236324                                        else 
    237325                                        { 
     326                                                // Add the address of the new point in the former vertex 
    238327                                                current_copy1->children.push_back(new_point1); 
    239328                                                current_copy2->children.push_back(new_point2); 
    240329 
     330                                                // Add the address of the former vertex in the new point 
    241331                                                new_point1->parents.push_back(current_copy1); 
    242332                                                new_point2->parents.push_back(current_copy2); 
    243333                                        } 
    244334 
     335                                        // Save the mother in its offspring 
    245336                                        current_copy1->children.push_back(*horiz_ref); 
    246337                                        current_copy2->children.push_back(*horiz_ref); 
    247338 
    248                                         new_statistic1[i+1].push_back(current_copy1); 
    249                                         new_statistic2[i+1].push_back(current_copy2); 
    250                                          
     339                                        // Save the offspring in its mother 
     340                                        (*horiz_ref)->parents.push_back(current_copy1); 
     341                                        (*horiz_ref)->parents.push_back(current_copy2);  
     342                                                                 
     343                                         
     344                                        // Add the copies into the relevant statistic. The statistic will later be appended to the previous 
     345                                        // Hasse diagram 
     346                                        new_statistic1[j].push_back(current_copy1); 
     347                                        new_statistic2[j].push_back(current_copy2); 
     348                                         
     349                                        // Raise the count in the vector of polyhedrons 
    251350                                        element_number++; 
     351                                         
    252352                                }                                
    253                         }                                
    254                          
     353                        } 
     354 
     355                        statistic[0].push_back(new_point1); 
     356                        statistic[0].push_back(new_point2); 
     357 
     358                        // Merge the new statistics into the old one. This will either be the final statistic or we will 
     359                        // reenter the widening loop.  
     360                        for(int j=0;j<new_statistic1.size();j++) 
     361                        { 
     362                                if(j+1==statistic.size()) 
     363                                { 
     364                                        vector<polyhedron*> support; 
     365                                        statistic.push_back(support); 
     366                                } 
     367                                 
     368                                statistic[j+1].insert(statistic[j+1].end(),new_statistic1[j].begin(),new_statistic1[j].end()); 
     369                                statistic[j+1].insert(statistic[j+1].end(),new_statistic2[j].begin(),new_statistic2[j].end()); 
     370                        } 
    255371                } 
    256372        }