root/applications/robust/robustlib.cpp @ 1301

Revision 1301, 7.7 kB (checked in by sindj, 13 years ago)

Dokoncovani mergovani polyhedronu a oprava chyb. Mergovani je skoro dokonceno, ale je tam nekde skryta chybka. JS

RevLine 
[976]1#include "robustlib.h"
2
[1282]3
[1254]4void polyhedron::triangulate(bool should_integrate)
[1273]5        {
[1301]6                triangulation.clear();
[1299]7
[1271]8                if(should_integrate)
9                {
10                        ((toprow *)this)->probability = 0.0;
11                }               
12               
[1301]13                if(vertices.size()==1)
14                {
15                        set<vertex*> vert_simplex;
16                        vert_simplex.insert((vertex*)this);
17
18                        triangulation.push_back(vert_simplex);
19                }
20
[1254]21                for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++)
[1271]22                {                       
[1254]23                        for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++)
24                        {
25                                set<vertex*> new_simplex;
[1301]26                                new_simplex.insert((*t_ref).begin(),(*t_ref).end());                           
27
28                                pair<set<vertex*>::iterator,bool> ret_val = new_simplex.insert(*vertices.begin());
29
30                                if(ret_val.second == true)
31                                {
32                                        triangulation.push_back(new_simplex);
33
34                                        if(should_integrate)
35                                        {
36                                                ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(new_simplex, 'S');
37                                        }
38                                } 
39                        }       
40                }               
41               
42                /*
43                for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++)
44                {                       
45                        for(list<set<vertex*>>::iterator t_ref = (*child_ref)->triangulation.begin();t_ref!=(*child_ref)->triangulation.end();t_ref++)
46                        {
47                                set<vertex*> new_simplex;
[1299]48                                new_simplex.insert((*t_ref).begin(),(*t_ref).end());                   
[1254]49
[1299]50                                for(set<vertex*>::iterator suitable_vert_ref = vertices.begin();*suitable_vert_ref<*(*t_ref).begin();suitable_vert_ref++)
51                                {
[1301]52                                        set<vertex*> suitable_simplex;
53                                        suitable_simplex.insert(new_simplex.begin(),new_simplex.end());
54                                       
55                                        suitable_simplex.insert(*suitable_vert_ref);
[1254]56
[1301]57                                        triangulation.push_back(suitable_simplex);
[1254]58
[1271]59                                        if(should_integrate)
[1254]60                                        {
[1301]61                                                ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(suitable_simplex, 'S');
[1271]62                                        }
[1299]63                                }                               
[1271]64                        }       
[1301]65                }*/             
[1271]66        }
[1268]67
68
[1271]69        double toprow::integrate_simplex(set<vertex*> simplex, char c)
70        {
[1282]71                // cout << ((toprow*)this)->condition << endl;         
72
[1280]73                int condition_order = ((toprow*)this)->condition_order-2; // -2 by bylo, pokud chceme uniformni apriorno
[1254]74
[1280]75                int sigma_order = condition_order-my_emlig->number_of_parameters;
76
[1271]77                // cout << "C:" << condition_order << "  N:" << my_emlig->number_of_parameters << "  C+N:" << condition_order-my_emlig->number_of_parameters << endl;
78                // pause(0.1);
[1254]79
[1280]80                if(sigma_order >= 0)
[1271]81                {
[1275]82
[1282]83                        //cout << endl;
84                        //cout << ((toprow*)this)->condition << endl;
85                        //cout << "C:" << condition_order+2 << "  N:" << my_emlig->number_of_parameters << "  C+N:" << condition_order-my_emlig->number_of_parameters+2 << endl;
86                       
[1275]87
[1271]88                        emlig* current_emlig;
[1254]89
[1271]90                        if(this->my_emlig!=NULL)
91                        {
92                                current_emlig = this->my_emlig;
93                        }
94                        else
95                        {
96                                throw exception("The statistic of the polyhedron you are trying to integrate over doesn't belong to any emlig!");
97                        }                                               
[1254]98
[1271]99                        toprow* as_toprow = (toprow*)this;
[1254]100
[1300]101                        vec cur_condition = as_toprow->condition_sum.get(1,as_toprow->condition_sum.size()-1);
[1254]102
[1275]103                        // cout << as_toprow->condition << endl;                       
[1272]104
[1271]105                        int dimension = simplex.size()-1;
[1254]106
[1275]107                        mat jacobian(dimension,dimension);                     
[1254]108
[1275]109                        double a_0;
[1271]110                        map<double,int> as;
[1275]111                        vertex* base_vertex;
112                        set<vertex*>::iterator base_ref = simplex.begin();
113                        bool order_correct;
114                                               
[1254]115
[1275]116                        do
[1271]117                        {
[1275]118                                order_correct = true;
119                                as.clear();
[1268]120
[1275]121                                base_vertex = (*base_ref);
[1268]122
[1282]123                       
[1301]124                                // cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl;
[1254]125
[1300]126                                a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition_sum[0];
[1282]127                               
[1281]128
[1275]129                                int row_count = 0;
130                                for(set<vertex*>::iterator vert_ref = simplex.begin(); vert_ref!=simplex.end();vert_ref++)
[1271]131                                {
[1275]132                                        if(vert_ref != base_ref)
133                                        {
[1276]134                                                vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates();                                           
[1275]135
136                                                jacobian.set_row(row_count,relative_coords);
137
[1281]138                                                double new_a = -relative_coords*cur_condition;
[1275]139
[1276]140                                                if(new_a + a_0 == 0 || new_a == 0)
[1275]141                                                {
142                                                        base_ref++;
143
144                                                        if(base_ref == simplex.end())
145                                                        {
146                                                                throw new exception("Equal local conditions are paired. If this ever occurs, the software has to be modified to include multiplied a_0!!");
147                                                        }
148                                                       
149                                                        order_correct = false;                                         
150
151                                                        break;
[1282]152                                                }                                               
153                                               
154                                               
155
156                                                if(a_0<current_emlig->likelihood_value)
157                                                {
158                                                        current_emlig->minimal_vertex = base_vertex;
159                                                        current_emlig->likelihood_value = a_0;
[1275]160                                                }
[1282]161
[1300]162                                                double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition_sum[0];
[1282]163                                                if(a_m<current_emlig->likelihood_value)
164                                                {
165                                                        current_emlig->minimal_vertex = (*vert_ref);
166                                                        current_emlig->likelihood_value = a_m;                                         
167                                                }
168
169                                                //cout << "a0:" << a_0 << " a0 coords:" << base_vertex->get_coordinates() << " am:" << a_m << " am coords:" << (*vert_ref)->get_coordinates() << endl;
170
[1301]171                                                // cout << "Absolute coords:(V"  << row_count << ")" << (*vert_ref)->get_coordinates() << endl;
[1282]172                                                //cout << "Relative coords:(V"  << row_count << ")" << relative_coords << endl;
[1275]173
174                                                pair<map<double,int>::iterator,bool> returned = as.insert(pair<double,int>(new_a,1));
175                                                if(returned.second == false)
176                                                {
177                                                        (*returned.first).second++;
178                                                }
179                                                /*
180                                                else
181                                                {
182                                                        cout << "a[" << row_count << "] = " << new_a << endl;
183                                                }
184                                                */
185                                               
186                                                //as.ins(as.size(),new_a);                                                     
187                                               
188                                                row_count++;
189                                        }
[1271]190                                }
191                        }
[1275]192                        while(!order_correct);
[1272]193                       
[1282]194                        /*
[1281]195                        cout << "a_0: " << a_0 << "    ";
196                        int as_count = 1;
197                        for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++)
198                        {
199                                cout << "a_" << as_count << ": " << (*as_ref).first << "    ";
200                                as_count++;
[1282]201                        }*/
[1269]202
[1271]203                        double int_value = 0;
[1268]204
[1271]205                        // cout << jacobian << endl;
[1268]206
[1272]207                        double det_jacobian    = abs(det(jacobian));
208                        double correction_term = det_jacobian;                 
[1271]209                        for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++)
210                        {
[1275]211                                double multiplier = det_jacobian;                               
[1272]212                               
[1275]213                                int as1_order = (*as_ref).second;
214                               
[1280]215                                correction_term /= pow(-(*as_ref).first,as1_order);                                     
[1275]216                               
217                                current_emlig->set_correction_factors(as1_order);
218
219                                vector<double> factors;
220                                int number_of_factors = 0;                                                             
221                                for(map<double,int>::iterator as2_ref = as.begin();as2_ref!=as.end();as2_ref++)
[1272]222                                {
223                                       
[1275]224                                        if(as2_ref!=as_ref)
225                                        {                                                                               
226                                                for(int k = 0;k<(*as2_ref).second;k++)
[1271]227                                                {
[1275]228                                                        factors.push_back((*as_ref).first-(*as2_ref).first);
[1272]229                                                }
[1270]230
[1275]231                                                multiplier        /= pow((*as_ref).first-(*as2_ref).first,(*as2_ref).second);
232                                                number_of_factors += (*as2_ref).second;
233                                        }
234                                        else
[1271]235                                        {
[1275]236                                                factors.push_back((*as_ref).first);
[1270]237
[1275]238                                                multiplier        /= (*as_ref).first;
239                                                number_of_factors += 1;
[1272]240                                        }
[1275]241                                       
[1280]242                                }       
[1254]243
[1280]244                               
245
[1281]246                                double bracket = fact(sigma_order)/fact(as1_order-1)/pow(a_0-(*as_ref).first,sigma_order+1);
[1275]247                                for(int k = 0;k < as1_order-1;k++)
248                                {
[1281]249                                        double bracket_factor = -pow((double)-1,k+1)*fact(sigma_order-k)/fact(as1_order-1-k)/pow(a_0-(*as_ref).first,sigma_order-k);
[1272]250                                       
[1280]251                                        ivec control_vec = ivec();
252                                        control_vec.ins(0,my_emlig->number_of_parameters-as1_order+1);         
253                                       
254                                       
255                                        for(multiset<my_ivec>::iterator combi_ref = this->my_emlig->correction_factors[k].begin();combi_ref!=this->my_emlig->correction_factors[k].upper_bound(my_ivec(control_vec));combi_ref++)
[1275]256                                        {
257                                                double bracket_combination = 1;
[1280]258                                                for(int j = 0;j<(*combi_ref).size();j++)
[1275]259                                                {
[1281]260                                                        //cout << "Factor vector:" << (*combi_ref) << endl;
[1280]261                                                       
262                                                        bracket_combination /= factors[(*combi_ref)[j]-1];
[1275]263                                                }
[1254]264
[1275]265                                                bracket+=bracket_factor*bracket_combination;                                                                   
266                                        }                                                                       
[1271]267                                }
[1275]268
269                               
270
271                                int_value += multiplier*bracket;
272                                                                                                                                               
[1272]273                        }
[1271]274
[1272]275                       
[1281]276                        correction_term *= fact(sigma_order)/abs(pow(a_0,sigma_order+1));
[1271]277
[1272]278                        // cout << c << int_value << endl;
279
280                        int_value += correction_term;
[1275]281               
[1272]282
[1282]283                        //cout << "Probability:" << int_value << endl;
[1275]284                       
285                        return int_value;
[1272]286
[1271]287                       
[1275]288                       
[1271]289                }
290                else
291                {
292                        return 0.0;
293                }
[1254]294        }
Note: See TracBrowser for help on using the browser.