ADMB Documentation  -a65f1c97
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
modspmin.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 # include <df1b2fun.h>
9 # include <adrndeff.h>
10 
11 #if ( (defined(_WINDOWS) || defined(_Windows)) && !defined(BORBUGS))
12 # include <windows.h>
13 #endif
14 #include<ctime>
15 #include <cassert>
16 
17 void ADSleep(unsigned int x);
18 
19  void test_mcmc_options_window(void);
20  void ad_open_mcmc_options_window(void);
21  void ad_open_mcmchist_window(void);
22  void ad_update_mcmc_report(double * v,int l);
23 #if defined (AD_DEMO)
24  void write_banner_stuff(void);
25 #endif
28  //int function_minimizer::in_likeprof_flag=0;
29 
30 class admb_javapointers;
31 extern admb_javapointers * adjm_ptr;
32 
33 std::string get_elapsed_time(
34  const std::chrono::time_point<std::chrono::system_clock>& from,
35  const std::chrono::time_point<std::chrono::system_clock>& to);
36 
37 extern std::chrono::time_point<std::chrono::system_clock> start_time;
38 
39  void function_minimizer::computations(int argc,char * argv[])
40  {
41  //traceflag=1;
43  //if (option_match(argc,argv,"-gui")>-1)
44  //{
45  // void vm_initialize(void);
46  // vm_initialize();
47  // cout << " called vm_initialize() " << endl;
48  //}
49 #if defined (AD_DEMO)
50  write_banner_stuff();
51 #endif
52  // ------------------------------------------------------------
53  // Cole added experimental new flag to improve console
54  // output to be more compact and informative
55 
57 
58  int on, nopt;
59  if ( (on=option_match(argc,argv,"-display",nopt)) > -1)
60  {
61  // Updated output option with argument '-display 1'
62  // Details and bug reports at: https://github.com/admb-project/admb/discussions/219"
63  if (nopt == 1){
64  int argument = atoi(argv[on+1]);
65  if (argument >= 0 && argument <= 2)
66  {
68  }
69  else
70  {
71  cerr << "Warning: Invaild argument for option -display (See -help).\n\n";
72  }
73  }
74  else
75  {
76  cerr << "Warning: Option -display needs a number argument (See -help).\n\n";
77  }
78  }
79 
81  {
82  start_time = std::chrono::system_clock::now();
83  }
84 
85  // ------------------------------------------------------------
86  if (option_match(argc,argv,"-mceval") == -1)
87  {
88  // if not mceval model do optimization or hess step
89  if(!(option_match(argc,argv,"-hess_step") == -1))
90  {
91  // Experimental feature to take Newton steps after
92  // previous optimization
93  // Note: ::computations1 is called at the end of hess_step function.
94  hess_step();
95  }
96  else
97  {
98  // main optimization call
99  computations1(argc,argv);
100  }
101  }
102  else
103  {
104  // mceval skips all the optimization stuff
106  mcmc_eval();
108  }
110  final_calcs();
111 
112  // clean up if have random effects
113  // cleanup_laplace_stuff(lapprox);
114 
115  // Print new concluding message unless in MCMC mode which already prints timing stuff
117  !(option_match(ad_comm::argc,ad_comm::argv,"-nuts") > -1) &&
118  !(option_match(ad_comm::argc,ad_comm::argv,"-rwm") > -1) &&
119  !(option_match(ad_comm::argc,ad_comm::argv,"-hmc") > -1)){
120 
121  std::string fullpath(argv[0]);
122 #if defined(_WIN32)
123  auto idx1 = fullpath.rfind("\\");
124  auto idx2 = fullpath.rfind(".");
125  auto total = idx2 - idx1 - 1;
126  std::string model_name = fullpath.substr(idx1 + 1, total);
127 #else
128  auto idx1 = fullpath.rfind("/");
129  if (idx1 > 0) ++idx1;
130  std::string model_name = fullpath.substr(idx1);
131 #endif
132  cout << "\nFinished running model '" << model_name << "' after "
133  << get_elapsed_time(start_time, std::chrono::system_clock::now())
134  << "." << endl;
135  }
136  }
137 
138  void function_minimizer::computations1(int argc,char * argv[])
139  {
141 
142  int on=-1;
143  int nopt=-1;
144 #if defined(USE_ADPVM)
146  {
147  switch (ad_comm::pvm_manager->mode)
148  {
149  case 1: //master
151  break;
152  case 2: //slave
154  break;
155  default:
156  cerr << "Illegal value for ad_comm::pvm_manager->mode"
157  " value was " << ad_comm::pvm_manager->mode << endl;
158  ad_exit(1);
159  }
160  }
161 #endif // #if defined(USE_ADPVM)
162 
163  set_runtime();
164 
165  if ( (on=option_match(argc,argv,"-hbf",nopt))>-1)
166  {
168  }
169 
170  // Sets the maximum number of function evaluation as determined from the
171  // command line
172  if ( (on=option_match(argc,argv,"-maxfn",nopt))>-1)
173  {
174  if (nopt ==1)
175  {
176  set_runtime_maxfn(argv[on+1]);
177  }
178  else
179  {
180  cerr << "Wrong number of options to -mafxn -- must be 1"
181  " you have " << nopt << endl;
182  }
183  }
184 
185  if ( (on=option_match(argc,argv,"-ttr",nopt))>-1)
186  {
187  test_trust_flag=1;
188  }
189 
190  if ( (on=option_match(argc,argv,"-crit",nopt))>-1)
191  {
192  if (nopt ==1)
193  {
194  set_runtime_crit(argv[on+1]);
195  }
196  else
197  {
198  cerr << "Wrong number of options to -crit -- must be 1"
199  " you have " << nopt << endl;
200  }
201  }
202 
204 
206 
207  repeatminflag=0;
208  do
209  {
210  /*
211  if (spminflag)
212  {
213  repeatminflag=1;
214  spminflag=0;
215  }
216  else
217  {
218  repeatminflag=0;
219  }
220  */
221 
222  if (option_match(argc,argv,"-noest") == -1)
223  {
225  {
226  minimize();
227  }
228  else
229  {
231  }
232  }
233  else
234  {
236  }
238 
239  //double ratio=100.*gradient_structure::max_last_offset/12000.0;
241  if (option_match(argc,argv,"-est") == -1)
242  {
243  if (!quit_flag)
244  {
245  // save the sparse Hessian for the random effects
247  {
249  {
251  adstring tmpstring = ad_comm::adprogram_name + ".shess";
252  uostream uos((char*)(tmpstring));
253  uos << dct.get_n() << dct.indexmin() << dct.indexmax()
254  << dct.get_coords() << dct.get_x();
255  }
256  }
257 
258  on=option_match(argc,argv,"-nohess");
259  int on1=option_match(argc,argv,"-noest");
260  if (on==-1 && on1==-1)
261  {
262  if (option_match(argc,argv,"-sdonly")==-1)
263  {
264  hess_routine();
265  }
266  // set this flag so that variables only needed for their std devs
267  // will be calculated
269 #if defined(USE_ADPVM)
271  {
272  if (ad_comm::pvm_manager->mode==1) //master
273  {
274  depvars_routine();
275  hess_inv();
276  if (spminflag==0)
277  {
278  sd_routine();
279  }
280  }
281  }
282  else
283 #endif
284  {
285  depvars_routine();
286  if (!hess_inv()) break;
287  if (spminflag==0)
288  {
289  sd_routine();
290  }
291  }
292  // if(function_minimizer::output_flag==1) function_minimizer::check_parameters_on_bounds();
293  }
294  else
295  {
297  }
298  if (spminflag==0)
299  {
300  if ( (on=option_match(argc,argv,"-lprof"))>-1)
301  {
303  {
304  #if defined(USE_ADPVM)
306  {
307  switch (ad_comm::pvm_manager->mode)
308  {
309  case 1: // master
311  break;
312  case 2: // slave
314  break;
315  default:
316  cerr << "error illega value for pvm_manager->mode" << endl;
317  ad_exit(1);
318  }
319  }
320  else
321  #endif
322  {
323  const double f = value(*objective_function_value::pobjfun);
324  likeprof_routine(f);
325  }
326  }
327  }
328  nopt=0;
329  int on2=-1;
330  int nopt2=-1;
331 
332  // stuff for mcmc
333  //cout << "checking for mcmc" << endl;
334  if ( (on=option_match(argc,argv,"-mcmc",nopt))>-1 ||
335  (on=option_match(argc,argv,"-mcmc2",nopt))>-1)
336  {
337  if ( (on2=option_match(argc,argv,"-mcmc2",nopt2))>-1)
338  mcmc2_flag=1;
339  else
340  mcmc2_flag=0;
341 
342  #if defined(USE_ADPVM)
344  {
345  switch (ad_comm::pvm_manager->mode)
346  {
347  case 1: // master
349  break;
350  case 2: // slave
352  break;
353  default:
354  cerr << "error illega value for pvm_manager->mode" << endl;
355  ad_exit(1);
356  }
357  }
358  else
359  #endif
360  {
362  }
363  }
364  if ( (on=option_match(argc,argv,"-sob",nopt))>-1)
365  {
366  int nsob=0;
367  //int iseed0=0;
368  //double dscale=1.0;
369  if (nopt)
370  {
371  nsob=atoi(argv[on+1]);
372  if (nsob <=0)
373  {
374  cerr << " Invalid option following command line option -sob"
375  " -- "
376  << endl << " ignored" << endl;
377  }
378  }
379  if ( (on=option_match(argc,argv,"-mcr",nopt))>-1)
380  {
381  //sob_routine(nsob,dscale,1);
382  //sobol_importance_routine(nsob,iseed0,dscale,1);
383  }
384  else
385  {
386  //sobol_importance_routine(nsob,iseed0,dscale,0);
387  }
388  }
390  }
391  }
392  }
393  }
394  while(spminflag || repeatminflag);
395  }
396 
398  {
399  // for now just do parameter estimates
400  //function_minimizer::minimize();
401  minimize();
402  }
403 
404 #if defined (AD_DEMO)
405 void write_banner_stuff(void)
406 {
407  if (ad_printf)
408  {
409  char banner0[56]={"*****************************************************"};
410  char banner1[56]={"This is the open source version of AD Model Builder"};
411  char banner1a[58]={"You can freely use AD Model Builder"};
412  char banner2[30]={"http://www.admb-project.org/"};
413  char banner3[55]={"http://www.admb-project.org/"};
414  char banner4[60]={"users@admb-project.org http://www.admb-project.org/"};
415  (*ad_printf)("\n%s\n", banner0);
416  (*ad_printf)("%s\n", banner1);
417  (*ad_printf)("%s\n", banner1a);
418  (*ad_printf)("%s\n", banner2);
419  (*ad_printf)("%s\n", banner3);
420  (*ad_printf)("%s\n", banner4);
421  //(*ad_printf)("%s\n", adcopy);
422  (*ad_printf)("%s\n", banner0);
423  (*ad_printf)("%s\n\n", banner0);
424  }
425  void adwait(double sec);
426  adwait(2.5);
427 }
428 #endif
429 
430 #ifdef DEBUG
431  void test_mcmc_options_window(void)
432  {
433  dvector v(1,1000);
434  random_number_generator rng(908);
435 
436  for (int i=5;i<=1000;i++)
437  {
438  rng.reinitialize(908);
439  v(1,i).fill_randn(rng);
440  for (int j=2;j<=i;j++)
441  {
442  v(j)=0.9*v(j-1)+0.435889*v(j);
443  }
444 
445  //ad_update_mcmc_report(&(v(1)),i);
446  ADSleep(500);
447  }
448  }
449 #endif
450 
452 
454  {
455  adstring opt="{" + adstring(s) + "}";
456  dvector temp1((char*)(opt));
461  }
462 
464  {
465  adstring opt="{" + adstring(s) + "}";
466  dvector temp1((char*)(opt));
469  convergence_criteria=temp1;
470  }
471 
473  {
474  int ton,tnopt = 0;
475  ton=option_match(ad_comm::argc,ad_comm::argv,"-mcmc",tnopt);
476  if (ton<0)
477  {
478  ton=option_match(ad_comm::argc,ad_comm::argv,"-mcmc2",tnopt);
479  }
480  int on=ton;
481  int nopt=tnopt;
482 
483  if (on>-1)
484  {
485  /*
486  if (adjm_ptr)
487  {
488  ad_open_mcmc_options_window();
489  ad_open_mcmchist_window();
490  //test_mcmc_options_window();
491  }
492  */
493  int nmcmc=0;
494  int iseed0=0;
495  double dscale=1.0;
496  if (nopt)
497  {
498  nmcmc=(int)atof(ad_comm::argv[on+1]);
499  if (nmcmc <=0)
500  {
501  cerr << " Invalid option following command line option -mcmc -- "
502  << endl << " ignored" << endl;
503  }
504  }
505  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcmult",nopt))>-1)
506  {
507  if (nopt)
508  {
509  char* end;
510  double _dscale = strtod(ad_comm::argv[on + 1], &end);
511  if (_dscale != 0.0)
512  {
513  cerr << "Invalid argument to option -mcmult" << endl;
514  }
515  else
516  {
517  dscale = _dscale;
518  }
519  }
520  }
521  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcseed",nopt))>-1)
522  {
523  if (nopt)
524  {
525  int _iseed0 = atoi(ad_comm::argv[on+1]);
526  if (_iseed0 <=0)
527  {
528  cerr << " Invalid option following command line option -mcseed -- "
529  << endl << " ignored" << endl;
530  }
531  else
532  {
533  iseed0 = _iseed0;
534  }
535  }
536  }
537  int hybrid_flag=0;
538  if (option_match(ad_comm::argc,ad_comm::argv,"-hybrid") > -1)
539  {
540  hybrid_flag=1;
542  }
543 
544  // start addition
545  // temporarily adding this here, need to fully merge in with other options still
546  if (option_match(ad_comm::argc,ad_comm::argv,"-hmc") > -1)
547  {
549  shmc_mcmc_routine(nmcmc,iseed0,dscale,0);
550  return;
551  }
552  if (option_match(ad_comm::argc,ad_comm::argv,"-nuts") > -1)
553  {
555  nuts_mcmc_routine(nmcmc,iseed0,dscale,0);
556  return;
557  }
558  // This one is my modified version of the one Dave wrote. Mostly
559  // cosmetic differences to get it to work with adnuts better.
560  if (option_match(ad_comm::argc,ad_comm::argv,"-rwm") > -1)
561  {
563  rwm_mcmc_routine(nmcmc,iseed0,dscale,0);
564  return;
565  }
566 
567  // Temporarily turn off this chunk if using HMC
568  else
569  {
570  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcr",nopt))>-1)
571  {
572  if (hybrid_flag==0)
573  {
574  mcmc_routine(nmcmc,iseed0,dscale,1);
575  }
576  else
577  {
578  hybrid_mcmc_routine(nmcmc,iseed0,dscale,1);
579  }
580  }
581  else
582  {
583  if (hybrid_flag==0)
584  {
585  mcmc_routine(nmcmc,iseed0,dscale,0);
586  }
587  else
588  {
589  hybrid_mcmc_routine(nmcmc,iseed0,dscale,0);
590  }
591  }
592  }
593  }
594  }
595 
596 
597 #if defined(USE_ADPVM)
599  {
600  int on,nopt = 0;
601  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcmc",nopt))>-1)
602  {
603  /*
604  if (adjm_ptr)
605  {
606  ad_open_mcmc_options_window();
607  ad_open_mcmchist_window();
608  //test_mcmc_options_window();
609  }
610  */
611  int nmcmc=0;
612  int iseed0=0;
613  double dscale=1.0;
614  if (nopt)
615  {
616  nmcmc=(int)atof(ad_comm::argv[on+1]);
617  if (nmcmc <=0)
618  {
619  cerr << " Invalid option following command line option -mcmc -- "
620  << endl << " ignored" << endl;
621  }
622  }
623  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcmult",nopt))>-1)
624  {
625  if (nopt)
626  {
627  char * end;
628  dscale=strtod(ad_comm::argv[on+1],&end);
629  if (!dscale)
630  {
631  cerr << "Invalid argument to option -mcmult" << endl;
632  dscale=1.0;
633  }
634  }
635  }
636  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcseed",nopt))>-1)
637  {
638  if (nopt)
639  {
640  iseed0=atoi(ad_comm::argv[on+1]);
641  if (iseed0 <=0)
642  {
643  cerr << " Invalid option following command line option -mcseed -- "
644  << endl << " ignored" << endl;
645  iseed0=0;
646  }
647  }
648  }
649  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcr",nopt))>-1)
650  {
651  //mcmc_routine(nmcmc,iseed0,dscale,1);
652  pvm_master_mcmc_routine(nmcmc,iseed0,dscale,1);
653  }
654  else
655  {
656  //mcmc_routine(nmcmc,iseed0,dscale,0);
657  pvm_master_mcmc_routine(nmcmc,iseed0,dscale,0);
658  }
659  }
660  }
662  {
664  }
665 #endif
bool hess_inv()
Symmetrize and invert the hessian.
Definition: mod_hess.cpp:574
void computations1(int argc, char *argv[])
Definition: modspmin.cpp:138
laplace_approximation_calculator * lapprox
Definition: admodel.h:1862
dcompressed_triplet * sparse_triplet2
Definition: adrndeff.h:271
static adpvm_manager * pvm_manager
Definition: fvar.hpp:8849
int indexmin(void)
Definition: fvar.hpp:9354
Description not yet available.
void shmc_mcmc_routine(int, int, double, int)
Definition: hmc.cpp:19
static int sd_phase
Definition: admodel.h:844
imatrix & get_coords(void)
Definition: fvar.hpp:9378
void nuts_mcmc_routine(int, int, double, int)
The function implements the MCMC algorithm NUTS, which is a self-tuning variant of Hamiltonian Monte ...
Definition: nuts.cpp:51
int indexmax(void)
Definition: fvar.hpp:9358
dvector & get_x(void)
Definition: fvar.hpp:9382
#define x
int allocated(const ivector &v)
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: fvar_a59.cpp:13
Vector of double precision numbers.
Definition: dvector.h:50
int indexmin() const
Get minimum valid index.
Definition: dvector.h:199
static char ** argv
Definition: fvar.hpp:8866
virtual void other_calculations(void)
Definition: admodel.h:1933
void allocate(int ncl, int ncu)
Allocate memory for a dvector.
Definition: dvector.cpp:409
virtual void set_runtime_maxfn(const char *)
Definition: modspmin.cpp:453
void test_mcmc_options_window(void)
void rwm_mcmc_routine(int, int, double, int)
Definition: rwm.cpp:92
void hybrid_mcmc_routine(int, int, double, int)
Description not yet available.
Definition: hybmcmc.cpp:83
exitptr ad_exit
Definition: gradstrc.cpp:53
virtual void set_runtime(void)
Definition: modspmin.cpp:451
void ADSleep(unsigned int t)
Description not yet available.
Definition: adsleep.cpp:24
static adstring adprogram_name
Definition: fvar.hpp:8860
static void send_all_to_slaves(void)
std::chrono::time_point< std::chrono::system_clock > start_time
Definition: newfmin.cpp:171
void computations(void)
Definition: modspmin.cpp:397
void likeprof_routine(double global_min)
Definition: mod_pmin.cpp:67
admb_javapointers * adjm_ptr
int atoi(adstring &s)
Returns a integer converted from input s.
Definition: atoi.cpp:20
std::string get_elapsed_time(const std::chrono::time_point< std::chrono::system_clock > &from, const std::chrono::time_point< std::chrono::system_clock > &to)
Definition: mod_sd.cpp:41
void ad_open_mcmchist_window(void)
void pvm_master_mcmc_computations(void)
Definition: modspmin.cpp:598
void pvm_master_mcmc_routine(int nmcmc, int iseed0, double dscale, int restart_flag)
void pvm_slave_mcmc_routine(void)
void ad_open_mcmc_options_window(void)
Description not yet available.
Definition: fvar.hpp:7951
void ad_update_mcmc_report(dmatrix &m, int i, int j, int ff=0)
prnstream & endl(prnstream &)
static int current_phase
Definition: admodel.h:842
static dvector maximum_function_evaluations
Definition: admodel.h:1917
static int max_number_phases
Definition: admodel.h:841
void adwait(double)
Definition: model34.cpp:42
Description not yet available.
Definition: fvar.hpp:3398
static int argc
Definition: fvar.hpp:8863
int indexmax() const
Get maximum valid index.
Definition: dvector.h:204
static void get_stddev_number_offset(void)
Definition: model2.cpp:73
virtual void final_calcs(void)
Definition: admodel.h:1934
static int have_constraints
Definition: admodel.h:1921
static objective_function_value * pobjfun
Definition: admodel.h:2394
Description not yet available.
Description not yet available.
Definition: fvar.hpp:9345
void mcmc_computations(void)
Definition: modspmin.cpp:472
void pvm_slave_mcmc_computations(void)
Definition: modspmin.cpp:661
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
void sd_routine(void)
Definition: mod_sd.cpp:101
void pvm_slave_likeprof_routine(void)
Definition: mod_pmin.cpp:50
int traceflag
Definition: fvar1.cpp:18
static int Hybrid_bounded_flag
void hess_step()
hess_step is used for HMC. See details in function_minimizer::hess_step.
static int mceval_phase
Definition: admodel.h:847
Description not yet available.
static int test_trust_flag
Definition: admodel.h:1856
void mcmc_routine(int, int, double, int)
Monte Carlo Markov Chain minimization routine Approximate the target distribution by performing a ran...
Definition: xxmcmc.cpp:126
static int output_flag
Definition: admodel.h:1972
static void get_all_from_master(void)
dvector value(const df1_one_vector &v)
Definition: df11fun.cpp:69
virtual void constraints_minimize(void)
void mcmc_eval(void)
Description not yet available.
Definition: mceval.cpp:14
void depvars_routine(void)
Calculate the derivatives of dependent variables with respect to the independent variables.
Definition: mod_hess.cpp:488
void tracing_message(int traceflag, const char *s)
Description not yet available.
Definition: newfmin.cpp:94
static dvector convergence_criteria
Definition: admodel.h:1915
static int first_hessian_flag
Definition: admodel.h:1855
int ad_printf(FILE *stream, const char *format, Args...args)
Definition: fvar.hpp:9487
virtual void set_runtime_crit(const char *)
Definition: modspmin.cpp:463
void hess_routine(void)
Definition: mod_hess.cpp:21
void deallocate(void)
Called by destructor to deallocate memory for a dvector object.
Definition: dvector.cpp:92
virtual void minimize(void)
Definition: xmodelm3.cpp:49
static int num_likeprof_params
Author: David Fournier Copyright (c) 2008-2012 Regents of the University of California.
Definition: admodel.h:2259