ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
dflocmin.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 /*
15  int fcount =0;
16 static int no_stuff=0;
17 
18 double evaluate_function(const dvector& x,function_minimizer * pfmin);
19 void get_newton_raphson_info(int xs,int us,const init_df1b2vector _y,dmatrix& Hess,
20  dvector& grad, df1b2_gradlist * f1b2gradlist,function_minimizer * pfmin);
21 
22 //dvariable AD_uf_inner(const dvector& x,const dvar_vector& u);
23 void get_second_ders(int xs,int us,const init_df1b2vector y,dmatrix& Hess,
24  dmatrix& Dux, df1b2_gradlist * f1b2gradlist,function_minimizer * pfmin,
25  laplace_approximation_calculator* lap);
26 
27  double re_objective_function_value::fun_without_pen=0;
28 
29 int laplace_approximation_calculator::saddlepointflag=0;
30 int laplace_approximation_calculator::print_importance_sampling_weights_flag=0;
31 
32 int laplace_approximation_calculator::where_are_we_flag=0;
33 dvar_vector *
34  laplace_approximation_calculator::variance_components_vector=0;
35 */
36 
42 (dvector& s,dmatrix& H,dvector& grad,double lambda)
43 {
44  dvector vbest(1,usize);
45  vbest.initialize();
46 
47  int better_flag=0;
48  int counter=0;
49  s.initialize();
50  s(1)=1.0;
51  double fbest=evaluate_function_no_derivatives(uhat,pmin);
52  std::ostream& output_stream = get_output_stream();
53  do
54  {
55  dvector v=local_minimization_routine(s,H,grad,lambda);
56  dvector xx=uhat+v;
57  double f2=evaluate_function_no_derivatives(xx,pmin);
58  output_stream << endl << fbest-f2 << endl;
59  if(f2<fbest)
60  {
61  better_flag=1;
62  fbest=f2;
63  lambda*=5.0;
64  vbest=v;
65  s=v;
66  }
67  else
68  {
69  if (better_flag==1)
70  {
71  // we have a better value so go with it
72  break;
73  }
74  else
75  {
76  // try a smaller trust region
77  lambda/=5;
78  s=vbest;
79  }
80  }
81  }
82  while (counter<20);
83 
84  if (better_flag==0)
85  {
86  cerr << "Error cannot find better value to try and get a"
87  " positive definite hessian" << endl;
88  }
89 
90  return vbest;
91 }
92 
98 (dvector& s,dmatrix& H,dvector& grad,double lambda)
99 {
100  double f=0.0;
101  double fb=1.e+100;
102  dvector g(1,usize);
103  dvector ub(1,usize);
104  fmc1.itn=0;
105  fmc1.ifn=0;
106  fmc1.ireturn=0;
107  fmc1.ialph=0;
108  fmc1.ihang=0;
109  fmc1.ihflag=0;
110  fmc1.crit=1.e-12;
111  long fmsave = fmc1.maxfn;
112  fmc1.maxfn=1000;;
113 
114  fmc1.dfn=1.e-2;
115  while (fmc1.ireturn>=0)
116  {
117  fmc1.fmin(f,s,g);
118  if (fmc1.ireturn>0)
119  {
120  double ns=norm(s);
121  double ns2=square(ns);
122  dvector v=s/ns;
123 
124  dvector z=H*v;
125  double vHv=v*z;
126 
127  double gradv=grad*v;
128  f=lambda*gradv+0.5*lambda*lambda*vHv+ square(ns2-1.0);
129  //f=0.5*lambda*lambda*s*H*s;
130  if (f<fb)
131  {
132  fb=f;
133  ub=s;
134  }
135  g=lambda*grad/ns -lambda * gradv*s/ns2
136  + lambda * lambda * z/ns
137  - lambda * lambda * vHv*s/ns2 + 4.0*(ns2-1.0)*s;
138  }
139  }
140  s=ub;
141  std::ostream& output_stream = get_output_stream();
142  output_stream << " inner maxg = " << std::scientific << setprecision(10) << fmc1.gmax;
143 
144  fmc1.maxfn=fmsave;
145  fmc1.ireturn=0;
146  fmc1.fbest=fb;
147  return ub;
148 }
149 
150  //
151  //
152  //dvector laplace_approximation_calculator::local_minimization_routine
153  //(dvector& s,dmatrix& H,dvector& grad,double lambda)
154  //{
155  // double f=0.0;
156  // double fb=1.e+100;
157  // dvector g(1,usize);
158  // dvector ub(1,usize);
159  // fmc1.itn=0;
160  // fmc1.ifn=0;
161  // fmc1.ireturn=0;
162  // fmc1.ialph=0;
163  // fmc1.ihang=0;
164  // fmc1.ihflag=0;
165  // fmc1.crit=1.e-12;
166  // double beta=.1;
167  //
168  // s.initialize();
169  //
170  // fmc1.dfn=1.e-2;
171  // while (fmc1.ireturn>=0)
172  // {
173  // fmc1.fmin(f,s,g);
174  // if (fmc1.ireturn>0)
175  // {
176  // double den=lambda-norm2(s);
177  //
178  // if (den<=0)
179  // {
180  // f=1.e+60;
181  // }
182  // else
183  // {
184  // f=grad*s+0.5*(s*(H*s))+0.5*beta/den;
185  // if (f<fb)
186  // {
187  // fb=f;
188  // ub=s;
189  // }
190  // g=grad + H*s + lambda*s +beta*s/square(den);
191  // }
192  // }
193  // }
194  // s=ub;
195  // cout << " inner maxg = " << fmc1.gmax;
196  //
197  // fmc1.ireturn=0;
198  // fmc1.fbest=fb;
199  // return ub;
200  //}
Description not yet available.
Vector of double precision numbers.
Definition: dvector.h:50
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
dvector local_minimization_routine(dvector &s, dmatrix &Hess, dvector &grad, double lambda)
Description not yet available.
Definition: dflocmin.cpp:98
prnstream & endl(prnstream &)
dvector local_minimization(dvector &s, dmatrix &Hess, dvector &grad, double lambda)
Description not yet available.
Definition: dflocmin.cpp:42
Description not yet available.
Description not yet available.
Definition: fvar.hpp:2819
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
std::ostream & get_output_stream()
Definition: adglobl.cpp:45
Description not yet available.
double evaluate_function_no_derivatives(const dvector &x, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lap.cpp:2196
double square(const double value)
Return square of value; constant object.
Definition: d3arr4.cpp:16