ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2lp2.cpp
Go to the documentation of this file.
1 /*
2  * $Id$
3  *
4  * Author: David Fournier
5  * Copyright (c) 2008-2012 Regents of the University of California
6  */
11 #include <admodel.h>
12 #include <df1b2fun.h>
13 #include <adrndeff.h>
14 #ifndef OPT_LIB
15  #include <cassert>
16  #include <climits>
17 #endif
18 void get_second_ders(int xs,int us,const init_df1b2vector y,dmatrix& Hess,
21 double calculate_laplace_approximation(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 static void xxx(ivector re_list,ivector fe_list){}
26 
32  (const dvector& _x,const double& _f,function_minimizer * pfmin)
33 {
34  // for use when there is no separability
36  ADUNCONST(double,f)
37 
40  initial_params::reset(x); // get current x values into the model
42 
44  //int lmn_flag=0;
45  if (!inner_lmnflag)
46  {
47  if (!ADqd_flag)
48  uhat=get_uhat_quasi_newton_block_diagonal(x,pfmin);
49  else
50  uhat=get_uhat_quasi_newton_qd(x,pfmin);
51  }
52  else
53  {
54  uhat=get_uhat_lm_newton(x,pfmin);
55  }
56  if (!allocated(scale))
57  {
58  scale.allocate(1,uhat.indexmax());
59  }
60  else
61  {
62  if (scale.indexmax() != uhat.indexmax())
63  {
64  scale.deallocate();
65  scale.allocate(1,uhat.indexmax());
66  }
67  }
68 
69  if (!allocated(curv))
70  {
71  curv.allocate(1,uhat.indexmax());
72  }
73  else
74  {
75  if (curv.indexmax() != uhat.indexmax())
76  {
77  curv.deallocate();
78  curv.allocate(1,uhat.indexmax());
79  }
80  }
81 
82  if (sparse_hessian_flag==0)
83  {
84  for (int i=1;i<=xsize;i++)
85  {
86  y(i)=x(i);
87  }
88  for (int i=1;i<=usize;i++)
89  {
90  y(i+xsize)=uhat(i);
91  }
92  }
93  else
94  {
95  for (int i=1;i<=xsize;i++)
96  {
97  value(y(i))=x(i);
98  }
99  for (int i=1;i<=usize;i++)
100  {
101  value(y(i+xsize))=uhat(i);
102  }
103  }
104 
105  std::ostream& output_stream = get_output_stream();
106  for(int ii=1;ii<=num_nr_iters;ii++)
107  {
108  {
109  // test newton raphson
110  //Hess.initialize();
111  /*int check=*/initial_params::stddev_scale(scale,uhat);
112  /*check=*/initial_params::stddev_curvscale(curv,uhat);
113  max_separable_g=0.0;
114  pmin->inner_opt_flag=1;
115  step=get_newton_raphson_info_block_diagonal(pfmin);
116  output_stream << "max separable g " << std::scientific << setprecision(10) << max_separable_g
117  << "\nNewton raphson " << std::fixed << ii << endl;
118  uhat+=step;
119 
120  evaluate_function(uhat,pfmin);
121  pmin->inner_opt_flag=0;
122  }
123 
124  if (sparse_hessian_flag==0)
125  {
126  for (int i=1;i<=usize;i++)
127  {
128  y(i+xsize)=uhat(i);
129  }
130  }
131  else
132  {
133  for (int i=1;i<=usize;i++)
134  {
135  value(y(i+xsize))=uhat(i);
136  }
137  }
138  }
139 
140  output_stream << std::scientific << setprecision(10) << initial_df1b2params::cobjfun << endl;
141  xadjoint.initialize();
142  uadjoint.initialize();
143  block_diagonal_flag=2;
144  used_flags.initialize();
146  if (use_gauss_hermite>0)
147  {
148  df1b2variable pen=0.0;
151  block_diagonal_flag=6;
152  num_separable_calls=0;
153  // get the block diagonal hessians to use in importance sampling
154  pfmin->user_function();
155  //cout << (*pfmin->lapprox->block_diagonal_hessian) << endl;
156  block_diagonal_flag=2;
158 
159  // do importance sampling and get ders bakc to Hessian adjoint
160  // new stuff for more than one random effect in each separable call
161  // Apr 17 07
162  if (multi_random_effects==0)
163  {
164  f=do_gauss_hermite_block_diagonal(x,uhat,Hess,xadjoint,
165  uadjoint,Hessadjoint,pfmin);
166  }
167  else
168  {
169  f=do_gauss_hermite_block_diagonal_multi(x,uhat,Hess,xadjoint,
170  uadjoint,Hessadjoint,pfmin);
171  }
172  int xmax=xadjoint.indexmax();
173  dvector x_con(1,xmax);
174  x_con.initialize();
175 #ifndef OPT_LIB
176  assert(nvar <= INT_MAX);
177 #endif
178  dvector dscale(1,(int)nvar); // need to get scale from somewhere
179  dscale=1.0;
180  /*int check=*/initial_params::stddev_scale(dscale,x);
181  dvector sscale=dscale(1,xsize);
182  // *******************************************************
183  // *******************************************************
184  // *******************************************************
185  // derivatives from hessian adjoint back
186  {
187  x_con.initialize();
188 
189  for (int i=1;i<=num_separable_calls;i++)
190  {
191  ivector& re_list=(*block_diagonal_re_list)(i);
192  ivector& fe_list=(*block_diagonal_fe_list)(i);
193  dmatrix& Dux=(*block_diagonal_Dux)(i);
194  dmatrix& H=(*block_diagonal_hessian)(i);
195  xxx(re_list,fe_list);
196  int mmax=re_list.indexmax();
197  dvector tmp(1,mmax);
198 
199  for (int j=1;j<=re_list.indexmax();j++)
200  {
201  tmp(j)=uadjoint(re_list(j)-xmax);
202  }
203 
204  if (allocated(fe_list))
205  {
206  if (allocated(H))
207  {
208  dvector tmp1=solve(H,tmp);
209  dvector xtmp=tmp1*Dux;
210  for (int j=1;j<=fe_list.indexmax();j++)
211  {
212  x_con(fe_list(j))+=xtmp(j);
213  }
214  }
215  }
216  }
218  {
219  x_con=elem_prod(x_con,sscale);
220  }
221  }
222  xadjoint-=x_con;
223  // *******************************************************
224  // *******************************************************
225  // *******************************************************
226 
227  block_diagonal_flag=3;
228  //pfmin->lapprox->xadjoint.initialize();
229  //pfmin->lapprox->uadjoint.initialize();
230  pfmin->lapprox->num_separable_calls=0;
235  //df1b2_gradlist::set_yes_derivatives();
236  //initial_df1b2params::reset(y,pen);
237  pfmin->user_function();
238  dvector lcx=elem_prod(check_local_xadjoint2,sscale);
239  xadjoint+=lcx;
240  //df1b2_gradlist::set_no_derivatives();
242  block_diagonal_flag=0;
244  }
245  else if (num_importance_samples>0)
246  {
247  df1b2variable pen=0.0;
250  block_diagonal_flag=6;
251  num_separable_calls=0;
252  // get the block diagonal hessians to use in importance sampling
253  pfmin->user_function();
254  //cout << (*pfmin->lapprox->block_diagonal_hessian) << endl;
255  block_diagonal_flag=2;
257  // do importance sampling and get ders bakc to Hessian adjoint
258  if (isfunnel_flag==0)
259  {
260  if (antiflag==0)
261  {
263  xadjoint,uadjoint,Hessadjoint,pfmin);
264  }
265  else
266  {
268  uhat,Hess,xadjoint,uadjoint,Hessadjoint,pfmin);
269  }
270  }
271  else
272  {
274  uadjoint,Hessadjoint,pfmin);
275  }
276 
277  int xmax=xadjoint.indexmax();
278  dvector x_con(1,xmax);
279  x_con.initialize();
280 #ifndef OPT_LIB
281  assert(nvar <= INT_MAX);
282 #endif
283  dvector dscale(1,(int)nvar); // need to get scale from somewhere
284  dscale=1.0;
285  /*int check=*/initial_params::stddev_scale(dscale,x);
286  dvector sscale=dscale(1,xsize);
287  // *******************************************************
288  // *******************************************************
289  // *******************************************************
290  // derivatives from hessian adjoint back
291  {
292  x_con.initialize();
293 
294  for (int i=1;i<=num_separable_calls;i++)
295  {
296  dmatrix& H=(*block_diagonal_hessian)(i);
297  if (allocated(H))
298  {
299  ivector& re_list=(*block_diagonal_re_list)(i);
300  ivector& fe_list=(*block_diagonal_fe_list)(i);
301  dmatrix& Dux=(*block_diagonal_Dux)(i);
302  xxx(re_list,fe_list);
303  int mmax=re_list.indexmax();
304  dvector tmp(1,mmax);
305 
306  for (int j=1;j<=re_list.indexmax();j++)
307  {
308  tmp(j)=uadjoint(re_list(j)-xmax);
309  }
310 
311  if (allocated(fe_list))
312  {
313  dvector tmp1=solve(H,tmp);
314  dvector xtmp=tmp1*Dux;
315  for (int j=1;j<=fe_list.indexmax();j++)
316  {
317  x_con(fe_list(j))+=xtmp(j);
318  }
319  }
320  }
321  }
323  {
324  //for (i=1;i<=usize;i++)
325  //{
326  // Dux(i)=elem_prod(Dux(i),sscale);
327  //}
328  x_con=elem_prod(x_con,sscale);
329  }
330  }
331  xadjoint-=x_con;
332  // *******************************************************
333  // *******************************************************
334  // *******************************************************
335 
336  block_diagonal_flag=3;
337  //pfmin->lapprox->xadjoint.initialize();
338  //pfmin->lapprox->uadjoint.initialize();
339  pfmin->lapprox->num_separable_calls=0;
344  //df1b2_gradlist::set_yes_derivatives();
345  //initial_df1b2params::reset(y,pen);
346  pfmin->user_function();
347  dvector lcx=elem_prod(check_local_xadjoint2,sscale);
348  xadjoint+=lcx;
349  //df1b2_gradlist::set_no_derivatives();
351  block_diagonal_flag=0;
353  }
354  else
355  {
357  {
358  // need hessin of random effects for stddeV report
359  df1b2variable pen=0.0;
362  block_diagonal_flag=6;
363  allocate_block_diagonal_stuff();
364  num_separable_calls=0;
365  // get the block diagonal hessians to use in importance sampling
366  pfmin->user_function();
367  //cout << (*pfmin->lapprox->block_diagonal_hessian) << endl;
368  block_diagonal_flag=2;
369  }
371  df1b2variable pen=0.0;
372  if (saddlepointflag==2)
373  {
374  pmin->inner_opt_flag=0;
375  f=get_fx_fu(pfmin);
376  }
378  pmin->inner_opt_flag=1;
379  pfmin->pre_user_function();
380  pmin->inner_opt_flag=0;
382  if (saddlepointflag!=2)
383  {
385  }
386  else
387  {
388  xadjoint=(*grad_x_u)(1,xsize)-(*grad_x);
389  }
390 
391  if (saddlepointflag!=2 && pmin->multinomial_weights==0)
392  {
393  f-=usize*.91893853320467241;
394  }
395 
397  block_diagonal_flag=0;
398  dvector scale1(1,xsize); // need to get scale from somewhere
400  /*int check=*/initial_params::stddev_scale(scale1,x);
401  for (int i=1;i<=xadjoint.indexmax();i++)
402  xadjoint(i)*=scale1(i);
403  }
404  //cout << initial_df1b2params::cobjfun << endl;
405  //f=initial_df1b2params::cobjfun;
406  return xadjoint;
407 }
408 
415 {
417  if (allocated(used_flags))
418  {
419  if (used_flags.indexmax() != nv)
420  {
421  used_flags.safe_deallocate();
422  }
423  }
424  if (!allocated(used_flags))
425  {
426  used_flags.safe_allocate(1,nv);
427  }
428 
429  //for (ip=1;ip<=num_der_blocks;ip++)
430  {
431  used_flags.initialize();
432 
433  // do we need to reallocate memory for df1b2variables?
434  int ip = 0;
435  check_for_need_to_reallocate(ip);
436 
438  //cout << re_objective_function_value::pobjfun << endl;
439  //cout << re_objective_function_value::pobjfun->ptr << endl;
440  (*re_objective_function_value::pobjfun)=0;
441  df1b2variable pen=0.0;
442  df1b2variable zz=0.0;
443 
445 
446  // call function to do block diagonal newton-raphson
447  // the step vector from the newton-raphson is in the vector step
449 
451  //cout << funnel_init_var::lapprox << endl;
452  block_diagonal_flag=1;
453  pfmin->pre_user_function();
454  //pfmin->user_function();
456  block_diagonal_flag=0;
457  }
458  return step;
459 }
static int straight_through_flag
Definition: admodel.h:839
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
static void reset(const init_df1b2vector &, const df1b2variable &)
Description not yet available.
Definition: df1b2f15.cpp:51
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
static void set_no_derivatives(void)
Definition: df1b2fun.h:760
Description not yet available.
Definition: adrndeff.h:182
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
Description not yet available.
static int stddev_curvscale(const dvector &d, const dvector &x)
Definition: model.cpp:214
static double cobjfun
Definition: df1b2fun.h:1360
#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
int ADqd_flag
Definition: model.cpp:28
static int separable_flag
Definition: df1b2fun.h:1351
dvector get_newton_raphson_info_block_diagonal(function_minimizer *pmin)
Description not yet available.
Definition: df1b2lp2.cpp:414
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
Description not yet available.
Definition: df1b2fun.h:745
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
Description not yet available.
Definition: df1b2fun.h:266
dvector block_diagonal_calculations(const dvector &_x, const double &_f, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lp2.cpp:32
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
static laplace_approximation_calculator * lapprox
Definition: df1b2fnl.h:61
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
static void set_active_only_random_effects(void)
Definition: model.cpp:251
double calculate_importance_sample_block_diagonal_option_antithetical(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: df1b2im5.cpp:24
Description not yet available.
double do_gauss_hermite_block_diagonal_multi(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: df1b2ghmult.cpp:23
void pre_user_function(void)
Description not yet available.
Definition: df1b2lp8.cpp:370
int indexmax() const
Definition: ivector.h:104
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
static void xxx(ivector re_list, ivector fe_list)
Definition: df1b2lp2.cpp:25
std::ostream & get_output_stream()
Definition: adglobl.cpp:45
Description not yet available.
Definition: df1b2fun.h:373
double do_gauss_hermite_block_diagonal(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: df1b2gh.cpp:23
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)
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
double calculate_importance_sample_block_diagonal_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: df1b2im3f.cpp:23
static int stddev_scale(const dvector &d, const dvector &x)
Definition: model.cpp:202
static int first_hessian_flag
Definition: admodel.h:1855
double calculate_importance_sample_block_diagonal_option2(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: df1b2im4.cpp:23
static void set_inactive_only_random_effects(void)
Definition: model.cpp:259