ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2lp11.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 #if defined(USE_DD)
12 # define USE_DD_STUFF
13 #endif
14 
15 # include <admodel.h>
16 # include <df1b2fun.h>
17 # include <adrndeff.h>
18 double calculate_laplace_approximation(const dvector& x,const dvector& u0,
19  const banded_symmetric_dmatrix& bHess,const dvector& _xadjoint,
20  const dvector& _uadjoint,
21  const banded_symmetric_dmatrix& _bHessadjoint,function_minimizer * pmin);
22 double calculate_laplace_approximation(const dvector& x,const dvector& u0,
23  const dmatrix& Hess,const dvector& _xadjoint,const dvector& _uadjoint,
24  const dmatrix& _Hessadjoint,function_minimizer * pmin);
25 
26 #if defined(USE_DD_STUFF)
27 # if defined(_MSC_VER)
28  extern "C" _export void dd_newton_raphson(int n,double * v,double * diag,
29  double * ldiag, double * yy);
30 # else
31  extern "C" void dd_newton_raphson(int n,double * v,double * diag,
32  double * ldiag, double * yy);
33 # endif
34 #endif
35 
40 void positivize(const banded_symmetric_dmatrix& _m,double id)
41 {
43  int mmin=m.indexmin();
44  int mmax=m.indexmax();
45  for (int i=mmin;i<=mmax;i++)
46  {
47  m(i,i)+=id;
48  }
49 }
50 
56 {
57 public:
58  safe_choleski_solver(double id);
59  int hadbad;
60  int dirty;
61  double id;
62  double angle;
63  dvector solve(const banded_symmetric_dmatrix& _m,const dvector&_v);
64 };
65 
71 {
72  id=_id;
73  hadbad=0;
74  dirty=0;
75 }
76 
78  const banded_symmetric_dmatrix& _M, int& ierr);
79 /*
80 banded_lower_triangular_dmatrix quiet_choleski_decomp(
81  const banded_symmetric_dmatrix& _M,const int& _ierr)
82 {
83  int & ierr = (int &) _ierr;
84  ADUNCONST(banded_symmetric_dmatrix,M)
85  int minsave=M.indexmin();
86  M.shift(1);
87  int n=M.indexmax();
88 
89  int bw=M.bandwidth();
90  banded_lower_triangular_dmatrix L(1,n,bw);
91 #ifndef SAFE_INITIALIZE
92  L.initialize();
93 #endif
94 
95  int i,j,k;
96  double tmp;
97  if (M(1,1)<=0)
98  {
99  ierr=1;
100  return L;
101  }
102  L(1,1)=sqrt(M(1,1));
103  for (i=2;i<=bw;i++)
104  {
105  L(i,1)=M(i,1)/L(1,1);
106  }
107 
108  for (i=2;i<=n;i++)
109  {
110  for (j=i-bw+1;j<=i-1;j++)
111  {
112  if (j>1)
113  {
114  tmp=M(i,j);
115  for (k=i-bw+1;k<=j-1;k++)
116  {
117  if (k>0 && k>j-bw)
118  tmp-=L(i,k)*L(j,k);
119  }
120  L(i,j)=tmp/L(j,j);
121  }
122  }
123  tmp=M(i,i);
124  for (k=i-bw+1;k<=i-1;k++)
125  {
126  if (k>0)
127  tmp-=L(i,k)*L(i,k);
128  }
129  if (tmp<=0)
130  {
131  ierr=1;
132  return L;
133  }
134  L(i,i)=sqrt(tmp);
135  }
136  M.shift(minsave);
137  L.shift(minsave);
138 
139  return L;
140 }
141 */
142 
148  (const banded_symmetric_dmatrix& _m,const dvector&_v)
149 {
150  int ierr=0;
151  ADUNCONST(dvector,v)
153  int mmin=m.indexmin();
154  //int mmax=m.indexmax();
155  if (hadbad && id>0.0)
156  {
157  positivize(m,id);
158  dirty=1;
159  }
160  m.shift(1);
161  v.shift(1);
162  int ibreak=1;
163  dvector w;
164  std::ostream& output_stream = get_output_stream();
165  do
166  {
168  if (ierr==0)
169  {
170  id/=2.0;
171  w=solve_trans(C,::solve(C,v));
172  dvector delta=m*w;
173  dvector err=solve_trans(C,::solve(C,v-delta));
174  dvector w1=w+err;
175  output_stream << norm(w1-w) << endl;
176  if (norm(err)>1.e-10)
177  {
178  cout << "precisionerror" << endl;
179  }
180  angle=w*v/(norm(w)*norm(v));
181  ibreak=0;
182  }
183  else
184  {
185  id*=2.0;
186  positivize(m,id);
187  ierr=0;
188  dirty=1;
189  hadbad=1;
190  }
191  }
192  while(ibreak);
193  m.shift(mmin);
194  w.shift(mmin);
195  v.shift(mmin);
196  return w;
197 }
198 
204  int& no_converge_flag)
205 {
206  std::ostream& output_stream = get_output_stream();
207 
209  double fbest=1.e+100;
210  double fval=1.e+100;
211  double maxg=fabs(evaluate_function(fbest,uhat,pfmin));
212 
214  dvector uhat_old(1,usize);
215  safe_choleski_solver scs(0.1);
216  //for(int ii=1;ii<=num_nr_iters;ii++)
217  int ii=0;
218  for (;;)
219  {
220  bHess->initialize();
221 
222  grad.initialize();
223 
225 
226 #ifdef DIAG
227  // check for degenerate Hessian
228  int check_hessian=0;
229  if (check_hessian)
230  {
231  ofstream ofs("hh");
232  ofs << colsum(dmatrix(*bHess)) << endl;
233  }
234 #endif
235 
237  {
238  output_stream << "Newton raphson " << ii << " ";
239  }
240 
242  {
245  }
246 
247  //int ierr=0;
248 
249  dvector g1(1,usize);
250  maxg=fabs(evaluate_function(fval,uhat,g1,pfmin));
251  if (fval>fbest)
252  fbest=fval;
253  if (nr_debug==1)
254  {
255  cout << " grad compare " << norm(g1-grad) << endl;
256  }
257  step=scs.solve(*bHess,g1);
258  //step=scs.solve(*bHess,grad);
259  if (nr_debug==1)
260  {
261  cout << " angle = " << step*grad/(norm(step)*norm(grad)) << endl;
262  }
263  int iic=0;
264  double testangle=-1;
265  int extra_try=0;
266  dvector utry(1,usize);
267  int smallshrink=0;
268  for (;;)
269  {
270  if (++iic>10)
271  {
272  break;
273  }
274  if (extra_try==0)
275  {
276  utry = uhat-step;
277  }
278  else
279  {
280  utry = uhat-0.5*step;
281  }
282  dvector g(1,usize);
283  maxg=fabs(evaluate_function(fval,utry,g,pfmin));
284  if (nr_debug==1)
285  {
286  cout << " fbest-fval = " << setprecision(15)
287  << fbest-fval << endl;
288  }
289  if (fval>fbest && maxg>1.e-10)
290  {
291  if (maxg<1.e-10)
292  smallshrink=3;
293  else if (maxg<1.e-9)
294  smallshrink=2;
295  else if (maxg<1.e-8)
296  smallshrink=1;
297 
298  if (nr_debug==1)
299  {
300  testangle=g*step/(norm(g)*norm(step));
301  cout << fval-fbest << " step too large angle = " << testangle
302  << endl;
303  }
304  }
305  if (fval==fbest)
306  {
307  testangle=g*step/(norm(g)*norm(step));
308  cout << " no progress angle = " << testangle << endl;
309  }
310  if (fval<=fbest)
311  {
312  uhat=utry;
313  fbest=fval;
314  testangle=g*step/(norm(g)*norm(step));
315  if (nr_debug==1)
316  {
317  cout << "inner angle = " << testangle << endl;
318  }
319  if (testangle>0.4)
320  {
321  extra_try=extra_try+1;
322  if (nr_debug==1)
323  {
324  cout << extra_try << endl;
325  }
326  }
327  else
328  {
329  break;
330  }
331  }
332  else
333  {
334  if (extra_try>0)
335  {
336  break;
337  }
338  else
339  {
340  if (smallshrink==0)
341  step/=100.0;
342  else if(smallshrink==1)
343  step/=10.0;
344  else if(smallshrink==2)
345  step/=5;
346  else if(smallshrink==3)
347  step/=2;
348  smallshrink=0;
349  }
350  }
351  }
352 
353  ii++;
354 
355  if (scs.dirty==1)
356  {
357  scs.dirty=0;
359 
360 #ifdef DIAG
361  int print_hessian=0;
362  if (print_hessian)
363  {
364  ofstream ofs("hh1");
365  ofs << setw(12) << setscientific() << setprecision(3) << endl;
366  }
367 #endif
368 
370  {
373  }
374  if (ii>=num_nr_iters || maxg < 1.e-13 )
375  {
376  step=scs.solve(*bHess,g1);
377  }
378  //solve(*bHess,grad);
379  }
380 
381  for (int i=1;i<=usize;i++)
382  {
383  y(i+xsize)=uhat(i);
384  }
385 
386  if (scs.dirty==0)
387  {
388  if (ii>=num_nr_iters || maxg < 1.e-13 )
389  {
390  break;
391  }
392  }
393  else
394  {
395  scs.dirty=0;
396  scs.hadbad=0;
397  if (ii>100)
398  {
399  cerr << "Can't get positive definite Hessian in inner optimization"
400  << endl;
401  break;
402  }
403  }
404  }
405 }
Description not yet available.
Definition: fvar.hpp:7981
Description not yet available.
#define x
#define ADUNCONST(type, obj)
Creates a shallow copy of obj that is not CONST.
Definition: fvar.hpp:140
Vector of double precision numbers.
Definition: dvector.h:50
static void get_cgradient_contribution(dvector, int)
Description not yet available.
Definition: quadpri.cpp:558
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
double calculate_laplace_approximation(const dvector &x, const dvector &u0, const dmatrix &Hess, const dvector &_xadjoint, const dvector &_uadjoint, const dmatrix &_Hessadjoint, function_minimizer *pmin)
Description not yet available.
Definition: df1b2lap.cpp:1476
void do_newton_raphson_state_space(function_minimizer *pfmin, double f_from_1, int &no_converge_flag)
Definition: df1b2lp11.cpp:203
double norm(const d3_array &a)
Return computed norm value of a.
Definition: d3arr2a.cpp:190
banded_symmetric_dmatrix * bHess
Definition: adrndeff.h:265
double evaluate_function(const dvector &x, function_minimizer *pfmin)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: df1b2lap.cpp:2026
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:46
prescientific setscientific(void)
Description not yet available.
Definition: admanip.cpp:80
prnstream & endl(prnstream &)
dvector solve(const banded_symmetric_dmatrix &_m, const dvector &_v)
Description not yet available.
Definition: df1b2lp11.cpp:148
dvector solve_trans(const lower_triangular_dmatrix &M, const dvector &y)
Description not yet available.
Definition: dmat36.cpp:80
banded_lower_triangular_dmatrix quiet_choleski_decomp(const banded_symmetric_dmatrix &_M, int &ierr)
Description not yet available.
Definition: mod_rhes.cpp:34
void initialize(void)
Description not yet available.
Definition: dmat41.cpp:17
Description not yet available.
Definition: df1b2lp11.cpp:55
double colsum(const dmatrix &m, int col)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat27.cpp:10
Description not yet available.
Description not yet available.
Definition: fvar.hpp:8120
void positivize(const banded_symmetric_dmatrix &_m, double id)
Description not yet available.
Definition: df1b2lp11.cpp:40
Description not yet available.
Definition: fvar.hpp:2819
dvector get_newton_raphson_info_banded(function_minimizer *pmin)
Description not yet available.
Definition: df1b2lp6.cpp:822
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
dvector & shift(int min)
Shift valid range of subscripts.
Definition: dvector.cpp:52
std::ostream & get_output_stream()
Definition: adglobl.cpp:45
#define w
Description not yet available.
Description not yet available.
Definition: admodel.h:1850
static void get_cHessian_contribution(dmatrix, int)
Description not yet available.
Definition: quadpri.cpp:591
static int mc_phase
Definition: admodel.h:846
safe_choleski_solver(double id)
Description not yet available.
Definition: df1b2lp11.cpp:70
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1916