ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
xfmmtr1.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  */
12 #ifdef __ZTC__
13  #include <conio.h>
14 #endif
15 
16 #include <admodel.h>
17 extern int ctlc_flag;
18 
19 #if defined(__TURBOC__)
20  #pragma hdrstop
21  #include <iostream.h>
22  #include <conio.h>
23 #endif
24 
25 #if defined (__WAT32__) || defined(_MSC_VER)
26  #include <conio.h>
27 #else
28  #include <iostream>
29  #include <signal.h>
30  // this is to get UNIX systems to use getchar
31  #define getch getchar
32 #endif
33 
34 #ifdef __ZTC__
35  #include <iostream.hpp>
36  #include <disp.h>
37  #define endl "\n"
38 // #define if (ad_printf) (*ad_printf) disp_if (ad_printf) (*ad_printf)
39 #endif
40 
41 #ifdef __SUN__
42  #include <iostream.h>
43  #include <signal.h>
44  #define getch getchar
45 #endif
46 
47 extern "C" void onintr(int k);
48 
49 #ifdef __NDPX__
50  #include <iostream.hxx>
51 #endif
52 
53 #include <math.h>
54 #include <stdlib.h>
55 #include <stdio.h>
56 #include <ctype.h>
57 
62 void do_evaluation(double& f,independent_variables& x,dvector& g,int nvar,
63  function_minimizer * pmp)
64 {
67  pmp->userfunction();
69  gradcalc(nvar,g);
70  f=value(vf);
71 }
72 
78  dvector& g,dvector & r,int nvar,function_minimizer * pmp)
79 {
80  const double stepsize=1.e-5;
81  dvector g1(1,nvar);
82  dvector g2(1,nvar);
83  x+=stepsize*r;
84  do_evaluation(f,x,g1,nvar,pmp);
85  x-=2.*stepsize*r;
86  do_evaluation(f,x,g2,nvar,pmp);
87  double scder=r*(g1-g2)/(2.0*stepsize);
88  cout << std::scientific < setprecision(10) << " f = " << f << endl;
89  cout << " second derivative = " ;
90  cout << " r*(g1-g)/stepsize = " << scder << endl;
91  return scder;
92 }
93 
94 
95 dvector update1(int nvar, int iter, int m, const dvector& g,
96  const dmatrix& xalpha, dmatrix& y, const dvector& x, const dvector& xold,
97  const dvector& gold, const dvector& xrho);
98 
99 double dafsqrt( double x );
100 
105 void fmmt1::fmin2(const double& _f, const independent_variables &_x,
106  const dvector& _g, function_minimizer *pmp)
107 {
108  //int itn=0; int bigbreak=0; int smallbreak=0; int midbreak=0;
109  int itn=0; int smallbreak=0; int midbreak=0;
110  int nvar=initial_params::nvarcalc(); // get the number of active
111  //double a,f, curf, rnorm, stepsize,b,epsilon;
112  double a,f, curf, stepsize,b,epsilon;
113  independent_variables x(1,nvar);
114  initial_params::xinit(x); // get the initial values into the
115  dvariable vf=0.0; // x vector
116  epsilon=1.e-2;
117 
118  dvector curx(1,nvar), g1(1,nvar), xtry(1,nvar), g(1,nvar), r(1,nvar);
119  do_evaluation(f,x,g,nvar,pmp); // get initial vales for f and g
120  curf=f; curx=x;
121  cout << std::scientific < setprecision(10) << " f = " << f << endl;
122 
123 
124  do
125  {
126  r=update1(n,itn,xm,g,xstep,xy,x,xold,gold,xrho); // get search
127 
128  cout << " norm(g) = " << norm(g) ;
129  cout << " r*g/norm(g) = " << r*g/norm(g) << endl;
130  do
131  {
132  x=curx;
133 
134  a=get_second_derivative(f,x,g,r,nvar,pmp);
135  b=r*g;
136 
137  stepsize=-b/a;
138  do
139  {
140  xtry=curx+stepsize*r; x=xtry;
141 
142  do_evaluation(f,x,g,nvar,pmp);
143  cout << std::scientific < setprecision(10) << " f = " << f << endl;
144  cout << " r*g/norm(g) = " << r*g/norm(g) << endl;
145 
146  if (f<curf+1.e-10)
147  {
148  curx=x; curf=f;
149  smallbreak=1;
150  if (fabs(g*r)/norm(g)<epsilon)
151  {
152  midbreak=1;
153  }
154  }
155  else
156  {
157  cout << setprecision(10) << f-curf << endl;
158  stepsize=0.001*stepsize; xtry=curx+stepsize*r;
159  }
160  }
161  while(!smallbreak);
162  smallbreak=0;
163  }
164  while (!midbreak);
165  midbreak=0;
166  itn++;
167  }
168  while (1);
169 
170  ad_exit(1);
171 }
172 
177 dvector update1(int nvar, int iter, int m, const dvector& g, const dmatrix& _s,
178  dmatrix& y, const dvector& x, const dvector& _xold, const dvector& _gold,
179  const dvector& _xrho)
180  {
181  dvector& xold= (dvector&) _xold;
182  dmatrix& s= (dmatrix&) _s;
183  dvector& gold= (dvector&) _gold;
184  dvector& xrho=(dvector&)_xrho;
185  dvector beta(1,nvar);
186  dvector alpha(0,m);
187  dvector r(1,nvar);
188  dvector t(1,nvar);
189  int m1=m+1;
190  if (iter<1)
191  {
192  xold=x;
193  gold=g;
194  r=g;
195  }
196  else
197  {
198  int k=iter-1;
199  int k1=k%(m1);
200  y(k1)=g-gold;
201  s(k1)=x-xold;
202  xrho(k1)=1./(y(k1)*s(k1));
203  xold=x;
204  gold=g;
205 
206  int i;
207  int lb=k-m+1;
208  if (lb <0) lb=0;
209  t=g;
210  for (i=k;i>=lb;i--)
211  {
212  int i1=i%(m1);
213  //int i2=(i+1)%(m1);
214  //if (i==k)
215  {
216  alpha(i-lb)=xrho(i1)*(s(i1)*t);
217  }
218  //else
219  //{
220  // alpha(i-lb)=xrho(i1)*( (s(i1)-(s(i1)*s(i2))*s(i2)) * t);
221  //}
222  t-=alpha(i-lb)*y(i1);
223  }
224  r=t;
225  for (i=lb;i<=k;i++)
226  {
227  int i1=i%(m1);
228  //r+= (alpha(i1)-xrho(i1)*(y(i1)*r)) * s(i1);
229  r+= (alpha(i-lb)-xrho(i1)*(y(i1)*r)) * s(i1);
230  }
231  }
232  //cout << r*g/(norm(r)*norm(g)) << endl;
233  r/=norm(r);
234  return -1.0*r;
235  }
#define x
Vector of double precision numbers.
Definition: dvector.h:50
dmatrix xy
Definition: fvar.hpp:3344
exitptr ad_exit
Definition: gradstrc.cpp:53
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
ADMB variable vector.
Definition: fvar.hpp:2172
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
static int nvarcalc()
Definition: model.cpp:152
double get_second_derivative(double f, independent_variables &x, dvector &g, dvector &r, int nvar, function_minimizer *pmp)
Description not yet available.
Definition: xfmmtr1.cpp:77
dvector update1(int nvar, int iter, int m, const dvector &g, const dmatrix &xalpha, dmatrix &y, const dvector &x, const dvector &xold, const dvector &gold, const dvector &xrho)
Description not yet available.
Definition: xfmmtr1.cpp:177
prnstream & endl(prnstream &)
Description not yet available.
Definition: fvar.hpp:1937
void fmin2(const double &f, const independent_variables &x, const dvector &g, function_minimizer *)
Description not yet available.
Definition: xfmmtr1.cpp:105
int xm
Definition: fvar.hpp:3340
static objective_function_value * pobjfun
Definition: admodel.h:2394
Description not yet available.
static void xinit(const dvector &x)
Definition: model.cpp:226
int itn
Definition: fvar.hpp:3353
dvariable beta(const prevariable &a, const prevariable &b)
Beta density function.
Definition: vbeta.cpp:18
Description not yet available.
Definition: fvar.hpp:2819
dmatrix xstep
Definition: fvar.hpp:3341
dvector gold
Definition: fvar.hpp:3346
dvector xrho
Definition: fvar.hpp:3342
double dafsqrt(double x)
Robust square root.
Definition: newfmin.cpp:1295
void do_evaluation(double &f, independent_variables &x, dvector &g, int nvar, function_minimizer *pmp)
Description not yet available.
Definition: xfmmtr1.cpp:62
Description not yet available.
Definition: admodel.h:1850
dvector xold
Definition: fvar.hpp:3345
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
virtual void userfunction(void)=0
int n
Definition: fvar.hpp:3361
int onintr(int *k)
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
int ctlc_flag
Definition: gradstrc.cpp:68