ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2chkder.cpp
Go to the documentation of this file.
1 
5 #include <fvar.hpp>
6 #include <admodel.h>
7 #include <df1b2fun.h>
8 #include <adrndeff.h>
9 #ifndef OPT_LIB
10  #include <cassert>
11  #include <climits>
12 #endif
13 double evaluate_function(const dvector& x,function_minimizer * pfmin);
14 void get_second_ders(int xs,int us,const init_df1b2vector y,dmatrix& Hess,
17 double calculate_laplace_approximation(const dvector& x,const dvector& u0,
18  const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
19  const dmatrix& _Hessadjoint,function_minimizer * pmin);
20 
21 double calculate_importance_sample(const dvector& x,const dvector& u0,
22  const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
23  const dmatrix& _Hessadjoint,function_minimizer * pmin);
24 
25 double calculate_importance_sample_funnel(const dvector& x,const dvector& u0,
26  const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
27  const dmatrix& _Hessadjoint,function_minimizer * pmin);
28 
29 dmatrix choleski_decomp_positive(const dmatrix& M,double b);
30 
31 #ifdef DIAG_TIMER
32 int ad_comm::time_flag = 0;
33 adtimer* ad_comm::ptm = nullptr;
34 adtimer* ad_comm::ptm1 = nullptr;
35 #endif
36 
42  check_derivatives(const dvector& _x,function_minimizer * pfmin,double f)
43 {
44 #ifdef DIAG
45  cerr << "need to define this" << endl;
46  ad_exit(1);
47 #endif
48 }
49 
56  function_minimizer * pfmin, const double& _f)
57 {
58  // for use when there is no separability
60  int i,j;
61  double& f = (double&)_f;
62 
65  initial_params::reset(x); // get current x values into the model
66 
67 
68  pfmin->AD_uf_inner();
70 
72 
74  initial_params::xinit(uhat); // get current x values into the model
75  //int lmn_flag=0;
76 #ifdef DIAG_TIMER
77  if (ad_comm::time_flag)
78  {
79  if (ad_comm::ptm1)
80  {
81  ad_comm::ptm1->get_elapsed_time_and_reset();
82  }
83  if (ad_comm::ptm)
84  {
85  ad_comm::ptm->get_elapsed_time_and_reset();
86  }
87  }
88  if (ad_comm::time_flag)
89  {
90  if (ad_comm::ptm)
91  {
92  double time=ad_comm::ptm->get_elapsed_time();
94  {
95  (*ad_comm::global_logfile) << " Time pos 0 "
96  << time << endl;
97  }
98  }
99  }
100 #endif
101 
102  double maxg = 0;
103  dvector uhat_old(1,usize);
104  //double f_from_1=0.0;
105 
106  for (i=1;i<=xsize;i++)
107  {
108  y(i)=x(i);
109  }
110  for (i=1;i<=usize;i++)
111  {
112  y(i+xsize)=uhat(i);
113  }
114 
115  int ierr=0;
116  int niters=0;
118  niters=num_nr_iters+1;
119  else
120  niters=num_nr_iters;
121 
122  int nv=0;
124  {
126  if (allocated(used_flags))
127  {
128  if (used_flags.indexmax() != nv)
129  {
131  }
132  }
133  if (!allocated(used_flags))
134  {
136  }
137  }
138 
139  for(int ii=1;ii<=niters;ii++)
140  {
142  {
143  check_pool_size();
144  }
145  {
146  // test newton raphson
147  Hess.initialize();
148 
149  cout << "Checking derivatives " << ii << endl;
150  check_derivatives(x,pfmin,fval1);
151 
153  {
155  /*double maxg = */evaluate_function_quiet(uhat,pfmin);
159  }
160 
161  /*
162  if (ii == 1)
163  { double diff = fabs(re_objective_function_value::fun_without_pen - objective_function_value::fun_without_pen); }
164  */
165 
166 #ifdef DIAG
167  int print_hess_in_newton_raphson_flag=0;
168  if (print_hess_in_newton_raphson_flag)
169  {
170  cout << norm2(Hess-trans(Hess)) << endl;
172  {
173  (*ad_comm::global_logfile) << setprecision(4) << setscientific()
174  << setw(12) << sort(eigenvalues(Hess)) << endl;
175  (*ad_comm::global_logfile) << setprecision(4) << setscientific()
176  << setw(12) << Hess << endl;
177  }
178  }
179 #endif
180 
181  dvector step;
182 #if defined(USE_ATLAS)
184  {
185  step=-atlas_solve_spd(Hess,grad,ierr);
186  }
187  else
188  {
190  if (!ierr)
191  {
192  step=-solve(Hess,grad);
193  //step=-solve(A*trans(A),grad);
194  }
195  }
196  if (ierr)
197  {
198  f1b2gradlist->reset();
205  break;
206  }
207 #else
208  step=-solve(Hess,grad);
209 #endif
210 
211 #ifdef DIAG_TIMER
212  if (ad_comm::time_flag)
213  {
214  if (ad_comm::ptm)
215  {
216  double time=ad_comm::ptm->get_elapsed_time_and_reset();
218  {
219  (*ad_comm::global_logfile) << " time_in solve " << ii << " "
220  << time << endl;
221  }
222  }
223  }
224 #endif
225 
226  f1b2gradlist->reset();
233 
234  uhat_old=uhat;
235  uhat+=step;
236 
237  double maxg_old=maxg;
238  maxg=fabs(evaluate_function(uhat,pfmin));
239  if (maxg>maxg_old)
240  {
241  uhat=uhat_old;
242  evaluate_function(uhat,pfmin);
243  break;
244  }
245  if (maxg < 1.e-13)
246  {
247  break;
248  }
249  }
250  for (i=1;i<=usize;i++)
251  {
252  y(i+xsize)=uhat(i);
253  }
254  }
255 
256  if (num_nr_iters<=0)
257  {
258  evaluate_function(uhat,pfmin);
259  }
260 
261  for (i=1;i<=usize;i++)
262  {
263  y(i+xsize)=uhat(i);
264  }
265 
266 
267 #ifdef DIAG_TIMER
268  if (ad_comm::time_flag)
269  {
270  if (ad_comm::ptm)
271  {
272  double time=ad_comm::ptm->get_elapsed_time_and_reset();
274  {
275  (*ad_comm::global_logfile) << " Time in reset and evaluate function"
276  << time << endl;
277  }
278  }
279  }
280 #endif
282  //int sgn=0;
283 
284 #ifdef DIAG_TIMER
285  if (ad_comm::time_flag)
286  {
287  if (ad_comm::ptm)
288  {
289  double time=ad_comm::ptm->get_elapsed_time_and_reset();
291  {
292  (*ad_comm::global_logfile) << " Time in dget second ders "
293  << time << endl;
294  }
295  }
296  }
297 #endif
298  if (!ierr)
299  {
300  if (num_importance_samples==0)
301  {
302  //cout << "Hess " << endl << Hess << endl;
304  Hessadjoint,pfmin);
305  }
306  else
307  {
308  if (isfunnel_flag==0)
309  {
311  Hessadjoint,pfmin);
312  }
313  else
314  {
316  Hessadjoint,pfmin);
317  }
318  }
319  }
320  else
321  {
322  f=1.e+30;
323  }
324 
325 #ifdef DIAG_TIMER
326  if (ad_comm::time_flag)
327  {
328  if (ad_comm::ptm)
329  {
330  double time=ad_comm::ptm->get_elapsed_time_and_reset();
332  {
333  (*ad_comm::global_logfile) << "Time in calculate laplace approximation "
334  << time << endl;
335  }
336  }
337  }
338 #endif
339  for (int ip=num_der_blocks;ip>=1;ip--)
340  {
343  int mind=y(1).minder;
344  int jmin=max(mind,xsize+1);
345  int jmax=min(y(1).maxder,xsize+usize);
346  for (i=1;i<=usize;i++)
347  {
348  for (j=jmin;j<=jmax;j++)
349  {
350  //Hess(i,j-xsize)=y(i+xsize).u_bar[j-mind];
351  y(i+xsize).get_u_bar_tilde()[j-mind]=Hessadjoint(i,j-xsize);
352  }
353  }
354 
356  {
357  for (j=1;j<=xsize+usize;j++)
358  {
359  *y(j).get_u_tilde()=0;
360  }
361  Hess.initialize();
363  pfmin->user_function();
364  }
365  else
366  {
367  if (ip<num_der_blocks)
368  {
369  f1b2gradlist->reset();
370  set_u_dot(ip);
372  (*re_objective_function_value::pobjfun)=0;
373  df1b2variable pen=0.0;
374  df1b2variable zz=0.0;
375 
377  pfmin->user_function();
378 
381 
382  (*re_objective_function_value::pobjfun)+=pen;
383  (*re_objective_function_value::pobjfun)+=zz;
384 
388  df1b2_gradcalc1();
389  }
390 
391  for (i=1;i<=usize;i++)
392  {
393  for (j=jmin;j<=jmax;j++)
394  {
395  //Hess(i,j-xsize)=y(i+xsize).u_bar[j-mind];
396  y(i+xsize).get_u_bar_tilde()[j-mind]=Hessadjoint(i,j-xsize);
397  }
398  }
399 
400  //int mind=y(1).minder;
402  df1b2_gradcalc1();
403 
405  df1b2_gradcalc1();
406 
407  f1b2gradlist->reset();
414  }
415 
416 #ifdef DIAG_TIMER
417  if (ad_comm::time_flag)
418  {
419  if (ad_comm::ptm)
420  {
421  double time=ad_comm::ptm->get_elapsed_time_and_reset();
423  {
424  (*ad_comm::global_logfile) << " time for 3rd derivatives "
425  << time << endl;
426  }
427  }
428  }
429 #endif
430 
431  dvector dtmp(1,xsize);
432  for (i=1;i<=xsize;i++)
433  {
434  dtmp(i)=*y(i).get_u_tilde();
435  }
437  {
438 #ifndef OPT_LIB
439  assert(nvar <= INT_MAX);
440 #endif
441  dvector scale(1,(int)nvar); // need to get scale from somewhere
442  /*int check=*/initial_params::stddev_scale(scale,x);
443  dvector sscale=scale(1,Dux(1).indexmax());
444  for (i=1;i<=usize;i++)
445  {
446  Dux(i)=elem_prod(Dux(i),sscale);
447  }
448  dtmp=elem_prod(dtmp,sscale);
449  }
450 
451  for (i=1;i<=xsize;i++)
452  {
453  xadjoint(i)+=dtmp(i);
454  }
455  for (i=1;i<=usize;i++)
456  uadjoint(i)+=*y(xsize+i).get_u_tilde();
457  }
458  // *****************************************************************
459  // new stuff to deal with quadraticprior
460  // *****************************************************************
461 
462  int xstuff=3;
464  {
468 #ifndef OPT_LIB
469  assert(nvar <= INT_MAX);
470 #endif
471  dvector scale1(1,(int)nvar); // need to get scale from somewhere
473  /*int check=*/initial_params::stddev_scale(scale1,x);
474 
479  dvector scale(1,(int)nvar); // need to get scale from somewhere
480  /*check=*/initial_params::stddev_scale(scale,x);
481  dvector sscale=scale(1,Dux(1).indexmax());
482 
483  for (i=1;i<=usize;i++)
484  {
485  Dux(i)=elem_div(Dux(i),sscale);
486  }
487 
488  if (xstuff>1)
489  {
491  }
495 
496  for (i=1;i<=usize;i++)
497  {
498  Dux(i)=elem_prod(Dux(i),sscale);
499  }
500  //local_dtemp=elem_prod(local_dtemp,sscale);
501 
502  if (xstuff>2)
503  {
505  for (i=1;i<=xsize;i++)
506  {
507  xadjoint(i)+=tmp(i);
508  }
509  }
510 
511  if (xstuff>2)
512  {
514  }
515  }
516 
517  // *****************************************************************
518  // new stuff to deal with quadraticprior
519  // *****************************************************************
520 #ifdef DIAG_TIMER
521  if (ad_comm::ptm)
522  {
523  /*double time=*/ad_comm::ptm->get_elapsed_time_and_reset();
524  }
525 #endif
526 #if defined(USE_ATLAS)
528  {
529  //xadjoint -= uadjoint*atlas_solve_spd_trans(Hess,Dux);
531  }
532  else
533  {
534  //xadjoint -= uadjoint*solve(Hess,Dux);
536  }
537 #else
538  //xadjoint -= uadjoint*solve(Hess,Dux);
540 #endif
541 
542 
543 #ifdef DIAG_TIMER
544  if (ad_comm::ptm)
545  {
546  double time=ad_comm::ptm->get_elapsed_time_and_reset();
548  {
549  (*ad_comm::global_logfile) << " Time in second solve "
550  << time << endl;
551  }
552  }
553  if (ad_comm::ptm1)
554  {
555  double time=ad_comm::ptm1->get_elapsed_time_and_reset();
557  {
558  (*ad_comm::global_logfile) << " Total time in function evaluation "
559  << time << endl << endl;
560  }
561  }
562 #endif
563 
564  return xadjoint;
565 }
static int straight_through_flag
Definition: admodel.h:839
void safe_deallocate()
Safely deallocates memory by reporting if shallow copies are still in scope.
Definition: ivector.cpp:119
void initialize(void)
Description not yet available.
Definition: df1b2f14.cpp:155
static void reset(const init_df1b2vector &, const df1b2variable &)
Description not yet available.
Definition: df1b2f15.cpp:51
virtual void AD_uf_inner()
Definition: xmodelm4.cpp:40
double get_elapsed_time(void)
Returns the elapsed time in milliseconds from the timer object.
Definition: timer.cpp:57
static void set_yes_derivatives(void)
Definition: df1b2fun.h:761
df1b2_gradlist * f1b2gradlist
Definition: df1b2glo.cpp:49
d3_array elem_prod(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements product of a(i, j, k) * b(i, j, k).
Definition: d3arr2a.cpp:92
void set_dependent_variable(const df1b2variable &_x)
Description not yet available.
Definition: df1b2fn2.cpp:699
static void set_no_derivatives(void)
Definition: df1b2fun.h:760
Description not yet available.
Definition: adrndeff.h:182
double calculate_importance_sample(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
Definition: df1b2imp.cpp:23
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
Description not yet available.
static ofstream * global_logfile
Definition: fvar.hpp:8858
void check_pool_size(void)
Description not yet available.
Definition: df1b2lp1.cpp:871
dvector atlas_solve_spd(const dmatrix &M, const dvector &x)
dvector default_calculations_check_derivatives(const dvector &_x, function_minimizer *pfmin, const double &f)
Description not yet available.
Definition: df1b2chkder.cpp:55
static void get_Lxu_contribution(dmatrix &)
Definition: fquadpri.cpp:249
dvector eigenvalues(const banded_symmetric_dmatrix &_SS)
Description not yet available.
Definition: dmat28.cpp:411
dmatrix trans(const dmatrix &m1)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat2.cpp:13
#define x
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_a59.cpp:13
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Vector of double precision numbers.
Definition: dvector.h:50
static int separable_flag
Definition: df1b2fun.h:1351
static void get_cgradient_contribution(dvector, int)
Description not yet available.
Definition: quadpri.cpp:558
static int passnumber
Definition: df1b2fun.h:289
static re_objective_function_value * pobjfun
Definition: df1b2fun.h:1693
d3_array elem_div(const d3_array &a, const d3_array &b)
Returns d3_array results with computed elements division of a(i, j, k) / b(i, j, k).
Definition: d3arr2a.cpp:112
dvector evaluate_function_with_quadprior(const dvector &x, int usize, function_minimizer *pfmin)
Description not yet available.
Definition: quadpri.cpp:45
static void get_cHessian_contribution_from_vHessian(dmatrix, int)
Description not yet available.
Definition: quadpri.cpp:668
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
double calculate_laplace_approximation(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
Definition: df1b2lap.cpp:1476
void initialize(void)
Description not yet available.
Definition: df1b2f10.cpp:171
Description not yet available.
Definition: df1b2fun.h:745
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
static int maxder
Definition: df1b2fun.h:292
Description not yet available.
Definition: df1b2fun.h:266
Description not yet available.
Definition: fvar.hpp:8901
void set_u_dot(int i)
Description not yet available.
Definition: df1b2lp1.cpp:856
fixed_smartlist nlist
Definition: df1b2fun.h:754
void initialize(void)
Description not yet available.
Definition: df1b2f13.cpp:158
double evaluate_function(const dvector &x, function_minimizer *pfmin)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: df1b2lap.cpp:2026
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:46
prescientific setscientific(void)
Description not yet available.
Definition: admanip.cpp:80
static laplace_approximation_calculator * lapprox
Definition: df1b2fnl.h:61
dmatrix sort(const dmatrix &m, int column, int NSTACK)
Description not yet available.
Definition: dmsort.cpp:17
dmatrix atlas_solve_spd_trans(const dmatrix &M, const dmatrix &x)
prnstream & endl(prnstream &)
test_smartlist list
Definition: df1b2fun.h:753
static void set_active_only_random_effects(void)
Definition: model.cpp:251
#define min(a, b)
Definition: cbivnorm.cpp:188
fixed_smartlist2 nlist3
Definition: df1b2fun.h:758
double calculate_importance_sample_funnel(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
Definition: df1b2impf.cpp:23
fixed_smartlist2 nlist2
Definition: df1b2fun.h:756
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
double get_elapsed_time_and_reset(void)
Returns elapsed time in milliseconds of timer object and then resets the timer to current time...
Definition: timer.cpp:31
static objective_function_value * pobjfun
Definition: admodel.h:2394
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
#define M
Definition: rngen.cpp:57
int indexmax() const
Definition: ivector.h:104
Description not yet available.
Definition: fvar.hpp:2819
double norm2(const d3_array &a)
Return sum of squared elements in a.
Definition: d3arr2a.cpp:167
test_smartlist list3
Definition: df1b2fun.h:757
Description not yet available.
Definition: df1b2fun.h:373
static int minder
Definition: df1b2fun.h:291
double evaluate_function_quiet(const dvector &x, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lap.cpp:2137
static int set_index(void)
Definition: df1b2f15.cpp:90
Description not yet available.
Description not yet available.
Definition: admodel.h:1850
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
virtual void user_function()
Definition: xmodelm4.cpp:43
void get_second_ders(int xs, int us, const init_df1b2vector y, dmatrix &Hess, dmatrix &Dux, df1b2_gradlist *f1b2gradlist, function_minimizer *pfmin)
static void get_cHessian_contribution(dmatrix, int)
Description not yet available.
Definition: quadpri.cpp:591
void safe_allocate(int ncl, int ncu)
Description not yet available.
Definition: ivector.cpp:78
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1967
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
static int no_atlas_flag
Definition: fvar.hpp:8840
static int stddev_scale(const dvector &d, const dvector &x)
Definition: model.cpp:202
#define max(a, b)
Definition: cbivnorm.cpp:189
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1916
static double fun_without_pen
Definition: df1b2fun.h:1694
void reset(void)
Description not yet available.
Definition: df1b2fn2.cpp:581
static int first_hessian_flag
Definition: admodel.h:1855
test_smartlist list2
Definition: df1b2fun.h:755
dmatrix choleski_decomp_positive(const dmatrix &MM, double bound)
Description not yet available.
Definition: dmat35.cpp:33
void df1b2_gradcalc1(void)
Description not yet available.
Definition: df1b2fun.cpp:145
static void set_inactive_only_random_effects(void)
Definition: model.cpp:259
void check_derivatives(const dvector &, function_minimizer *pfmin, double fval1)
Description not yet available.
Definition: df1b2chkder.cpp:42
static int separable_calculation_type
Definition: df1b2fun.h:1352
static int in_qp_calculations
Definition: df1b2fun.h:1909