ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
shared.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  */
7 #include <admodel.h>
8 
9 # if defined(USE_SHARE_FLAGS)
10  static int integer(const index_type& it)
11  {
12  return it.integer();
13  }
14 
16  {
17  cerr << " Error -- shared_size_count not defined for this class"
18  << endl;
19  ad_exit(1);
20  return 0;
21  }
22 
24  const int& ii,const dvariable& pen)
25  {
26  cerr << " Error -- shared_scaled_set_value_inv not defined for this class"
27  << endl;
28  ad_exit(1);
29  }
30 
31  void initial_params::shared_set_value_inv(const dvector& x,const int& ii)
32  {
33  cerr << " Error -- shared_scaled_set_value_inv not defined for this class"
34  << endl;
35  ad_exit(1);
36  }
38  {
40  {
42  }
43  return share_flags->get_maxshare();
44  }
45 
47  {
49  {
51  }
52  return share_flags->get_maxshare();
53  }
54 
55  void param_init_matrix::shared_set_value_inv(const dvector& _x,const int& _ii)
56  {
57  ADUNCONST(int,ii)
60  //int mmin=share_flags->invflags->indexmin();
61  //int mmax=share_flags->invflags->indexmax();
62  int mmin=it.indexmin();
63  int mmax=it.indexmax();
64  for (int i=mmin;i<=mmax;i++)
65  {
66  x(ii++)=value((*this)(it(i)(1).integer(),it(i)(2).integer()));
67  }
68  }
69 
70  void param_init_vector::shared_set_value_inv(const dvector& _x,const int& _ii)
71  {
72  ADUNCONST(int,ii)
75  int mmin=it.indexmin();
76  int mmax=it.indexmax();
77  for (int i=mmin;i<=mmax;i++)
78  {
79  x(ii++)=value((*this)(it(i).integer()));
80  }
81  }
82 
84  (const dvector& _x,const int& _ii)
85  {
86  ADUNCONST(int,ii)
88  index_type& it=*(share_flags->get_invflags());
89  int mmin=it.indexmin();
90  int mmax=it.indexmax();
91  for (int i=mmin;i<=mmax;i++)
92  {
93  x(ii++)=
94  boundpin((*this)(it(i)(1).integer(),it(i)(2).integer()),
95  minb,maxb);
96  }
97  }
98 
100  const int& _ii,const dvariable& pen)
101  {
102  ADUNCONST(int,ii)
104  int mmin=indexmin();
105  int mmax=indexmax();
108  for (int i=mmin;i<=mmax;i++)
109  {
110  int jmin=(*this)(i).indexmin();
111  int jmax=(*this)(i).indexmax();
112  for (int j=jmin;j<=jmax;j++)
113  {
114  int afvalue=integer(af(i)(j));
115  if (afvalue && afvalue<=current_phase)
116  {
117  int is=sf(i)(j).integer();
118  (*this)(i,j)=x(ii+is-1);
119  }
120  }
121  }
122  ii+=share_flags->get_maxshare();
123  }
124 
126  const int& _ii,const dvariable& pen)
127  {
128  ADUNCONST(int,ii)
130  int mmin=indexmin();
131  int mmax=indexmax();
134  for (int i=mmin;i<=mmax;i++)
135  {
136  int afvalue=integer(af(i));
137  if (afvalue && afvalue<=current_phase)
138  {
139  int is=sf(i).integer();
140  (*this)(i)=x(ii+is-1);
141  }
142  }
143  ii+=share_flags->get_maxshare();
144  }
145 
147  const int& _ii,const dvariable& pen)
148  {
149  ADUNCONST(int,ii)
151  int mmin=indexmin();
152  int mmax=indexmax();
155  for (int i=mmin;i<=mmax;i++)
156  {
157  int jmin=(*this)(i).indexmin();
158  int jmax=(*this)(i).indexmax();
159  for (int j=jmin;j<=jmax;j++)
160  {
161  int afvalue=integer(af(i)(j));
162  if (afvalue && afvalue<=current_phase)
163  {
164  int is=sf(i)(j).integer();
165  //(*this)(i,j)=x(ii+is-1);
166  (*this)(i,j)=boundp(x(ii+is-1),minb,maxb,pen);
167  }
168  }
169  }
170  ii+=share_flags->get_maxshare();
171  }
172 
174  const index_type& af)
175  {
176  cerr << " setshare not yet defined for this class " << endl;
177  ad_exit(1);
178  }
179 
181  {
182  delete sflags;
183  delete original_sflags;
184  delete aflags;
185  delete invflags;
186  delete bmap;
187  sflags=0;
188  aflags=0;
189  original_sflags=0;
190  invflags=0;
191  current_phase=-1;
192  maxshare=0;
193  bmap=0;
194  }
195 
197  {
198  return current_phase;
199  }
201  {
202  return original_sflags;
203  }
205  {
206  return sflags;
207  }
209  {
210  return *bmap;
211  }
213  {
214  return invflags;
215  }
217  {
218  return aflags;
219  }
221  {
222  invflags=new index_type(sf);
223  }
224  void shareinfo::set_bmap(const i3_array & _bmap)
225  {
226  bmap=new i3_array(_bmap);
227  }
228  void shareinfo::reset_bmap(const i3_array & _bmap)
229  {
230  if (bmap)
231  {
232  delete bmap;
233  bmap=0;
234  }
235  bmap=new i3_array(_bmap);
236  }
238  {
239  if (sflags)
240  {
241  delete sflags;
242  sflags=0;
243  }
244  sflags=new index_type(sf);
245  }
247  {
248  original_sflags=new index_type(sf);
249  }
251  {
252  sflags=new index_type(sf);
253  }
255  {
256  aflags=new index_type(af);
257  }
259  {
260  set_shareflags(sf);
262  set_activeflags(af);
263  invflags=0;
264  current_phase=-1;
265  bmap = new i3_array();
266  dimension = 0;
267  }
268 
270  {
271  if (current_phase != _cf)
272  {
273  current_phase = _cf;
274  const index_type& sf=*(get_original_shareflags());
275  const index_type& af=*(get_activeflags());
276  if (sf.dimension() !=2)
277  {
278  cerr << "error " << endl;
279  ad_exit(1);
280  }
281  int imin= sf.indexmin();
282  int imax= sf.indexmax();
283  int fmin = 0, fmax = 0;
284  int ibreak=0;
285  // get intial values for min and max active flag values
286  for (int i=imin;i<=imax;i++)
287  {
288  int jmin= sf(i).indexmin();
289  int jmax= sf(i).indexmax();
290  for (int j=jmin;j<=jmax;j++)
291  {
292  int afvalue=integer(af(i)(j));
293  if (afvalue && afvalue<=current_phase)
294  {
295  fmax=integer(sf(i)(j));
296  fmin=integer(sf(i)(j));
297  ibreak=1;
298  break;
299  }
300  }
301  if (ibreak)
302  break;
303  }
304  // get initial values for minimum and maximum shared
305  // flags -- not just active ones
306  int fmax1=integer(sf(imin)(sf(imin).indexmin()));
307  int fmin1=integer(sf(imin)(sf(imin).indexmin()));
308  for (int i=imin;i<=imax;i++)
309  {
310  int jmin= sf(i).indexmin();
311  int jmax= sf(i).indexmax();
312  for (int j=jmin;j<=jmax;j++)
313  {
314  fmax1=max(fmax1,integer(sf(i)(j)));
315  fmin1=min(fmin1,integer(sf(i)(j)));
316  int afvalue=integer(af(i)(j));
317  if (afvalue && afvalue<=current_phase)
318  {
319  fmax=max(fmax,integer(sf(i)(j)));
320  fmin=min(fmin,integer(sf(i)(j)));
321  }
322  }
323  }
324  // set up info for sanity test on shared and active flags
325  ivector icount2(fmin1,fmax1);
326  icount2.initialize();
327  for (int i=imin;i<=imax;i++)
328  {
329  int jmin= sf(i).indexmin();
330  int jmax= sf(i).indexmax();
331  for (int j=jmin;j<=jmax;j++)
332  {
333  int sfvalue=integer(sf(i)(j));
334  icount2(sfvalue)++;
335  }
336  }
337  i3_array bmap2(fmin1,fmax1,1,icount2,1,2);
338  icount2.initialize();
339  for (int i=imin;i<=imax;i++)
340  {
341  int jmin= sf(i).indexmin();
342  int jmax= sf(i).indexmax();
343  for (int j=jmin;j<=jmax;j++)
344  {
345  int sfvalue=integer(sf(i)(j));
346  int ind=++icount2(sfvalue);
347  bmap2(sfvalue,ind,1)=i;
348  bmap2(sfvalue,ind,2)=j;
349  }
350  }
351  for (int k=fmin1;k<=fmax1;k++)
352  {
353  int lmin=bmap2(k).indexmin();
354  int lmax=bmap2(k).indexmax();
355 
356  if (lmax>0)
357  {
358  int i1=bmap2(k,lmin,1);
359  int j1=bmap2(k,lmin,2);
360  int a1=integer(af(i1)(j1));
361 
362  for (int l=lmin+1;l<=lmax;l++)
363  {
364  int i2=bmap2(k,l,1);
365  int j2=bmap2(k,l,2);
366  int a2=integer(af(i2)(j2));
367  if (a1 !=a2)
368  {
369  cerr << "Sanity error in grouping/active flags"
370  << endl << "for indices (" << i2 << "," << j2 << ")"
371  << endl;
372  ad_exit(1);
373  }
374  }
375  }
376  }
377 
378  // indirect will cotain pointers for the number
379  // of active parameters it starts out with the
380  // number of shared flags and removes the inactive ones
381  ivector indirect(fmin1,fmax1);
382  ivector processed_flag(fmin1,fmax1);
383  processed_flag.initialize();
384  ivector mindx(imin,imax);
385  ivector maxx(imin,imax);
386  indirect.fill_seqadd(1,1);
387  for (int i=imin;i<=imax;i++)
388  {
389  int jmin= sf(i).indexmin();
390  int jmax= sf(i).indexmax();
391  mindx(i)=jmin;
392  maxx(i)=jmax;
393  for (int j=jmin;j<=jmax;j++)
394  {
395  int afvalue=integer(af(i)(j));
396  if (afvalue==0 || afvalue>current_phase)
397  {
398  int in=integer(sf(i)(j));
399  if (processed_flag(in)==0)
400  {
401  processed_flag(in)=1;
402  for (int k=in;k<=fmax1;k++)
403  {
404  indirect(k)-=1;
405  }
406  }
407  }
408  }
409  }
410  imatrix tmp1(imin,imax,mindx,maxx);
411  for (int i=imin;i<=imax;i++)
412  {
413  int jmin= sf(i).indexmin();
414  int jmax= sf(i).indexmax();
415  for (int j=jmin;j<=jmax;j++)
416  {
417  int afvalue=integer(af(i)(j));
418  if (afvalue && afvalue<=current_phase)
419  {
420  tmp1(i,j)=indirect(integer(sf(i)(j)));
421  }
422  else
423  {
424  tmp1(i,j)=0;
425  }
426  }
427  }
428  int itmp=indirect(fmax1);
429  imatrix tmp(1,itmp,1,2);
430  ivector counter(1,itmp);
431  counter.initialize();
432  for (int i=imin;i<=imax;i++)
433  {
434  int jmin= sf(i).indexmin();
435  int jmax= sf(i).indexmax();
436  for (int j=jmin;j<=jmax;j++)
437  {
438  int afvalue=integer(af(i)(j));
439  if (afvalue && afvalue<=current_phase)
440  {
441  if (++counter(tmp1(i,j))==1)
442  {
443  tmp(tmp1(i,j),1)=i;
444  tmp(tmp1(i,j),2)=j;
445  }
446  }
447  }
448  }
449  i3_array _bmap(1,itmp,1,counter,1,2);
450 
451  counter.initialize();
452  _bmap.initialize();
453  for (int i=imin;i<=imax;i++)
454  {
455  int jmin= sf(i).indexmin();
456  int jmax= sf(i).indexmax();
457  for (int j=jmin;j<=jmax;j++)
458  {
459  int afvalue=integer(af(i)(j));
460  if (afvalue && afvalue<=current_phase)
461  {
462  int ind=++counter(tmp1(i,j));
463  _bmap(tmp1(i,j),ind,1)=i;
464  _bmap(tmp1(i,j),ind,2)=j;
465  }
466  }
467  }
468  {
469  cout << endl;
470  for (int i=1;i<=itmp;i++)
471  cout << _bmap(i) << endl << endl;
472  }
473  set_bmap(_bmap);
474 
475  get_maxshare()=itmp;
476  reset_shareflags(tmp1);
477  set_invflags(tmp);
478  cout << tmp1 << endl;
479  //return tmp1;
480  }
481  }
482 
484  {
485  if (current_phase != _cf)
486  {
487  current_phase = _cf;
488  const index_type& sf=*(get_original_shareflags());
489  const index_type& af=*(get_activeflags());
490  if (sf.dimension() !=1)
491  {
492  cerr << "error " << endl;
493  ad_exit(1);
494  }
495  int imin= sf.indexmin();
496  int imax= sf.indexmax();
497  //int ibreak=0;
498  // get intial values for min and max active flag values
499  // get initial values for minimum and maximum shared
500  // flags -- not just active ones
501  int fmax1=integer(sf(imin));
502  int fmin1=integer(sf(imin));
503  for (int i=imin;i<=imax;i++)
504  {
505  fmax1=max(fmax1,integer(sf(i)));
506  fmin1=min(fmin1,integer(sf(i)));
507  }
508 
509  ivector indirect(fmin1,fmax1);
510  ivector processed_flag(fmin1,fmax1);
511  processed_flag.initialize();
512  indirect.fill_seqadd(1,1);
513  for (int i=imin;i<=imax;i++)
514  {
515  {
516  int afvalue=integer(af(i));
517  if (afvalue==0 || afvalue>current_phase)
518  {
519  int in=integer(sf(i));
520  if (processed_flag(in)==0)
521  {
522  processed_flag(in)=1;
523  for (int k=in;k<=fmax1;k++)
524  {
525  indirect(k)-=1;
526  }
527  }
528  }
529  }
530  }
531  ivector tmp1(imin,imax);
532  for (int i=imin;i<=imax;i++)
533  {
534  {
535  int afvalue=integer(af(i));
536  if (afvalue && afvalue<=current_phase)
537  {
538  tmp1(i)=indirect(integer(sf(i)));
539  }
540  else
541  {
542  tmp1(i)=0;
543  }
544  }
545  }
546  int itmp=indirect(fmax1);
547  ivector tmp(1,itmp);
548  ivector counter(1,itmp);
549  counter.initialize();
550  for (int i=imin;i<=imax;i++)
551  {
552  int afvalue=integer(af(i));
553  if (afvalue && afvalue<=current_phase)
554  {
555  if (++counter(tmp1(i))==1)
556  {
557  tmp(tmp1(i))=i;
558  }
559  }
560  }
561  get_maxshare()=itmp;
562  reset_shareflags(tmp1);
563  set_invflags(tmp);
564  }
565  }
566 
568  {
569  share_flags = new shareinfo(sf,af);
570  int idim1= share_flags->get_shareflags()->dimension();
571  share_flags->get_dimension()=idim1;
572  //int idim2= share_flags->get_activeflags()->dimension();
573  if (idim1==2)
574  {
575  cout << " Error dim too high" << endl;
576  ad_exit(1);
577  }
578  // check rationality
579  /*
580  int mmin1= share_flags->get_shareflags()->indexmin();
581  int mmax1= share_flags->get_shareflags()->indexmax();
582  int mmin2= share_flags->get_activeflags()->indexmin();
583  int mmax2= share_flags->get_activeflags()->indexmax();
584  int mmin=indexmin();
585  int mmax=indexmax();
586  if (mmin1 != mmin || mmax1 != mmax ||
587  mmin2 != mmin || mmax2 != mmax)
588  {
589  cerr << "sanity error" << endl;
590  ad_exit(1);
591  }
592  */
594  }
595 
597  {
598  share_flags = new shareinfo(sf,af);
599  int idim1= share_flags->get_shareflags()->dimension();
600  share_flags->get_dimension()=idim1;
601  int idim2= share_flags->get_activeflags()->dimension();
602  switch (idim1)
603  {
604  case 1:
606  break;
607  case 2:
608  {
609  cout << idim1 << " " << idim2 << endl;
610  // check rationality
611  int mmin1= share_flags->get_shareflags()->indexmin();
612  int mmax1= share_flags->get_shareflags()->indexmax();
613  int mmin2= share_flags->get_activeflags()->indexmin();
614  int mmax2= share_flags->get_activeflags()->indexmax();
615  int mmin=indexmin();
616  int mmax=indexmax();
617  if (mmin1 != mmin || mmax1 != mmax ||
618  mmin2 != mmin || mmax2 != mmax)
619  {
620  cerr << "sanity error" << endl;
621  ad_exit(1);
622  }
624  }
625  break;
626  default:
627  cerr << "Error" << endl;
628  ad_exit(1);
629  }
630  }
631 # endif
index_type * get_shareflags(void)
Definition: shared.cpp:204
~shareinfo()
Definition: shared.cpp:180
Uses polymorphism to get index information from various data types to be used in constructing and all...
Definition: fvar.hpp:7731
virtual void shared_set_value_inv(const dvector &, const int &)
Definition: shared.cpp:31
index_type * original_sflags
Definition: admodel.h:780
Description not yet available.
Definition: imatrix.h:69
int maxshare
Definition: admodel.h:785
index_type * aflags
Definition: admodel.h:781
#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
friend dvector value(const dvar_vector &v1)
Description not yet available.
Definition: fvar_ar3.cpp:43
void fmin(double f, const independent_variables &x, const dvector &g, const int &n, const dvector &w, const dvector &h, const fmm_control &fmc)
void set_shareflags(const index_type &sf)
Definition: shared.cpp:250
virtual int shared_size_count(void)
Definition: shared.cpp:15
int dimension() const
Definition: fvar.hpp:7756
exitptr ad_exit
Definition: gradstrc.cpp:53
virtual void shared_set_value(const dvar_vector &, const int &, const dvariable &pen)
Definition: shared.cpp:146
int & get_dimension(void)
Definition: admodel.h:792
void reset_bmap(const i3_array &af)
Definition: shared.cpp:228
ADMB variable vector.
Definition: fvar.hpp:2172
void set_activeflags(const index_type &af)
Definition: shared.cpp:254
int indexmin() const
Definition: fvar.hpp:4021
index_type * get_activeflags(void)
Definition: shared.cpp:216
void fill_seqadd(int, int)
Fills ivector elements with values starting from base and incremented by offset.
Definition: cranfill.cpp:73
virtual void setshare(const index_type &sf, const index_type &af)
Definition: shared.cpp:173
void get_inv_matrix_shared(int _cf)
Definition: shared.cpp:269
virtual void shared_set_value_inv(const dvector &, const int &)
Definition: shared.cpp:70
int & get_maxshare(void)
Definition: admodel.h:790
index_type * invflags
Definition: admodel.h:782
shareinfo * share_flags
Definition: admodel.h:818
void set_bmap(const i3_array &af)
Definition: shared.cpp:224
prnstream & endl(prnstream &)
int & get_current_phase(void)
Definition: shared.cpp:196
double boundpin(double x, double fmin, double fmax, double s)
Scale model variable over [-1,1]; constant objects.
Definition: boundfun.cpp:378
Array of integers(int) with indexes from index_min to indexmax.
Definition: ivector.h:50
static int current_phase
Definition: admodel.h:842
virtual void shared_set_value_inv(const dvector &x, const int &ii)
Definition: shared.cpp:55
Description not yet available.
Definition: admodel.h:777
#define min(a, b)
Definition: cbivnorm.cpp:188
int dimension
Definition: admodel.h:784
Description not yet available.
i3_array & get_bmap(void)
Definition: shared.cpp:208
int indexmax() const
Definition: fvar.hpp:7764
index_type * get_original_shareflags(void)
Definition: shared.cpp:200
shareinfo(const index_type &sf, const index_type &af)
Definition: shared.cpp:258
void reset_shareflags(const index_type &sf)
Definition: shared.cpp:237
virtual void shared_set_value(const dvar_vector &, const int &, const dvariable &pen)
Definition: shared.cpp:125
int indexmin() const
Definition: fvar.hpp:2287
void initialize(void)
Description not yet available.
Definition: ivec2.cpp:17
void get_inv_vector_shared(int _cf)
Definition: shared.cpp:483
int indexmax(void) const
Definition: fvar.hpp:2572
long int integer
Definition: cbivnorm.cpp:31
i3_array * bmap
Definition: admodel.h:783
virtual int shared_size_count(void)
Definition: shared.cpp:37
virtual void setshare(const index_type &sf, const index_type &af)
Definition: shared.cpp:567
int integer() const
Definition: indextyp.cpp:181
int indexmax() const
Definition: fvar.hpp:4025
dvariable boundp(const prevariable &x, double fmin, double fmax, const prevariable &_fpen, double s)
Compute penalty for exceeding bounds on parameter; variable ojbects.
Definition: boundfun.cpp:89
virtual void shared_set_value(const dvar_vector &, const int &, const dvariable &pen)
Definition: shared.cpp:99
void set_original_shareflags(const index_type &sf)
Definition: shared.cpp:246
virtual void setshare(const index_type &sf, const index_type &af)
Definition: shared.cpp:596
virtual void shared_set_value_inv(const dvector &, const int &)
Definition: shared.cpp:84
int current_phase
Definition: admodel.h:786
void initialize(int sl, int sh, int nrl, const ivector &nrh, int ncl, const ivector &nch)
index_type * get_invflags(void)
Definition: shared.cpp:212
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
Description not yet available.
Definition: fvar.hpp:3944
virtual int shared_size_count(void)
Definition: shared.cpp:46
#define max(a, b)
Definition: cbivnorm.cpp:189
void set_invflags(const index_type &af)
Definition: shared.cpp:220
int indexmin(void) const
Definition: fvar.hpp:2568
virtual void shared_set_value(const dvar_vector &, const int &, const dvariable &pen)
Definition: shared.cpp:23
int indexmax() const
Definition: fvar.hpp:2292
Fundamental data type for reverse mode automatic differentiation.
Definition: fvar.hpp:1518
index_type * sflags
Definition: admodel.h:779
int indexmin() const
Definition: fvar.hpp:7760