ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
f1b2fnl5.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 
24 {
25  //***********************************************************
26  //***********************************************************
32 
35  int num_local_re=0;
36  int num_fixed_effects=0;
37 
38  //cout << list << endl;
39 #ifndef OPT_LIB
40  assert(funnel_init_var::num_active_parameters <= INT_MAX);
41 #endif
43  ivector lfe_index(1,(int)funnel_init_var::num_active_parameters);
44 
45  for (int i=1;i<=(int)funnel_init_var::num_active_parameters;i++)
46  {
47  if (list(i,1)>xsize)
48  {
49  lre_index(++num_local_re)=i;
50  }
51  else if (list(i,1)>0)
52  {
53  lfe_index(++num_fixed_effects)=i;
54  }
55  }
56 
57  if (num_local_re > 0)
58  {
59  dmatrix& local_Hess=(*block_diagonal_hessian)(num_separable_calls);
60  dmatrix& local_Dux=(*block_diagonal_Dux)(num_separable_calls);
61  ivector& local_re_list=(*block_diagonal_re_list)(num_separable_calls);
62  ivector& local_fe_list=(*block_diagonal_fe_list)(num_separable_calls);
63  local_re_list.initialize();
64  local_fe_list.initialize();
65  local_Hess.initialize();
66 
67  int* plocal_re_listi = local_re_list.get_v() + 1;
68  int* plre_indexi = lre_index.get_v() + 1;
69  for (int i = 1;i <= num_local_re; ++i)
70  {
71  *plocal_re_listi = list(*plre_indexi, 1);
72 
73  ++plocal_re_listi;
74  ++plre_indexi;
75  }
76 
77  int* plocal_fe_listi = local_fe_list.get_v() + 1;
78  int* plfe_indexi = lfe_index.get_v() + 1;
79  for (int i=1;i<=num_fixed_effects;i++)
80  {
81  *plocal_fe_listi = list(*plfe_indexi, 1);
82 
83  ++plocal_fe_listi;
84  ++plfe_indexi;
85  }
86 
87  dvector* plocal_Hessi = &local_Hess(1);
88  plre_indexi = lre_index.get_v() + 1;
89  for (int i=1;i<=num_local_re;i++)
90  {
91  int lrei = *plre_indexi;
92  int i2=list(lrei,2);
93 
94  double* plocal_Hessij = plocal_Hessi->get_v() + 1;
95  int* plre_indexj = lre_index.get_v() + 1;
96  for (int j=1;j<=num_local_re;j++)
97  {
98  int lrej = *plre_indexj;
99  int j2=list(lrej,2);
100 
101  *plocal_Hessij += locy(i2).u_bar[j2-1];
102 
103  ++plocal_Hessij;
104  ++plre_indexj;
105  }
106 
107  ++plocal_Hessi;
108  ++plre_indexi;
109  }
110 
111  dvector* plocal_Duxi = &local_Dux(1);
112  plre_indexi = lre_index.get_v() + 1;
113  for (int i=1;i<=num_local_re;i++)
114  {
115  int i2=list(*plre_indexi, 2);
116 
117  double* plocal_Duxij = plocal_Duxi->get_v() + 1;
118  int* plfe_indexj = lfe_index.get_v() + 1;
119  for (int j=1;j<=num_fixed_effects;j++)
120  {
121  int j2=list(*plfe_indexj, 2);
122  *plocal_Duxij = locy(i2).u_bar[j2-1];
123 
124  ++plocal_Duxij;
125  ++plfe_indexj;
126  }
127 
128  ++plocal_Duxi;
129  ++plre_indexi;
130  }
131 
134  {
135  plocal_Hessi = &local_Hess(1);
136  plre_indexi = lre_index.get_v() + 1;
137  for (int i=1;i<=num_local_re;i++)
138  {
139  int lrei = *plre_indexi;
140  int i1=list(lrei,1);
141  double* plocal_Hessij = plocal_Hessi->get_v() + 1;
142  int* plre_indexj = lre_index.get_v() + 1;
143  for (int j=1;j<=num_local_re;j++)
144  {
145  int lrej = *plre_indexj;
146  int j1=list(lrej,1);
147  *plocal_Hessij *= scale(i1-xsize)*scale(j1-xsize);
148 
149  ++plocal_Hessij;
150  ++plre_indexj;
151  }
152  ++plocal_Hessi;
153  ++plre_indexi;
154  }
155  }
156  }
157 
158  f1b2gradlist->reset();
166  funnel_init_var::num_active_parameters=0;
168 }
169 
177 {
178  num_separable_calls++;
180  //df1b2_gradlist::set_no_derivatives();
182  df1b2_gradcalc1();
183 
186 
187  int us=0; int xs=0;
188 #ifndef OPT_LIB
189  assert(funnel_init_var::num_active_parameters <= INT_MAX);
190 #endif
192  ivector lfe_index(1,(int)funnel_init_var::num_active_parameters);
193 
194  for (int i=1;i<=(int)funnel_init_var::num_active_parameters;i++)
195  {
196  if (list(i,1)>xsize)
197  {
198  lre_index(++us)=i;
199  }
200  else if (list(i,1)>0)
201  {
202  lfe_index(++xs)=i;
203  }
204  }
205 
206  dvector local_xadjoint(1,xs);
207 
208  if (us>0)
209  {
210  dmatrix local_Hess(1,us,1,us);
211  dvector local_grad(1,us);
212  dmatrix local_Dux(1,us,1,xs);
213  local_Hess.initialize();
214  dvector local_uadjoint(1,us);
215  for (int i=1;i<=us;i++)
216  {
217  for (int j=1;j<=us;j++)
218  {
219  int i2=list(lre_index(i),2);
220  int j2=list(lre_index(j),2);
221  local_Hess(i,j)+=locy(i2).u_bar[j2-1];
222  }
223  }
224 
225  for (int i=1;i<=us;i++)
226  {
227  for (int j=1;j<=xs;j++)
228  {
229  int i2=list(lre_index(i),2);
230  int j2=list(lfe_index(j),2);
231  local_Dux(i,j)=locy(i2).u_bar[j2-1];
232  }
233  }
234 
235  double f=0.0;
237 
238  for (int i=1;i<=us;i++)
239  {
240  for (int j=1;j<=us;j++)
241  {
242  int i2=list(lre_index(i),2);
243  int j2=list(lre_index(j),2);
244  locy(i2).get_u_bar_tilde()[j2-1]=
245  (*block_diagonal_vhessianadjoint)(num_separable_calls)(i,j);
246  }
247  }
248 
250  df1b2_gradcalc1();
251 
253  df1b2_gradcalc1();
254  dvector xtmp(1,xs);
255  xtmp.initialize();
256 
257  local_uadjoint.initialize();
258  local_xadjoint.initialize();
259  for (int i=1;i<=xs;i++)
260  {
261  int i2=list(lfe_index(i),2);
262  xtmp(i)+=locy[i2].u_tilde[0];
263  local_xadjoint(i)+=locy[i2].u_tilde[0];
264  }
265  dvector utmp(1,us);
266  utmp.initialize();
267  for (int i=1;i<=us;i++)
268  {
269  int i2=list(lre_index(i),2);
270  utmp(i)+=locy[i2].u_tilde[0];
271  local_uadjoint(i)+=locy[i2].u_tilde[0];
272  }
273  if (xs>0)
274  local_xadjoint -= solve(local_Hess,local_uadjoint)*local_Dux;
275  }
276  for (int i=1;i<=xs;i++)
277  {
278  int ii=lfe_index(i);
279  check_local_xadjoint2(list(ii,1))+=local_xadjoint(i);
280  }
281  f1b2gradlist->reset();
289  funnel_init_var::num_active_parameters=0;
291 }
void initialize(void)
Description not yet available.
Definition: df1b2f14.cpp:155
df1b2_gradlist * f1b2gradlist
Definition: df1b2glo.cpp:49
void set_dependent_variable(const df1b2variable &_x)
Description not yet available.
Definition: df1b2fn2.cpp:699
static void set_no_derivatives(void)
Definition: df1b2fun.h:760
Description not yet available.
Definition: imatrix.h:69
Description not yet available.
static double cobjfun
Definition: df1b2fun.h:1360
Vector of double precision numbers.
Definition: dvector.h:50
static int passnumber
Definition: df1b2fun.h:289
static int num_inactive_vars
Definition: df1b2fnl.h:66
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
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
void initialize(void)
Description not yet available.
Definition: df1b2f13.cpp:158
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
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 initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
void initialize(void)
Description not yet available.
Definition: ivec2.cpp:17
test_smartlist list3
Definition: df1b2fun.h:757
Description not yet available.
Definition: df1b2fun.h:373
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
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
void df1b2_gradcalc1(void)
Description not yet available.
Definition: df1b2fun.cpp:145