ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2lmn2.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,int nsteps)
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 << "Usage -fntrap option needs two non-negative integers -- ignored"
35  << endl;
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  fmmt1 fmc(negdirections ? negdirections->indexmax() : nvar, nsteps);
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  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=0;
241 
242  for (int i=1;i<df1b2variable::adpool_counter;i++)
243  {
247  }
248  df1b2variable::adpool_counter=0;
249  }
250  lapprox=new laplace_approximation_calculator(nvar,unvar,1,nvar+unvar,
251  this);
252  if (!lapprox)
253  {
254  cerr << "Error allocating memory for lapprox" << endl;
255  ad_exit(1);
256  }
258 
260  allocate();
262 
267 
269  //vmon_begin();
270  // see what kind of hessian we are dealing with
272  {
274  {
275  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-newht",nopt))>-1)
276  {
278  }
279  else
280  {
282  }
283  std::ostream& output_stream = get_output_stream();
284  output_stream << "Hessian type = " << lapprox->hesstype << endl;
285  }
286  }
287 
288  /*
289  cout << "NEED to remove !!!! " << endl;
290  initial_df1b2params::separable_flag=0;
291  lapprox->hesstype=1;
292  */
293 
294  // linear mixed effects optimization
296  {
298  }
299 
300  if (negdirections==0)
301  {
302  while (fmc.ireturn>=0)
303  {
304  fmc.fmin(f,x,g);
305  if (fmc.ireturn>0)
306  {
307  if (ifn_trap)
308  {
309  if (ifn_trap==fmc.ifn && itn_trap == fmc.itn)
310  {
311  cout << "we are here" << endl;
312  }
313  }
314  g=(*lapprox)(x,f,this);
315  /* don't have this in official yet
316  if (bad_step_flag==1)
317  {
318  g=1.e+4;
319  f=100.*fmc.fbest;
320  }
321  */
322  if (lapprox->init_switch==0)
323  {
324  if (f<fmc.fbest)
325  {
327  }
328  }
329  }
330  }
331  }
332  else
333  {
334  int i;
335  int nx=negdirections->indexmax();
336  independent_variables u(1,nx);
337  dvector x0(1,x.indexmax());
338  x0=x;
339  dvector gu(1,nx);
340  for (i=1;i<=nx;i++) u(i)=1.e-3;
341  while (fmc.ireturn>=0)
342  {
343  fmc.fmin(f,u,gu);
344  if (fmc.ireturn>0)
345  {
346  x=x0;
347  for (i=1;i<=nx;i++)
348  {
349  x+=u(i)*(*negdirections)(i);
350  }
351  g=(*lapprox)(x,f,this);
352  gu=(*negdirections)*g;
353  }
354  }
355  x=x0;
356  for (i=1;i<=nx;i++)
357  {
358  x+=u(i)*(*negdirections)(i);
359  }
360  delete negdirections;
361  negdirections=0;
362  }
364  }
365 
367  {
368  delete funnel_init_var::py;
370  }
372  ffbest=fmc.fbest;
373  g=fmc.gbest(1,fmc.gbest.indexmax());
374  iexit=fmc.iexit;
375  ifn=fmc.ifn;
376  ihflag=fmc.ihflag;
377  ihang=fmc.ihang;
378  maxfn_flag=fmc.maxfn_flag;
379  quit_flag=fmc.quit_flag;
381 } // 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
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
Description not yet available.
void limited_memory_quasi_newton_block(int nvar, int _crit, independent_variables &x, const dvector &_g, const double &_f, int nsteps)
Description not yet available.
Definition: df1b2lmn2.cpp:23
#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
static int no_stuff
Definition: df1b2lmn2.cpp:17
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.
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
Description not yet available.
Definition: fvar.hpp:3335
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
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