ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2lp10.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 
20 {
21  ofstream ofs("callset.rpt");
22 
23  imatrix& callset=(*lapprox->calling_set);
24 
25  ofs << "Total num_separable calls " << callset(0,0)-1 << endl;
26 
27  for (int i=1;i<=callset.indexmax();i++)
28  {
29  ofs << "Variable " << i << " num calls = " << callset(i)(0) << endl;
30  ofs << callset(i)(1,callset(i).indexmax())<< endl;
31  }
32 }
33 
39 bool check_order(const ivector& v)
40 {
41  int mmin=v.indexmin();
42  int mmax=v.indexmax();
43  for (int i=mmin;i<=mmax-1;i++)
44  {
45  if (v(i+1)<v(i))
46  {
47  return false;
48  }
49  }
50  return true;
51 }
52 
59 {
60  if (!check_order(v)) v = sort(v);
61  if (!check_order(w)) w = sort(w);
62  //int vmin=v.indexmin();
63  int wmin=w.indexmin();
64  int vmax=v.indexmax();
65  int wmax=w.indexmax();
66  int common_flag=0;
67  int i=wmin; int j=wmin;
68  for (;;)
69  {
70  if (v(i)==w(j))
71  {
72  common_flag=1;
73  break;
74  }
75  else if (v(i)>w(j))
76  {
77  if (j<wmax)
78  j++;
79  else
80  break;
81  }
82  else if (v(i)<w(j))
83  {
84  if (i<vmax)
85  i++;
86  else
87  break;
88  }
89  }
90  return common_flag;
91 }
92 
99 {
100  int ip = 0;
102  {
103  hesstype=4;
104  if (allocated(Hess))
105  {
106  if (Hess.indexmax()!=usize)
107  {
108  Hess.deallocate();
109  Hess.allocate(1,usize,1,usize);
110  }
111  }
112  else
113  {
114  Hess.allocate(1,usize,1,usize);
115  }
116  if (allocated(Hessadjoint))
117  {
118  if (Hessadjoint.indexmax()!=usize)
119  {
122  }
123  }
124  else
125  {
127  }
128  return;
129  }
130  else
131  {
133  if (allocated(used_flags))
134  {
135  if (used_flags.indexmax() != nv)
136  {
138  }
139  }
140  if (!allocated(used_flags))
141  {
143  }
144 
145  //for (ip=1;ip<=num_der_blocks;ip++)
146  {
148  // do we need to reallocate memory for df1b2variables?
151  //cout << re_objective_function_value::pobjfun << endl;
152  //cout << re_objective_function_value::pobjfun->ptr << endl;
153  (*re_objective_function_value::pobjfun)=0;
154  df1b2variable pen=0.0;
155  df1b2variable zz=0.0;
156 
158  // call function to do block diagonal newton-raphson
159  // the step vector from the newton-raphson is in the vector step
161 
164 
166  pfmin->pre_user_function();
168 
169  int non_block_diagonal=0;
170  for (int i=xsize+1;i<=xsize+usize;i++)
171  {
172  if (used_flags(i)>1)
173  {
174  non_block_diagonal=1;
175  break;
176  }
177  }
178  if (non_block_diagonal)
179  {
180  if (bw< usize/2)
181  {
182  hesstype=3; //banded
183  if (bHess)
184  {
185  if (bHess->bandwidth() !=bw)
186  {
187  delete bHess;
188  bHess = new banded_symmetric_dmatrix(1,usize,bw);
189  if (bHess==0)
190  {
191  cerr << "Error allocating banded_symmetric_dmatrix" << endl;
192  ad_exit(1);
193  }
194  }
195  }
196  else
197  {
198  bHess = new banded_symmetric_dmatrix(1,usize,bw);
199  if (bHess==0)
200  {
201  cerr << "Error allocating banded_symmetric_dmatrix" << endl;
202  ad_exit(1);
203  }
204  }
205  if (bHessadjoint)
206  {
207  if (bHessadjoint->bandwidth() !=bw)
208  {
209  delete bHessadjoint;
211  if (bHessadjoint==0)
212  {
213  cerr << "Error allocating banded_symmetric_dmatrix" << endl;
214  ad_exit(1);
215  }
216  }
217  }
218  else
219  {
221  if (bHessadjoint==0)
222  {
223  cerr << "Error allocating banded_symmetric_dmatrix" << endl;
224  ad_exit(1);
225  }
226  }
227  }
228  else
229  {
230  //check_sparse_matrix_structure();
231  hesstype=4; // band is so wide so use full matrix
232  if (bHess)
233  {
234  delete bHess;
235  bHess=0;
236  }
237 
238  if (bHessadjoint)
239  {
240  delete bHessadjoint;
241  bHessadjoint=0;
242  }
243 
244  if (allocated(Hess))
245  {
246  if (Hess.indexmax() != usize)
247  {
248  Hess.deallocate();
249  Hess.allocate(1,usize,1,usize);
250  }
251  }
252  else
253  {
254  Hess.allocate(1,usize,1,usize);
255  }
256  if (allocated(Hessadjoint))
257  {
258  if (Hessadjoint.indexmax() != usize)
259  {
261  Hessadjoint.allocate(1,usize,1,usize);
262  }
263  }
264  else
265  {
266  Hessadjoint.allocate(1,usize,1,usize);
267  }
268  }
269  }
270  else
271  {
272  hesstype=2;
273  }
274  if (hesstype==2 && num_importance_samples>0)
275  {
277  {
280  }
284  }
285  // check for containg block diagonal structure
286  used_flags(1);
287  if (calling_set)
288  {
289  delete calling_set;
290  }
291  int mmin=used_flags.indexmin()-1;
292  int mmax=used_flags.indexmax();
293  ivector tmp(mmin,mmax);
294  tmp(mmin)=0;
295  tmp(mmin+1,mmax)=used_flags;
296  {
297  calling_set=new imatrix(mmin,mmax,0,tmp);
299  (*calling_set)(0,0)=1;
300  }
303  pfmin->pre_user_function();
305  report_calling_set(this);
306 
308  {
309  const ivector & itmp=(*num_local_re_array)(1,num_separable_calls);
310  const ivector & itmpf=(*num_local_fixed_array)(1,num_separable_calls);
311 
312  // ****************************************************
313  // ****************************************************
314  if (use_gauss_hermite>0)
315  {
316  if (gh)
317  {
318  delete gh;
319  gh=0;
320  }
322  num_separable_calls,itmp);
323  }
324 
325  if (block_diagonal_vch)
326  {
327  delete block_diagonal_vch;
329  }
330 
332  1,itmp,1,itmp);
333  if (block_diagonal_ch)
334  {
335  delete block_diagonal_ch;
337  }
339  1,itmp,1,itmp);
341  {
342  delete block_diagonal_hessian;
344  }
346  1,itmp,1,itmp);
347  if (block_diagonal_hessian ==0)
348  {
349  cerr << "error_allocating d3_array" << endl;
350  ad_exit(1);
351  }
353  1,itmp);
354  if (block_diagonal_re_list ==0)
355  {
356  cerr << "error_allocating imatrix" << endl;
357  ad_exit(1);
358  }
360  1,itmpf);
361  if (block_diagonal_fe_list ==0)
362  {
363  cerr << "error_allocating imatrix" << endl;
364  ad_exit(1);
365  }
366  // ****************************************************
367  if (block_diagonal_Dux)
368  {
369  delete block_diagonal_Dux;
371  }
373  1,itmp,1,itmpf);
374  if (block_diagonal_Dux ==0)
375  {
376  cerr << "error_allocating d3_array" << endl;
377  ad_exit(1);
378  }
379 
380  // ****************************************************
381  // ****************************************************
383  {
386  }
388  1,itmp,1,itmp);
389  if (block_diagonal_vhessian ==0)
390  {
391  cerr << "error_allocating d3_array" << endl;
392  ad_exit(1);
393  }
394 
396  {
399  }
401  1,itmp,1,itmp);
403  {
404  cerr << "error_allocating d3_array" << endl;
405  ad_exit(1);
406  }
407  }
410  pen.deallocate();
411  }
412  }
413 }
Description not yet available.
Definition: fvar.hpp:7981
void safe_deallocate()
Safely deallocates memory by reporting if shallow copies are still in scope.
Definition: ivector.cpp:119
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
static void reset(const init_df1b2vector &, const df1b2variable &)
Description not yet available.
Definition: df1b2f15.cpp:51
static void set_no_derivatives(void)
Definition: df1b2fun.h:760
Description not yet available.
Definition: adrndeff.h:182
function_minimizer * pmin
Definition: adrndeff.h:246
int indexmax() const
Definition: imatrix.h:142
Description not yet available.
Definition: imatrix.h:69
Description not yet available.
void allocate(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat0.cpp:8
void deallocate()
Deallocate dmatrix memory.
Definition: dmat.cpp:363
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_a59.cpp:13
dvar_matrix * importance_sampling_components
Definition: adrndeff.h:224
bool check_order(const ivector &v)
Check if v is ordered from low to high.
Definition: df1b2lp10.cpp:39
gauss_hermite_stuff * gh
Definition: adrndeff.h:223
exitptr ad_exit
Definition: gradstrc.cpp:53
void check_hessian_type2(function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lp10.cpp:98
Description not yet available.
Definition: df1b2fun.h:266
banded_symmetric_dmatrix * bHess
Definition: adrndeff.h:265
void deallocate(void)
If no other copies exist, free df1b2variable::ptr.
Definition: df1b2fn2.cpp:286
void check_for_need_to_reallocate(int ip)
Does Nothing.
Definition: df1b2lap.cpp:1968
dvar3_array * block_diagonal_vch
Definition: adrndeff.h:228
static laplace_approximation_calculator * lapprox
Definition: df1b2fnl.h:61
dmatrix sort(const dmatrix &m, int column, int NSTACK)
Description not yet available.
Definition: dmsort.cpp:17
prnstream & endl(prnstream &)
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
void initialize(void)
Description not yet available.
Definition: imat3.cpp:20
Description not yet available.
int indexmin() const
Definition: ivector.h:99
void pre_user_function(void)
Description not yet available.
Definition: df1b2lp8.cpp:370
int indexmax() const
Definition: ivector.h:104
Description not yet available.
Definition: fvar.hpp:4197
d3_array * block_diagonal_vhessianadjoint
Definition: adrndeff.h:232
int indexmax() const
Definition: fvar.hpp:2921
void initialize(void)
Description not yet available.
Definition: ivec2.cpp:17
dvar3_array * block_diagonal_vhessian
Definition: adrndeff.h:233
Description not yet available.
Definition: adrndeff.h:406
Class definition of matrix with derivitive information .
Definition: fvar.hpp:2480
#define w
int common(ivector &v, ivector &w)
Check vectors v and w for single common value.
Definition: df1b2lp10.cpp:58
static int set_index(void)
Definition: df1b2f15.cpp:90
banded_symmetric_dmatrix * bHessadjoint
Definition: adrndeff.h:266
Description not yet available.
void report_calling_set(laplace_approximation_calculator *lapprox)
Description not yet available.
Definition: df1b2lp10.cpp:19
Description not yet available.
Definition: admodel.h:1850
void safe_allocate(int ncl, int ncu)
Description not yet available.
Definition: ivector.cpp:78
Description not yet available.
Definition: fvar.hpp:3727
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1916
int bandwidth(void) const
Definition: fvar.hpp:7991
static int in_qp_calculations
Definition: df1b2fun.h:1909