ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
randeff.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  */
7 #include <admodel.h>
8 
9 #if defined(max)
10  #undef max
11 #endif
12 
13 #ifdef ISZERO
14  #undef ISZERO
15 #endif
16 #define ISZERO(d) ((d)==0.0)
17 
18 typedef int integer;
19 typedef long int logical;
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 int xlbfgs_(integer *n, integer *m, dvar_vector & x, dvariable & f,
26  dvar_vector & g, logical *diagco, dvar_vector & diag, integer *iprint,
27  double* eps, double* xtol, dvar_vector & w, integer *iflag, integer* iter);
28 
29 #ifdef __cplusplus
30 }
31 #endif
32 
34 {
35  integer m=5;
36  double crit=0.0;
37  //double crit=1.e-3;
38  int maxfn=400;
39  int maxiter=50;
40  int iprint = -defaults::iprint;
41 
42  dvar_vector& x = (dvar_vector&)(_x);
43 
44  integer nvar=x.indexmax()-x.indexmin()+1; // get the number of active
45  if (m<=0)
46  {
47  cerr << "illegal value for number of steps in limited memory"
48  " quasi-newton method -- set equal to 10" << endl;
49  }
50  integer noprintx=0;
51  int on;
52  int maxfn_option=0;
53  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-maxfn"))>-1)
54  {
55  maxfn_option=1;
56  }
57  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-nox"))>-1)
58  {
59  noprintx=1;
60  }
61  double _crit=0;
62  integer itn=0;
63  int ifn=0;
64  int nopt = 0;
65  // set the convergence criterion by command line
66  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-crit",nopt))>-1)
67  {
68  if (!nopt)
69  {
70  cerr << "Usage -crit option needs number -- ignored" << endl;
71  }
72  else
73  {
74  char * end;
75  _crit=strtod(ad_comm::argv[on+1],&end);
76  if (_crit<=0)
77  {
78  cerr << "Usage -crit option needs positive number -- ignored" << endl;
79  _crit=0.0;
80  }
81  }
82  }
84  // set convergence criterion for this phase
86  if (!(!convergence_criteria))
87  {
88  int ind=min(convergence_criteria.indexmax(),
90  crit=convergence_criteria(ind);
91  }
92  if (!ISZERO(_crit))
93  {
94  crit = _crit;
95  }
97  if (!(!maximum_function_evaluations) && !maxfn_option)
98  {
99  int ind=min(maximum_function_evaluations.indexmax(),
101  maxfn= (int) maximum_function_evaluations(ind);
102  }
103  dvariable vf=0.0;
104 
105  double xtol;
106  dvariable f;
107  dvar_vector diag(1,nvar);
108  //int j, n, icall;
109  int icall;
110  integer iflag;
111  dvariable fbest=1.e+100;
112  dvar_vector g(1,nvar);
113  dvar_vector xbest(1,nvar);
114  dvar_vector gbest(1,nvar);
115  g.initialize();
116  //double t1, t2;
117  long int diagco = 0;
118  integer iprintx[2];
119  //double epsx;
120  //m = 35;
121  dvar_vector w(1,nvar+2*m+2*nvar*m);
122  iprintx[0] = iprint;
123  iprintx[1] = 0;
124  crit = 1e-5;
125  xtol = 1e-16;
126  icall = 0;
127  iflag = 0;
128 
129 L20:
130  f = 0.;
131  //vf=initial_params::reset(dvar_vector(x));
132  f=user_randeff(x);
133  ifn++;
134  if (f<fbest)
135  {
136  fbest=f;
137  xbest=x;
138  gbest=g;
139  }
140 
141  //gradcalc(nvar,g);
142  g=user_dfrandeff(x);
143 
144 #if defined(USE_DDOUBLE)
145 #undef double
146  if(fmod(double(itn),double(iprint)) == 0)
147 #define double dd_real
148 #else
149  if(fmod(double(itn),double(iprint)) == 0)
150 #endif
151  {
152  if (iprint>0)
153  {
154  if (!itn)
155  {
156  ad_printf("\nInitial statistics: ");
157  }
158  else
159  {
160  ad_printf("\nIntermediate statistics: ");
161  }
162 
163  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
164  nvar, itn, ifn);
165 
166  if (!itn)
167  {
168  double xf=value(f);
169  double xg=max(value(g));
170  ad_printf(
171  "Function value %12.4le; maximum gradient component mag %12.4le\n",
172  xf, xg);
173  }
174  else
175  {
176  double xf=value(fbest);
177  double xg=max(value(gbest));
178  ad_printf(
179  "Function value %12.4le; maximum gradient component mag %12.4le\n",
180  xf, xg);
181  }
182  if (!noprintx)
183  {
184  if (!itn)
185  fmmdisp(value(x), value(g), nvar, 0,noprintx);
186  else
187  fmmdisp(value(xbest), value(gbest), nvar, 0,noprintx);
188  }
189  }
190  }
191  crit=1.e-15;
192  xlbfgs_(&nvar, &m, x , f, g, &diagco, diag,
193  iprintx, &crit, &xtol, w, &iflag,&itn);
194 
195  if (iflag <= 0)
196  {
197  goto L50;
198  }
199  if (iflag > 1)
200  {
201  goto L50;
202  }
203  ++icall;
204  if (icall > maxfn)
205  if (itn > maxiter)
206  {
207  cout << "Exceeded maxiter" << endl;
208  goto L50;
209  }
210  goto L20;
211 L50:
212  if (f<fbest)
213  {
214  fbest=f;
215  xbest=x;
216  gbest=g;
217  }
218  if (iprint>0)
219  {
220  double xf=value(f);
221  double xg=max(value(g));
222  ad_printf("\nfinal statistics: ");
223  ad_printf("%d variables; iteration %ld; function evaluation %ld\n",
224  nvar, itn, ifn);
225  ad_printf(
226  "Function value %12.4le; maximum gradient component mag %12.4le\n",
227  xf, xg);
228  fmmdisp(value(x),value(g), nvar, 0,noprintx);
229  }
230  //gradient_structure::set_NO_DERIVATIVES();
231  //objective_function_value::gmax=fabs(max(value(gbest)));
232  //user_d2frandeff(x);
233  int sgn=1;
235  dvariable ld=ln_det(hess,sgn);
236  if (sgn==-1)
237  {
238  dmatrix v=eigenvectors(value(hess));
239  dvector e=eigenvalues(value(hess));
240  cout << endl << e << endl;
241  for (int i=e.indexmin();i<=e.indexmax();i++)
242  {
243  if (e(i)<0.0)
244  cout << i << " " << v(i) << endl;
245  }
246  }
247  x=xbest;
248  return fbest+0.5*ln_det(user_d2frandeff(xbest),sgn);
249 }
void fmmdisp(const dvector &x, const dvector &g, const int &nvar, int scroll_flag, int noprintx)
Description not yet available.
Definition: fmm_disp.cpp:102
dvector eigenvalues(const banded_symmetric_dmatrix &_SS)
Description not yet available.
Definition: dmat28.cpp:411
#define x
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
static char ** argv
Definition: fvar.hpp:8866
dvariable random_effects_maximization(const dvar_vector &v)
Definition: randeff.cpp:33
const int iprint
Definition: fvar.hpp:9504
ADMB variable vector.
Definition: fvar.hpp:2172
virtual dvar_vector user_dfrandeff(const dvar_vector &x)
Definition: model37.cpp:18
ivector sgn(const dvector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dvect24.cpp:11
#define ISZERO(d)
Definition: randeff.cpp:16
dmatrix symmetrize(const dmatrix &matrix)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat12.cpp:14
prnstream & endl(prnstream &)
dmatrix eigenvectors(const banded_symmetric_dmatrix &_SS, const dvector &_e)
Description not yet available.
Definition: dmat28.cpp:442
static int current_phase
Definition: admodel.h:842
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
Description not yet available.
Description not yet available.
Definition: fvar.hpp:2819
int xlbfgs_(integer *n, integer *m, dvar_vector &x, dvariable &f, dvar_vector &g, logical *diagco, dvar_vector &diag, integer *iprint, double *eps, double *xtol, dvar_vector &w, integer *iflag, integer *iter)
double ln_det(const dmatrix &m1, int &sgn)
Compute log determinant of a constant matrix.
Definition: dmat3.cpp:536
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
int indexmin() const
Definition: fvar.hpp:2287
long int integer
Definition: cbivnorm.cpp:31
long int logical
Definition: cbivnorm.cpp:39
virtual dvar_matrix user_d2frandeff(const dvar_vector &x)
Definition: model37.cpp:26
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
#define w
double eps
Definition: ftweak.cpp:13
virtual dvariable user_randeff(const dvar_vector &x)
Definition: model37.cpp:9
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
void initialize(const dvector &ww)
Description not yet available.
Definition: fvar_a24.cpp:63
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
#define max(a, b)
Definition: cbivnorm.cpp:189
static dvector convergence_criteria
Definition: admodel.h:1915
int indexmax() const
Definition: fvar.hpp:2292
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487