00001
00002
00003
00004
00005
00006
00011 # include <fvar.hpp>
00012 # include <admodel.h>
00013 # include <df1b2fun.h>
00014 # include <adrndeff.h>
00015 double evaluate_function(const dvector& x,function_minimizer * pfmin);
00016 void get_second_ders(int xs,int us,const init_df1b2vector y,dmatrix& Hess,
00017 dmatrix& Dux, df1b2_gradlist * f1b2gradlist,function_minimizer * pfmin,
00018 laplace_approximation_calculator* lap);
00019 double calculate_laplace_approximation(const dvector& x,const dvector& u0,
00020 const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
00021 const dmatrix& _Hessadjoint,function_minimizer * pmin);
00022
00023 double calculate_importance_sample(const dvector& x,const dvector& u0,
00024 const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
00025 const dmatrix& _Hessadjoint,function_minimizer * pmin);
00026
00027 dmatrix choleski_decomp_positive(const dmatrix& M,double b);
00028
00029
00030
00031
00032 #if defined(USE_ADPVM)
00033
00037 dvector laplace_approximation_calculator::test_trust_region_method
00038 (const dvector& _x,const double& _f,function_minimizer * pfmin)
00039 {
00040
00041 ADUNCONST(dvector,x)
00042
00043
00044 int i;
00045
00046 initial_params::set_inactive_only_random_effects();
00047 gradient_structure::set_NO_DERIVATIVES();
00048 initial_params::reset(x);
00049 gradient_structure::set_YES_DERIVATIVES();
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
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
00111 for (i=1;i<=xsize;i++)
00112 {
00113 y(i)=x(i);
00114 }
00115 for (i=1;i<=usize;i++)
00116 {
00117 y(i+xsize)=uhat(i);
00118 }
00119 int n=xsize+usize;
00120 dvector xx(1,n);
00121 for (i=1;i<=n;i++)
00122 {
00123 xx(i)=value(y(i));
00124 }
00125 double bestf=do_one_feval(xx,pfmin);
00126 double Delta=10;
00127 double lambda;
00128 cout << "input Delta" << endl;
00129 cin >> Delta;
00130 cout << "input lambda" << endl;
00131 cin >> lambda;
00132 int outer_iter=0;
00133 int max_iteration=10;
00134 do
00135 {
00136 outer_iter++;
00137
00138
00139 dvector g(1,n);
00140 dmatrix H(1,n,1,n);
00141
00142
00143 get_complete_hessian(H,g,pfmin);
00144
00145 double tol=1.e-6;
00146
00147
00148
00149
00150
00151
00152
00153
00154
00155
00156
00157 int i;
00158
00159 for (i=1;i<=n;i++)
00160 {
00161 H(i,i)+=lambda;
00162 }
00163
00164 cout << "initial fun value - " << double(0.5)*xx*(H*xx)+xx*g;
00165 double truef=do_one_feval(xx,pfmin);
00166 cout << "real function value = " << truef << endl;
00167 double estdiff;
00168 double truediff;
00169 int iflag=0;
00170 int inner_iter=0;
00171
00172 int maxfn=15;
00173 dvector xret=lincg(xx,g,H,tol,Delta,pfmin,truef,estdiff,
00174 truediff,bestf,iflag,inner_iter,maxfn);
00175 cout << " norm(g) = " << norm(g)
00176 << " norm(H*xret+g) = " << norm(H*xret+g)
00177 << " norm(H*xret-g) = " << norm(H*xret-g)
00178 << " inner_iter = " << inner_iter << endl;
00179 if (iflag==1)
00180 {
00181 Delta/=5.0;
00182 }
00183 for (i=1;i<=n;i++)
00184 {
00185 y(i)+=xret(i);
00186 }
00187 for (i=1;i<=n;i++)
00188 {
00189 xx(i)=value(y(i));
00190 }
00191
00192 f1b2gradlist->reset();
00193 f1b2gradlist->list.initialize();
00194 f1b2gradlist->list2.initialize();
00195 f1b2gradlist->list3.initialize();
00196 f1b2gradlist->nlist.initialize();
00197 f1b2gradlist->nlist2.initialize();
00198 f1b2gradlist->nlist3.initialize();
00199 }
00200 while(outer_iter<=max_iteration);
00201 return xx;
00202 }
00203 #endif
00204
00209 void laplace_approximation_calculator::get_complete_hessian
00210 (dmatrix& H,dvector& g,function_minimizer * pfmin)
00211 {
00212 int i,j,ip;
00213
00214 if (ad_comm::time_flag)
00215 {
00216 if (ad_comm::ptm)
00217 {
00218 (*ad_comm::global_logfile) << " Starting Newton-Raphson "
00219 << endl;
00220 }
00221 }
00222
00223 for (ip=1;ip<=num_der_blocks;ip++)
00224 {
00225 df1b2variable::minder=minder(ip);
00226 df1b2variable::maxder=maxder(ip);
00227
00228 set_u_dot(ip);
00229
00230
00231 check_for_need_to_reallocate(ip);
00232 df1b2_gradlist::set_yes_derivatives();
00233
00234
00235 (*re_objective_function_value::pobjfun)=double(0.0);
00236
00237 #if defined(USE_DDOUBLE)
00238 #undef double
00239 df1b2variable pen=double(0.0);
00240 df1b2variable zz=double(0.0);
00241 #define double dd_real
00242 #else
00243 df1b2variable pen=0.0;
00244 df1b2variable zz=0.0;
00245 #endif
00246 initial_df1b2params::reset(y,pen);
00247
00248 if (initial_df1b2params::separable_flag)
00249 {
00250 H.initialize();
00251 grad.initialize();
00252 }
00253
00254 double time1 = 0;
00255 if (ad_comm::time_flag)
00256 {
00257 if (ad_comm::ptm)
00258 {
00259 time1=ad_comm::ptm->get_elapsed_time();
00260 }
00261 }
00262
00263 pfmin->user_function();
00264
00265 if (ad_comm::time_flag)
00266 {
00267 if (ad_comm::ptm)
00268 {
00269 if (ad_comm::global_logfile)
00270 {
00271 double time=ad_comm::ptm->get_elapsed_time();
00272 (*ad_comm::global_logfile) << " Time in user_function() "
00273 << ip << " " << time-time1 << endl;
00274 }
00275 }
00276 }
00277
00278 re_objective_function_value::fun_without_pen
00279 =value(*re_objective_function_value::pobjfun);
00280
00281 (*re_objective_function_value::pobjfun)+=pen;
00282 (*re_objective_function_value::pobjfun)+=zz;
00283
00284
00285
00286 if (!initial_df1b2params::separable_flag)
00287 {
00288 set_dependent_variable(*re_objective_function_value::pobjfun);
00289 df1b2_gradlist::set_no_derivatives();
00290 df1b2variable::passnumber=1;
00291 df1b2_gradcalc1();
00292 int mind=y(1).minder;
00293 int jmin=max(mind,1);
00294 int jmax=min(y(1).maxder,xsize+usize);
00295 for (i=1;i<=xsize+usize;i++)
00296 for (j=jmin;j<=jmax;j++)
00297 H(i,j)=y(i).u_bar[j-mind];
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 for (j=jmin;j<=jmax;j++)
00324 g(j)= re_objective_function_value::pobjfun->u_dot[j-mind];
00325 }
00326 if (ip<num_der_blocks)
00327 f1b2gradlist->reset();
00328 }
00329
00330
00331
00332 if (ad_comm::time_flag)
00333 {
00334 if (ad_comm::ptm)
00335 {
00336 ad_comm::ptm->get_elapsed_time();
00337 }
00338 }
00339 }
00340
00341
00342
00343 #define EPS 1.0e-14
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385
00386
00387
00388
00389
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
00428
00429
00430
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
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487
00488
00489
00490
00491
00492
00493
00494
00495
00496
00497
00498
00499
00500
00501
00502
00503
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00526 double laplace_approximation_calculator::do_one_feval
00527 (const dvector& x,function_minimizer * pfmin)
00528 {
00529 double f=0.0;
00530
00531 dvector g(1,usize);
00532 dvector ub(1,usize);
00533 initial_params::set_active_random_effects();
00534 int nvar=initial_params::nvarcalc();
00535 int nvar1=initial_params::nvarcalc_all();
00536 cout << nvar << " " << nvar1 << endl;
00537 gradient_structure::set_NO_DERIVATIVES();
00538 dvariable vf=0.0;
00539 dvariable pen=initial_params::reset(dvar_vector(x));
00540 *objective_function_value::pobjfun=0.0;
00541 pfmin->AD_uf_inner();
00542 vf+=*objective_function_value::pobjfun;
00543 vf+=pen;
00544 f=value(vf);
00545 return f;
00546 }
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557
00558
00559
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586
00587
00588
00589
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 #if defined(USE_ADPVM)
00626
00630 dvector laplace_approximation_calculator::lincg(dvector& xinit,
00631 dvector& c, dmatrix& H1,double tol,double Delta,function_minimizer * pfmin,
00632 double& truef,double& estdiff,double& truediff,double& bestf,
00633 int& iflag,int& inner_iter,int maxfn)
00634 {
00635 iflag=0;
00636 int n=c.indexmax();
00637 dmatrix r(1,20,1,n);
00638 dmatrix p(1,20,1,n);
00639 dmatrix x(1,20,1,n);
00640 dvector alpha(1,20);
00641 dvector beta(1,20);
00642 dmatrix H=-H1;
00643 int k=1;
00644 x(1)=0.0;
00645 r(1)=-c;
00646 p(1)=r(1);
00647 cout << norm(H*x(k)+c) << endl;
00648 cout << norm(H*x(k)-c) << endl;
00649
00650 do
00651 {
00652 dvector w=H*p(k);
00653 alpha(k)=(r(k)*H*r(k))/norm2(w);
00654 r(k+1)=r(k)-alpha(k)*(H*p(k));
00655 beta(k)=(r(k+1)*H*r(k+1))/(r(k)*H*r(k));
00656 p(k+1)=r(k)+beta(k)*p(k);
00657 x(k+1)=x(k)+alpha(k)*p(k);
00658 k++;
00659 cout << norm(H*x(k)+c) << endl;
00660 cout << norm(H*x(k)-c) << endl;
00661 }
00662 while(1);
00663 return 0;
00664 }
00665 #endif