ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
model7.cpp
Go to the documentation of this file.
1 
7 #include <iostream>
8 #include <df1b2fun.h>
9 #include <admodel.h>
10 #include <signal.h>
11 
12 //#include <parallel.h>
13 
14 #ifdef __MINGW64__
15  #include <cassert>
16 #endif
17 
19 {
20  size_t n = _s.size();
21  size_t i = n - 1;
22  for (; i >= 1; i--)
23  {
24 #if defined(_WIN32)
25  if (_s(i) == '\\' || _s(i) == '/' || _s(i) == ':') break;
26 #else
27  if (_s(i) == '/' || _s(i) == ':') break;
28 #endif
29  }
30 
31  adstring s(1, n - i);
32  s = _s(i + 1, n);
33 
34  return s;
35 }
36 
38 {
39  signal(SIGFPE,exit_handler);
40  signal(SIGSEGV,exit_handler);
41  signal(SIGILL,exit_handler);
42  signal(SIGINT,exit_handler);
43 }
44 
45 ad_comm::ad_comm(int _argc,char * _argv[])
46 {
47  if (option_match(_argc,_argv,"-version") > -1
48  || option_match(_argc,_argv,"--version") > -1)
49  {
50  void banner(const adstring& program_name);
51  banner(_argv[0]);
52 
53  ad_exit(0);
54  }
55 
56  ad_comm::argc=_argc;
57  ad_comm::argv=_argv;
58 #ifdef DIAG_TIMER
59  if (option_match(_argc,_argv,"-time")>-1)
60  {
61  time_flag=1;
62  }
63  else
64  {
65  time_flag=0;
66  }
67  if (time_flag)
68  {
69  if (!ptm)
70  {
71  ptm=new adtimer();
72  }
73  if (!ptm1)
74  {
75  ptm1=new adtimer();
76  }
77  }
78 #endif
79  no_atlas_flag=0;
80  if (option_match(_argc,_argv,"-noatlas")>-1) no_atlas_flag=1;
81 
82 #if defined(USE_ADPVM)
83  int pvm_flag=0;
84  if (option_match(_argc,_argv,"-slave")>-1) pvm_flag=2;
85  if (option_match(_argc,_argv,"-master")>-1) pvm_flag=1;
86 
87  if (pvm_flag)
88  pvm_manager = new adpvm_manager(pvm_flag);
89  else
90  pvm_manager = NULL;
91 
92  if (pvm_manager)
93  {
94  if (pvm_manager->mode==2) //slave
95  {
96  int on=0; int nopt=0;
97  if ( (on=option_match(_argc,_argv,"-slave",nopt))>-1)
98  {
99  if (nopt ==1)
100  {
101  pvm_manager->slave_number=atoi(ad_comm::argv[on+1]);
102  }
103  else
104  {
105  cerr << "Wrong number of options to -slave -- must be 1"
106  " you have " << nopt << endl;
107  ad_exit(1);
108  }
109  }
110  if ( (on=option_match(_argc,_argv,"-slavedir",nopt))>-1)
111  {
112  if (nopt ==1)
113  {
114  ad_chdir(_argv[on+1]);
115  }
116  else
117  {
118  cerr << "Wrong number of options to -slavedir -- must be 1"
119  " you have " << nopt << endl;
120  }
121  }
122  }
123  }
124 #endif
125 
126  /*
127  if (option_match(_argc,_argv,"-gui")>-1)
128  {
129  void vm_initialize(void);
130  vm_initialize();
131  }
132  */
135  //int len=strlen(_argv[0]);
136  //for (int i=1;i<=len;i++) adprogram_name[i]=tolower(adprogram_name[i]);
137  adstring workdir;
138  ad_getcd(workdir);
139  if (_argc>1)
140  {
141  if (option_match(_argc,_argv,"-?")>-1
142  || option_match(_argc,_argv,"-help")>-1
143  || option_match(_argc,_argv,"--help")>-1)
144  {
145  // remove path (if user runs -help)
146  for (size_t i = adprogram_name.size(); i >= 1; i--)
147  {
148 #ifdef _WIN32
149  if (adprogram_name(i) == '\\')
150 #else
151  if (adprogram_name(i) == '/')
152 #endif
153  {
155  break;
156  }
157  }
158 
159  //(*ad_printf)(" %s", (char*)admb_banner);
160  ad_printf( "Usage: %s [options]\n\n",(char*)(adprogram_name));
161 
162  ad_printf( "Options:\n");
163  ad_printf( " -ainp FILE change default ascii input parameter "
164  "filename to FILE\n");
165  ad_printf( " -binp FILE change default binary input parameter "
166  "filename to FILE\n");
167  ad_printf( " -est only do the parameter estimation\n");
168  ad_printf( " -noest do not do the parameter estimation "
169  "(optimization)\n");
170  ad_printf( " -ind FILE change default input data filename to "
171  "FILE\n");
172  ad_printf( " -lmn N use limited memory quasi newton -- keep "
173  "N steps\n");
174  ad_printf( " -lmn2 N use other limited memory quasi newton -- "
175  "keep N steps\n");
176  ad_printf( " -ilmn N use other limited memory quasi newton "
177  "for random effects models - keep N steps\n");
178  ad_printf( " -dd N check derivatives after N function "
179  "evaluations\n");
180  ad_printf( " -lprof perform profile likelihood "
181  "calculations\n");
182  ad_printf( " -maxph N increase the maximum phase number to "
183  "N\n");
184  ad_printf( " -mcdiag use diagonal covariance matrix for mcmc "
185  "with diagonal values 1\n");
186  ad_printf( " -mcmc [N] perform markov chain monte carlo with N "
187  "simulations\n");
188  ad_printf( " -mcmult N multiplier N for mcmc default\n");
189  ad_printf( " -mcr resume previous mcmc\n");
190  ad_printf( " -mcrb N reduce amount of correlation in the "
191  "covariance matrix 1<=N<=9\n");
192  ad_printf( " -mcnoscale don't rescale step size for mcmc "
193  "depending on acceptance rate\n");
194  ad_printf( " -nosdmcmc turn off mcmc histogram calcs to make "
195  "mcsave run faster\n");
196  ad_printf( " -mcprobe N use probing strategy for mcmc with "
197  "factor N\n");
198  ad_printf( " -mcgrope N Deprecated, same as -mcprobe\n");
199  ad_printf( " -mcseed N seed for random number generator for "
200  "markov chain monte carlo\n");
201  ad_printf( " -mcscale N rescale step size for first N "
202  "evaluations\n");
203  ad_printf( " -mcsave N save the parameters for every Nth "
204  "simulation\n");
205  ad_printf( " -mceval go through the saved mcmc values from a "
206  "previous mcsave\n");
207  ad_printf( " -nuts MCMC draws with the no-U-turn sampler\n");
208  ad_printf( " -rwm MCMC draws with a Metopolis sampler.\n");
209  ad_printf( " -mcu use uniformly distributed steps for "
210  "mcmc instead of random normal\n");
211  ad_printf( " -crit N1,N2,... set gradient magnitude convergence "
212  "criterion to N\n");
213  ad_printf( " -iprint N print out function minimizer report "
214  "every N iterations (default %i).\n", defaults::iprint);
215  ad_printf( " -maxfn N1,N2,.. set maximum number opf function eval's "
216  "to N\n");
217  ad_printf( " -rs if function minimizer can't make "
218  "progress rescale and try again\n");
219  //(*ad_printf)( " -sp for DLL running from splus write to "
220  //"command window\n");
221  ad_printf( " -nox suppress vector and gradient values in "
222  "minimizer screen report\n");
223  ad_printf( " -phase N start minimization in phase N\n");
224  ad_printf( " -simplex use simplex for minimization -- "
225  "deprecated, use -neldmead\n");
226  ad_printf( " -neldmead use Nelder-Mead simplex algorithm for "
227  "minimization\n");
228  ad_printf( " -nohess don't do hessian or delta method for std "
229  "dev\n");
230  ad_printf( " -eigvec calculate eigenvectors of the Hessian\n");
231  ad_printf( " -sdonly do delta method for std dev estimates "
232  "without redoing hessian\n");
233  ad_printf( " -ams N set arrmblsize to N "
234  "(ARRAY_MEMBLOCK_SIZE)\n");
235  ad_printf( " -cbs N set CMPDIF_BUFFER_SIZE to N "
236  "(ARRAY_MEMBLOCK_SIZE)\n");
237  ad_printf( " -mno N set the maximum number of independent "
238  "variables to N\n");
239  ad_printf( " -mdl N set the maximum number of dvariables to "
240  "N\n");
241  ad_printf( " -gbs N set GRADSTACK_BUFFER_SIZE to N "
242  "(ARRAY_MEMBLOCK_SIZE)\n");
243  ad_printf( " -hess_step N take N Newton steps with inverse Hessian\n");
244  ad_printf( " -hess_step_tol eps set hess_step tolerance to eps\n");
245  ad_printf( " -mip N set maximum the number of initial parameters "
246  "to a value N that is greater than zero (default is 4000).\n");
247 #if defined(USE_ADPVM)
248  ad_printf( " -master run as PVM master program\n");
249  ad_printf( " -slave run as PVM slave program\n");
250  ad_printf( " -pvmtime record timing information for PVM "
251  "performance analysis\n");
252 #endif
253  ad_printf( " -display N N sets display output with \'0\' for quiet display, "
254  "\'1\' for concise display, or \'2\' for legacy display (default is %i).\n",
256  ad_printf( " -info show how to cite ADMB, license, and "
257  "acknowledgements\n");
258  ad_printf( " -version show version information\n");
259  ad_printf( " -help show this message\n\n");
260  //if (function_minimizer::random_effects_flag)
261  //{
262  ad_printf( "Random effects options if applicable\n");
263  ad_printf( " -nr N maximum number of Newton-Raphson "
264  "steps\n");
265  ad_printf( " -imaxfn N maximum number of evals in quasi-Newton "
266  "inner optimization\n");
267  ad_printf( " -is N set importance sampling size to N\n");
268  ad_printf( " -isf N set importance sampling size funnel "
269  "blocks to N\n");
270  ad_printf( " -isdiag print importance sampling diagnostics\n");
271  ad_printf( " -hybrid do hybrid Monte Carlo version of MCMC\n");
272  ad_printf( " -hbf set the hybrid bounded flag for bounded "
273  "parameters\n");
274  ad_printf( " -hyeps mean step size for hybrid Monte Carlo\n");
275  ad_printf( " -hynstep number of steps for hybrid Monte "
276  "Carlo\n");
277  ad_printf( " -noinit do not initialize RE before inner "
278  "optimization\n");
279  ad_printf( " -ndi N set maximum number of separable calls\n");
280  ad_printf( " -ndb N set number of blocks for RE derivatives "
281  "(reduce temp file size)\n");
282  ad_printf( " -ddnr use high precision Newton-Raphson, for "
283  "banded Hessian case only\n");
284  ad_printf( " -nrdbg verbose reporting for debugging "
285  "newton-raphson\n");
286 # if defined(__MINI_MAX__)
287  ad_printf( " -mm N do minimax optimization\n");
288 # endif
289  ad_printf( " -shess use sparse Hessian structure inner "
290  "optimzation\n\n");
291 
292  ad_printf("Read online documentation at http://admb-project.org\n");
293  ad_printf("Contact <users@admb-project.org> for help.\n");
294  //}
295  ad_exit(0);
296  }
297  else if (option_match(_argc,_argv,"-info") > -1)
298  {
299  ad_printf("ADMB Information\n");
300  ad_printf("================\n\n");
301 
302  ad_printf("How to Cite ADMB\n");
303  ad_printf("----------------\n\n");
304 
305  ad_printf("Fournier, D.A., H.J. Skaug, J. Ancheta, J. Ianelli, "
306  "A. Magnusson, M.N. Maunder,\n");
307  ad_printf("A. Nielsen, and J. Sibert. 2012. AD Model Builder: using "
308  "automatic\n");
309  ad_printf("differentiation for statistical inference of highly "
310  "parameterized complex\n");
311  ad_printf("nonlinear models. Optim. Methods Softw. 27:233-249.\n\n");
312 
313  //(*ad_printf)(" %s", (char*)admb_banner);
314  ad_printf("License\n");
315  ad_printf("-------\n\n");
316 
317  ad_printf("Copyright (c) 2008-2021\n");
318  ad_printf("ADMB Foundation and Regents of the University of California\n\n");
319  ad_printf("ADMB is free software and comes with ABSOLUTELY NO "
320  "WARRANTY.\n");
321  ad_printf("You are welcome to redistribute it under certain "
322  "conditions.\n\n");
323  ad_printf("AD Model Builder, or ADMB, was developed by David Fournier "
324  "of Otter Research\n");
325  ad_printf("Ltd, Sidney, BC, Canada. In 2007, scientists from the "
326  "University of Hawai'i at\n");
327  ad_printf("Manoa Pelagic Fisheries Research Program (John Sibert and "
328  "Anders Nielsen) and\n");
329  ad_printf("the Inter-American Tropical Tuna Commission (Mark "
330  "Maunder), in consultation with\n");
331  ad_printf("scientists from NOAA Fisheries (Richard Methot), created "
332  "the non-profit ADMB\n");
333  ad_printf("Foundation (admb-foundation.org) with the goal of "
334  "increasing the number of ADMB\n");
335  ad_printf("users by making the software free and open source. In "
336  "partnership with NOAA\n");
337  ad_printf("Fisheries and the National Center for Ecological Analysis "
338  "and Synthesis (NCEAS,\n");
339  ad_printf("www.nceas.ucsb.edu), the ADMB Foundation obtained funding "
340  "from the Gordon and\n");
341  ad_printf("Betty Moore Foundation (www.moore.org) to acquire the "
342  "copyright to the ADMB\n");
343  ad_printf("software suite, in order to make it broadly and freely "
344  "available to the research\n");
345  ad_printf("community. In 2008 the copyright was transferred from "
346  "Otter Research Ltd to the\n");
347  ad_printf("University of California. The binary files were released "
348  "in November 2008 and\n");
349  ad_printf("the source code was released in December 2009. More "
350  "information about the ADMB\n");
351  ad_printf("Project can be found at admb-project.org.\n\n");
352  ad_printf("ADMB was originally developed by David Fournier of Otter "
353  "Research Ltd.\n\n");
354  ad_printf("It is now maintained by the ADMB Core Team, whose members "
355  "are listed on\n");
356  ad_printf("http://admb-project.org/developers/core-team.\n");
357 
358  ad_exit(0);
359  }
360  }
361  allocate();
362 }
367 {
368  allocate();
369 }
371 {
372 #if defined (_WIN32)
373  directory_prefix='\\';
374 #else
375  directory_prefix='/';
376 #endif
377  adstring tmpstring;
378 
379 #if defined(_MSC_VER)
380  //remove path
381  for (int i = (int)adprogram_name.size(); i >= 1; i--)
382  {
384  {
386  break;
387  }
388  }
389 #endif
390 
391 #if defined(_WIN32)
392  // strip off the .exe
393  #ifdef __MINGW64__
394  size_t _n = adprogram_name.size();
395  assert(_n <= INT_MAX);
396  int n = (int)_n;
397  #else
398  int n = (int)adprogram_name.size();
399  #endif
400  if (n > 4)
401  {
402  if (adprogram_name(n - 3) == '.'
403  && tolower(adprogram_name(n - 2)) == 'e'
404  && tolower(adprogram_name(n - 1)) == 'x'
405  && tolower(adprogram_name(n)) == 'e')
406  {
407  n -= 4;
408  }
409  }
411 #endif
412 
413  // change the working directory name
414  if (argc > 1)
415  {
416  int on=0;
417  if ( (on=option_match(argc,argv,"-wd"))>-1)
418  {
419  if (on>argc-2 || argv[on+1][0] == '-')
420  {
421  cerr << "Invalid input data command line option"
422  " -- ignored" << endl;
423  }
424  else
425  {
426  tmpstring = adstring(argv[on+1]);
427  wd_flag=1;
428  }
429  }
430  }
431  if (length(tmpstring))
432  {
433  if (tmpstring(length(tmpstring)) == directory_prefix)
434  {
435  adprogram_name=tmpstring + adprogram_name;
436  working_directory_path = tmpstring;
437  }
438  else
439  {
442  }
443  }
444 
445 /*
446  tmpstring=adprogram_name + adstring(".dat");
447  if (argc > 1)
448  {
449  int on=0;
450  if ( (on=option_match(argc,argv,"-ind"))>-1)
451  {
452  if (on>argc-2 || argv[on+1][0] == '-')
453  {
454  cerr << "Invalid input data command line option"
455  " -- ignored" << endl;
456  }
457  else
458  {
459  tmpstring = adstring(argv[on+1]);
460  }
461  }
462  }
463  global_datafile= new cifstream(tmpstring);
464  if (!global_datafile)
465  {
466  cerr << "Error trying to open data input file "
467  << tmpstring << endl;
468  }
469  else
470  {
471  if (!(*global_datafile))
472  {
473  cerr << "Error trying to open data input file "
474  << tmpstring << endl;
475  delete global_datafile;
476  global_datafile=NULL;
477  }
478  }
479 */
480  adstring ts=adprogram_name + adstring(".log");
481  global_logfile= new ofstream( (char*)ts);
482 
483  int biopt=-1;
484  int aiopt=-1;
485  biopt=option_match(argc,argv,"-binp");
486  aiopt=option_match(argc,argv,"-ainp");
487 
488  tmpstring=adprogram_name + adstring(".bin");
489  if (!global_bparfile && aiopt == -1)
490  {
491  if (biopt>-1)
492  {
493  if (biopt>argc-2 || argv[biopt+1][0] == '-')
494  {
495  cerr << "Invalid input parameter file command line option"
496  " -- ignored" << endl;
497  }
498  else
499  {
500  tmpstring = adstring(argv[biopt+1]);
501  }
502  }
503  global_bparfile= new uistream(tmpstring);
504  if (global_bparfile)
505  {
506  if (!(*global_bparfile))
507  {
508  if (biopt>-1)
509  {
510  cerr << "Error trying to open binary inoput par file "
511  << tmpstring << endl;
512  ad_exit(1);
513  }
514  delete global_bparfile;
515  global_bparfile=NULL;
516  }
517  }
518  }
519  tmpstring=adprogram_name + adstring(".pin");
520  if (!global_parfile)
521  {
522  if (aiopt>-1)
523  {
524  if (aiopt>argc-2 || argv[aiopt+1][0] == '-')
525  {
526  cerr << "Invalid input parameter file command line option"
527  " -- ignored" << endl;
528  }
529  else
530  {
531  tmpstring = adstring(argv[aiopt+1]);
532  }
533  }
534  global_parfile= new cifstream(tmpstring);
535  if (global_parfile)
536  {
537  if (!(*global_parfile))
538  {
539  if (aiopt>-1)
540  {
541  cerr << "Error trying to open ascii inoput par file "
542  << tmpstring << endl;
543  ad_exit(1);
544  }
545  delete global_parfile;
546  global_parfile=NULL;
547  }
548  }
549  }
550 }
551 
556 {
557 #ifdef DIAG_TIMER
558  if (ptm)
559  {
560  delete ptm;
561  ptm=0;
562  }
563  if (ptm1)
564  {
565  delete ptm1;
566  ptm1=0;
567  }
568 #endif
569  if (global_datafile)
570  {
571  delete global_datafile;
572  global_datafile=NULL;
573  }
574  if (global_parfile)
575  {
576  delete global_parfile;
577  global_parfile=NULL;
578  }
579  if (global_logfile)
580  {
581  delete global_logfile;
582  global_logfile=NULL;
583  }
584 }
585 
587 {
588  if (lapprox)
589  {
590  if (lapprox->hesstype==2)
591  {
592  //lapprox->num_separable_calls=0;
594  }
595  }
596 #if defined(__GNUC__)
597  #if __GNUC__ > 4
598  cout << std::defaultfloat;
599  #endif
600 #else
601  cout << std::defaultfloat;
602 #endif
603  userfunction();
604 #if defined(__GNUC__)
605  #if __GNUC__ > 4
606  cout << std::defaultfloat;
607  #endif
608 #else
609  cout << std::defaultfloat;
610 #endif
611  if (lapprox)
612  {
613  if (lapprox->hesstype==2)
614  {
616 
617  double tmp=0.0;
618  int inner_opt_value=inner_opt();
619  if (lapprox->saddlepointflag==2)
620  {
621  if (inner_opt_value !=0 )
622  {
623  for (int i = 1; i <= lapprox->num_separable_calls; i++)
624  {
626  }
628  }
629  }
630  else
631  {
632  for (int i = 1; i <= lapprox->num_separable_calls; i++)
633  {
635  }
637  }
638  }
639  }
640 }
size_t size() const
Definition: string.cpp:58
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
static adpvm_manager * pvm_manager
Definition: fvar.hpp:8849
static ofstream * global_logfile
Definition: fvar.hpp:8858
int ad_chdir(const char *s)
Description not yet available.
Definition: makesub.cpp:43
static char ** argv
Definition: fvar.hpp:8866
static unsigned char directory_prefix
Definition: fvar.hpp:8865
static adstring working_directory_path
Definition: fvar.hpp:8861
static uistream * global_bparfile
Definition: fvar.hpp:8859
static int inner_opt(void)
Definition: xmodelm3.cpp:39
exitptr ad_exit
Definition: gradstrc.cpp:53
ad_comm()
Default constructor.
Definition: model7.cpp:366
void allocate()
Definition: model7.cpp:370
const int iprint
Definition: fvar.hpp:9504
static adstring adprogram_name
Definition: fvar.hpp:8860
Description not yet available.
Definition: fvar.hpp:8901
int atoi(adstring &s)
Returns a integer converted from input s.
Definition: atoi.cpp:20
void set_signal_handlers(void)
Definition: model7.cpp:37
void exit_handler(int k)
Description not yet available.
Definition: signalh.cpp:20
prnstream & endl(prnstream &)
adstring strip_full_path(const adstring &_s)
$Id$
Definition: model7.cpp:18
static int argc
Definition: fvar.hpp:8863
static objective_function_value * pobjfun
Definition: admodel.h:2394
Description not yet available.
void ad_getcd(const adstring &s)
Description not yet available.
Definition: makesub.cpp:56
static cifstream * global_datafile
Definition: fvar.hpp:8856
int option_match(int argc, char *argv[], const char *string)
Checks if the program has been invoked with a particular command line argument (&quot;string&quot;).
Definition: optmatch.cpp:25
const int output
Definition: fvar.hpp:9505
virtual ~ad_comm()
Destructor.
Definition: model7.cpp:555
Description not yet available.
Description not yet available.
Definition: fvar.hpp:3516
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
static cifstream * global_parfile
Definition: fvar.hpp:8857
virtual void userfunction(void)=0
static int no_atlas_flag
Definition: fvar.hpp:8840
void pre_userfunction(void)
Definition: model7.cpp:586
static unsigned int wd_flag
Definition: fvar.hpp:8864
size_t length(const adstring &t)
Returns the size of adstr.
Definition: string1.cpp:228
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487