Changeset 1214 for applications/robust

Show
Ignore:
Timestamp:
10/13/10 20:15:15 (14 years ago)
Author:
sindj
Message:

Dodelavani splitu v pridavani podminky - nedokonceno. JS

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • applications/robust/robustlib.h

    r1213 r1214  
    7171        polyhedron* negativeparent; 
    7272 
     73        polyhedron* next_poly; 
     74 
     75        polyhedron* prev_poly; 
     76 
    7377        int message_counter; 
    7478 
     
    222226}; 
    223227 
     228class c_statistic 
     229{ 
     230        polyhedron* end_poly; 
     231        polyhedron* start_poly; 
     232 
     233public: 
     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 
    224374 
    225375//! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density 
     
    229379        /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result 
    230380        /// 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; 
    232382 
    233383        vector<list<polyhedron*>> for_splitting; 
     
    241391        void alter_toprow_conditions(vec condition, bool should_be_added) 
    242392        { 
    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) 
    244394                { 
    245395                        double product = 0; 
    246396 
    247                         list<vertex*>::iterator vertex_ref = (*horiz_ref)->vertices.begin(); 
     397                        list<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); 
    248398 
    249399                        do 
     
    255405                        if((product>0 && should_be_added)||(product<0 && !should_be_added)) 
    256406                        { 
    257                                 ((toprow*) (*horiz_ref))->condition += condition; 
     407                                ((toprow*) horiz_ref)->condition += condition; 
    258408                        } 
    259409                        else 
    260410                        { 
    261                                 ((toprow*) (*horiz_ref))->condition -= condition; 
    262                         }                                                        
     411                                ((toprow*) horiz_ref)->condition -= condition; 
     412                        }                                
    263413                } 
    264414        } 
     
    427577        /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. 
    428578        emlig(int number_of_parameters) 
    429         { 
     579        {        
     580 
    430581                create_statistic(number_of_parameters); 
    431582 
    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++) 
    433584                { 
    434585                        list<polyhedron*> empty_split; 
     
    442593        /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a 
    443594        /// 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) 
    445596        { 
    446597                this->statistic = statistic; 
     
    519670 
    520671                 
    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) 
    523674                {                
    524                         vertex* current_vertex = (vertex*)(*horizontal_position); 
     675                        vertex* current_vertex = (vertex*)horizontal_position; 
    525676                         
    526677                        if(should_add||should_remove) 
     
    556707                        } 
    557708 
    558                         send_state_message(current_vertex, toadd, toremove, 0);                  
     709                        send_state_message(current_vertex, toadd, toremove, 0);          
     710                         
    559711                } 
    560712 
    561713                if(should_add) 
    562714                { 
    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++) 
    564720                        {                        
    565721 
     
    570726                                        polyhedron* current_polyhedron = (*split_ref); 
    571727                                         
    572                                         if(vert_ref == for_splitting.begin()) 
     728                                        if(vert_ref == beginning_ref) 
    573729                                        { 
    574730                                                vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates();                                              
     
    617773                                                                                                                        current_polyhedron->negativechildren.end()); 
    618774 
     775                                        statistic.append_polyhedron(k-1, new_totally_neutral_child); 
    619776                                         
    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);                              
    624781                                } 
    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                } 
    629792        } 
    630793 
     
    644807                origin->vertices.push_back(origin); 
    645808 
     809                /* 
    646810                // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse 
    647811                // diagram. First we create a vector of polyhedrons.. 
     
    653817                // ..and we fill the statistic with the created vector. 
    654818                statistic.push_back(origin_vec); 
     819                */ 
     820 
     821                statistic = *(new c_statistic()); 
     822 
     823                statistic.append_polyhedron(0, origin); 
    655824 
    656825                // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to  
     
    680849 
    681850 
     851                        /* 
    682852                        // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram 
    683853                        vector<vector<polyhedron*>> new_statistic1; 
    684854                        vector<vector<polyhedron*>> new_statistic2; 
     855                        */ 
     856 
     857                        c_statistic* new_statistic1 = new c_statistic(); 
     858                        c_statistic* new_statistic2 = new c_statistic(); 
    685859 
    686860                         
     
    693867                                int element_number = 0; 
    694868 
     869                                /* 
    695870                                vector<polyhedron*> supportnew_1; 
    696871                                vector<polyhedron*> supportnew_2; 
     
    698873                                new_statistic1.push_back(supportnew_1); 
    699874                                new_statistic2.push_back(supportnew_2); 
     875                                */ 
    700876 
    701877                                // 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) 
    703879                                {        
    704880                                        // Append an extra zero coordinate to each of the vertices for the new dimension 
     
    707883                                        { 
    708884                                                // 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); 
    710886                                        } 
    711887                                        /* 
     
    716892 
    717893                                        // if it has parents 
    718                                         if(!(*horiz_ref)->parents.empty()) 
     894                                        if(!horiz_ref->parents.empty()) 
    719895                                        { 
    720896                                                // save the relative address of this child in a vector kids_rel_addresses of all its parents. 
    721897                                                // This information will later be used for copying the whole Hasse diagram with each of the 
    722898                                                // 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++) 
    724900                                                { 
    725901                                                        (*parent_ref)->kids_rel_addresses.push_back(element_number);                                                     
     
    749925                                        // between each polyhedron and its offspring (comming from the copy) and a parent has all the 
    750926                                        // 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++) 
    752928                                        { 
    753929                                                current_copy1->vertices.push_back(*vertex_ref); 
     
    768944                                        // kids and when we do that and know the child, in the child we will remember the parent we came from. 
    769945                                        // 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++) 
    773949                                                {        
     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                                                         
    774964                                                        // 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); 
    777967 
    778968                                                        // 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); 
    781971                                                }                                                
    782972 
    783973                                                // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the 
    784974                                                // Hasse diagram again) 
    785                                                 (*horiz_ref)->kids_rel_addresses.clear(); 
     975                                                horiz_ref->kids_rel_addresses.clear(); 
    786976                                        } 
    787977                                        // If there were no children previously, we are copying a polyhedron that has been a vertex before. 
     
    800990 
    801991                                        // 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); 
    804994 
    805995                                        // 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);     
    808998                                                                 
    809999                                         
    8101000                                        // Add the copies into the relevant statistic. The statistic will later be appended to the previous 
    8111001                                        // 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); 
    8141004                                         
    8151005                                        // Raise the count in the vector of polyhedrons 
    816                                         element_number++; 
     1006                                        element_number++;                        
    8171007                                         
    8181008                                } 
     
    8201010                        } 
    8211011 
     1012                        /* 
    8221013                        statistic.begin()->push_back(new_point1); 
    8231014                        statistic.begin()->push_back(new_point2); 
     1015                        */ 
     1016 
     1017                        statistic.append_polyhedron(0, new_point1); 
     1018                        statistic.append_polyhedron(0, new_point2); 
    8241019 
    8251020                        // Merge the new statistics into the old one. This will either be the final statistic or we will 
    8261021                        // 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                                /* 
    8291025                                if(j+1==statistic.size()) 
    8301026                                { 
     
    8351031                                (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); 
    8361032                                (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]); 
    8371036                        } 
    8381037 
     
    8401039                } 
    8411040 
     1041                /* 
    8421042                vector<list<toprow*>> toprow_statistic; 
    8431043                int line_count = 0; 
     
    8561056 
    8571057                        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                 
    8591067        } 
    8601068