ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
f1b2fnl2.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 <df1b2fnl.h>
12 #include <adrndeff.h>
13 #ifndef OPT_LIB
14  #include <cassert>
15  #include <climits>
16 #endif
17 
19 
20 extern int noboundepen_flag;
21 
27 {
29  if (block_diagonal_flag==0)
30  {
42  {
45  }
46  return;
47  }
49  {
50  if (noboundepen_flag==0)
51  {
53  }
56  }
57 
58  // this should not be called a block diagopnal flag but it is for now.
59  //if (pool_check_flag)
60  // check_pool_depths();
61  switch (block_diagonal_flag)
62  {
63  case 1:
64  switch(hesstype)
65  {
66  case 2:
68  break;
69  case 3: //banded
70  case 4: // full matrix
72  //if (pool_check_flag)
73  // check_pool_depths();
74  break;
75  default:
76  cerr << "Illegal value for hesstype" << endl;
77  ad_exit(1);
78  }
79  break;
80  case 2:
81  switch(hesstype)
82  {
83  case 2:
85  if (saddlepointflag==2)
86  {
88  }
89  else
90  {
92  }
93  break;
94  case 3:
95  case 4: // full matrix
97  break;
98  default:
99  cerr << "Illegal value for hesstype" << endl;
100  ad_exit(1);
101  }
102  break;
103  case 3:
104  switch(hesstype)
105  {
106  case 2:
108  break;
109  case 3:
110  case 4: // full matrix
112  break;
113  default:
114  cerr << "Illegal value for hesstype" << endl;
115  ad_exit(1);
116  }
117  break;
118  case 5: // get hessian type information
120  break;
121  case 6: // get hessian type information
123  break;
124  default:
125  cerr << "illegal value for block_diagonal_flag = "
127  ad_exit(1);
128  }
131  if (df1b2variable::pool->size<=0)
132  {
133  cerr << "this can't happen" << endl;
134  ad_exit(1);
135  }
136 }
137 
144 {
145  //***********************************************************
146  //***********************************************************
150  df1b2_gradcalc1();
151 
154  int num_local_re=0;
155  int num_fixed_effects=0;
156 
157  //cout << list << endl;
158 #ifndef OPT_LIB
159  assert(funnel_init_var::num_active_parameters <= INT_MAX);
160 #endif
162  ivector lfe_index(1, (int)funnel_init_var::num_active_parameters);
163  for (int i=1;i<=(int)funnel_init_var::num_active_parameters;i++)
164  {
165  double listi1 = list(i, 1);
166  if (listi1 > xsize)
167  {
168  lre_index(++num_local_re)=i;
169  }
170  else if (listi1 > 0)
171  {
172  lfe_index(++num_fixed_effects)=i;
173  }
174  }
175 
176  if (num_local_re > 0)
177  {
178  //cout << " num_local_re = " << num_local_re << endl;
179  //cout << " num_fixed_effects = " << num_fixed_effects << endl;
180  //cout << " list = " << endl << list << endl;
181  //cout << " lre_index= " << endl << lre_index << endl;
182  //cout << " lfe_index= " << endl << lfe_index << endl;
183  //cout << "the number of local res is " << num_local_re << endl;
184  dmatrix local_Hess(1,num_local_re,1,num_local_re);
185  dvector local_grad(1,num_local_re);
186  //dmatrix local_Dux(1,num_local_re,1,
187  // funnel_init_var::num_active_parameters-num_local_re);
188  //dmatrix Hess1(1,funnel_init_var::num_vars,1,funnel_init_var::num_vars);
189  local_Hess.initialize();
190 
191  dvector* plocal_Hessi = &local_Hess(1);
192  int* plre_indexi = lre_index.get_v() + 1;
193  for (int i=1;i<=num_local_re;i++)
194  {
195  int lrei = *plre_indexi;
196  int i2=list(lrei,2);
197 
198  double* plocal_Hessij = plocal_Hessi->get_v() + 1;
199  int* plre_indexj = lre_index.get_v() + 1;
200  for (int j=1;j<=num_local_re;j++)
201  {
202  int lrej = *plre_indexj;
203  int j2=list(lrej,2);
204 
205  //Hess(i1-xsize,j1-xsize)+=locy(i2).u_bar[j2-1];
206  *plocal_Hessij += locy(i2).u_bar[j2-1];
207 
208  ++plocal_Hessij;
209  ++plre_indexj;
210  }
211 
212  ++plocal_Hessi;
213  ++plre_indexi;
214  }
215  // i<=funnel_init_var::num_vars;i++)
216  double* plocal_gradi = local_grad.get_v() + 1;
217  plre_indexi = lre_index.get_v() + 1;
218  for (int i=1;i<=num_local_re;i++)
219  {
220  int lrei = *plre_indexi;
221  //int i1=list(lrei,1);
222  int i2=list(lrei,2);
223  //grad(i1-xsize)= re_objective_function_value::pobjfun->u_dot[i2-1];
224  //grad(i1-xsize)= ff.u_dot[i2-1];
225  *plocal_gradi = ff.u_dot[i2-1];
226 
227  ++plocal_gradi;
228  ++plre_indexi;
229  }
230 
233  {
234  plocal_Hessi = &local_Hess(1);
235  plre_indexi = lre_index.get_v() + 1;
236  for (int i=1;i<=num_local_re;i++)
237  {
238  int lrei=lre_index(i);
239  int i1=list(lrei,1);
240 
241  double* plocal_Hessij = plocal_Hessi->get_v() + 1;
242  int* plre_indexj = lre_index.get_v() + 1;
243  for (int j=1;j<=num_local_re;j++)
244  {
245  int lrej = *plre_indexj;
246  int j1 = list(lrej,1);
247  *plocal_Hessij *= scale(i1-xsize)*scale(j1-xsize);
248 
249  ++plocal_Hessij;
250  ++plre_indexj;
251  }
252 
253  ++plocal_Hessi;
254  ++plre_indexi;
255  }
256 
257  plocal_Hessi = &local_Hess(1);
258  plocal_gradi = local_grad.get_v() + 1;
259  plre_indexi = lre_index.get_v() + 1;
260  for (int i=1;i<=num_local_re;i++)
261  {
262  int lrei = *plre_indexi;
263  int i1=list(lrei,1);
264  *(plocal_Hessi->get_v() + i) += *plocal_gradi * curv(i1-xsize);
265 
266  ++plocal_gradi;
267  ++plocal_Hessi;
268  ++plre_indexi;
269  }
270 
271  plocal_gradi = local_grad.get_v() + 1;
272  plre_indexi = lre_index.get_v() + 1;
273  for (int i=1;i<=num_local_re;i++)
274  {
275  int lrei = *plre_indexi;
276  int i1=list(lrei,1);
277  *plocal_gradi *= scale(i1-xsize);
278 
279  ++plocal_gradi;
280  ++plre_indexi;
281  }
282  }
283 
284  double mg=max(fabs(local_grad));
285  if (max_separable_g< mg) max_separable_g=mg;
286  dvector local_step=-solve(local_Hess,local_grad);
287 
288  double* plocal_stepi = local_step.get_v() + 1;
289  plre_indexi = lre_index.get_v() + 1;
290  for (int i=1;i<=num_local_re;i++)
291  {
292  int lrei = *plre_indexi;
293  int i1=list(lrei,1);
294  //int i2=list(lrei,2);
295  step(i1-xsize) = *plocal_stepi;
296 
297  ++plocal_stepi;
298  ++plre_indexi;
299  }
300  }
301 
302  f1b2gradlist->reset();
310  funnel_init_var::num_active_parameters=0;
312 }
static adpool * pool
Definition: df1b2fun.h:273
void initialize(void)
Description not yet available.
Definition: df1b2f14.cpp:155
int noboundepen_flag
Definition: df1b2lap.cpp:31
df1b2_gradlist * f1b2gradlist
Definition: df1b2glo.cpp:49
void set_dependent_variable(const df1b2variable &_x)
Description not yet available.
Definition: df1b2fn2.cpp:699
static adpool * adpool_vector[]
Definition: df1b2fun.h:282
static void set_no_derivatives(void)
Definition: df1b2fun.h:760
Description not yet available.
Definition: imatrix.h:69
Description not yet available.
void do_separable_stuff(void)
Description not yet available.
Definition: f1b2fnl2.cpp:26
Vector of double precision numbers.
Definition: dvector.h:50
static int passnumber
Definition: df1b2fun.h:289
static re_objective_function_value * pobjfun
Definition: df1b2fun.h:1693
static int num_inactive_vars
Definition: df1b2fnl.h:66
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
exitptr ad_exit
Definition: gradstrc.cpp:53
void do_separable_stuff_laplace_approximation_importance_sampling_adjoint(df1b2variable &)
Description not yet available.
Definition: f1b2fnl5.cpp:176
void initialize(void)
Description not yet available.
Definition: df1b2f10.cpp:171
void do_separable_stuff_laplace_approximation_banded(df1b2variable &)
Description not yet available.
Definition: df1b2lp6.cpp:897
Description not yet available.
Definition: df1b2fun.h:266
void get_block_diagonal_hessian(df1b2variable &)
Description not yet available.
Definition: f1b2fnl5.cpp:23
fixed_smartlist nlist
Definition: df1b2fun.h:754
static df1b2variable * funnel_constraints_penalty
Definition: df1b2fnl.h:62
void initialize(void)
Description not yet available.
Definition: df1b2f13.cpp:158
int pool_check_flag
Definition: f1b2fnl2.cpp:18
void do_separable_stuff_newton_raphson_block_diagonal(df1b2variable &)
Description not yet available.
Definition: f1b2fnl2.cpp:143
dvector solve(const dmatrix &aa, const dvector &z)
Solve a linear system using LU decomposition.
Definition: dmat34.cpp:46
int * get_v() const
Definition: ivector.h:114
prnstream & endl(prnstream &)
test_smartlist list
Definition: df1b2fun.h:753
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
static unsigned int num_active_parameters
Definition: df1b2fnl.h:67
fixed_smartlist2 nlist3
Definition: df1b2fun.h:758
Description not yet available.
fixed_smartlist2 nlist2
Definition: df1b2fun.h:756
static imatrix * plist
Definition: df1b2fnl.h:69
Description not yet available.
Definition: fvar.hpp:2819
void do_separable_stuff_newton_raphson_banded(df1b2variable &)
Description not yet available.
Definition: df1b2lp6.cpp:680
test_smartlist list3
Definition: df1b2fun.h:757
void do_separable_stuff_laplace_approximation_banded_adjoint(const df1b2variable &ff)
Description not yet available.
Definition: df1b2lp7.cpp:24
Description not yet available.
Definition: df1b2fun.h:373
static unsigned int nvar
Definition: df1b2fun.h:290
void do_separable_stuff_hessian_type_information(void)
Description not yet available.
Definition: df1b2lp8.cpp:1019
void do_separable_stuff_x_u_block_diagonal(df1b2variable &ff)
Description not yet available.
Definition: f1b2fnl3.cpp:24
#define max(a, b)
Definition: cbivnorm.cpp:189
static unsigned int num_vars
Definition: df1b2fnl.h:64
static init_df1b2vector * py
Definition: df1b2fnl.h:68
void initialize(void)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: dmat7.cpp:12
void do_separable_stuff_laplace_approximation_block_diagonal(df1b2variable &)
Calculates the Laplace approximation for a single separable function in the &quot;block diagonal&quot;...
Definition: f1b2fnl3.cpp:159
double *& get_v(void)
Definition: dvector.h:148
void reset(void)
Description not yet available.
Definition: df1b2fn2.cpp:581
test_smartlist list2
Definition: df1b2fun.h:755
double * u_dot
Definition: df1b2fun.h:196
void df1b2_gradcalc1(void)
Description not yet available.
Definition: df1b2fun.cpp:145
unsigned int nvar
Definition: adpool.h:77