ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
df1b2lp9.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 #ifdef DEBUG
15  #include <cassert>
16 #endif
17 
18 static int no_stuff=0;
19 /*
20 static void print(double ff,dvector& uuu,dvector& gg)
21 {
22  cout << setprecision(10) << setw(19) << ff << " "
23  << setw(19) << uuu << " " << setw(19) << gg << endl;
24 }
25 */
26 
27 typedef fmm* pfmm;
28 
34  (const dvector& x,function_minimizer * pfmin)
35 {
36  if (separable_function_difference)
37  {
38  delete separable_function_difference;
39  separable_function_difference=0;
40  }
41 #ifdef DEBUG
42  assert(num_separable_calls > 0);
43 #endif
44 
45  separable_function_difference = new dvector(1,num_separable_calls);
46 
47  fmm** pfmc1 = new pfmm[static_cast<unsigned int>(num_separable_calls)];
48  pfmc1--;
49  ivector ishape(1,num_separable_calls);
50  dvector gmax(1,num_separable_calls);
51  gmax.initialize();
52 
53  int* pishapei = ishape.get_v() + 1;
54  for (int i=1;i<=num_separable_calls;i++)
55  {
56  int m = (*derindex)(i).indexmax();
57  *pishapei = m;
58  if (m>0)
59  {
60  fmm* pfmc1i = new fmm(m);
61  pfmc1i->iprint=0;
62  pfmc1i->crit=inner_crit;
63  pfmc1i->ireturn=0;
64  pfmc1i->itn=0;
65  pfmc1i->ifn=0;
66  pfmc1i->ialph=0;
67  pfmc1i->ihang=0;
68  pfmc1i->ihflag=0;
69  pfmc1i->maxfn=100;
70  pfmc1i->gmax=1.e+100;
71  pfmc1i->use_control_c=0;
72  pfmc1[i] = pfmc1i;
73  }
74  else
75  {
76  pfmc1[i]= (fmm *)(0);
77  }
78 
79  ++pishapei;
80  }
81  dmatrix gg(1,num_separable_calls,1,ishape);
82  dmatrix ggb(1,num_separable_calls,1,ishape);
83  dmatrix uu(1,num_separable_calls,1,ishape);
84  dmatrix uub(1,num_separable_calls,1,ishape);
85  dvector ff(1,num_separable_calls);
86  dvector ffb(1,num_separable_calls);
87  ivector icon(1,num_separable_calls);
88  icon.initialize();
89  ffb=1.e+100;
90 
91  double f=0.0;
92  double fb=1.e+100;
93  dvector g(1,usize);
94  dvector ub(1,usize);
95  independent_variables u(1,usize);
96  gradcalc(0,g);
97  fmc1.itn=0;
98  fmc1.ifn=0;
99  fmc1.ireturn=0;
100  initial_params::xinit(u); // get the initial values into the
101  fmc1.ialph=0;
102  fmc1.ihang=0;
103  fmc1.ihflag=0;
104 
105  if (init_switch)
106  {
107  u.initialize();
108  }
109 
110  std::ostream& output_stream = get_output_stream();
111  for (int ii=1;ii<=2;ii++)
112  {
113  // get the initial u into the uu's
114  dvector* puui = &uu(1);
115  for (int i=1;i<=num_separable_calls;i++)
116  {
117  int m=(*derindex)(i).indexmax();
118  double* puuij = puui->get_v() + 1;
119  for (int j=1;j<=m;j++)
120  {
121  *puuij = u((*derindex)(i)(j));
122 
123  ++puuij;
124  }
125  ++puui;
126  }
127 
128 #ifdef DIAG
129  bool loop_flag = false;
130  int loop_counter = 0;
131 #endif
132 
133  fmc1.dfn=1.e-2;
134  dvariable pen=0.0;
135  int converged=0;
136  int initrun_flag=1;
137  while (converged==0)
138  {
139 #ifdef DIAG
140  if (loop_flag) loop_counter++;
141  if (loop_counter > 18)
142  {
143  cout << loop_counter;
144  }
145 #endif
146  if (!initrun_flag)
147  {
148  converged=1;
149  }
150  for (int i=1;i<=num_separable_calls;i++)
151  {
152  if (ishape(i)>0) //check to see if there are any active randoem effects
153  { // in this function call
154  if (!icon(i))
155  {
156  independent_variables& uuu=*(independent_variables*)(&(uu(i)));
157  (pfmc1[i])->fmin(ff[i],uuu,gg(i));
158  gmax(i)=fabs(pfmc1[i]->gmax);
159  if (!initrun_flag)
160  {
161  if (gmax(i)<1.e-4 || pfmc1[i]->ireturn<=0)
162  {
163  icon(i)=1;
164  }
165  else
166  {
167  converged=0;
168  }
169  }
170  }
171  }
172  }
173  initrun_flag=0;
174  for (int i2=1;i2<=num_separable_calls;i2++)
175  {
176  int m=(*derindex)(i2).indexmax();
177  for (int j=1;j<=m;j++)
178  {
179  u((*derindex)(i2)(j))=uu(i2,j);
180  }
181  }
182  // put the
183  //if (fmc1.ireturn>0)
184  {
185  dvariable vf=0.0;
188 
189  //num_separable_calls=0;
190 
191  pmin->inner_opt_flag=1;
192  pfmin->AD_uf_inner();
193  pmin->inner_opt_flag=0;
194 
195  if (saddlepointflag)
196  {
198  }
200  {
202  }
204 
206  vf+=pen;
207 
208  gradcalc(usize,g);
209  dvector* pggi = &gg(1);
210  for (int i=1;i<=num_separable_calls;i++)
211  {
212  int m=(*derindex)(i).indexmax();
213  double* pggij = pggi->get_v() + 1;
214  for (int j=1;j<=m;j++)
215  {
216  *pggij = g((*derindex)(i)(j));
217 
218  ++pggij;
219  }
220 
221  ++pggi;
222  }
223  /*
224  {
225  ofstream ofs("l:/temp1.dat");
226  ofs << g.indexmax() << " " << setprecision(15) << g << endl;
227  }
228  */
229  if (saddlepointflag==2)
230  {
231  ff[1]=-(*separable_function_difference)(1);
232  for (int i=2;i<=num_separable_calls;i++)
233  {
234  ff[i]=-(*separable_function_difference)(i);
235  //ff[i]=-(*separable_function_difference)(i)
236  // +(*separable_function_difference)(i-1);
237 
238  if (ff[i] < ffb[i])
239  {
240  ffb[i]=ff[i];
241  uub[i]=uu[i];
242  ggb[i]=gg[i];
243  }
244  }
245  }
246  else
247  {
248  ff[1]=(*separable_function_difference)(1);
249  for (int i=2;i<=num_separable_calls;i++)
250  {
251  ff[i]=(*separable_function_difference)(i);
252  //ff[i]=(*separable_function_difference)(i)
253  // -(*separable_function_difference)(i-1);
254 
255  if (ff[i] < ffb[i])
256  {
257  ffb[i]=ff[i];
258  uub[i]=uu[i];
259  ggb[i]=gg[i];
260  }
261  }
262  }
263  f=0.0;
264  for (int i2=1;i2<=num_separable_calls;i2++)
265  {
266  f+=ff[i2];
267  }
268  if (f<fb)
269  {
270  fb=f;
271  ub=u;
272  }
273  }
274  u=ub;
275  }
276  double tmax=max(gmax);
277  output_stream << " inner maxg = " << std::scientific << setprecision(10) << tmax;
278  if (tmax< 1.e-4) break;
279  }
280  fmc1.ireturn=0;
281  fmc1.fbest=fb;
283  //num_separable_calls=0;
284  pmin->inner_opt_flag=1;
285  pfmin->AD_uf_inner();
286  pmin->inner_opt_flag=0;
288  {
290  }
292  for (int i=1;i<=num_separable_calls;i++)
293  {
294  if (pfmc1[i])
295  {
296  delete pfmc1[i];
297  }
298  }
299  pfmc1++;
300  delete [] pfmc1;
301  pfmc1 = 0;
302  return u;
303 }
long ialph
Definition: fvar.hpp:3190
virtual void AD_uf_inner()
Definition: xmodelm4.cpp:40
double crit
Definition: fvar.hpp:3184
static void set_NO_DERIVATIVES(void)
Disable accumulation of derivative information.
Definition: gradstrc.cpp:641
Description not yet available.
#define x
Vector of double precision numbers.
Definition: dvector.h:50
long maxfn
Definition: fvar.hpp:3182
void fmin(double f, const independent_variables &x, const dvector &g, const int &n, const dvector &w, const dvector &h, const fmm_control &fmc)
df1_two_variable fabs(const df1_two_variable &x)
Definition: df12fun.cpp:891
static dvariable reset(const dvar_vector &x)
Definition: model.cpp:345
ADMB variable vector.
Definition: fvar.hpp:2172
double gmax
maximum gradient
Definition: fvar.hpp:3303
void gradcalc(int nvar, const dvector &g)
Definition: sgradclc.cpp:77
int * get_v() const
Definition: ivector.h:114
Description not yet available.
Definition: fvar.hpp:1937
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
long ihang
Definition: fvar.hpp:3192
static objective_function_value * pobjfun
Definition: admodel.h:2394
Description not yet available.
long ifn
Definition: fvar.hpp:3188
static int no_stuff
Definition: df1b2lp9.cpp:18
static void xinit(const dvector &x)
Definition: model.cpp:226
Functions and variables for function minimizer.
Definition: fvar.hpp:3290
Description not yet available.
Definition: fvar.hpp:2819
int use_control_c
Definition: fvar.hpp:3199
void initialize(void)
Initialze all elements of dvector to zero.
Definition: dvect5.cpp:10
std::ostream & get_output_stream()
Definition: adglobl.cpp:45
void initialize(void)
Description not yet available.
Definition: ivec2.cpp:17
dvector get_uhat_quasi_newton_block_diagonal(const dvector &x, function_minimizer *pfmin)
Description not yet available.
Definition: df1b2lp9.cpp:34
long ihflag
Definition: fvar.hpp:3191
fmm * pfmm
Definition: df1b2lp9.cpp:27
Description not yet available.
Description not yet available.
Definition: admodel.h:1850
static void set_YES_DERIVATIVES(void)
Enable accumulation of derivative information.
Definition: gradstrc.cpp:650
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
long iprint
Definition: fvar.hpp:3183
#define max(a, b)
Definition: cbivnorm.cpp:189
static int get_num_quadratic_prior(void)
Definition: df1b2fun.h:1916
long int itn
Definition: fvar.hpp:3301
double *& get_v(void)
Definition: dvector.h:148
int ireturn
Definition: fvar.hpp:3197
static double fun_without_pen
Definition: admodel.h:2395
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
static void get_M_calculations(void)
Description not yet available.
Definition: quadpri.cpp:30