ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2qnm.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 <sstream>
12 using std::istringstream;
13 
14 # include <admodel.h>
15 # include <df1b2fun.h>
16 # include <adrndeff.h>
17 static int no_stuff=0;
18 
24  independent_variables& x,const dvector& _g,const double& _f)
25 {
26  int ifn_trap=0;
27  int itn_trap=0;
28  int on=0;
29  int nopt=0;
30  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-fntrap",nopt))>-1)
31  {
32  if (nopt !=2)
33  {
34  cerr <<
35  "Usage -fntrap option needs two non-negative integers -- ignored.\n";
36  }
37  else
38  {
39  ifn_trap=atoi(ad_comm::argv[on+1]);
40  itn_trap=atoi(ad_comm::argv[on+2]);
41  }
42  }
43 
44  double & f= (double&)_f;
45  dvector & g= (dvector&)_g;
46  // *********************************************************
47  // block for quasi-newton minimization
48  //int itnold=0;
49  fmm fmc(negdirections ? negdirections->indexmax() : nvar);
50  int on1;
51  if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-nox"))>-1)
52  {
53  fmc.noprintx=1;
54  }
55  fmc.maxfn= maxfn;
56  if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-dd",nopt))>-1)
57  {
58  if (!nopt)
59  {
60  cerr << "Usage -iprint option needs integer -- ignored" << endl;
61  //fmc.iprint=iprint;
62  }
63  else
64  {
65  int jj=atoi(ad_comm::argv[on1+1]);
66  fmc.dcheck_flag=jj;
67  }
68  }
69  nopt=0;
70  if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-iprint",nopt))>-1)
71  {
72  if (!nopt)
73  {
74  cerr << "Usage -iprint option needs integer -- ignored" << endl;
75  fmc.iprint=iprint;
76  }
77  else
78  {
79  int jj=atoi(ad_comm::argv[on1+1]);
80  fmc.iprint=jj;
81  }
82  }
83  else
84  {
85  fmc.iprint= iprint;
86  }
87  fmc.crit = crit;
88  fmc.imax = imax;
89  fmc.dfn= dfn;
90 
91  double _dfn=1.e-2;
92  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-dfn",nopt))>-1)
93  {
94  if (!nopt)
95  {
96  cerr << "Usage -dfn option needs number -- ignored" << endl;
97  }
98  else
99  {
100  istringstream ist(ad_comm::argv[on+1]);
101  ist >> _dfn;
102 
103  if (_dfn<=0)
104  {
105  cerr << "Usage -dfn option needs positive number -- ignored" << endl;
106  _dfn=0.0;
107  }
108  }
109  }
110  if (_dfn>0)
111  {
112  fmc.dfn=_dfn;
113  }
114 
115  fmc.scroll_flag= scroll_flag;
116  fmc.min_improve=min_improve;
118  // set convergence criterion for this phase
119  if (_crit)
120  {
121  fmc.crit = _crit;
122  }
123  if (!(!convergence_criteria))
124  {
127  fmc.crit=convergence_criteria(ind);
128  }
130  {
133  fmc.maxfn= (int) maximum_function_evaluations(ind);
134  }
135  int unvar=1;
137  {
139  //cout << nvar << endl;
140  unvar=initial_params::nvarcalc(); // get the number of active
143  int nvar1=initial_params::nvarcalc(); // get the number of active
144  if (nvar1 != nvar)
145  {
146  cerr << "failed sanity check in "
147  "void function_minimizer::quasi_newton_block" << endl;
148  ad_exit(1);
149  }
150  }
151 
152  if (!random_effects_flag || !unvar)
153  {
156  gradcalc(0,g);
157  if (negdirections==0)
158  {
159  while (fmc.ireturn>=0)
160  {
161  fmc.fmin(f,x,g);
162  if (fmc.ireturn>0)
163  {
164  dvariable vf=0.0;
169  {
171  }
173  f=value(vf);
174  gradcalc(nvar,g);
175  }
176  }
177  }
178  else
179  {
180  int i;
181  int nx=negdirections->indexmax();
182  independent_variables u(1,nx);
183  dvector gu(1,nx);
184  for (i=1;i<=nx;i++) u(i)=1.e-3;
185  dvector x0(1,x.indexmax());
186  x0=x;
187  while (fmc.ireturn>=0)
188  {
189  fmc.fmin(f,u,gu);
190  if (fmc.ireturn>0)
191  {
192  dvariable vf=0.0;
193  x=x0;
194  for (i=1;i<=nx;i++)
195  {
196  x+=u(i)*(*negdirections)(i);
197  }
202  {
204  }
206  f=value(vf);
207  gradcalc(nvar,g);
208  gu=(*negdirections)*g;
209  }
210  }
211  x=x0;
212  for (i=1;i<=nx;i++)
213  {
214  x+=u(i)*(*negdirections)(i);
215  }
216  delete negdirections;
217  negdirections=0;
218  }
219  }
220  else
221  {
222  // calculate the number of random effects unvar
223  // this turns on random effects variables and turns off
224  // everything else
225  //cout << nvar << endl;
227  //cout << nvar << endl;
228  int _unvar=initial_params::nvarcalc(); // get the number of active
229  //df1b2_gradlist::set_no_derivatives();
230 
232  {
233  delete funnel_init_var::py;
235  }
236  if (lapprox)
237  {
238  delete lapprox;
239  lapprox = NULL;
240 
241  deallocate();
243 
244  for (int i=1;i<df1b2variable::adpool_counter;i++)
245  {
249  }
250  df1b2variable::adpool_counter=0;
251  }
252  lapprox=new laplace_approximation_calculator(nvar,_unvar,1,nvar+_unvar,
253  this);
254  if (lapprox == NULL)
255  {
256  cerr << "Error allocating memory for lapprox" << endl;
257  ad_exit(1);
258  }
260 
262  allocate();
264 
269 
271  //vmon_begin();
272  // see what kind of hessian we are dealing with
274  {
276  {
277  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-newht",nopt))>-1)
278  {
280  }
281  else
282  {
284  }
285 
286  std::ostream& output_stream = get_output_stream();
287  // Print Hessian type and related info
288  switch(lapprox->hesstype)
289  {
290  case 1:
291  output_stream << "Hessian type 1 " << endl;
292  break;
293  case 2:
294  output_stream << "\nBlock diagonal Hessian (Block size =" <<
295  (lapprox->usize)/(lapprox->num_separable_calls) << ")\n" << endl;
296  break;
297  case 3:
298  output_stream << "\nBanded Hessian (Band width = " << lapprox->bw << ")\n"
299  << endl;
300  break;
301  case 4:
302  output_stream << "Hessian type 4 " << endl;
303  break;
304  default:
305  cerr << "This can't happen" << endl;
306  ad_exit(1);
307  }
308  }
309  }
310 
311  /*
312  cout << "NEED to remove !!!! " << endl;
313  initial_df1b2params::separable_flag=0;
314  lapprox->hesstype=1;
315  */
316 
317  // linear mixed effects optimization
319  {
320 #ifdef DIAG
321  if (!lapprox)
322  {
323  cerr << "this can't happen" << endl;
324  ad_exit(1);
325  }
326 #endif
328  }
329 
330  if (negdirections==0)
331  {
332  while (fmc.ireturn>=0)
333  {
334  fmc.fmin(f,x,g);
335  if (fmc.ireturn>0)
336  {
337  if (ifn_trap)
338  {
339  if (ifn_trap==fmc.ifn && itn_trap == fmc.itn)
340  {
341  cout << "we are here" << endl;
342  }
343  }
344  g=(*lapprox)(x,f,this);
345  if (bad_step_flag==1)
346  {
347  g=1.e+4;
348  f=2.*fmc.fbest;
349  bad_step_flag=0;
350  }
351 
352  if (lapprox->init_switch==0)
353  {
354  if (f<fmc.fbest)
355  {
357  }
358  }
359  }
360  }
361  }
362  else
363  {
364  int i;
365  int nx=negdirections->indexmax();
366  independent_variables u(1,nx);
367  dvector x0(1,x.indexmax());
368  x0=x;
369  dvector gu(1,nx);
370  for (i=1;i<=nx;i++) u(i)=1.e-3;
371  while (fmc.ireturn>=0)
372  {
373  fmc.fmin(f,u,gu);
374  if (fmc.ireturn>0)
375  {
376  x=x0;
377  for (i=1;i<=nx;i++)
378  {
379  x+=u(i)*(*negdirections)(i);
380  }
381  g=(*lapprox)(x,f,this);
382  gu=(*negdirections)*g;
383  }
384  }
385  x=x0;
386  for (i=1;i<=nx;i++)
387  {
388  x+=u(i)*(*negdirections)(i);
389  }
390  delete negdirections;
391  negdirections=0;
392  }
394  }
395 
397  {
398  delete funnel_init_var::py;
400  }
402  ffbest=fmc.fbest;
403  g=fmc.gbest(1,fmc.gbest.indexmax());
404  iexit=fmc.iexit;
405  ifn=fmc.ifn;
406  ihflag=fmc.ihflag;
407  ihang=fmc.ihang;
408  maxfn_flag=fmc.maxfn_flag;
409  quit_flag=fmc.quit_flag;
411 } // end block for quasi newton minimization
static adpool * pool
Definition: df1b2fun.h:273
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
static dvar_vector * variance_components_vector
Definition: adrndeff.h:204
static void reset_all(const dvector &)
Description not yet available.
Definition: df1b2f18.cpp:17
static adpool * adpool_vector[]
Definition: df1b2fun.h:282
static void set_no_derivatives(void)
Definition: df1b2fun.h:760
Description not yet available.
Definition: adrndeff.h:182
virtual void deallocate()
Definition: admodel.h:1904
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
Description not yet available.
#define x
Vector of double precision numbers.
Definition: dvector.h:50
static void xinit_all(const dvector &x)
Description not yet available.
Definition: nvarcall.cpp:31
static int separable_flag
Definition: df1b2fun.h:1351
int noprintx
Definition: fvar.hpp:3181
static unsigned int nvar_vector[]
Definition: df1b2fun.h:288
static char ** argv
Definition: fvar.hpp:8866
void get_hessian_components_banded_lme(function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lme.cpp:20
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
ADMB variable vector.
Definition: fvar.hpp:2172
static void restore_start_phase(void)
Definition: model.cpp:275
void check_hessian_type(const dvector &_x, function_minimizer *)
Description not yet available.
Definition: df1b2lp8.cpp:360
void check_hessian_type2(function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lp10.cpp:98
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
int atoi(adstring &s)
Returns a integer converted from input s.
Definition: atoi.cpp:20
static void save_varsptr(void)
Description not yet available.
Definition: df1b2fun.cpp:70
static int nvarcalc()
Definition: model.cpp:152
void deallocate(void)
Description not yet available.
Definition: adpool.cpp:319
static int current_phase
Definition: df1b2fun.h:1350
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
static int current_phase
Definition: admodel.h:842
static void set_active_only_random_effects(void)
Definition: model.cpp:251
static dvector maximum_function_evaluations
Definition: admodel.h:1917
#define min(a, b)
Definition: cbivnorm.cpp:188
static int argc
Definition: fvar.hpp:8863
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
static double gmax
Definition: admodel.h:2396
static objective_function_value * pobjfun
Definition: admodel.h:2394
Description not yet available.
Functions and variables for function minimizer.
Definition: fvar.hpp:3290
static int bad_step_flag
Definition: admodel.h:1853
static int nvarcalc_all(void)
Description not yet available.
Definition: nvarcall.cpp:17
static void set_inactive_random_effects(void)
Definition: model.cpp:288
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument (&quot;string&quot;).
Definition: optmatch.cpp:25
dmatrix * negdirections
Definition: admodel.h:1858
int indexmax() const
Definition: fvar.hpp:2921
std::ostream & get_output_stream()
Definition: adglobl.cpp:45
void quasi_newton_block(int nvar, int crit, independent_variables &x, const dvector &g, const double &f)
Description not yet available.
Definition: df1b2qnm.cpp:23
Description not yet available.
void reset_gradient_stack(void)
Rewind buffer.
Definition: sgradclc.cpp:367
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
static int no_stuff
Definition: df1b2qnm.cpp:17
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
double min_improve
Definition: admodel.h:1900
void pre_userfunction(void)
Definition: model7.cpp:586
static init_df1b2vector * py
Definition: df1b2fnl.h:68
static int adpool_counter
Definition: df1b2fun.h:279
virtual void allocate()
Definition: admodel.h:1903
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1916
static void restore_varsptr(void)
Description not yet available.
Definition: df1b2fun.cpp:90
static dvector convergence_criteria
Definition: admodel.h:1915
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
static void set_inactive_only_random_effects(void)
Definition: model.cpp:259
static int random_effects_flag
Definition: admodel.h:1857
static void get_M_calculations(void)
Description not yet available.
Definition: quadpri.cpp:30