root/applications/robust/robustlib.cpp @ 1320

Revision 1320, 7.9 kB (checked in by sindj, 13 years ago)

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