root/applications/robust/robustlib.cpp @ 1310

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

Line 
1#include "robustlib.h"
2
3
4void polyhedron::triangulate(bool should_integrate)
5        {
6                triangulation.clear();
7
8                if(should_integrate)
9                {
10                        ((toprow *)this)->probability = 0.0;
11                }               
12               
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
21                for(list<polyhedron*>::iterator child_ref = children.begin();child_ref!=children.end();child_ref++)
22                {                       
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;
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;
48                                new_simplex.insert((*t_ref).begin(),(*t_ref).end());                   
49
50                                for(set<vertex*>::iterator suitable_vert_ref = vertices.begin();*suitable_vert_ref<*(*t_ref).begin();suitable_vert_ref++)
51                                {
52                                        set<vertex*> suitable_simplex;
53                                        suitable_simplex.insert(new_simplex.begin(),new_simplex.end());
54                                       
55                                        suitable_simplex.insert(*suitable_vert_ref);
56
57                                        triangulation.push_back(suitable_simplex);
58
59                                        if(should_integrate)
60                                        {
61                                                ((toprow *)this)->probability += ((toprow *)this)->integrate_simplex(suitable_simplex, 'S');
62                                        }
63                                }                               
64                        }       
65                }*/             
66        }
67
68
69        double toprow::integrate_simplex(set<vertex*> simplex, char c)
70        {
71                // cout << ((toprow*)this)->condition << endl;         
72
73                int condition_order = ((toprow*)this)->condition_order-2; // -2 by bylo, pokud chceme uniformni apriorno
74
75                int sigma_order = condition_order-my_emlig->number_of_parameters;
76
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);
79
80                if(sigma_order >= 0)
81                {
82
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                       
87
88                        emlig* current_emlig;
89
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                        }                                               
98
99                        toprow* as_toprow = (toprow*)this;
100
101                        vec cur_condition = as_toprow->condition_sum.get(1,as_toprow->condition_sum.size()-1);
102
103                        // cout << as_toprow->condition << endl;                       
104
105                        int dimension = simplex.size()-1;
106
107                        mat jacobian(dimension,dimension);                     
108
109                        double a_0;
110                        map<double,int> as;
111                        vertex* base_vertex;
112                        set<vertex*>::iterator base_ref = simplex.begin();
113                        bool order_correct;
114                                               
115
116                        do
117                        {
118                                order_correct = true;
119                                as.clear();
120
121                                base_vertex = (*base_ref);
122
123                       
124                                // cout << endl << "Base coords:" << base_vertex->get_coordinates() << endl;
125
126                                a_0 = base_vertex->get_coordinates()*cur_condition-as_toprow->condition_sum[0];
127                               
128
129                                int row_count = 0;
130                                for(set<vertex*>::iterator vert_ref = simplex.begin(); vert_ref!=simplex.end();vert_ref++)
131                                {
132                                        if(vert_ref != base_ref)
133                                        {
134                                                vec relative_coords = (*vert_ref)->get_coordinates()-base_vertex->get_coordinates();                                           
135
136                                                jacobian.set_row(row_count,relative_coords);
137
138                                                double new_a = -relative_coords*cur_condition;
139
140                                                if(new_a + a_0 == 0 || new_a == 0)
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;
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;
160                                                }
161
162                                                double a_m = (*vert_ref)->get_coordinates()*cur_condition-as_toprow->condition_sum[0];
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
171                                                // cout << "Absolute coords:(V"  << row_count << ")" << (*vert_ref)->get_coordinates() << endl;
172                                                //cout << "Relative coords:(V"  << row_count << ")" << relative_coords << endl;
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                                        }
190                                }
191                        }
192                        while(!order_correct);
193                       
194                        /*
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++;
201                        }*/
202
203                        double int_value = 0;
204
205                        // cout << jacobian << endl;
206
207                        double det_jacobian    = abs(det(jacobian));
208                        double correction_term = det_jacobian;                 
209                        for(map<double,int>::iterator as_ref = as.begin();as_ref!=as.end();as_ref++)
210                        {
211                                double multiplier = det_jacobian;                               
212                               
213                                int as1_order = (*as_ref).second;
214                               
215                                correction_term /= pow(-(*as_ref).first,as1_order);                                     
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++)
222                                {
223                                       
224                                        if(as2_ref!=as_ref)
225                                        {                                                                               
226                                                for(int k = 0;k<(*as2_ref).second;k++)
227                                                {
228                                                        factors.push_back((*as_ref).first-(*as2_ref).first);
229                                                }
230
231                                                multiplier        /= pow((*as_ref).first-(*as2_ref).first,(*as2_ref).second);
232                                                number_of_factors += (*as2_ref).second;
233                                        }
234                                        else
235                                        {
236                                                factors.push_back((*as_ref).first);
237
238                                                multiplier        /= (*as_ref).first;
239                                                number_of_factors += 1;
240                                        }
241                                       
242                                }       
243
244                               
245
246                                double bracket = fact(sigma_order)/fact(as1_order-1)/pow(a_0-(*as_ref).first,sigma_order+1);
247                                for(int k = 0;k < as1_order-1;k++)
248                                {
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);
250                                       
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++)
256                                        {
257                                                double bracket_combination = 1;
258                                                for(int j = 0;j<(*combi_ref).size();j++)
259                                                {
260                                                        //cout << "Factor vector:" << (*combi_ref) << endl;
261                                                       
262                                                        bracket_combination /= factors[(*combi_ref)[j]-1];
263                                                }
264
265                                                bracket+=bracket_factor*bracket_combination;                                                                   
266                                        }                                                                       
267                                }
268
269                               
270
271                                int_value += multiplier*bracket;
272                                                                                                                                               
273                        }
274
275                       
276                        correction_term *= fact(sigma_order)/abs(pow(a_0,sigma_order+1));
277
278                        // cout << c << int_value << endl;
279
280                        int_value += correction_term;
281               
282
283                        //cout << "Probability:" << int_value << endl;
284                       
285                        return int_value;
286
287                       
288                       
289                }
290                else
291                {
292                        return 0.0;
293                }
294        }
Note: See TracBrowser for help on using the browser.