00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00027 #ifndef _INFB_EVALUATOR_H_
00028 #define _INFB_EVALUATOR_H_
00029
00030 #include <coconut_config.h>
00031 #include <evaluator.h>
00032 #include <expression.h>
00033 #include <model.h>
00034 #include <eval_main.h>
00035 #include <linalg.h>
00036 #include <math.h>
00037 #include <infb.h>
00038
00039 using namespace vgtl;
00040
00041 typedef infbound (*infbound_evaluator)(const std::vector<infbound>* __x,
00042 const variable_indicator& __v);
00043
00044 struct infbound_eval_type
00045 {
00046 const std::vector<infbound>* x;
00047 std::vector<infbound>* cache;
00048 const model* mod;
00049 void *p;
00050 infbound d;
00051 int info;
00052 infbound r;
00053 unsigned int n;
00054 };
00055
00056 class infbound_eval : public
00057 cached_forward_evaluator_base<infbound_eval_type,expression_node,infbound,
00058 model::walker>
00059 {
00060 private:
00061 typedef cached_forward_evaluator_base<infbound_eval_type,expression_node,
00062 infbound,model::walker> _Base;
00063
00064 protected:
00065 bool is_cached(const node_data_type& __data)
00066 {
00067 if(__data.operator_type == EXPRINFO_LIN ||
00068 __data.operator_type == EXPRINFO_QUAD)
00069 return true;
00070 else
00071 return false;
00072 #if 0
00073 if(eval_data.cache && __data.n_parents > 1 && __data.n_children > 0)
00074 return true;
00075 else
00076 return false;
00077 #endif
00078 }
00079
00080 private:
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 public:
00111 infbound_eval(const std::vector<infbound>& __x, const variable_indicator& __v,
00112 const model& __m, std::vector<infbound>* __c) : _Base()
00113 {
00114 eval_data.x = &__x;
00115 eval_data.cache = __c;
00116 eval_data.mod = &__m;
00117 eval_data.d = 0.;
00118 eval_data.p = NULL;
00119 v_ind = &__v;
00120 eval_data.n = 0;
00121 }
00122
00123 infbound_eval(const infbound_eval& __v) : _Base(__v) {}
00124
00125 ~infbound_eval() {}
00126
00127 model::walker short_cut_to(const expression_node& __data)
00128 { return eval_data.mod->node(0); }
00129
00130 void initialize() { return; }
00131
00132 int initialize(const expression_node& __data)
00133 {
00134 if(__data.ev && (*__data.ev)[FUNC_INFB])
00135
00136 {
00137 eval_data.r =
00138 (*(infbound_evaluator)(*__data.ev)[FUNC_INFB])(eval_data.x,
00139 *v_ind);
00140 return 0;
00141 }
00142 else
00143 {
00144 switch(__data.operator_type)
00145 {
00146 case EXPRINFO_SUM:
00147 case EXPRINFO_PROD:
00148 case EXPRINFO_MAX:
00149 case EXPRINFO_MIN:
00150 case EXPRINFO_INVERT:
00151 case EXPRINFO_DIV:
00152 eval_data.r = __data.params.nd();
00153 break;
00154 case EXPRINFO_MONOME:
00155 case EXPRINFO_IN:
00156 case EXPRINFO_AND:
00157 case EXPRINFO_NOGOOD:
00158 eval_data.r = 1.;
00159 break;
00160 case EXPRINFO_ALLDIFF:
00161 eval_data.p = (void*) new std::vector<infbound>;
00162 ((std::vector<infbound>*)eval_data.p)->reserve(__data.n_children);
00163
00164 case EXPRINFO_MEAN:
00165 case EXPRINFO_IF:
00166 case EXPRINFO_OR:
00167 case EXPRINFO_NOT:
00168 case EXPRINFO_COUNT:
00169 case EXPRINFO_SCPROD:
00170 case EXPRINFO_NORM:
00171 case EXPRINFO_LEVEL:
00172 eval_data.r = 0.;
00173 break;
00174 case EXPRINFO_DET:
00175 case EXPRINFO_PSD:
00176
00177 break;
00178 case EXPRINFO_COND:
00179 case EXPRINFO_FEM:
00180 case EXPRINFO_MPROD:
00181
00182 break;
00183 }
00184 return 1;
00185 }
00186 }
00187
00188 void calculate(const expression_node& __data)
00189 {
00190 #if 0
00191 if(__data.operator_type > 0)
00192 {
00193 eval_data.r = __data.i_evaluate(-1, __data.params.nn(), *eval_data.x,
00194 *v_ind, eval_data.r, cintval(0.0), NULL);
00195 }
00196 eval_data.r.intersect(__data.f_bounds);
00197 #endif
00198 }
00199
00200 void retrieve_from_cache(const expression_node& __data)
00201 {
00202 if(__data.operator_type == EXPRINFO_LIN)
00203 {
00204 const matrix<double>::Row& lrw(eval_data.mod->lin[__data.params.nn()]);
00205 matrix<double>::Row::const_iterator _x, _e;
00206
00207 eval_data.r = infbound(0.);
00208 for(_x = lrw.begin(); _x != _e; ++_x)
00209 eval_data.r += (*eval_data.x)[_x.index()] * *_x;
00210 }
00211 else if(__data.operator_type == EXPRINFO_QUAD)
00212 {
00213 throw "Interval Evaluation of QUAD: NYI!";
00214 }
00215 else
00216 eval_data.r = (*eval_data.cache)[__data.node_num];
00217 }
00218
00219 int update(const infbound& __rval)
00220 {
00221 eval_data.r = __rval;
00222 return 0;
00223 }
00224
00225 int update(const expression_node& __data, const infbound& __rval)
00226 {
00227 infbound __x;
00228 int ret = 0;
00229 if(__data.operator_type < 0)
00230 {
00231 switch(__data.operator_type)
00232 {
00233 case EXPRINFO_CONSTANT:
00234 eval_data.r = __data.params.nd();
00235 break;
00236 case EXPRINFO_VARIABLE:
00237 eval_data.r = (*eval_data.x)[__data.params.nn()];
00238 break;
00239 case EXPRINFO_SUM:
00240 case EXPRINFO_MEAN:
00241 eval_data.r += __data.coeffs[eval_data.n++]*__rval;
00242 break;
00243 case EXPRINFO_PROD:
00244
00245 eval_data.r *= __rval;
00246 if(eval_data.r == 0.) ret = -1;
00247 break;
00248 case EXPRINFO_MONOME:
00249
00250
00251
00252
00253
00254 std::cerr << "Momomials NYI" << std::endl;
00255 throw "NYI";
00256 break;
00257 case EXPRINFO_MAX:
00258
00259
00260
00261
00262 std::cerr << "MAXIMUM NYI" << std::endl;
00263 throw "NYI";
00264 break;
00265 case EXPRINFO_MIN:
00266
00267 std::cerr << "MAXIMUM NYI" << std::endl;
00268 throw "NYI";
00269 break;
00270
00271
00272
00273
00274 case EXPRINFO_SCPROD:
00275 { infbound h = __data.coeffs[eval_data.n]*__rval;
00276
00277
00278 if(eval_data.n & 1)
00279 eval_data.r += __data.coeffs[eval_data.n]*__rval*h;
00280 else
00281 eval_data.d = h;
00282 }
00283 eval_data.n++;
00284 break;
00285 case EXPRINFO_NORM:
00286 if(__data.params.nd() != 2.)
00287 {
00288 std::cerr << "Non-euclidean norms NYI" << std::endl;
00289 throw "NYI";
00290 }
00291 else
00292 {
00293 infbound h = __data.coeffs[eval_data.n++]*__rval;
00294 eval_data.r += sqr(h);
00295 }
00296 if(eval_data.n == __data.n_children && __data.params.nd() == 2.)
00297 eval_data.r = sqrt(eval_data.r);
00298 break;
00299 case EXPRINFO_INVERT:
00300 eval_data.r /= __rval;
00301 break;
00302 case EXPRINFO_DIV:
00303
00304
00305 if(eval_data.n == 0)
00306 eval_data.r *= __rval;
00307 else
00308 eval_data.r /= __rval;
00309 ++eval_data.n;
00310 break;
00311 case EXPRINFO_SQUARE:
00312 { infbound h = __data.coeffs[0]*__rval+__data.params.nd();
00313 eval_data.r = sqr(h);
00314 }
00315 break;
00316 case EXPRINFO_INTPOWER:
00317 std::cerr << "INTPOWER NYI" << std::endl;
00318 throw "NYI";
00319 break;
00320
00321
00322 case EXPRINFO_SQROOT:
00323 eval_data.r = sqrt(__data.coeffs[0]*__rval+__data.params.nd());
00324 break;
00325 case EXPRINFO_ABS:
00326 eval_data.r = abs(__data.coeffs[0]*__rval+__data.params.nd());
00327 break;
00328 case EXPRINFO_POW:
00329 std::cerr << "cinterval^cinterval NYI" << std::endl;
00330 throw "NYI";
00331 break;
00332
00333
00334
00335
00336
00337
00338
00339
00340 case EXPRINFO_EXP:
00341 eval_data.r = exp(__rval*__data.coeffs[0]+__data.params.nd());
00342 break;
00343 case EXPRINFO_LOG:
00344 std::cerr << "LOG(infb) NYI" << std::endl;
00345 throw "NYI";
00346
00347 break;
00348 case EXPRINFO_SIN:
00349 eval_data.r = sin(__rval*__data.coeffs[0]+__data.params.nd());
00350 break;
00351 case EXPRINFO_COS:
00352 std::cerr << "LOG(infb) NYI" << std::endl;
00353 throw "NYI";
00354
00355 break;
00356 case EXPRINFO_ATAN2:
00357 std::cerr << "ATAN2 NYI" << std::endl;
00358 throw "NYI";
00359 break;
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369 case EXPRINFO_GAUSS:
00370 std::cerr << "Polynomes NYI" << std::endl;
00371 throw "NYI";
00372 break;
00373
00374
00375
00376
00377
00378 case EXPRINFO_POLY:
00379 std::cerr << "Polynomials NYI" << std::endl;
00380 throw "NYI";
00381 break;
00382 case EXPRINFO_LIN:
00383 case EXPRINFO_QUAD:
00384
00385 break;
00386 case EXPRINFO_IN:
00387 std::cerr << "Containment NYI" << std::endl;
00388 throw "NYI";
00389 break;
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427 case EXPRINFO_IF:
00428 std::cerr << "Cinterval-IF's NYI" << std::endl;
00429 throw "NYI";
00430 break;
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458
00459
00460
00461 case EXPRINFO_AND:
00462 std::cerr << "Cinterval-AND's NYI" << std::endl;
00463 throw "NYI";
00464 break;
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481 case EXPRINFO_OR:
00482 std::cerr << "Cinterval-OR's NYI" << std::endl;
00483 throw "NYI";
00484 break;
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500 case EXPRINFO_NOT:
00501 std::cerr << "Cinterval-NOT's NYI" << std::endl;
00502 throw "NYI";
00503 break;
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514 case EXPRINFO_IMPLIES:
00515 std::cerr << "Cinterval-IMPLIES NYI" << std::endl;
00516 throw "NYI";
00517 break;
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 case EXPRINFO_COUNT:
00544 std::cerr << "Cinterval-COUNT NYI" << std::endl;
00545 throw "NYI";
00546 break;
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556 case EXPRINFO_ALLDIFF:
00557 std::cerr << "Cinterval-ALLDIFF NYI" << std::endl;
00558 throw "NYI";
00559 break;
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582 case EXPRINFO_HISTOGRAM:
00583 std::cerr << "int_evaluator: histogram NYI" << std::endl;
00584 throw "NYI";
00585 break;
00586 case EXPRINFO_LEVEL:
00587 std::cerr << "Cinterval-LEVEL NYI" << std::endl;
00588 throw "NYI";
00589 break;
00590
00591
00592
00593
00594
00595
00596
00597
00598
00599
00600
00601
00602
00603
00604
00605
00606
00607
00608
00609
00610
00611
00612
00613
00614
00615
00616
00617
00618
00619
00620
00621
00622
00623
00624
00625
00626
00627
00628
00629
00630
00631 case EXPRINFO_NEIGHBOR:
00632 std::cerr << "Cinterval-Neighbor NYI" << std::endl;
00633 throw "NYI";
00634 break;
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653
00654
00655
00656
00657
00658
00659
00660
00661
00662
00663 case EXPRINFO_NOGOOD:
00664 std::cerr << "Cinterval-nogood NYI" << std::endl;
00665 throw "NYI";
00666 break;
00667
00668
00669
00670
00671
00672
00673
00674
00675
00676
00677
00678
00679
00680 case EXPRINFO_EXPECTATION:
00681 std::cerr << "int_evaluator: E NYI" << std::endl;
00682 throw "NYI";
00683 break;
00684 case EXPRINFO_INTEGRAL:
00685 std::cerr << "int_evaluator: INT NYI" << std::endl;
00686 throw "NYI";
00687 break;
00688 case EXPRINFO_LOOKUP:
00689 case EXPRINFO_PWLIN:
00690 case EXPRINFO_SPLINE:
00691 case EXPRINFO_PWCONSTLC:
00692 case EXPRINFO_PWCONSTRC:
00693 std::cerr << "int_evaluator: Table Operations NYI" << std::endl;
00694 throw "NYI";
00695 break;
00696 case EXPRINFO_DET:
00697 case EXPRINFO_COND:
00698 case EXPRINFO_PSD:
00699 case EXPRINFO_MPROD:
00700 case EXPRINFO_FEM:
00701 std::cerr << "int_evaluator: Matrix Operations NYI" << std::endl;
00702 throw "NYI";
00703 break;
00704 case EXPRINFO_RE:
00705 case EXPRINFO_IM:
00706 case EXPRINFO_ARG:
00707 case EXPRINFO_CPLXCONJ:
00708 std::cerr << "int_evaluator: Complex Numbers NYI" << std::endl;
00709 throw "NYI";
00710 break;
00711 case EXPRINFO_CMPROD:
00712 case EXPRINFO_CGFEM:
00713 std::cerr << "int_evaluator: Const Matrix Operations NYI" << std::endl;
00714 throw "NYI";
00715 break;
00716 default:
00717 std::cerr << "int_evaluator: unknown function type " <<
00718 __data.operator_type << std::endl;
00719 throw "Programming error";
00720 break;
00721 }
00722 }
00723 #if 0
00724 else if(__data.operator_type > 0)
00725
00726
00727
00728
00729
00730 #endif
00731 if(eval_data.cache)
00732 (*eval_data.cache)[__data.node_num] = eval_data.r;
00733 return ret;
00734 }
00735
00736 infbound calculate_value(bool eval_all)
00737 {
00738 return eval_data.r;
00739 }
00740 };
00741
00742 #endif