root/applications/robust/robustlib.cpp @ 1299

Revision 1299, 6.8 kB (checked in by sindj, 13 years ago)

Skoro dodelano slucovani polyhedronu v pripade odstraneni podminky (bylo nutne udelat znovu, kvuli predchozi ztrate prace zpusebene budto SVN nebo mnou). JS

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