| 1 | /*! |
|---|
| 2 | \file |
|---|
| 3 | \brief Robust Bayesian auto-regression model |
|---|
| 4 | \author Jan Sindelar. |
|---|
| 5 | */ |
|---|
| 6 | |
|---|
| 7 | #ifndef ROBUST_H |
|---|
| 8 | #define ROBUST_H |
|---|
| 9 | |
|---|
| 10 | //#include <stat/exp_family.h> |
|---|
| 11 | #include <itpp/itbase.h> |
|---|
| 12 | #include <map> |
|---|
| 13 | #include <limits> |
|---|
| 14 | #include <vector> |
|---|
| 15 | #include <list> |
|---|
| 16 | #include <set> |
|---|
| 17 | #include <algorithm> |
|---|
| 18 | |
|---|
| 19 | //using namespace bdm; |
|---|
| 20 | using namespace std; |
|---|
| 21 | using namespace itpp; |
|---|
| 22 | |
|---|
| 23 | const double max_range = 10;//numeric_limits<double>::max()/10e-10; |
|---|
| 24 | |
|---|
| 25 | enum actions {MERGE, SPLIT}; |
|---|
| 26 | |
|---|
| 27 | |
|---|
| 28 | |
|---|
| 29 | class polyhedron; |
|---|
| 30 | class vertex; |
|---|
| 31 | |
|---|
| 32 | /* |
|---|
| 33 | class t_simplex |
|---|
| 34 | { |
|---|
| 35 | public: |
|---|
| 36 | set<vertex*> minima; |
|---|
| 37 | |
|---|
| 38 | set<vertex*> simplex; |
|---|
| 39 | |
|---|
| 40 | t_simplex(vertex* origin_vertex) |
|---|
| 41 | { |
|---|
| 42 | simplex.insert(origin_vertex); |
|---|
| 43 | minima.insert(origin_vertex); |
|---|
| 44 | } |
|---|
| 45 | };*/ |
|---|
| 46 | |
|---|
| 47 | class emlig; |
|---|
| 48 | class polyhedron; |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | |
|---|
| 52 | /// A class describing a single polyhedron of the split complex. From a collection of such classes a Hasse diagram |
|---|
| 53 | /// of the structure in the exponent of a Laplace-Inverse-Gamma density will be created. |
|---|
| 54 | class polyhedron |
|---|
| 55 | { |
|---|
| 56 | /// A property having a value of 1 usually, with higher value only if the polyhedron arises as a coincidence of |
|---|
| 57 | /// more than just the necessary number of conditions. For example if a newly created line passes through an already |
|---|
| 58 | /// existing point, the points multiplicity will rise by 1. |
|---|
| 59 | int multiplicity; |
|---|
| 60 | |
|---|
| 61 | int split_state; |
|---|
| 62 | |
|---|
| 63 | int merge_state; |
|---|
| 64 | |
|---|
| 65 | |
|---|
| 66 | |
|---|
| 67 | public: |
|---|
| 68 | emlig* my_emlig; |
|---|
| 69 | |
|---|
| 70 | /// A list of polyhedrons parents within the Hasse diagram. |
|---|
| 71 | list<polyhedron*> parents; |
|---|
| 72 | |
|---|
| 73 | /// A list of polyhedrons children withing the Hasse diagram. |
|---|
| 74 | list<polyhedron*> children; |
|---|
| 75 | |
|---|
| 76 | /// All the vertices of the given polyhedron |
|---|
| 77 | set<vertex*> vertices; |
|---|
| 78 | |
|---|
| 79 | /// A list used for storing children that lie in the positive region related to a certain condition |
|---|
| 80 | list<polyhedron*> positivechildren; |
|---|
| 81 | |
|---|
| 82 | /// A list used for storing children that lie in the negative region related to a certain condition |
|---|
| 83 | list<polyhedron*> negativechildren; |
|---|
| 84 | |
|---|
| 85 | /// Children intersecting the condition |
|---|
| 86 | list<polyhedron*> neutralchildren; |
|---|
| 87 | |
|---|
| 88 | set<polyhedron*> totallyneutralgrandchildren; |
|---|
| 89 | |
|---|
| 90 | set<polyhedron*> totallyneutralchildren; |
|---|
| 91 | |
|---|
| 92 | set<polyhedron*> grandparents; |
|---|
| 93 | |
|---|
| 94 | set<vertex*> positiveneutralvertices; |
|---|
| 95 | |
|---|
| 96 | set<vertex*> negativeneutralvertices; |
|---|
| 97 | |
|---|
| 98 | bool totally_neutral; |
|---|
| 99 | |
|---|
| 100 | polyhedron* mergechild; |
|---|
| 101 | |
|---|
| 102 | polyhedron* positiveparent; |
|---|
| 103 | |
|---|
| 104 | polyhedron* negativeparent; |
|---|
| 105 | |
|---|
| 106 | polyhedron* next_poly; |
|---|
| 107 | |
|---|
| 108 | polyhedron* prev_poly; |
|---|
| 109 | |
|---|
| 110 | int message_counter; |
|---|
| 111 | |
|---|
| 112 | /// List of triangulation polyhedrons of the polyhedron given by their relative vertices. |
|---|
| 113 | list<set<vertex*>> triangulation; |
|---|
| 114 | |
|---|
| 115 | /// A list of relative addresses serving for Hasse diagram construction. |
|---|
| 116 | list<int> kids_rel_addresses; |
|---|
| 117 | |
|---|
| 118 | /// Default constructor |
|---|
| 119 | polyhedron() |
|---|
| 120 | { |
|---|
| 121 | multiplicity = 1; |
|---|
| 122 | |
|---|
| 123 | message_counter = 0; |
|---|
| 124 | |
|---|
| 125 | totally_neutral = NULL; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | /// Setter for raising multiplicity |
|---|
| 129 | void raise_multiplicity() |
|---|
| 130 | { |
|---|
| 131 | multiplicity++; |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | /// Setter for lowering multiplicity |
|---|
| 135 | void lower_multiplicity() |
|---|
| 136 | { |
|---|
| 137 | multiplicity--; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | int get_multiplicity() |
|---|
| 141 | { |
|---|
| 142 | return multiplicity; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
|---|
| 146 | int operator==(polyhedron polyhedron2) |
|---|
| 147 | { |
|---|
| 148 | return true; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /// An obligatory operator, when the class is used within a C++ STL structure like a vector |
|---|
| 152 | int operator<(polyhedron polyhedron2) |
|---|
| 153 | { |
|---|
| 154 | return false; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | void set_state(double state_indicator, actions action) |
|---|
| 160 | { |
|---|
| 161 | switch(action) |
|---|
| 162 | { |
|---|
| 163 | case MERGE: |
|---|
| 164 | merge_state = (int)sign(state_indicator); |
|---|
| 165 | break; |
|---|
| 166 | case SPLIT: |
|---|
| 167 | split_state = (int)sign(state_indicator); |
|---|
| 168 | break; |
|---|
| 169 | } |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | int get_state(actions action) |
|---|
| 173 | { |
|---|
| 174 | switch(action) |
|---|
| 175 | { |
|---|
| 176 | case MERGE: |
|---|
| 177 | return merge_state; |
|---|
| 178 | break; |
|---|
| 179 | case SPLIT: |
|---|
| 180 | return split_state; |
|---|
| 181 | break; |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | int number_of_children() |
|---|
| 186 | { |
|---|
| 187 | return children.size(); |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | void triangulate(bool should_integrate); |
|---|
| 192 | }; |
|---|
| 193 | |
|---|
| 194 | |
|---|
| 195 | /// A class for representing 0-dimensional polyhedron - a vertex. It will be located in the bottom row of the Hasse |
|---|
| 196 | /// diagram representing a complex of polyhedrons. It has its coordinates in the parameter space. |
|---|
| 197 | class vertex : public polyhedron |
|---|
| 198 | { |
|---|
| 199 | /// A dynamic array representing coordinates of the vertex |
|---|
| 200 | vec coordinates; |
|---|
| 201 | |
|---|
| 202 | public: |
|---|
| 203 | |
|---|
| 204 | double function_value; |
|---|
| 205 | |
|---|
| 206 | /// Default constructor |
|---|
| 207 | vertex(); |
|---|
| 208 | |
|---|
| 209 | /// Constructor of a vertex from a set of coordinates |
|---|
| 210 | vertex(vec coordinates) |
|---|
| 211 | { |
|---|
| 212 | this->coordinates = coordinates; |
|---|
| 213 | |
|---|
| 214 | vertices.insert(this); |
|---|
| 215 | |
|---|
| 216 | set<vertex*> vert_simplex; |
|---|
| 217 | |
|---|
| 218 | vert_simplex.insert(this); |
|---|
| 219 | |
|---|
| 220 | triangulation.push_back(vert_simplex); |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | /// A method that widens the set of coordinates of given vertex. It is used when a complex in a parameter |
|---|
| 224 | /// space of certain dimension is established, but the dimension is not known when the vertex is created. |
|---|
| 225 | void push_coordinate(double coordinate) |
|---|
| 226 | { |
|---|
| 227 | coordinates = concat(coordinates,coordinate); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | /// A method obtaining the set of coordinates of a vertex. These coordinates are not obtained as a pointer |
|---|
| 231 | /// (not given by reference), but a new copy is created (they are given by value). |
|---|
| 232 | vec get_coordinates() |
|---|
| 233 | { |
|---|
| 234 | return coordinates; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | |
|---|
| 238 | }; |
|---|
| 239 | |
|---|
| 240 | |
|---|
| 241 | /// A class representing a polyhedron in a top row of the complex. Such polyhedron has a condition that differitiates |
|---|
| 242 | /// it from polyhedrons in other rows. |
|---|
| 243 | class toprow : public polyhedron |
|---|
| 244 | { |
|---|
| 245 | |
|---|
| 246 | public: |
|---|
| 247 | double probability; |
|---|
| 248 | |
|---|
| 249 | vertex* minimal_vertex; |
|---|
| 250 | |
|---|
| 251 | /// A condition used for determining the function of a Laplace-Inverse-Gamma density resulting from Bayesian estimation |
|---|
| 252 | vec condition; |
|---|
| 253 | |
|---|
| 254 | int condition_order; |
|---|
| 255 | |
|---|
| 256 | /// Default constructor |
|---|
| 257 | toprow(){}; |
|---|
| 258 | |
|---|
| 259 | /// Constructor creating a toprow from the condition |
|---|
| 260 | toprow(vec condition, int condition_order) |
|---|
| 261 | { |
|---|
| 262 | this->condition = condition; |
|---|
| 263 | this->condition_order = condition_order; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | double integrate_simplex(set<vertex*> simplex, char c); |
|---|
| 267 | |
|---|
| 268 | }; |
|---|
| 269 | |
|---|
| 270 | |
|---|
| 271 | class condition |
|---|
| 272 | { |
|---|
| 273 | public: |
|---|
| 274 | vec value; |
|---|
| 275 | |
|---|
| 276 | int multiplicity; |
|---|
| 277 | |
|---|
| 278 | condition(vec value) |
|---|
| 279 | { |
|---|
| 280 | this->value = value; |
|---|
| 281 | multiplicity = 1; |
|---|
| 282 | } |
|---|
| 283 | }; |
|---|
| 284 | |
|---|
| 285 | |
|---|
| 286 | |
|---|
| 287 | |
|---|
| 288 | class c_statistic |
|---|
| 289 | { |
|---|
| 290 | |
|---|
| 291 | public: |
|---|
| 292 | polyhedron* end_poly; |
|---|
| 293 | polyhedron* start_poly; |
|---|
| 294 | |
|---|
| 295 | vector<polyhedron*> rows; |
|---|
| 296 | |
|---|
| 297 | vector<polyhedron*> row_ends; |
|---|
| 298 | |
|---|
| 299 | c_statistic() |
|---|
| 300 | { |
|---|
| 301 | end_poly = new polyhedron(); |
|---|
| 302 | start_poly = new polyhedron(); |
|---|
| 303 | }; |
|---|
| 304 | |
|---|
| 305 | void append_polyhedron(int row, polyhedron* appended_start, polyhedron* appended_end) |
|---|
| 306 | { |
|---|
| 307 | if(row>((int)rows.size())-1) |
|---|
| 308 | { |
|---|
| 309 | if(row>rows.size()) |
|---|
| 310 | { |
|---|
| 311 | throw new exception("You are trying to append a polyhedron whose children are not in the statistic yet!"); |
|---|
| 312 | return; |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | rows.push_back(end_poly); |
|---|
| 316 | row_ends.push_back(end_poly); |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | // POSSIBLE FAILURE: the function is not checking if start and end are connected |
|---|
| 320 | |
|---|
| 321 | if(rows[row] != end_poly) |
|---|
| 322 | { |
|---|
| 323 | appended_start->prev_poly = row_ends[row]; |
|---|
| 324 | row_ends[row]->next_poly = appended_start; |
|---|
| 325 | |
|---|
| 326 | } |
|---|
| 327 | else if((row>0 && rows[row-1]!=end_poly)||row==0) |
|---|
| 328 | { |
|---|
| 329 | appended_start->prev_poly = start_poly; |
|---|
| 330 | rows[row]= appended_start; |
|---|
| 331 | } |
|---|
| 332 | else |
|---|
| 333 | { |
|---|
| 334 | throw new exception("Wrong polyhedron insertion into statistic: missing intermediary polyhedron!"); |
|---|
| 335 | } |
|---|
| 336 | |
|---|
| 337 | appended_end->next_poly = end_poly; |
|---|
| 338 | row_ends[row] = appended_end; |
|---|
| 339 | } |
|---|
| 340 | |
|---|
| 341 | void append_polyhedron(int row, polyhedron* appended_poly) |
|---|
| 342 | { |
|---|
| 343 | append_polyhedron(row,appended_poly,appended_poly); |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | void insert_polyhedron(int row, polyhedron* inserted_poly, polyhedron* following_poly) |
|---|
| 347 | { |
|---|
| 348 | if(following_poly != end_poly) |
|---|
| 349 | { |
|---|
| 350 | inserted_poly->next_poly = following_poly; |
|---|
| 351 | inserted_poly->prev_poly = following_poly->prev_poly; |
|---|
| 352 | |
|---|
| 353 | if(following_poly->prev_poly == start_poly) |
|---|
| 354 | { |
|---|
| 355 | rows[row] = inserted_poly; |
|---|
| 356 | } |
|---|
| 357 | else |
|---|
| 358 | { |
|---|
| 359 | inserted_poly->prev_poly->next_poly = inserted_poly; |
|---|
| 360 | } |
|---|
| 361 | |
|---|
| 362 | following_poly->prev_poly = inserted_poly; |
|---|
| 363 | } |
|---|
| 364 | else |
|---|
| 365 | { |
|---|
| 366 | this->append_polyhedron(row, inserted_poly); |
|---|
| 367 | } |
|---|
| 368 | |
|---|
| 369 | } |
|---|
| 370 | |
|---|
| 371 | |
|---|
| 372 | |
|---|
| 373 | |
|---|
| 374 | void delete_polyhedron(int row, polyhedron* deleted_poly) |
|---|
| 375 | { |
|---|
| 376 | if(deleted_poly->prev_poly != start_poly) |
|---|
| 377 | { |
|---|
| 378 | deleted_poly->prev_poly->next_poly = deleted_poly->next_poly; |
|---|
| 379 | } |
|---|
| 380 | else |
|---|
| 381 | { |
|---|
| 382 | rows[row] = deleted_poly->next_poly; |
|---|
| 383 | } |
|---|
| 384 | |
|---|
| 385 | if(deleted_poly->next_poly!=end_poly) |
|---|
| 386 | { |
|---|
| 387 | deleted_poly->next_poly->prev_poly = deleted_poly->prev_poly; |
|---|
| 388 | } |
|---|
| 389 | else |
|---|
| 390 | { |
|---|
| 391 | row_ends[row] = deleted_poly->prev_poly; |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | |
|---|
| 395 | |
|---|
| 396 | deleted_poly->next_poly = NULL; |
|---|
| 397 | deleted_poly->prev_poly = NULL; |
|---|
| 398 | } |
|---|
| 399 | |
|---|
| 400 | int size() |
|---|
| 401 | { |
|---|
| 402 | return rows.size(); |
|---|
| 403 | } |
|---|
| 404 | |
|---|
| 405 | polyhedron* get_end() |
|---|
| 406 | { |
|---|
| 407 | return end_poly; |
|---|
| 408 | } |
|---|
| 409 | |
|---|
| 410 | polyhedron* get_start() |
|---|
| 411 | { |
|---|
| 412 | return start_poly; |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | int row_size(int row) |
|---|
| 416 | { |
|---|
| 417 | if(this->size()>row && row>=0) |
|---|
| 418 | { |
|---|
| 419 | int row_size = 0; |
|---|
| 420 | |
|---|
| 421 | for(polyhedron* row_poly = rows[row]; row_poly!=end_poly; row_poly=row_poly->next_poly) |
|---|
| 422 | { |
|---|
| 423 | row_size++; |
|---|
| 424 | } |
|---|
| 425 | |
|---|
| 426 | return row_size; |
|---|
| 427 | } |
|---|
| 428 | else |
|---|
| 429 | { |
|---|
| 430 | throw new exception("There is no row to obtain size from!"); |
|---|
| 431 | } |
|---|
| 432 | } |
|---|
| 433 | }; |
|---|
| 434 | |
|---|
| 435 | |
|---|
| 436 | class my_ivec : public ivec |
|---|
| 437 | { |
|---|
| 438 | public: |
|---|
| 439 | my_ivec():ivec(){}; |
|---|
| 440 | |
|---|
| 441 | my_ivec(ivec origin):ivec() |
|---|
| 442 | { |
|---|
| 443 | this->ins(0,origin); |
|---|
| 444 | } |
|---|
| 445 | |
|---|
| 446 | bool operator>(const my_ivec &second) const |
|---|
| 447 | { |
|---|
| 448 | return max(*this)>max(second); |
|---|
| 449 | |
|---|
| 450 | /* |
|---|
| 451 | int size1 = this->size(); |
|---|
| 452 | int size2 = second.size(); |
|---|
| 453 | |
|---|
| 454 | int counter1 = 0; |
|---|
| 455 | while(0==0) |
|---|
| 456 | { |
|---|
| 457 | if((*this)[counter1]==0) |
|---|
| 458 | { |
|---|
| 459 | size1--; |
|---|
| 460 | } |
|---|
| 461 | |
|---|
| 462 | if((*this)[counter1]!=0) |
|---|
| 463 | break; |
|---|
| 464 | |
|---|
| 465 | counter1++; |
|---|
| 466 | } |
|---|
| 467 | |
|---|
| 468 | int counter2 = 0; |
|---|
| 469 | while(0==0) |
|---|
| 470 | { |
|---|
| 471 | if(second[counter2]==0) |
|---|
| 472 | { |
|---|
| 473 | size2--; |
|---|
| 474 | } |
|---|
| 475 | |
|---|
| 476 | if(second[counter2]!=0) |
|---|
| 477 | break; |
|---|
| 478 | |
|---|
| 479 | counter2++; |
|---|
| 480 | } |
|---|
| 481 | |
|---|
| 482 | if(size1!=size2) |
|---|
| 483 | { |
|---|
| 484 | return size1>size2; |
|---|
| 485 | } |
|---|
| 486 | else |
|---|
| 487 | { |
|---|
| 488 | for(int i = 0;i<size1;i++) |
|---|
| 489 | { |
|---|
| 490 | if((*this)[counter1+i]!=second[counter2+i]) |
|---|
| 491 | { |
|---|
| 492 | return (*this)[counter1+i]>second[counter2+i]; |
|---|
| 493 | } |
|---|
| 494 | } |
|---|
| 495 | |
|---|
| 496 | return false; |
|---|
| 497 | }*/ |
|---|
| 498 | } |
|---|
| 499 | |
|---|
| 500 | |
|---|
| 501 | bool operator==(const my_ivec &second) const |
|---|
| 502 | { |
|---|
| 503 | return max(*this)==max(second); |
|---|
| 504 | |
|---|
| 505 | /* |
|---|
| 506 | int size1 = this->size(); |
|---|
| 507 | int size2 = second.size(); |
|---|
| 508 | |
|---|
| 509 | int counter = 0; |
|---|
| 510 | while(0==0) |
|---|
| 511 | { |
|---|
| 512 | if((*this)[counter]==0) |
|---|
| 513 | { |
|---|
| 514 | size1--; |
|---|
| 515 | } |
|---|
| 516 | |
|---|
| 517 | if((*this)[counter]!=0) |
|---|
| 518 | break; |
|---|
| 519 | |
|---|
| 520 | counter++; |
|---|
| 521 | } |
|---|
| 522 | |
|---|
| 523 | counter = 0; |
|---|
| 524 | while(0==0) |
|---|
| 525 | { |
|---|
| 526 | if(second[counter]==0) |
|---|
| 527 | { |
|---|
| 528 | size2--; |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | if(second[counter]!=0) |
|---|
| 532 | break; |
|---|
| 533 | |
|---|
| 534 | counter++; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | if(size1!=size2) |
|---|
| 538 | { |
|---|
| 539 | return false; |
|---|
| 540 | } |
|---|
| 541 | else |
|---|
| 542 | { |
|---|
| 543 | for(int i=0;i<size1;i++) |
|---|
| 544 | { |
|---|
| 545 | if((*this)[size()-1-i]!=second[second.size()-1-i]) |
|---|
| 546 | { |
|---|
| 547 | return false; |
|---|
| 548 | } |
|---|
| 549 | } |
|---|
| 550 | |
|---|
| 551 | return true; |
|---|
| 552 | }*/ |
|---|
| 553 | } |
|---|
| 554 | |
|---|
| 555 | bool operator<(const my_ivec &second) const |
|---|
| 556 | { |
|---|
| 557 | return !(((*this)>second)||((*this)==second)); |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | bool operator!=(const my_ivec &second) const |
|---|
| 561 | { |
|---|
| 562 | return !((*this)==second); |
|---|
| 563 | } |
|---|
| 564 | |
|---|
| 565 | bool operator<=(const my_ivec &second) const |
|---|
| 566 | { |
|---|
| 567 | return !((*this)>second); |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | bool operator>=(const my_ivec &second) const |
|---|
| 571 | { |
|---|
| 572 | return !((*this)<second); |
|---|
| 573 | } |
|---|
| 574 | |
|---|
| 575 | my_ivec right(my_ivec original) |
|---|
| 576 | { |
|---|
| 577 | |
|---|
| 578 | } |
|---|
| 579 | }; |
|---|
| 580 | |
|---|
| 581 | |
|---|
| 582 | |
|---|
| 583 | |
|---|
| 584 | |
|---|
| 585 | |
|---|
| 586 | |
|---|
| 587 | //! Conditional(e) Multicriteria-Laplace-Inverse-Gamma distribution density |
|---|
| 588 | class emlig // : eEF |
|---|
| 589 | { |
|---|
| 590 | |
|---|
| 591 | /// A statistic in a form of a Hasse diagram representing a complex of convex polyhedrons obtained as a result |
|---|
| 592 | /// of data update from Bayesian estimation or set by the user if this emlig is a prior density |
|---|
| 593 | |
|---|
| 594 | |
|---|
| 595 | vector<list<polyhedron*>> for_splitting; |
|---|
| 596 | |
|---|
| 597 | vector<list<polyhedron*>> for_merging; |
|---|
| 598 | |
|---|
| 599 | list<condition*> conditions; |
|---|
| 600 | |
|---|
| 601 | double normalization_factor; |
|---|
| 602 | |
|---|
| 603 | |
|---|
| 604 | |
|---|
| 605 | void alter_toprow_conditions(vec condition, bool should_be_added) |
|---|
| 606 | { |
|---|
| 607 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
|---|
| 608 | { |
|---|
| 609 | double product = 0; |
|---|
| 610 | |
|---|
| 611 | set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin(); |
|---|
| 612 | |
|---|
| 613 | do |
|---|
| 614 | { |
|---|
| 615 | product = (*vertex_ref)->get_coordinates()*condition; |
|---|
| 616 | } |
|---|
| 617 | while(product == 0); |
|---|
| 618 | |
|---|
| 619 | if((product>0 && should_be_added)||(product<0 && !should_be_added)) |
|---|
| 620 | { |
|---|
| 621 | ((toprow*) horiz_ref)->condition += condition; |
|---|
| 622 | } |
|---|
| 623 | else |
|---|
| 624 | { |
|---|
| 625 | ((toprow*) horiz_ref)->condition -= condition; |
|---|
| 626 | } |
|---|
| 627 | } |
|---|
| 628 | } |
|---|
| 629 | |
|---|
| 630 | |
|---|
| 631 | |
|---|
| 632 | void send_state_message(polyhedron* sender, vec toadd, vec toremove, int level) |
|---|
| 633 | { |
|---|
| 634 | |
|---|
| 635 | bool shouldmerge = (toremove.size() != 0); |
|---|
| 636 | bool shouldsplit = (toadd.size() != 0); |
|---|
| 637 | |
|---|
| 638 | if(shouldsplit||shouldmerge) |
|---|
| 639 | { |
|---|
| 640 | for(list<polyhedron*>::iterator parent_iterator = sender->parents.begin();parent_iterator!=sender->parents.end();parent_iterator++) |
|---|
| 641 | { |
|---|
| 642 | polyhedron* current_parent = *parent_iterator; |
|---|
| 643 | |
|---|
| 644 | current_parent->message_counter++; |
|---|
| 645 | |
|---|
| 646 | bool is_last = (current_parent->message_counter == current_parent->number_of_children()); |
|---|
| 647 | |
|---|
| 648 | if(shouldmerge) |
|---|
| 649 | { |
|---|
| 650 | int child_state = sender->get_state(MERGE); |
|---|
| 651 | int parent_state = current_parent->get_state(MERGE); |
|---|
| 652 | |
|---|
| 653 | if(parent_state == 0) |
|---|
| 654 | { |
|---|
| 655 | current_parent->set_state(child_state, MERGE); |
|---|
| 656 | } |
|---|
| 657 | |
|---|
| 658 | if(child_state == 0) |
|---|
| 659 | { |
|---|
| 660 | if(current_parent->mergechild == NULL) |
|---|
| 661 | { |
|---|
| 662 | current_parent->mergechild = sender; |
|---|
| 663 | } |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | if(is_last) |
|---|
| 667 | { |
|---|
| 668 | if(current_parent->mergechild!=NULL) |
|---|
| 669 | { |
|---|
| 670 | if(current_parent->mergechild->get_multiplicity()==1) |
|---|
| 671 | { |
|---|
| 672 | if(parent_state > 0) |
|---|
| 673 | { |
|---|
| 674 | current_parent->mergechild->positiveparent = current_parent; |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | if(parent_state < 0) |
|---|
| 678 | { |
|---|
| 679 | current_parent->mergechild->negativeparent = current_parent; |
|---|
| 680 | } |
|---|
| 681 | } |
|---|
| 682 | } |
|---|
| 683 | |
|---|
| 684 | |
|---|
| 685 | if(parent_state == 0) |
|---|
| 686 | { |
|---|
| 687 | for_merging[level+1].push_back(current_parent); |
|---|
| 688 | } |
|---|
| 689 | |
|---|
| 690 | current_parent->mergechild = NULL; |
|---|
| 691 | } |
|---|
| 692 | } |
|---|
| 693 | |
|---|
| 694 | if(shouldsplit) |
|---|
| 695 | { |
|---|
| 696 | current_parent->totallyneutralgrandchildren.insert(sender->totallyneutralchildren.begin(),sender->totallyneutralchildren.end()); |
|---|
| 697 | |
|---|
| 698 | for(set<polyhedron*>::iterator tot_child_ref = sender->totallyneutralchildren.begin();tot_child_ref!=sender->totallyneutralchildren.end();tot_child_ref++) |
|---|
| 699 | { |
|---|
| 700 | (*tot_child_ref)->grandparents.insert(current_parent); |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | switch(sender->get_state(SPLIT)) |
|---|
| 704 | { |
|---|
| 705 | case 1: |
|---|
| 706 | current_parent->positivechildren.push_back(sender); |
|---|
| 707 | current_parent->positiveneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
|---|
| 708 | break; |
|---|
| 709 | case 0: |
|---|
| 710 | current_parent->neutralchildren.push_back(sender); |
|---|
| 711 | current_parent->positiveneutralvertices.insert(sender->positiveneutralvertices.begin(),sender->positiveneutralvertices.end()); |
|---|
| 712 | current_parent->negativeneutralvertices.insert(sender->negativeneutralvertices.begin(),sender->negativeneutralvertices.end()); |
|---|
| 713 | |
|---|
| 714 | if(current_parent->totally_neutral == NULL) |
|---|
| 715 | { |
|---|
| 716 | current_parent->totally_neutral = sender->totally_neutral; |
|---|
| 717 | } |
|---|
| 718 | else |
|---|
| 719 | { |
|---|
| 720 | current_parent->totally_neutral = current_parent->totally_neutral && sender->totally_neutral; |
|---|
| 721 | } |
|---|
| 722 | |
|---|
| 723 | if(sender->totally_neutral) |
|---|
| 724 | { |
|---|
| 725 | current_parent->totallyneutralchildren.insert(sender); |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | break; |
|---|
| 729 | case -1: |
|---|
| 730 | current_parent->negativechildren.push_back(sender); |
|---|
| 731 | current_parent->negativeneutralvertices.insert(sender->vertices.begin(),sender->vertices.end()); |
|---|
| 732 | break; |
|---|
| 733 | } |
|---|
| 734 | |
|---|
| 735 | if(is_last) |
|---|
| 736 | { |
|---|
| 737 | if((current_parent->negativechildren.size()>0&¤t_parent->positivechildren.size()>0)|| |
|---|
| 738 | (current_parent->neutralchildren.size()>0&¤t_parent->totally_neutral==false)) |
|---|
| 739 | { |
|---|
| 740 | |
|---|
| 741 | for_splitting[level+1].push_back(current_parent); |
|---|
| 742 | |
|---|
| 743 | current_parent->set_state(0, SPLIT); |
|---|
| 744 | } |
|---|
| 745 | else |
|---|
| 746 | { |
|---|
| 747 | |
|---|
| 748 | |
|---|
| 749 | if(current_parent->negativechildren.size()>0) |
|---|
| 750 | { |
|---|
| 751 | current_parent->set_state(-1, SPLIT); |
|---|
| 752 | |
|---|
| 753 | ((toprow*)current_parent)->condition-=toadd; |
|---|
| 754 | |
|---|
| 755 | |
|---|
| 756 | } |
|---|
| 757 | else if(current_parent->positivechildren.size()>0) |
|---|
| 758 | { |
|---|
| 759 | current_parent->set_state(1, SPLIT); |
|---|
| 760 | |
|---|
| 761 | ((toprow*)current_parent)->condition+=toadd; |
|---|
| 762 | } |
|---|
| 763 | else |
|---|
| 764 | { |
|---|
| 765 | current_parent->raise_multiplicity(); |
|---|
| 766 | } |
|---|
| 767 | |
|---|
| 768 | ((toprow*)current_parent)->condition_order++; |
|---|
| 769 | |
|---|
| 770 | if(level == number_of_parameters - 1) |
|---|
| 771 | { |
|---|
| 772 | toprow* cur_par_toprow = ((toprow*)current_parent); |
|---|
| 773 | cur_par_toprow->probability = 0.0; |
|---|
| 774 | |
|---|
| 775 | for(list<set<vertex*>>::iterator t_ref = current_parent->triangulation.begin();t_ref!=current_parent->triangulation.end();t_ref++) |
|---|
| 776 | { |
|---|
| 777 | cur_par_toprow->probability += cur_par_toprow->integrate_simplex(*t_ref,'C'); |
|---|
| 778 | } |
|---|
| 779 | } |
|---|
| 780 | |
|---|
| 781 | current_parent->positivechildren.clear(); |
|---|
| 782 | current_parent->negativechildren.clear(); |
|---|
| 783 | current_parent->neutralchildren.clear(); |
|---|
| 784 | current_parent->totallyneutralchildren.clear(); |
|---|
| 785 | current_parent->totallyneutralgrandchildren.clear(); |
|---|
| 786 | current_parent->positiveneutralvertices.clear(); |
|---|
| 787 | current_parent->negativeneutralvertices.clear(); |
|---|
| 788 | current_parent->totally_neutral = NULL; |
|---|
| 789 | current_parent->kids_rel_addresses.clear(); |
|---|
| 790 | current_parent->message_counter = 0; |
|---|
| 791 | } |
|---|
| 792 | } |
|---|
| 793 | } |
|---|
| 794 | |
|---|
| 795 | if(is_last) |
|---|
| 796 | { |
|---|
| 797 | send_state_message(current_parent,toadd,toremove,level+1); |
|---|
| 798 | } |
|---|
| 799 | |
|---|
| 800 | } |
|---|
| 801 | |
|---|
| 802 | } |
|---|
| 803 | } |
|---|
| 804 | |
|---|
| 805 | public: |
|---|
| 806 | c_statistic statistic; |
|---|
| 807 | |
|---|
| 808 | vertex* minimal_vertex; |
|---|
| 809 | |
|---|
| 810 | double likelihood_value; |
|---|
| 811 | |
|---|
| 812 | vector<multiset<my_ivec>> correction_factors; |
|---|
| 813 | |
|---|
| 814 | int number_of_parameters; |
|---|
| 815 | |
|---|
| 816 | /// A default constructor creates an emlig with predefined statistic representing only the range of the given |
|---|
| 817 | /// parametric space, where the number of parameters of the needed model is given as a parameter to the constructor. |
|---|
| 818 | emlig(int number_of_parameters) |
|---|
| 819 | { |
|---|
| 820 | this->number_of_parameters = number_of_parameters; |
|---|
| 821 | |
|---|
| 822 | create_statistic(number_of_parameters); |
|---|
| 823 | |
|---|
| 824 | likelihood_value = numeric_limits<double>::max(); |
|---|
| 825 | } |
|---|
| 826 | |
|---|
| 827 | /// A constructor for creating an emlig when the user wants to create the statistic by himself. The creation of a |
|---|
| 828 | /// statistic is needed outside the constructor. Used for a user defined prior distribution on the parameters. |
|---|
| 829 | emlig(c_statistic statistic) |
|---|
| 830 | { |
|---|
| 831 | this->statistic = statistic; |
|---|
| 832 | |
|---|
| 833 | likelihood_value = numeric_limits<double>::max(); |
|---|
| 834 | } |
|---|
| 835 | |
|---|
| 836 | void step_me(int marker) |
|---|
| 837 | { |
|---|
| 838 | /* |
|---|
| 839 | for(int i = 0;i<statistic.size();i++) |
|---|
| 840 | { |
|---|
| 841 | for(polyhedron* horiz_ref = statistic.rows[i];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
|---|
| 842 | { |
|---|
| 843 | if(i==statistic.size()-1) |
|---|
| 844 | { |
|---|
| 845 | cout << ((toprow*)horiz_ref)->condition << " " << ((toprow*)horiz_ref)->probability << endl; |
|---|
| 846 | cout << "Order:" << ((toprow*)horiz_ref)->condition_order << endl; |
|---|
| 847 | } |
|---|
| 848 | char* string = "Checkpoint"; |
|---|
| 849 | } |
|---|
| 850 | } |
|---|
| 851 | */ |
|---|
| 852 | |
|---|
| 853 | /* |
|---|
| 854 | list<vec> table_entries; |
|---|
| 855 | for(polyhedron* horiz_ref = statistic.rows[statistic.size()-1];horiz_ref!=statistic.row_ends[statistic.size()-1];horiz_ref=horiz_ref->next_poly) |
|---|
| 856 | { |
|---|
| 857 | toprow *current_toprow = (toprow*)(horiz_ref); |
|---|
| 858 | for(list<set<vertex*>>::iterator tri_ref = current_toprow->triangulation.begin();tri_ref!=current_toprow->triangulation.end();tri_ref++) |
|---|
| 859 | { |
|---|
| 860 | for(set<vertex*>::iterator vert_ref = (*tri_ref).begin();vert_ref!=(*tri_ref).end();vert_ref++) |
|---|
| 861 | { |
|---|
| 862 | vec table_entry = vec(); |
|---|
| 863 | |
|---|
| 864 | table_entry.ins(0,(*vert_ref)->get_coordinates()*current_toprow->condition.get(1,current_toprow->condition.size()-1)-current_toprow->condition.get(0,0)); |
|---|
| 865 | |
|---|
| 866 | table_entry.ins(0,(*vert_ref)->get_coordinates()); |
|---|
| 867 | |
|---|
| 868 | table_entries.push_back(table_entry); |
|---|
| 869 | } |
|---|
| 870 | } |
|---|
| 871 | } |
|---|
| 872 | |
|---|
| 873 | unique(table_entries.begin(),table_entries.end()); |
|---|
| 874 | |
|---|
| 875 | |
|---|
| 876 | |
|---|
| 877 | for(list<vec>::iterator entry_ref = table_entries.begin();entry_ref!=table_entries.end();entry_ref++) |
|---|
| 878 | { |
|---|
| 879 | ofstream myfile; |
|---|
| 880 | myfile.open("robust_data.txt", ios::out | ios::app); |
|---|
| 881 | if (myfile.is_open()) |
|---|
| 882 | { |
|---|
| 883 | for(int i = 0;i<(*entry_ref).size();i++) |
|---|
| 884 | { |
|---|
| 885 | myfile << (*entry_ref)[i] << ";"; |
|---|
| 886 | } |
|---|
| 887 | myfile << endl; |
|---|
| 888 | |
|---|
| 889 | myfile.close(); |
|---|
| 890 | } |
|---|
| 891 | else |
|---|
| 892 | { |
|---|
| 893 | cout << "File problem." << endl; |
|---|
| 894 | } |
|---|
| 895 | } |
|---|
| 896 | */ |
|---|
| 897 | |
|---|
| 898 | |
|---|
| 899 | return; |
|---|
| 900 | } |
|---|
| 901 | |
|---|
| 902 | int statistic_rowsize(int row) |
|---|
| 903 | { |
|---|
| 904 | return statistic.row_size(row); |
|---|
| 905 | } |
|---|
| 906 | |
|---|
| 907 | void add_condition(vec toadd) |
|---|
| 908 | { |
|---|
| 909 | vec null_vector = ""; |
|---|
| 910 | |
|---|
| 911 | add_and_remove_condition(toadd, null_vector); |
|---|
| 912 | } |
|---|
| 913 | |
|---|
| 914 | |
|---|
| 915 | void remove_condition(vec toremove) |
|---|
| 916 | { |
|---|
| 917 | vec null_vector = ""; |
|---|
| 918 | |
|---|
| 919 | add_and_remove_condition(null_vector, toremove); |
|---|
| 920 | |
|---|
| 921 | } |
|---|
| 922 | |
|---|
| 923 | |
|---|
| 924 | void add_and_remove_condition(vec toadd, vec toremove) |
|---|
| 925 | { |
|---|
| 926 | likelihood_value = numeric_limits<double>::max(); |
|---|
| 927 | |
|---|
| 928 | bool should_remove = (toremove.size() != 0); |
|---|
| 929 | bool should_add = (toadd.size() != 0); |
|---|
| 930 | |
|---|
| 931 | for_splitting.clear(); |
|---|
| 932 | for_merging.clear(); |
|---|
| 933 | |
|---|
| 934 | for(int i = 0;i<statistic.size();i++) |
|---|
| 935 | { |
|---|
| 936 | list<polyhedron*> empty_split; |
|---|
| 937 | list<polyhedron*> empty_merge; |
|---|
| 938 | |
|---|
| 939 | for_splitting.push_back(empty_split); |
|---|
| 940 | for_merging.push_back(empty_merge); |
|---|
| 941 | } |
|---|
| 942 | |
|---|
| 943 | list<condition*>::iterator toremove_ref = conditions.end(); |
|---|
| 944 | bool condition_should_be_added = false; |
|---|
| 945 | |
|---|
| 946 | for(list<condition*>::iterator ref = conditions.begin();ref!=conditions.end();ref++) |
|---|
| 947 | { |
|---|
| 948 | if(should_remove) |
|---|
| 949 | { |
|---|
| 950 | if((*ref)->value == toremove) |
|---|
| 951 | { |
|---|
| 952 | if((*ref)->multiplicity>1) |
|---|
| 953 | { |
|---|
| 954 | (*ref)->multiplicity--; |
|---|
| 955 | |
|---|
| 956 | alter_toprow_conditions(toremove,false); |
|---|
| 957 | |
|---|
| 958 | should_remove = false; |
|---|
| 959 | } |
|---|
| 960 | else |
|---|
| 961 | { |
|---|
| 962 | toremove_ref = ref; |
|---|
| 963 | } |
|---|
| 964 | } |
|---|
| 965 | } |
|---|
| 966 | |
|---|
| 967 | if(should_add) |
|---|
| 968 | { |
|---|
| 969 | if((*ref)->value == toadd) |
|---|
| 970 | { |
|---|
| 971 | (*ref)->multiplicity++; |
|---|
| 972 | |
|---|
| 973 | alter_toprow_conditions(toadd,true); |
|---|
| 974 | |
|---|
| 975 | should_add = false; |
|---|
| 976 | } |
|---|
| 977 | else |
|---|
| 978 | { |
|---|
| 979 | condition_should_be_added = true; |
|---|
| 980 | } |
|---|
| 981 | } |
|---|
| 982 | } |
|---|
| 983 | |
|---|
| 984 | if(toremove_ref!=conditions.end()) |
|---|
| 985 | { |
|---|
| 986 | conditions.erase(toremove_ref); |
|---|
| 987 | } |
|---|
| 988 | |
|---|
| 989 | if(condition_should_be_added) |
|---|
| 990 | { |
|---|
| 991 | conditions.push_back(new condition(toadd)); |
|---|
| 992 | } |
|---|
| 993 | |
|---|
| 994 | |
|---|
| 995 | |
|---|
| 996 | for(polyhedron* horizontal_position = statistic.rows[0];horizontal_position!=statistic.get_end();horizontal_position=horizontal_position->next_poly) |
|---|
| 997 | { |
|---|
| 998 | vertex* current_vertex = (vertex*)horizontal_position; |
|---|
| 999 | |
|---|
| 1000 | if(should_add||should_remove) |
|---|
| 1001 | { |
|---|
| 1002 | vec appended_vec = current_vertex->get_coordinates(); |
|---|
| 1003 | appended_vec.ins(0,-1.0); |
|---|
| 1004 | |
|---|
| 1005 | if(should_add) |
|---|
| 1006 | { |
|---|
| 1007 | double local_condition = toadd*appended_vec; |
|---|
| 1008 | |
|---|
| 1009 | current_vertex->set_state(local_condition,SPLIT); |
|---|
| 1010 | |
|---|
| 1011 | if(local_condition == 0) |
|---|
| 1012 | { |
|---|
| 1013 | current_vertex->totally_neutral = true; |
|---|
| 1014 | |
|---|
| 1015 | current_vertex->raise_multiplicity(); |
|---|
| 1016 | |
|---|
| 1017 | current_vertex->negativeneutralvertices.insert(current_vertex); |
|---|
| 1018 | current_vertex->positiveneutralvertices.insert(current_vertex); |
|---|
| 1019 | } |
|---|
| 1020 | } |
|---|
| 1021 | |
|---|
| 1022 | if(should_remove) |
|---|
| 1023 | { |
|---|
| 1024 | double local_condition = toremove*appended_vec; |
|---|
| 1025 | |
|---|
| 1026 | current_vertex->set_state(local_condition,MERGE); |
|---|
| 1027 | |
|---|
| 1028 | if(local_condition == 0) |
|---|
| 1029 | { |
|---|
| 1030 | for_merging[0].push_back(current_vertex); |
|---|
| 1031 | } |
|---|
| 1032 | } |
|---|
| 1033 | } |
|---|
| 1034 | |
|---|
| 1035 | send_state_message(current_vertex, toadd, toremove, 0); |
|---|
| 1036 | |
|---|
| 1037 | } |
|---|
| 1038 | |
|---|
| 1039 | if(should_remove) |
|---|
| 1040 | { |
|---|
| 1041 | set<vertex*> vertices_to_be_reduced; |
|---|
| 1042 | |
|---|
| 1043 | int k = 1; |
|---|
| 1044 | |
|---|
| 1045 | for(vector<list<polyhedron*>>::iterator vert_ref = for_merging.begin();vert_ref<for_merging.end();vert_ref++) |
|---|
| 1046 | { |
|---|
| 1047 | for(list<polyhedron*>::reverse_iterator merge_ref = vert_ref->rbegin();merge_ref!=vert_ref->rend();merge_ref++) |
|---|
| 1048 | { |
|---|
| 1049 | if((*merge_ref)->get_multiplicity()>1) |
|---|
| 1050 | { |
|---|
| 1051 | if(k==1) |
|---|
| 1052 | { |
|---|
| 1053 | vertices_to_be_reduced.insert((vertex*)(*merge_ref)); |
|---|
| 1054 | } |
|---|
| 1055 | else |
|---|
| 1056 | { |
|---|
| 1057 | (*merge_ref)->lower_multiplicity(); |
|---|
| 1058 | } |
|---|
| 1059 | } |
|---|
| 1060 | else |
|---|
| 1061 | { |
|---|
| 1062 | toprow* current_positive = (toprow*)(*merge_ref)->positiveparent; |
|---|
| 1063 | toprow* current_negative = (toprow*)(*merge_ref)->negativeparent; |
|---|
| 1064 | |
|---|
| 1065 | current_positive->condition -= toremove; |
|---|
| 1066 | current_positive->condition_order--; |
|---|
| 1067 | |
|---|
| 1068 | current_positive->children.insert(current_positive->children.end(),current_negative->children.begin(),current_negative->children.end()); |
|---|
| 1069 | current_positive->children.remove(*merge_ref); |
|---|
| 1070 | |
|---|
| 1071 | for(list<polyhedron*>::iterator child_ref = current_negative->children.begin();child_ref!=current_negative->children.end();child_ref++) |
|---|
| 1072 | { |
|---|
| 1073 | (*child_ref)->parents.remove(current_negative); |
|---|
| 1074 | (*child_ref)->parents.push_back(current_positive); |
|---|
| 1075 | } |
|---|
| 1076 | |
|---|
| 1077 | current_positive->negativechildren.insert(current_positive->negativechildren.end(),current_negative->negativechildren.begin(),current_negative->negativechildren.end()); |
|---|
| 1078 | |
|---|
| 1079 | current_positive->positivechildren.insert(current_positive->positivechildren.end(),current_negative->positivechildren.begin(),current_negative->positivechildren.end()); |
|---|
| 1080 | |
|---|
| 1081 | current_positive->neutralchildren.insert(current_positive->neutralchildren.end(),current_negative->positivechildren.begin(),current_negative->positivechildren.end()); |
|---|
| 1082 | |
|---|
| 1083 | switch((*merge_ref)->get_state(SPLIT)) |
|---|
| 1084 | { |
|---|
| 1085 | case -1: |
|---|
| 1086 | current_positive->negativechildren.remove(*merge_ref); |
|---|
| 1087 | break; |
|---|
| 1088 | case 0: |
|---|
| 1089 | current_positive->neutralchildren.remove(*merge_ref); |
|---|
| 1090 | break; |
|---|
| 1091 | case 1: |
|---|
| 1092 | current_positive->positivechildren.remove(*merge_ref); |
|---|
| 1093 | break; |
|---|
| 1094 | } |
|---|
| 1095 | |
|---|
| 1096 | current_positive->parents.insert(current_positive->parents.begin(),current_negative->parents.begin(),current_negative->parents.end()); |
|---|
| 1097 | unique(current_positive->parents.begin(),current_positive->parents.end()); |
|---|
| 1098 | |
|---|
| 1099 | for(list<polyhedron*>::iterator parent_ref = current_negative->parents.begin();parent_ref!=current_negative->parents.end();parent_ref++) |
|---|
| 1100 | { |
|---|
| 1101 | (*parent_ref)->children.remove(current_negative); |
|---|
| 1102 | (*parent_ref)->children.push_back(current_positive); |
|---|
| 1103 | } |
|---|
| 1104 | |
|---|
| 1105 | current_positive->totallyneutralchildren.insert(current_negative->totallyneutralchildren.begin(),current_negative->totallyneutralchildren.end()); |
|---|
| 1106 | current_positive->totallyneutralchildren.erase(*merge_ref); |
|---|
| 1107 | |
|---|
| 1108 | current_positive->totallyneutralgrandchildren.insert(current_negative->totallyneutralgrandchildren.begin(),current_negative->totallyneutralgrandchildren.end()); |
|---|
| 1109 | |
|---|
| 1110 | current_positive->vertices.insert(current_negative->vertices.begin(),current_negative->vertices.end()); |
|---|
| 1111 | current_positive->negativeneutralvertices.insert(current_negative->negativeneutralvertices.begin(),current_negative->negativeneutralvertices.end()); |
|---|
| 1112 | current_positive->positiveneutralvertices.insert(current_negative->positiveneutralvertices.begin(),current_negative->positiveneutralvertices.end()); |
|---|
| 1113 | |
|---|
| 1114 | for(set<vertex*>::iterator vert_ref = (*merge_ref)->vertices.begin();vert_ref!=(*merge_ref)->vertices.end();vert_ref++) |
|---|
| 1115 | { |
|---|
| 1116 | if((*vert_ref)->get_multiplicity()==1) |
|---|
| 1117 | { |
|---|
| 1118 | current_positive->vertices.erase(*vert_ref); |
|---|
| 1119 | current_positive->negativeneutralvertices.erase(*vert_ref); |
|---|
| 1120 | current_positive->positiveneutralvertices.erase(*vert_ref); |
|---|
| 1121 | } |
|---|
| 1122 | } |
|---|
| 1123 | |
|---|
| 1124 | if(current_negative->get_state(SPLIT)==0&&!current_negative->totally_neutral) |
|---|
| 1125 | { |
|---|
| 1126 | for_splitting[k].remove(current_negative); |
|---|
| 1127 | |
|---|
| 1128 | if(current_positive->get_state(SPLIT)!=0||current_positive->totally_neutral) |
|---|
| 1129 | { |
|---|
| 1130 | for_splitting[k].push_back(current_positive); |
|---|
| 1131 | } |
|---|
| 1132 | } |
|---|
| 1133 | |
|---|
| 1134 | if(current_positive->totally_neutral) |
|---|
| 1135 | { |
|---|
| 1136 | if(!current_negative->totally_neutral) |
|---|
| 1137 | { |
|---|
| 1138 | for(set<polyhedron*>::iterator grand_ref = current_positive->grandparents.begin();grand_ref!=current_positive->grandparents.end();grand_ref++) |
|---|
| 1139 | { |
|---|
| 1140 | (*grand_ref)->totallyneutralgrandchildren.erase(current_positive); |
|---|
| 1141 | } |
|---|
| 1142 | |
|---|
| 1143 | current_positive->grandparents.clear(); |
|---|
| 1144 | } |
|---|
| 1145 | else |
|---|
| 1146 | { |
|---|
| 1147 | for(set<polyhedron*>::iterator grand_ref = current_negative->grandparents.begin();grand_ref!=current_negative->grandparents.end();grand_ref++) |
|---|
| 1148 | { |
|---|
| 1149 | (*grand_ref)->totallyneutralgrandchildren.erase(current_negative); |
|---|
| 1150 | (*grand_ref)->totallyneutralgrandchildren.insert(current_positive); |
|---|
| 1151 | } |
|---|
| 1152 | |
|---|
| 1153 | current_positive->grandparents.insert(current_negative->grandparents.begin(),current_negative->grandparents.end()); |
|---|
| 1154 | } |
|---|
| 1155 | } |
|---|
| 1156 | else |
|---|
| 1157 | { |
|---|
| 1158 | if(current_negative->totally_neutral) |
|---|
| 1159 | { |
|---|
| 1160 | for(set<polyhedron*>::iterator grand_ref = current_negative->grandparents.begin();grand_ref!=current_negative->grandparents.end();grand_ref++) |
|---|
| 1161 | { |
|---|
| 1162 | (*grand_ref)->totallyneutralgrandchildren.erase(current_negative); |
|---|
| 1163 | } |
|---|
| 1164 | } |
|---|
| 1165 | } |
|---|
| 1166 | |
|---|
| 1167 | current_positive->totally_neutral = (current_positive->totally_neutral && current_negative->totally_neutral); |
|---|
| 1168 | |
|---|
| 1169 | current_positive->triangulate(k==for_splitting.size()-1); |
|---|
| 1170 | |
|---|
| 1171 | statistic.delete_polyhedron(k,current_negative); |
|---|
| 1172 | |
|---|
| 1173 | delete current_negative; |
|---|
| 1174 | |
|---|
| 1175 | for(list<polyhedron*>::iterator child_ref = (*merge_ref)->children.begin();child_ref!=(*merge_ref)->children.end();child_ref++) |
|---|
| 1176 | { |
|---|
| 1177 | (*child_ref)->parents.remove(*merge_ref); |
|---|
| 1178 | } |
|---|
| 1179 | |
|---|
| 1180 | for(list<polyhedron*>::iterator parent_ref = (*merge_ref)->parents.begin();parent_ref!=(*merge_ref)->parents.end();parent_ref++) |
|---|
| 1181 | { |
|---|
| 1182 | (*parent_ref)->positivechildren.remove(*merge_ref); |
|---|
| 1183 | (*parent_ref)->negativechildren.remove(*merge_ref); |
|---|
| 1184 | (*parent_ref)->neutralchildren.remove(*merge_ref); |
|---|
| 1185 | (*parent_ref)->children.remove(*merge_ref); |
|---|
| 1186 | } |
|---|
| 1187 | |
|---|
| 1188 | for(set<polyhedron*>::iterator grand_ch_ref = (*merge_ref)->totallyneutralgrandchildren.begin();grand_ch_ref!=(*merge_ref)->totallyneutralgrandchildren.end();grand_ch_ref++) |
|---|
| 1189 | { |
|---|
| 1190 | (*grand_ch_ref)->grandparents.erase(*merge_ref); |
|---|
| 1191 | } |
|---|
| 1192 | |
|---|
| 1193 | for(set<polyhedron*>::iterator grand_p_ref = (*merge_ref)->grandparents.begin();grand_p_ref!=(*merge_ref)->grandparents.end();grand_p_ref++) |
|---|
| 1194 | { |
|---|
| 1195 | (*grand_p_ref)->totallyneutralgrandchildren.erase(*merge_ref); |
|---|
| 1196 | } |
|---|
| 1197 | |
|---|
| 1198 | statistic.delete_polyhedron(k-1,*merge_ref); |
|---|
| 1199 | |
|---|
| 1200 | if(k==1) |
|---|
| 1201 | { |
|---|
| 1202 | vertices_to_be_reduced.insert((vertex*)(*merge_ref)); |
|---|
| 1203 | } |
|---|
| 1204 | else |
|---|
| 1205 | { |
|---|
| 1206 | delete *merge_ref; |
|---|
| 1207 | } |
|---|
| 1208 | } |
|---|
| 1209 | } |
|---|
| 1210 | |
|---|
| 1211 | k++; |
|---|
| 1212 | |
|---|
| 1213 | } |
|---|
| 1214 | |
|---|
| 1215 | for(set<vertex*>::iterator vert_ref = vertices_to_be_reduced.begin();vert_ref!=vertices_to_be_reduced.end();vert_ref++) |
|---|
| 1216 | { |
|---|
| 1217 | if((*vert_ref)->get_multiplicity()>1) |
|---|
| 1218 | { |
|---|
| 1219 | (*vert_ref)->lower_multiplicity(); |
|---|
| 1220 | } |
|---|
| 1221 | else |
|---|
| 1222 | { |
|---|
| 1223 | delete *vert_ref; |
|---|
| 1224 | } |
|---|
| 1225 | } |
|---|
| 1226 | } |
|---|
| 1227 | |
|---|
| 1228 | if(should_add) |
|---|
| 1229 | { |
|---|
| 1230 | int k = 1; |
|---|
| 1231 | |
|---|
| 1232 | vector<list<polyhedron*>>::iterator beginning_ref = ++for_splitting.begin(); |
|---|
| 1233 | |
|---|
| 1234 | for(vector<list<polyhedron*>>::iterator vert_ref = beginning_ref;vert_ref<for_splitting.end();vert_ref++) |
|---|
| 1235 | { |
|---|
| 1236 | |
|---|
| 1237 | for(list<polyhedron*>::reverse_iterator split_ref = vert_ref->rbegin();split_ref != vert_ref->rend();split_ref++) |
|---|
| 1238 | { |
|---|
| 1239 | polyhedron* new_totally_neutral_child; |
|---|
| 1240 | |
|---|
| 1241 | polyhedron* current_polyhedron = (*split_ref); |
|---|
| 1242 | |
|---|
| 1243 | if(vert_ref == beginning_ref) |
|---|
| 1244 | { |
|---|
| 1245 | vec coordinates1 = ((vertex*)(*(current_polyhedron->children.begin())))->get_coordinates(); |
|---|
| 1246 | vec coordinates2 = ((vertex*)(*(++current_polyhedron->children.begin())))->get_coordinates(); |
|---|
| 1247 | |
|---|
| 1248 | vec extended_coord2 = coordinates2; |
|---|
| 1249 | extended_coord2.ins(0,-1.0); |
|---|
| 1250 | |
|---|
| 1251 | double t = (-toadd*extended_coord2)/((toadd(1,toadd.size()-1)*(coordinates1-coordinates2))); |
|---|
| 1252 | |
|---|
| 1253 | vec new_coordinates = coordinates2+t*(coordinates1-coordinates2); |
|---|
| 1254 | |
|---|
| 1255 | // cout << "c1:" << coordinates1 << endl << "c2:" << coordinates2 << endl << "nc:" << new_coordinates << endl; |
|---|
| 1256 | |
|---|
| 1257 | vertex* neutral_vertex = new vertex(new_coordinates); |
|---|
| 1258 | |
|---|
| 1259 | new_totally_neutral_child = neutral_vertex; |
|---|
| 1260 | } |
|---|
| 1261 | else |
|---|
| 1262 | { |
|---|
| 1263 | toprow* neutral_toprow = new toprow(); |
|---|
| 1264 | |
|---|
| 1265 | neutral_toprow->condition = ((toprow*)current_polyhedron)->condition; // tohle tu bylo driv: zeros(number_of_parameters+1); |
|---|
| 1266 | neutral_toprow->condition_order = ((toprow*)current_polyhedron)->condition_order+1; |
|---|
| 1267 | |
|---|
| 1268 | new_totally_neutral_child = neutral_toprow; |
|---|
| 1269 | } |
|---|
| 1270 | |
|---|
| 1271 | new_totally_neutral_child->my_emlig = this; |
|---|
| 1272 | |
|---|
| 1273 | new_totally_neutral_child->children.insert(new_totally_neutral_child->children.end(), |
|---|
| 1274 | current_polyhedron->totallyneutralgrandchildren.begin(), |
|---|
| 1275 | current_polyhedron->totallyneutralgrandchildren.end()); |
|---|
| 1276 | |
|---|
| 1277 | |
|---|
| 1278 | |
|---|
| 1279 | // cout << ((toprow*)current_polyhedron)->condition << endl << toadd << endl; |
|---|
| 1280 | |
|---|
| 1281 | toprow* positive_poly = new toprow(((toprow*)current_polyhedron)->condition+toadd, ((toprow*)current_polyhedron)->condition_order+1); |
|---|
| 1282 | toprow* negative_poly = new toprow(((toprow*)current_polyhedron)->condition-toadd, ((toprow*)current_polyhedron)->condition_order+1); |
|---|
| 1283 | |
|---|
| 1284 | positive_poly->my_emlig = this; |
|---|
| 1285 | negative_poly->my_emlig = this; |
|---|
| 1286 | |
|---|
| 1287 | for(set<polyhedron*>::iterator grand_ref = current_polyhedron->totallyneutralgrandchildren.begin(); grand_ref != current_polyhedron->totallyneutralgrandchildren.end();grand_ref++) |
|---|
| 1288 | { |
|---|
| 1289 | (*grand_ref)->parents.push_back(new_totally_neutral_child); |
|---|
| 1290 | (*grand_ref)->grandparents.insert(positive_poly); |
|---|
| 1291 | (*grand_ref)->grandparents.insert(negative_poly); |
|---|
| 1292 | |
|---|
| 1293 | new_totally_neutral_child->vertices.insert((*grand_ref)->vertices.begin(),(*grand_ref)->vertices.end()); |
|---|
| 1294 | } |
|---|
| 1295 | |
|---|
| 1296 | positive_poly->children.push_back(new_totally_neutral_child); |
|---|
| 1297 | negative_poly->children.push_back(new_totally_neutral_child); |
|---|
| 1298 | |
|---|
| 1299 | |
|---|
| 1300 | for(list<polyhedron*>::iterator parent_ref = current_polyhedron->parents.begin();parent_ref!=current_polyhedron->parents.end();parent_ref++) |
|---|
| 1301 | { |
|---|
| 1302 | (*parent_ref)->totallyneutralgrandchildren.insert(new_totally_neutral_child); |
|---|
| 1303 | new_totally_neutral_child->grandparents.insert(*parent_ref); |
|---|
| 1304 | |
|---|
| 1305 | (*parent_ref)->neutralchildren.remove(current_polyhedron); |
|---|
| 1306 | (*parent_ref)->children.remove(current_polyhedron); |
|---|
| 1307 | |
|---|
| 1308 | (*parent_ref)->children.push_back(positive_poly); |
|---|
| 1309 | (*parent_ref)->children.push_back(negative_poly); |
|---|
| 1310 | (*parent_ref)->positivechildren.push_back(positive_poly); |
|---|
| 1311 | (*parent_ref)->negativechildren.push_back(negative_poly); |
|---|
| 1312 | } |
|---|
| 1313 | |
|---|
| 1314 | positive_poly->parents.insert(positive_poly->parents.end(), |
|---|
| 1315 | current_polyhedron->parents.begin(), |
|---|
| 1316 | current_polyhedron->parents.end()); |
|---|
| 1317 | |
|---|
| 1318 | negative_poly->parents.insert(negative_poly->parents.end(), |
|---|
| 1319 | current_polyhedron->parents.begin(), |
|---|
| 1320 | current_polyhedron->parents.end()); |
|---|
| 1321 | |
|---|
| 1322 | |
|---|
| 1323 | |
|---|
| 1324 | new_totally_neutral_child->parents.push_back(positive_poly); |
|---|
| 1325 | new_totally_neutral_child->parents.push_back(negative_poly); |
|---|
| 1326 | |
|---|
| 1327 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->positivechildren.begin();child_ref!=current_polyhedron->positivechildren.end();child_ref++) |
|---|
| 1328 | { |
|---|
| 1329 | (*child_ref)->parents.remove(current_polyhedron); |
|---|
| 1330 | (*child_ref)->parents.push_back(positive_poly); |
|---|
| 1331 | } |
|---|
| 1332 | |
|---|
| 1333 | positive_poly->children.insert(positive_poly->children.end(), |
|---|
| 1334 | current_polyhedron->positivechildren.begin(), |
|---|
| 1335 | current_polyhedron->positivechildren.end()); |
|---|
| 1336 | |
|---|
| 1337 | for(list<polyhedron*>::iterator child_ref = current_polyhedron->negativechildren.begin();child_ref!=current_polyhedron->negativechildren.end();child_ref++) |
|---|
| 1338 | { |
|---|
| 1339 | (*child_ref)->parents.remove(current_polyhedron); |
|---|
| 1340 | (*child_ref)->parents.push_back(negative_poly); |
|---|
| 1341 | } |
|---|
| 1342 | |
|---|
| 1343 | negative_poly->children.insert(negative_poly->children.end(), |
|---|
| 1344 | current_polyhedron->negativechildren.begin(), |
|---|
| 1345 | current_polyhedron->negativechildren.end()); |
|---|
| 1346 | |
|---|
| 1347 | positive_poly->vertices.insert(current_polyhedron->positiveneutralvertices.begin(),current_polyhedron->positiveneutralvertices.end()); |
|---|
| 1348 | positive_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
|---|
| 1349 | |
|---|
| 1350 | negative_poly->vertices.insert(current_polyhedron->negativeneutralvertices.begin(),current_polyhedron->negativeneutralvertices.end()); |
|---|
| 1351 | negative_poly->vertices.insert(new_totally_neutral_child->vertices.begin(),new_totally_neutral_child->vertices.end()); |
|---|
| 1352 | |
|---|
| 1353 | new_totally_neutral_child->triangulate(false); |
|---|
| 1354 | |
|---|
| 1355 | positive_poly->triangulate(k==for_splitting.size()-1); |
|---|
| 1356 | negative_poly->triangulate(k==for_splitting.size()-1); |
|---|
| 1357 | |
|---|
| 1358 | statistic.append_polyhedron(k-1, new_totally_neutral_child); |
|---|
| 1359 | |
|---|
| 1360 | statistic.insert_polyhedron(k, positive_poly, current_polyhedron); |
|---|
| 1361 | statistic.insert_polyhedron(k, negative_poly, current_polyhedron); |
|---|
| 1362 | |
|---|
| 1363 | statistic.delete_polyhedron(k, current_polyhedron); |
|---|
| 1364 | |
|---|
| 1365 | delete current_polyhedron; |
|---|
| 1366 | } |
|---|
| 1367 | |
|---|
| 1368 | k++; |
|---|
| 1369 | } |
|---|
| 1370 | } |
|---|
| 1371 | |
|---|
| 1372 | |
|---|
| 1373 | vector<int> sizevector; |
|---|
| 1374 | for(int s = 0;s<statistic.size();s++) |
|---|
| 1375 | { |
|---|
| 1376 | sizevector.push_back(statistic.row_size(s)); |
|---|
| 1377 | } |
|---|
| 1378 | |
|---|
| 1379 | /* |
|---|
| 1380 | for(polyhedron* topr_ref = statistic.rows[statistic.size()-1];topr_ref!=statistic.row_ends[statistic.size()-1]->next_poly;topr_ref=topr_ref->next_poly) |
|---|
| 1381 | { |
|---|
| 1382 | cout << ((toprow*)topr_ref)->condition << endl; |
|---|
| 1383 | } |
|---|
| 1384 | */ |
|---|
| 1385 | |
|---|
| 1386 | } |
|---|
| 1387 | |
|---|
| 1388 | void set_correction_factors(int order) |
|---|
| 1389 | { |
|---|
| 1390 | for(int remaining_order = correction_factors.size();remaining_order<order;remaining_order++) |
|---|
| 1391 | { |
|---|
| 1392 | multiset<my_ivec> factor_templates; |
|---|
| 1393 | multiset<my_ivec> final_factors; |
|---|
| 1394 | |
|---|
| 1395 | my_ivec orig_template = my_ivec(); |
|---|
| 1396 | |
|---|
| 1397 | for(int i = 1;i<number_of_parameters-remaining_order+1;i++) |
|---|
| 1398 | { |
|---|
| 1399 | bool in_cycle = false; |
|---|
| 1400 | for(int j = 0;j<=remaining_order;j++) { |
|---|
| 1401 | |
|---|
| 1402 | multiset<my_ivec>::iterator fac_ref = factor_templates.begin(); |
|---|
| 1403 | |
|---|
| 1404 | do |
|---|
| 1405 | { |
|---|
| 1406 | my_ivec current_template; |
|---|
| 1407 | if(!in_cycle) |
|---|
| 1408 | { |
|---|
| 1409 | current_template = orig_template; |
|---|
| 1410 | in_cycle = true; |
|---|
| 1411 | } |
|---|
| 1412 | else |
|---|
| 1413 | { |
|---|
| 1414 | current_template = (*fac_ref); |
|---|
| 1415 | fac_ref++; |
|---|
| 1416 | } |
|---|
| 1417 | |
|---|
| 1418 | current_template.ins(current_template.size(),i); |
|---|
| 1419 | |
|---|
| 1420 | // cout << "template:" << current_template << endl; |
|---|
| 1421 | |
|---|
| 1422 | if(current_template.size()==remaining_order+1) |
|---|
| 1423 | { |
|---|
| 1424 | final_factors.insert(current_template); |
|---|
| 1425 | } |
|---|
| 1426 | else |
|---|
| 1427 | { |
|---|
| 1428 | factor_templates.insert(current_template); |
|---|
| 1429 | } |
|---|
| 1430 | } |
|---|
| 1431 | while(fac_ref!=factor_templates.end()); |
|---|
| 1432 | } |
|---|
| 1433 | } |
|---|
| 1434 | |
|---|
| 1435 | correction_factors.push_back(final_factors); |
|---|
| 1436 | |
|---|
| 1437 | } |
|---|
| 1438 | } |
|---|
| 1439 | |
|---|
| 1440 | protected: |
|---|
| 1441 | |
|---|
| 1442 | /// A method for creating plain default statistic representing only the range of the parameter space. |
|---|
| 1443 | void create_statistic(int number_of_parameters) |
|---|
| 1444 | { |
|---|
| 1445 | for(int i = 0;i<number_of_parameters;i++) |
|---|
| 1446 | { |
|---|
| 1447 | vec condition_vec = zeros(number_of_parameters+1); |
|---|
| 1448 | condition_vec[i+1] = 1; |
|---|
| 1449 | |
|---|
| 1450 | condition* new_condition = new condition(condition_vec); |
|---|
| 1451 | |
|---|
| 1452 | conditions.push_back(new_condition); |
|---|
| 1453 | } |
|---|
| 1454 | |
|---|
| 1455 | // An empty vector of coordinates. |
|---|
| 1456 | vec origin_coord; |
|---|
| 1457 | |
|---|
| 1458 | // We create an origin - this point will have all the coordinates zero, but now it has an empty vector of coords. |
|---|
| 1459 | vertex *origin = new vertex(origin_coord); |
|---|
| 1460 | |
|---|
| 1461 | origin->my_emlig = this; |
|---|
| 1462 | |
|---|
| 1463 | /* |
|---|
| 1464 | // As a statistic, we have to create a vector of vectors of polyhedron pointers. It will then represent the Hasse |
|---|
| 1465 | // diagram. First we create a vector of polyhedrons.. |
|---|
| 1466 | list<polyhedron*> origin_vec; |
|---|
| 1467 | |
|---|
| 1468 | // ..we fill it with the origin.. |
|---|
| 1469 | origin_vec.push_back(origin); |
|---|
| 1470 | |
|---|
| 1471 | // ..and we fill the statistic with the created vector. |
|---|
| 1472 | statistic.push_back(origin_vec); |
|---|
| 1473 | */ |
|---|
| 1474 | |
|---|
| 1475 | statistic = *(new c_statistic()); |
|---|
| 1476 | |
|---|
| 1477 | statistic.append_polyhedron(0, origin); |
|---|
| 1478 | |
|---|
| 1479 | // Now we have a statistic for a zero dimensional space. Regarding to how many dimensional space we need to |
|---|
| 1480 | // describe, we have to widen the descriptional default statistic. We use an iterative procedure as follows: |
|---|
| 1481 | for(int i=0;i<number_of_parameters;i++) |
|---|
| 1482 | { |
|---|
| 1483 | // We first will create two new vertices. These will be the borders of the parameter space in the dimension |
|---|
| 1484 | // of newly added parameter. Therefore they will have all coordinates except the last one zero. We get the |
|---|
| 1485 | // right amount of zero cooridnates by reading them from the origin |
|---|
| 1486 | vec origin_coord = origin->get_coordinates(); |
|---|
| 1487 | |
|---|
| 1488 | // And we incorporate the nonzero coordinates into the new cooordinate vectors |
|---|
| 1489 | vec origin_coord1 = concat(origin_coord,-max_range); |
|---|
| 1490 | vec origin_coord2 = concat(origin_coord,max_range); |
|---|
| 1491 | |
|---|
| 1492 | |
|---|
| 1493 | // Now we create the points |
|---|
| 1494 | vertex* new_point1 = new vertex(origin_coord1); |
|---|
| 1495 | vertex* new_point2 = new vertex(origin_coord2); |
|---|
| 1496 | |
|---|
| 1497 | new_point1->my_emlig = this; |
|---|
| 1498 | new_point2->my_emlig = this; |
|---|
| 1499 | |
|---|
| 1500 | //********************************************************************************************************* |
|---|
| 1501 | // The algorithm for recursive build of a new Hasse diagram representing the space structure from the old |
|---|
| 1502 | // diagram works so that you create two copies of the old Hasse diagram, you shift them up one level (points |
|---|
| 1503 | // will be segments, segments will be areas etc.) and you connect each one of the original copied polyhedrons |
|---|
| 1504 | // with its offspring by a parent-child relation. Also each of the segments in the first (second) copy is |
|---|
| 1505 | // connected to the first (second) newly created vertex by a parent-child relation. |
|---|
| 1506 | //********************************************************************************************************* |
|---|
| 1507 | |
|---|
| 1508 | |
|---|
| 1509 | /* |
|---|
| 1510 | // Create the vectors of vectors of pointers to polyhedrons to hold the copies of the old Hasse diagram |
|---|
| 1511 | vector<vector<polyhedron*>> new_statistic1; |
|---|
| 1512 | vector<vector<polyhedron*>> new_statistic2; |
|---|
| 1513 | */ |
|---|
| 1514 | |
|---|
| 1515 | c_statistic* new_statistic1 = new c_statistic(); |
|---|
| 1516 | c_statistic* new_statistic2 = new c_statistic(); |
|---|
| 1517 | |
|---|
| 1518 | |
|---|
| 1519 | // Copy the statistic by rows |
|---|
| 1520 | for(int j=0;j<statistic.size();j++) |
|---|
| 1521 | { |
|---|
| 1522 | |
|---|
| 1523 | |
|---|
| 1524 | // an element counter |
|---|
| 1525 | int element_number = 0; |
|---|
| 1526 | |
|---|
| 1527 | /* |
|---|
| 1528 | vector<polyhedron*> supportnew_1; |
|---|
| 1529 | vector<polyhedron*> supportnew_2; |
|---|
| 1530 | |
|---|
| 1531 | new_statistic1.push_back(supportnew_1); |
|---|
| 1532 | new_statistic2.push_back(supportnew_2); |
|---|
| 1533 | */ |
|---|
| 1534 | |
|---|
| 1535 | // for each polyhedron in the given row |
|---|
| 1536 | for(polyhedron* horiz_ref = statistic.rows[j];horiz_ref!=statistic.get_end();horiz_ref=horiz_ref->next_poly) |
|---|
| 1537 | { |
|---|
| 1538 | // Append an extra zero coordinate to each of the vertices for the new dimension |
|---|
| 1539 | // If vert_ref is at the first index => we loop through vertices |
|---|
| 1540 | if(j == 0) |
|---|
| 1541 | { |
|---|
| 1542 | // cast the polyhedron pointer to a vertex pointer and push a zero to its vector of coordinates |
|---|
| 1543 | ((vertex*) horiz_ref)->push_coordinate(0); |
|---|
| 1544 | } |
|---|
| 1545 | /* |
|---|
| 1546 | else |
|---|
| 1547 | { |
|---|
| 1548 | ((toprow*) (*horiz_ref))->condition.ins(0,0); |
|---|
| 1549 | }*/ |
|---|
| 1550 | |
|---|
| 1551 | // if it has parents |
|---|
| 1552 | if(!horiz_ref->parents.empty()) |
|---|
| 1553 | { |
|---|
| 1554 | // save the relative address of this child in a vector kids_rel_addresses of all its parents. |
|---|
| 1555 | // This information will later be used for copying the whole Hasse diagram with each of the |
|---|
| 1556 | // relations contained within. |
|---|
| 1557 | for(list<polyhedron*>::iterator parent_ref = horiz_ref->parents.begin();parent_ref != horiz_ref->parents.end();parent_ref++) |
|---|
| 1558 | { |
|---|
| 1559 | (*parent_ref)->kids_rel_addresses.push_back(element_number); |
|---|
| 1560 | } |
|---|
| 1561 | } |
|---|
| 1562 | |
|---|
| 1563 | // ************************************************************************************************** |
|---|
| 1564 | // Here we begin creating a new polyhedron, which will be a copy of the old one. Each such polyhedron |
|---|
| 1565 | // will be created as a toprow, but this information will be later forgotten and only the polyhedrons |
|---|
| 1566 | // in the top row of the Hasse diagram will be considered toprow for later use. |
|---|
| 1567 | // ************************************************************************************************** |
|---|
| 1568 | |
|---|
| 1569 | // First we create vectors specifying a toprow condition. In the case of a preconstructed statistic |
|---|
| 1570 | // this condition will be a vector of zeros. There are two vectors, because we need two copies of |
|---|
| 1571 | // the original Hasse diagram. |
|---|
| 1572 | vec vec1(number_of_parameters+1); |
|---|
| 1573 | vec1.zeros(); |
|---|
| 1574 | |
|---|
| 1575 | vec vec2(number_of_parameters+1); |
|---|
| 1576 | vec2.zeros(); |
|---|
| 1577 | |
|---|
| 1578 | // We create a new toprow with the previously specified condition. |
|---|
| 1579 | toprow* current_copy1 = new toprow(vec1, 0); |
|---|
| 1580 | toprow* current_copy2 = new toprow(vec2, 0); |
|---|
| 1581 | |
|---|
| 1582 | current_copy1->my_emlig = this; |
|---|
| 1583 | current_copy2->my_emlig = this; |
|---|
| 1584 | |
|---|
| 1585 | // The vertices of the copies will be inherited, because there will be a parent/child relation |
|---|
| 1586 | // between each polyhedron and its offspring (comming from the copy) and a parent has all the |
|---|
| 1587 | // vertices of its child plus more. |
|---|
| 1588 | for(set<vertex*>::iterator vertex_ref = horiz_ref->vertices.begin();vertex_ref!=horiz_ref->vertices.end();vertex_ref++) |
|---|
| 1589 | { |
|---|
| 1590 | current_copy1->vertices.insert(*vertex_ref); |
|---|
| 1591 | current_copy2->vertices.insert(*vertex_ref); |
|---|
| 1592 | } |
|---|
| 1593 | |
|---|
| 1594 | // The only new vertex of the offspring should be the newly created point. |
|---|
| 1595 | current_copy1->vertices.insert(new_point1); |
|---|
| 1596 | current_copy2->vertices.insert(new_point2); |
|---|
| 1597 | |
|---|
| 1598 | // This method guarantees that each polyhedron is already triangulated, therefore its triangulation |
|---|
| 1599 | // is only one set of vertices and it is the set of all its vertices. |
|---|
| 1600 | set<vertex*> t_simplex1; |
|---|
| 1601 | set<vertex*> t_simplex2; |
|---|
| 1602 | |
|---|
| 1603 | t_simplex1.insert(current_copy1->vertices.begin(),current_copy1->vertices.end()); |
|---|
| 1604 | t_simplex2.insert(current_copy2->vertices.begin(),current_copy2->vertices.end()); |
|---|
| 1605 | |
|---|
| 1606 | current_copy1->triangulation.push_back(t_simplex1); |
|---|
| 1607 | current_copy2->triangulation.push_back(t_simplex2); |
|---|
| 1608 | |
|---|
| 1609 | // Now we have copied the polyhedron and we have to copy all of its relations. Because we are copying |
|---|
| 1610 | // in the Hasse diagram from bottom up, we always have to copy the parent/child relations to all the |
|---|
| 1611 | // kids and when we do that and know the child, in the child we will remember the parent we came from. |
|---|
| 1612 | // This way all the parents/children relations are saved in both the parent and the child. |
|---|
| 1613 | if(!horiz_ref->kids_rel_addresses.empty()) |
|---|
| 1614 | { |
|---|
| 1615 | for(list<int>::iterator kid_ref = horiz_ref->kids_rel_addresses.begin();kid_ref!=horiz_ref->kids_rel_addresses.end();kid_ref++) |
|---|
| 1616 | { |
|---|
| 1617 | polyhedron* new_kid1 = new_statistic1->rows[j-1]; |
|---|
| 1618 | polyhedron* new_kid2 = new_statistic2->rows[j-1]; |
|---|
| 1619 | |
|---|
| 1620 | // THIS IS NOT EFFECTIVE: It could be improved by having the list indexed for new_statistic, but |
|---|
| 1621 | // not indexed for statistic. Hopefully this will not cause a big slowdown - happens only offline. |
|---|
| 1622 | if(*kid_ref) |
|---|
| 1623 | { |
|---|
| 1624 | for(int k = 1;k<=(*kid_ref);k++) |
|---|
| 1625 | { |
|---|
| 1626 | new_kid1=new_kid1->next_poly; |
|---|
| 1627 | new_kid2=new_kid2->next_poly; |
|---|
| 1628 | } |
|---|
| 1629 | } |
|---|
| 1630 | |
|---|
| 1631 | // find the child and save the relation to the parent |
|---|
| 1632 | current_copy1->children.push_back(new_kid1); |
|---|
| 1633 | current_copy2->children.push_back(new_kid2); |
|---|
| 1634 | |
|---|
| 1635 | // in the child save the parents' address |
|---|
| 1636 | new_kid1->parents.push_back(current_copy1); |
|---|
| 1637 | new_kid2->parents.push_back(current_copy2); |
|---|
| 1638 | } |
|---|
| 1639 | |
|---|
| 1640 | // Here we clear the parents kids_rel_addresses vector for later use (when we need to widen the |
|---|
| 1641 | // Hasse diagram again) |
|---|
| 1642 | horiz_ref->kids_rel_addresses.clear(); |
|---|
| 1643 | } |
|---|
| 1644 | // If there were no children previously, we are copying a polyhedron that has been a vertex before. |
|---|
| 1645 | // In this case it is a segment now and it will have a relation to its mother (copywise) and to the |
|---|
| 1646 | // newly created point. Here we create the connection to the new point, again from both sides. |
|---|
| 1647 | else |
|---|
| 1648 | { |
|---|
| 1649 | // Add the address of the new point in the former vertex |
|---|
| 1650 | current_copy1->children.push_back(new_point1); |
|---|
| 1651 | current_copy2->children.push_back(new_point2); |
|---|
| 1652 | |
|---|
| 1653 | // Add the address of the former vertex in the new point |
|---|
| 1654 | new_point1->parents.push_back(current_copy1); |
|---|
| 1655 | new_point2->parents.push_back(current_copy2); |
|---|
| 1656 | } |
|---|
| 1657 | |
|---|
| 1658 | // Save the mother in its offspring |
|---|
| 1659 | current_copy1->children.push_back(horiz_ref); |
|---|
| 1660 | current_copy2->children.push_back(horiz_ref); |
|---|
| 1661 | |
|---|
| 1662 | // Save the offspring in its mother |
|---|
| 1663 | horiz_ref->parents.push_back(current_copy1); |
|---|
| 1664 | horiz_ref->parents.push_back(current_copy2); |
|---|
| 1665 | |
|---|
| 1666 | |
|---|
| 1667 | // Add the copies into the relevant statistic. The statistic will later be appended to the previous |
|---|
| 1668 | // Hasse diagram |
|---|
| 1669 | new_statistic1->append_polyhedron(j,current_copy1); |
|---|
| 1670 | new_statistic2->append_polyhedron(j,current_copy2); |
|---|
| 1671 | |
|---|
| 1672 | // Raise the count in the vector of polyhedrons |
|---|
| 1673 | element_number++; |
|---|
| 1674 | |
|---|
| 1675 | } |
|---|
| 1676 | |
|---|
| 1677 | } |
|---|
| 1678 | |
|---|
| 1679 | /* |
|---|
| 1680 | statistic.begin()->push_back(new_point1); |
|---|
| 1681 | statistic.begin()->push_back(new_point2); |
|---|
| 1682 | */ |
|---|
| 1683 | |
|---|
| 1684 | statistic.append_polyhedron(0, new_point1); |
|---|
| 1685 | statistic.append_polyhedron(0, new_point2); |
|---|
| 1686 | |
|---|
| 1687 | // Merge the new statistics into the old one. This will either be the final statistic or we will |
|---|
| 1688 | // reenter the widening loop. |
|---|
| 1689 | for(int j=0;j<new_statistic1->size();j++) |
|---|
| 1690 | { |
|---|
| 1691 | /* |
|---|
| 1692 | if(j+1==statistic.size()) |
|---|
| 1693 | { |
|---|
| 1694 | list<polyhedron*> support; |
|---|
| 1695 | statistic.push_back(support); |
|---|
| 1696 | } |
|---|
| 1697 | |
|---|
| 1698 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic1[j].begin(),new_statistic1[j].end()); |
|---|
| 1699 | (statistic.begin()+j+1)->insert((statistic.begin()+j+1)->end(),new_statistic2[j].begin(),new_statistic2[j].end()); |
|---|
| 1700 | */ |
|---|
| 1701 | statistic.append_polyhedron(j+1,new_statistic1->rows[j],new_statistic1->row_ends[j]); |
|---|
| 1702 | statistic.append_polyhedron(j+1,new_statistic2->rows[j],new_statistic2->row_ends[j]); |
|---|
| 1703 | } |
|---|
| 1704 | } |
|---|
| 1705 | |
|---|
| 1706 | /* |
|---|
| 1707 | vector<list<toprow*>> toprow_statistic; |
|---|
| 1708 | int line_count = 0; |
|---|
| 1709 | |
|---|
| 1710 | for(vector<list<polyhedron*>>::iterator polyhedron_ref = ++statistic.begin(); polyhedron_ref!=statistic.end();polyhedron_ref++) |
|---|
| 1711 | { |
|---|
| 1712 | list<toprow*> support_list; |
|---|
| 1713 | toprow_statistic.push_back(support_list); |
|---|
| 1714 | |
|---|
| 1715 | for(list<polyhedron*>::iterator polyhedron_ref2 = polyhedron_ref->begin(); polyhedron_ref2 != polyhedron_ref->end(); polyhedron_ref2++) |
|---|
| 1716 | { |
|---|
| 1717 | toprow* support_top = (toprow*)(*polyhedron_ref2); |
|---|
| 1718 | |
|---|
| 1719 | toprow_statistic[line_count].push_back(support_top); |
|---|
| 1720 | } |
|---|
| 1721 | |
|---|
| 1722 | line_count++; |
|---|
| 1723 | }*/ |
|---|
| 1724 | |
|---|
| 1725 | /* |
|---|
| 1726 | vector<int> sizevector; |
|---|
| 1727 | for(int s = 0;s<statistic.size();s++) |
|---|
| 1728 | { |
|---|
| 1729 | sizevector.push_back(statistic.row_size(s)); |
|---|
| 1730 | } |
|---|
| 1731 | */ |
|---|
| 1732 | |
|---|
| 1733 | } |
|---|
| 1734 | |
|---|
| 1735 | |
|---|
| 1736 | |
|---|
| 1737 | |
|---|
| 1738 | }; |
|---|
| 1739 | |
|---|
| 1740 | /* |
|---|
| 1741 | |
|---|
| 1742 | //! Robust Bayesian AR model for Multicriteria-Laplace-Inverse-Gamma density |
|---|
| 1743 | class RARX : public BM |
|---|
| 1744 | { |
|---|
| 1745 | private: |
|---|
| 1746 | |
|---|
| 1747 | emlig posterior; |
|---|
| 1748 | |
|---|
| 1749 | public: |
|---|
| 1750 | RARX():BM() |
|---|
| 1751 | { |
|---|
| 1752 | }; |
|---|
| 1753 | |
|---|
| 1754 | void bayes(const itpp::vec &yt, const itpp::vec &cond = empty_vec) |
|---|
| 1755 | { |
|---|
| 1756 | |
|---|
| 1757 | } |
|---|
| 1758 | |
|---|
| 1759 | };*/ |
|---|
| 1760 | |
|---|
| 1761 | |
|---|
| 1762 | |
|---|
| 1763 | #endif //TRAGE_H |
|---|