Beispiel #1
0
void do_dll_housekeeping(int argc,char ** argv)
{
#if !defined(_MSC_VER)
  int on = option_match(argc,argv,"-sp");
  if (on > -1)
  {
  #if defined(_WIN32)
    get_sp_printf();
  #endif
    ad_exit=spdll_exit;
  }
  else if ( (on=option_match(argc,argv,"-spexit"))>-1)
  {
    ad_printf=printf;
    ad_exit=spdll_exit;
  }
  else
  {
    ad_exit=exit;
    if (!ad_printf) ad_printf=printf;
  }
#else
  ad_exit=exit;

  if (!ad_printf) ad_printf=printf;
#endif
}
Beispiel #2
0
adpvm_manager::adpvm_manager(int _mode)
{
  cout << "calling load library" << endl;
  if (load_adpvm_library() < 0)
  {
    cerr << "error loading pvm library" << endl;
    exit(1);
  }
  pvm_setopt(PvmRoute, PvmRouteDirect);  /* channel for communication */
  /* get and display configuration of the parallel machine */
  int status=pvm_config( &nhost, &narch, &hostp );  /* get configuration */
  if (status<0)
  {
    cerr << "error trying to get configuration of pvm (virtual machine)" << endl;
    if (status == PvmSysErr)
      cerr << " PVM Daemon not responing -- maybe it is not started" << endl;
    ad_exit(1);
  }
  printf("I found the following hosts in your virtual machine\n");
  int i;
  for (i = 0; i < nhost; i++)
  {
    printf("    %s\n", hostp[i].hi_name);
  }
  //id.allocate(0,nhost);
  mode=_mode;
  if (mode == 1)  // master
  {
    slave_argv = new adpvm_slave_args(20,20);
    int ierr=pvm_catchout(stdout);
    if (ierr<0)
    {
      cerr << "Error in pvm_catchout" << endl;
    }
    strcpy(*slave_argv,"progname");
    int on1,nopt1;
    if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-exec",nopt1))>-1)
    {
      if (nopt1 !=1)
      {
        cerr << "Wrong number of options to -exec -- must be 1"
          " you have " << nopt1 << endl;
        ad_exit(1);
      }
      slave_names+= ad_comm::argv[on1+1];
    }
    else
    {
      slave_names+="test";
    }
  }
  timing_flag=0;
  int on1,nopt1;
  if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-pvmtime",nopt1))>-1)
  {
    timing_flag=1;
  }
}
Beispiel #3
0
TEST_F(test_ad_comm, option_match_ind)
{
  int argc = 7;
  char* argv[] = {"./dogsbmDIC", "-ind", "dog4s.ctl", "-ainp", "dog4p.pin", "-nox", "-est"};
  int opt = option_match(argc, argv, "-ind");
  EXPECT_EQ(1, opt);
}
Beispiel #4
0
  void function_minimizer::computations(int argc,char * argv[])
  {
    //traceflag=1;
    tracing_message(traceflag,"A1");
    //if (option_match(argc,argv,"-gui")>-1)
    //{
    //  void vm_initialize(void);
    //  vm_initialize();
    //  cout << " called vm_initialize() " << endl;
    //}
#if defined (AD_DEMO)
     write_banner_stuff();
#endif
    if (option_match(argc,argv,"-mceval") == -1)
    {
        computations1(argc,argv);
    }
    else
    {
      initial_params::mceval_phase=1;
      mcmc_eval();
      initial_params::mceval_phase=0;
    }
    other_calculations();

    final_calcs();
    // clean up if have random effects
     // cleanup_laplace_stuff(lapprox);
  }
Beispiel #5
0
TEST_F(test_ad_comm, option_match_ind_option_with_unicode_char)
{
  int argc = 3;
  //the '-' is a unicode char
  char* argv[] = {"./dogsbmDIC", "–ind", "dog4s.ctl"};
  EXPECT_EQ('\xE2', argv[1][0]);
  EXPECT_EQ('\x80', argv[1][1]);
  EXPECT_EQ('\x93', argv[1][2]);
  int opt = option_match(argc, argv, "-ind");
  EXPECT_EQ(-1, opt);
}
Beispiel #6
0
TEST_F(test_option_match, calls)
{
  //ASSERT_NE(0, strcmp(0, "-option"));

  int option_match(int argc, char *argv[], const char *option, const int& _nopt);

  int nopt = -1;
  ASSERT_EQ(-1, option_match(0, 0, 0, nopt));
  ASSERT_EQ(-1, option_match(0, 0, "-check", nopt));

  //Should not happen, but still need to check.
  ASSERT_EQ(-1, option_match(1, 0, "-check", nopt));

  int argc = 1;
  char* argv[] = { "./simple"};

  char* option = "-none";
  ASSERT_EQ(-1, option_match(argc, argv, option, nopt));
  ASSERT_EQ(0, nopt);

  int argc2 = 2;
  char* argv2[] = { "./simple", "-myoption" };
  char* myoption = "-myoption";
  ASSERT_EQ(1, option_match(argc2, argv2, myoption, nopt));
  ASSERT_EQ(0, nopt);

  int argc3 = 3;
  char* argv3[] = { "./simple", "\0", "-myoption" };
  char* option3 = "-myoption";
  ASSERT_EQ(2, option_match(argc3, argv3, option3, nopt));
  ASSERT_EQ(0, nopt);
}
Beispiel #7
0
/**
Checks if the program has been invoked with a particular command line argument
("string"). If so, counts the number of arguments ("nopt") to this command line
option. For example if the program has been invoked with the command line option
"-ind FILE", then nopt=1.

\param argc Number of command line arguments (as in all C programs)
\param argv Array  (of length argc) of command line arguments (as in all C programs)
\param option Should be one of the possible command line arguments to an ADMB
program.
\param nopt On return holds the number arguments/options associated with "string".
\return An index into "argv" where the match with "string" is obtained. In case
of no match, the value "-1" is returned.
*/
int option_match(int argc, char *argv[], const char *option, int& nopt)
{
  int match = -1;
  nopt=0;

  int i = option_match(argc, argv, option);
  if (i >= 0)
  {
    match = i;

    for (int j = i + 1; j < argc; j++)
    {
      if (argv[j][0] == '-') break;
      nopt++;
    }
  }

  return match;
}
Beispiel #8
0
/**
 * Description not yet available.
 * \param
 */
 gradient_structure::gradient_structure(long int _size)
 {
#ifndef OPT_LIB
  assert(_size > 0);
#endif
   gradient_structure::NVAR=0;
   atexit(cleanup_temporary_files);
   fill_ad_random_part();

   const unsigned long int size = (unsigned long int)_size;

   if (instances++ > 0)
   {
     cerr << "More than one gradient_structure object has been declared.\n"
          << "  Only one gradient_structure object can exist. Check the scope\n"
          << "  of the objects declared.\n";
     ad_exit(1);
   }
   gradient_structure::ARRAY_MEMBLOCK_SIZE=size; //js

   char * path = getenv("ADTMP1"); // NULL if not defined
   if (path != NULL)
   {
     #ifdef __SUN__
     sprintf(&cmpdif_file_name[0],"%s/cmpdiff.%s", path,
          ad_random_part);
     #else
        if (lastchar(path)!='\\')
        {
          sprintf(&cmpdif_file_name[0],"%s\\cmpdiff.%s", path,
            ad_random_part);
        }
        else
        {
          sprintf(&cmpdif_file_name[0],"%scmpdiff.%s", path,
            ad_random_part);
        }
     #endif
   }
   else
   {
      sprintf(&cmpdif_file_name[0],"cmpdiff.%s",ad_random_part);
   }

   if (DEPVARS_INFO!= NULL)
   {
      cerr << "  0 Trying to allocate to a non NULL pointer in gradient"
              "_structure" << endl;
   }
   else
   {
     int on,nopt = 0;
     if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-ndv",nopt))>-1)
     {
       if (!nopt)
       {
         cerr << "Usage -ndv option needs integer  -- ignored" << endl;
       }
       else
       {
         int jj=atoi(ad_comm::argv[on+1]);
         if (jj<=0)
         {
           cerr << "Usage -ndv option needs positive integer"
              "  -- ignored" << endl;
         }
         else
         {
           NUM_DEPENDENT_VARIABLES=jj;
         }
       }
     }
     DEPVARS_INFO=new dependent_variables_information(NUM_DEPENDENT_VARIABLES);
     memory_allocate_error("DEPVARS_INFO", (void *) DEPVARS_INFO);
   }

   if (fp!= NULL)
   {
      cerr << "  0 Trying to allocate to a non NULL pointer in gradient"
              "_structure" << endl;
   }
   else
   {
     fp=new DF_FILE(CMPDIF_BUFFER_SIZE);
     memory_allocate_error("fp", (void *) fp);
   }

   void * temp_ptr;
  // double_and_int * tmp;
   #ifdef DIAG
     cerr <<" In gradient_structure::gradient_structure()\n";
     cerr <<"  ARRAY_MEMBLOCK_SIZE = " << ARRAY_MEMBLOCK_SIZE << "\n";
   #endif

   if ( GRAD_LIST!= NULL)
   {
    cerr << "Trying to allocate to a non NULL pointer in gradient structure\n";
   }
   else
   {
      GRAD_LIST = new dlist;
      memory_allocate_error("GRAD_LIST", (void *) GRAD_LIST);
   }

   if ( ARR_LIST1!= NULL)
   {
     cerr << "Trying to allocate to a non NULL pointer in gradient structure\n";
   }
   else
   {
      ARR_LIST1 = new arr_list;
      memory_allocate_error("ARR_LIST1", (void *) ARR_LIST1);
   }

 /*
   if ( ARR_FREE_LIST1!= NULL)
   {
cerr << "  2 Trying to allocate to a non NULL pointer in gradient structure \n";
   }
   else
   {
      ARR_FREE_LIST1 = new arr_list;
      memory_allocate_error("ARR_FREE_LIST1", (void *) ARR_FREE_LIST1);
   }
 */

#ifdef __ZTC__
   if ((temp_ptr = farmalloc(ARRAY_MEMBLOCK_SIZE)) == 0)
#else
   if ((temp_ptr = (void*)malloc(ARRAY_MEMBLOCK_SIZE)) == 0)
#endif
   {
     cerr << "insufficient memory to allocate space for ARRAY_MEMBLOCKa\n";
     ad_exit(1);
   }

  /*
   if (ARRAY_MEMBLOCK_BASE != NULL)
   {
cerr << "Trying to allocate to a non NULL pointer in gradient structure \n";
   }
 */

   ARRAY_MEMBLOCK_BASE = temp_ptr;

   const size_t adjustment = (8 -((size_t)ARRAY_MEMBLOCK_BASE.ptr) % 8) % 8;
   ARRAY_MEMBLOCK_BASE.adjust(adjustment);

   if (GRAD_STACK1 != NULL)
   {
      cerr << "Trying to allocate to a non NULL pointer\n";
   }
   else
   {
     GRAD_STACK1 = new grad_stack;
     memory_allocate_error("GRAD_STACK1",GRAD_STACK1);
     gradient_structure::hessian_ptr= (double*) GRAD_STACK1->true_ptr_first;
   }
    #ifdef DIAG
        cout << "GRAD_STACK1= "<< farptr_tolong(GRAD_STACK1)<<"\n";
    #endif

   if ( INDVAR_LIST!= NULL)
   {
      cerr <<
        "Trying to allocate to a non NULL pointer in gradient structure \n";
      ad_exit(1);
   }
   else
   {
     INDVAR_LIST = new indvar_offset_list;
     memory_allocate_error("INDVAR_LIST",INDVAR_LIST);
 // ****************************************************************
 // ****************************************************************
      int nopt=0;
      int on=0;

      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mno",nopt))>-1)
      {
        if (nopt ==1)
        {
          const int i = atoi(ad_comm::argv[on+1]);
          MAX_NVAR_OFFSET = (unsigned int)i;
        }
        else
        {
          cerr << "Wrong number of options to -mno -- must be 1"
            " you have " << nopt << endl;
          ad_exit(1);
        }
      }

 // ****************************************************************
 // ****************************************************************

     INDVAR_LIST->address = new double * [ (size_t) MAX_NVAR_OFFSET];
     memory_allocate_error("INDVAR_LIST->address",INDVAR_LIST->address);
   }

   //allocate_dvariable_space();

   if ( RETURN_ARRAYS!= NULL)
   {
cerr << "Trying to allocate to a non NULL pointer in gradient structure \n";
      ad_exit(1);
   }
   else
   {
      RETURN_ARRAYS = new dvariable*[NUM_RETURN_ARRAYS];
      memory_allocate_error("RETURN_ARRAYS",RETURN_ARRAYS);

      //allocate_dvariable_space();
      for (int i=0; i< NUM_RETURN_ARRAYS; i++)
      {
        RETURN_ARRAYS[i]=new dvariable[RETURN_ARRAYS_SIZE];
        memory_allocate_error("RETURN_ARRAYS[i]",RETURN_ARRAYS[i]);
      }
      RETURN_ARRAYS_PTR=0;
      MIN_RETURN = RETURN_ARRAYS[RETURN_ARRAYS_PTR];
      MAX_RETURN = RETURN_ARRAYS[RETURN_ARRAYS_PTR]+RETURN_ARRAYS_SIZE-1;
      RETURN_PTR = MIN_RETURN;
   }
   //RETURN_INDEX = 0;

   RETURN_PTR_CONTAINER=new dvariable*[NUM_RETURN_ARRAYS];
   memory_allocate_error("RETURN_INDICES_CONTAINER",RETURN_PTR_CONTAINER);

   for (int i=0; i< NUM_RETURN_ARRAYS; i++)
   {
     RETURN_PTR_CONTAINER[i]=0;
   }
 }
Beispiel #9
0
/**
 * Description not yet available.
 * \param
 */
void allocate_dvariable_space()
{
  int on,nopt = 0;
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mdl",nopt))>-1)
  {
    if (nopt ==1)
    {
      const int i = atoi(ad_comm::argv[on+1]);
      if (i > 0)
      {
        gradient_structure::MAX_DLINKS = (unsigned int)i;
      }
    }
    else
    {
      cerr << "Wrong number of options to -mdl -- must be 1"
        " you have " << nopt << endl;
      ad_exit(1);
    }
  }
  unsigned int numlinks=gradient_structure::MAX_DLINKS;
  //cout << sizeof(dlink) << endl;

#ifndef OPT_LIB
  //cerr << "sizeof(char) is not equal 1) --"
  // " need to modify allocate_dvariable_space in gradstrc.cpp" << endl;
  assert(sizeof(char) == 1);

  //cerr << "sizeof(dlink) is greater than 2*sizeof(double) --"
  // " need to modify allocate_dvariable_space in gradstrc.cpp" << endl;
  assert(sizeof(dlink) == 2 * sizeof(double));
#endif
  const size_t size = 2 * sizeof(double) * (numlinks + 1);
  char* tmp1 = (char*)malloc(size * sizeof(char));
  if (!tmp1)
  {
    cerr << "Error[" << __FILE__ << ":" << __LINE__
         << "]: unable to allocate memory.\n";
    ad_exit(1);
  }
  else
  {
    dlink * dl=(dlink*)tmp1;
    tmp1+=2*sizeof(double);
    dl->prev=NULL;
    dlink * prev=dl;
    int& nlinks=(int&)gradient_structure::GRAD_LIST->nlinks;
    gradient_structure::GRAD_LIST->dlink_addresses[nlinks++]=dl;
    for (unsigned int i=1;i<=numlinks;i++)
    {
      dl=(dlink*)tmp1;
      dl->prev=prev;
      prev=dl;
      tmp1+=2*sizeof(double);

      gradient_structure::GRAD_LIST->dlink_addresses[nlinks++]=dl;
      // keep track of the links so you can zero them out
    }
    gradient_structure::GRAD_LIST->last=dl;
  }
}
void wasm_parse_options(WasmOptionParser* parser,
                        int argc,
                        char** argv) {
  parser->argv0 = argv[0];

  int i;
  int j;
  int k;
  for (i = 1; i < argc; ++i) {
    char* arg = argv[i];
    if (arg[0] == '-') {
      if (arg[1] == '-') {
        /* long option */
        int best_index = -1;
        int best_length = 0;
        int best_count = 0;
        for (j = 0; j < parser->num_options; ++j) {
          WasmOption* option = &parser->options[j];
          if (option->long_name) {
            int match_length =
                option_match(&arg[2], option->long_name, option->has_argument);
            if (match_length > best_length) {
              best_index = j;
              best_length = match_length;
              best_count = 1;
            } else if (match_length == best_length && best_length > 0) {
              best_count++;
            }
          }
        }

        if (best_count > 1) {
          error(parser, "ambiguous option \"%s\"", arg);
          continue;
        } else if (best_count == 0) {
          error(parser, "unknown option \"%s\"", arg);
          continue;
        }

        WasmOption* best_option = &parser->options[best_index];
        const char* option_argument = NULL;
        if (best_option->has_argument) {
          if (arg[best_length] == '=') {
            option_argument = &arg[best_length + 1];
          } else {
            if (i + 1 == argc || argv[i + 1][0] == '-') {
              error(parser, "option \"--%s\" requires argument",
                    best_option->long_name);
              continue;
            }
            ++i;
            option_argument = argv[i];
          }
        }
        parser->on_option(parser, best_option, option_argument);
      } else {
        /* short option */
        if (arg[1] == '\0') {
          /* just "-" */
          parser->on_argument(parser, arg);
          continue;
        }

        /* allow short names to be combined, e.g. "-d -v" => "-dv" */
        for (k = 1; arg[k]; ++k) {
          WasmBool matched = WASM_FALSE;
          for (j = 0; j < parser->num_options; ++j) {
            WasmOption* option = &parser->options[j];
            if (option->short_name && arg[k] == option->short_name) {
              const char* option_argument = NULL;
              if (option->has_argument) {
                /* a short option with a required argument cannot be followed
                 * by other short options */
                if (arg[k + 1] != '\0') {
                  error(parser, "option \"-%c\" requires argument",
                        option->short_name);
                  break;
                }

                if (i + 1 == argc || argv[i + 1][0] == '-') {
                  error(parser, "option \"-%c\" requires argument",
                        option->short_name);
                  break;
                }
                ++i;
                option_argument = argv[i];
              }
              parser->on_option(parser, option, option_argument);
              matched = WASM_TRUE;
              break;
            }
          }

          if (!matched) {
            error(parser, "unknown option \"-%c\"", arg[k]);
            continue;
          }
        }
      }
    } else {
      /* non-option argument */
      parser->on_argument(parser, arg);
    }
  }
}
Beispiel #11
0
ad_comm::ad_comm(int _argc,char * _argv[])
{
    if (option_match(_argc,_argv,"-version") > -1
            || option_match(_argc,_argv,"--version") > -1)
    {
        void banner(const adstring& program_name);
        banner(_argv[0]);

        exit(0);
    }

    ad_comm::argc=_argc;
    ad_comm::argv=_argv;
    if (option_match(_argc,_argv,"-time")>-1)
    {
        time_flag=1;
    }
    else
    {
        time_flag=0;
    }
    if (time_flag)
    {
        if (!ptm)
        {
            ptm=new adtimer();
        }
        if (!ptm1)
        {
            ptm1=new adtimer();
        }
    }
    no_atlas_flag=0;
    if (option_match(_argc,_argv,"-noatlas")>-1) no_atlas_flag=1;

#if defined(USE_ADPVM)
    int pvm_flag=0;
    if (option_match(_argc,_argv,"-slave")>-1)  pvm_flag=2;
    if (option_match(_argc,_argv,"-master")>-1) pvm_flag=1;

    if (pvm_flag)
        pvm_manager = new adpvm_manager(pvm_flag);
    else
        pvm_manager = NULL;

    if (pvm_manager)
    {
        if (pvm_manager->mode==2)  //slave
        {
            int on=0;
            int nopt=0;
            if ( (on=option_match(_argc,_argv,"-slave",nopt))>-1)
            {
                if (nopt ==1)
                {
                    pvm_manager->slave_number=atoi(ad_comm::argv[on+1]);
                }
                else
                {
                    cerr << "Wrong number of options to -slave -- must be 1"
                         " you have " << nopt << endl;
                    ad_exit(1);
                }
            }
            if ( (on=option_match(_argc,_argv,"-slavedir",nopt))>-1)
            {
                if (nopt ==1)
                {
                    ad_chdir(_argv[on+1]);
                }
                else
                {
                    cerr << "Wrong number of options to -slavedir -- must be 1"
                         " you have " << nopt << endl;
                }
            }
        }
    }
#endif

    /*
      if (option_match(_argc,_argv,"-gui")>-1)
      {
        void vm_initialize(void);
        vm_initialize();
      }
    */
    set_signal_handlers();
    adprogram_name=_argv[0];
    //int len=strlen(_argv[0]);
    //for (int i=1;i<=len;i++) adprogram_name[i]=tolower(adprogram_name[i]);
#if defined(_MSC_VER)
    strip_full_path(adprogram_name);
#endif
    adstring workdir;
    ad_getcd(workdir);
    if (_argc>1)
    {
        if (option_match(_argc,_argv,"-?")>-1
                || option_match(_argc,_argv,"-help")>-1
                || option_match(_argc,_argv,"--help")>-1)
        {
            // remove path (if user runs -help)
            for (size_t i = adprogram_name.size(); i >= 1; i--)
            {
#ifdef _WIN32
                if (adprogram_name(i) == '\\')
#else
                if (adprogram_name(i) == '/')
#endif
                {
                    adprogram_name=adprogram_name(i+1,adprogram_name.size());
                    break;
                }
            }

            //(*ad_printf)(" %s", (char*)admb_banner);
            (*ad_printf)( "Usage: %s [options]\n\n",(char*)(adprogram_name));

            (*ad_printf)( "Options:\n");
            (*ad_printf)( " -ainp FILE      change default ascii input parameter "
                          "filename to FILE\n");
            (*ad_printf)( " -binp FILE      change default binary input parameter "
                          "filename to FILE\n");
            (*ad_printf)( " -est            only do the parameter estimation\n");
            (*ad_printf)( " -noest          do not do the parameter estimation "
                          "(optimization) \n");
            (*ad_printf)( " -ind FILE       change default input data filename to "
                          "FILE\n");
            (*ad_printf)( " -lmn N          use limited memory quasi newton -- keep "
                          "N steps\n");
            (*ad_printf)( " -lmn2 N         use other limited memory quasi newton -- "
                          "keep N steps\n");
            (*ad_printf)( " -ilmn N         use other limited memory quasi newton "
                          "for random effects models - keep N steps\n");
            (*ad_printf)( " -dd N           check derivatives after N function "
                          "evaluations\n");
            (*ad_printf)( " -lprof          perform profile likelihood "
                          "calculations\n");
            (*ad_printf)( " -maxph N        increase the maximum phase number to "
                          "N\n");
            (*ad_printf)( " -mcdiag         use diagonal covariance matrix for mcmc "
                          "with diagonal values 1\n");
            (*ad_printf)( " -mcmc [N]       perform markov chain monte carlo with N "
                          "simulations\n");
            (*ad_printf)( " -mcmult N       multiplier N for mcmc default\n");
            (*ad_printf)( " -mcr            resume previous mcmc\n");
            (*ad_printf)( " -mcrb  N        reduce amount of correlation in the "
                          "covariance matrix 1<=N<=9\n");
            (*ad_printf)( " -mcnoscale      don't rescale step size for mcmc "
                          "depending on acceptance rate\n");
            (*ad_printf)( " -nosdmcmc       turn off mcmc histogram calcs to make "
                          "mcsave run faster\n");
            (*ad_printf)( " -mcprobe N      use probing strategy for mcmc with "
                          "factor N\n");
            (*ad_printf)( " -mcgrope N      Deprecated, same as -mcprobe\n");
            (*ad_printf)( " -mcseed N       seed for random number generator for "
                          "markov chain monte carlo\n");
            (*ad_printf)( " -mcscale N      rescale step size for first N "
                          "evaluations\n");
            (*ad_printf)( " -mcsave N       save the parameters for every Nth "
                          "simulation\n");
            (*ad_printf)( " -mceval         go through the saved mcmc values from a "
                          "previous mcsave\n");
            (*ad_printf)( " -mcu            use uniformaly distributed steps for "
                          "mcmc instead of random normal\n");
            (*ad_printf)( " -crit N1,N2,... set gradient magnitude convergence "
                          "criterion to N\n");
            (*ad_printf)( " -iprint N       print out function minimizer report "
                          "every N iterations\n");
            (*ad_printf)( " -maxfn N1,N2,.. set maximum number opf function eval's "
                          "to N\n");
            (*ad_printf)( " -rs             if function minimizer can't make "
                          "progress rescale and try again\n");
            //(*ad_printf)( " -sp             for DLL running from splus write to "
            //"command window\n");
            (*ad_printf)( " -nox            suppress vector and gradient values in "
                          "minimizer screen report\n");
            (*ad_printf)( " -phase N        start minimization in phase N\n");
            (*ad_printf)( " -simplex        use simplex for minimization -- "
                          "deprecated, use -neldmead\n");
            (*ad_printf)( " -neldmead       use Nelder-Mead simplex algorithm for "
                          "minimization\n");
            (*ad_printf)( " -nohess         don't do hessian or delta method for std "
                          "dev\n");
            (*ad_printf)( " -eigvec         calculate eigenvectors of the Hessian\n");
            (*ad_printf)( " -sdonly         do delta method for std dev estimates "
                          "without redoing hessian\n");
            (*ad_printf)( " -ams N          set arrmblsize to N "
                          "(ARRAY_MEMBLOCK_SIZE)\n");
            (*ad_printf)( " -cbs N          set CMPDIF_BUFFER_SIZE to N "
                          "(ARRAY_MEMBLOCK_SIZE)\n");
            (*ad_printf)( " -mno N          set the maximum number of independent "
                          "variables to N\n");
            (*ad_printf)( " -mdl N          set the maximum number of dvariables to "
                          "N\n");
            (*ad_printf)( " -gbs N          set GRADSTACK_BUFFER_SIZE to N "
                          "(ARRAY_MEMBLOCK_SIZE)\n");
#if defined(USE_ADPVM)
            (*ad_printf)( " -master         run as PVM master program\n");
            (*ad_printf)( " -slave          run as PVM slave program\n");
            (*ad_printf)( " -pvmtime        record timing information for PVM "
                          "performance analysis\n");
#endif
            (*ad_printf)( " -info           show how to cite ADMB, license, and "
                          "acknowledgements\n");
            (*ad_printf)( " -version        show version information\n");
            (*ad_printf)( " -help           show this message\n\n");
            //if (function_minimizer::random_effects_flag)
            //{
            (*ad_printf)( "Random effects options if applicable\n");
            (*ad_printf)( " -nr N           maximum number of Newton-Raphson "
                          "steps\n");
            (*ad_printf)( " -imaxfn N       maximum number of evals in quasi-Newton "
                          "inner optimization\n");
            (*ad_printf)( " -is N           set importance sampling size to N\n");
            (*ad_printf)( " -isf N          set importance sampling size funnel "
                          "blocks to N\n");
            (*ad_printf)( " -isdiag         print importance sampling diagnostics\n");
            (*ad_printf)( " -hybrid         do hybrid Monte Carlo version of MCMC\n");
            (*ad_printf)( " -hbf            set the hybrid bounded flag for bounded "
                          "parameters\n");
            (*ad_printf)( " -hyeps          mean step size for hybrid Monte Carlo\n");
            (*ad_printf)( " -hynstep        number of steps for hybrid Monte "
                          "Carlo\n");
            (*ad_printf)( " -noinit         do not initialize RE before inner "
                          "optimization\n");
            (*ad_printf)( " -ndi N          set maximum number of separable calls\n");
            (*ad_printf)( " -ndb N          set number of blocks for RE derivatives "
                          "(reduce temp file size)\n");
            (*ad_printf)( " -ddnr           use high precision Newton-Raphson, for "
                          "banded Hessian case only\n");
            (*ad_printf)( " -nrdbg          verbose reporting for debugging "
                          "newton-raphson\n");
#  if defined(__MINI_MAX__)
            (*ad_printf)( " -mm N           do minimax optimization\n");
#  endif
            (*ad_printf)( " -shess          use sparse Hessian structure inner "
                          "optimzation\n\n");

            (*ad_printf)("Read online documentation at http://admb-project.org\n");
            (*ad_printf)("Contact <*****@*****.**> for help.\n");
            //}
            ad_exit(0);
        }
        else if (option_match(_argc,_argv,"-info") > -1)
        {
            (*ad_printf)("ADMB Information\n");
            (*ad_printf)("================\n\n");

            (*ad_printf)("How to Cite ADMB\n");
            (*ad_printf)("----------------\n\n");

            (*ad_printf)("Fournier, D.A., H.J. Skaug, J. Ancheta, J. Ianelli, "
                         "A. Magnusson, M.N. Maunder,\n");
            (*ad_printf)("A. Nielsen, and J. Sibert. 2012. AD Model Builder: using "
                         "automatic\n");
            (*ad_printf)("differentiation for statistical inference of highly "
                         "parameterized complex\n");
            (*ad_printf)("nonlinear models. Optim. Methods Softw. 27:233-249.\n\n");

            //(*ad_printf)(" %s", (char*)admb_banner);
            (*ad_printf)("License\n");
            (*ad_printf)("-------\n\n");

            (*ad_printf)("Copyright (c) 2008-2013\n");
            (*ad_printf)("Regents of the University of California and ADMB "
                         "Foundation\n\n");
            (*ad_printf)("ADMB is free software and comes with ABSOLUTELY NO "
                         "WARRANTY.\n");
            (*ad_printf)("You are welcome to redistribute it under certain "
                         "conditions.\n\n");
            (*ad_printf)("AD Model Builder, or ADMB, was developed by David Fournier "
                         "of Otter Research\n");
            (*ad_printf)("Ltd, Sidney, BC, Canada. In 2007, scientists from the "
                         "University of Hawai'i at\n");
            (*ad_printf)("Manoa Pelagic Fisheries Research Program (John Sibert and "
                         "Anders Nielsen) and\n");
            (*ad_printf)("the Inter-American Tropical Tuna Commission (Mark "
                         "Maunder), in consultation with\n");
            (*ad_printf)("scientists from NOAA Fisheries (Richard Methot), created "
                         "the non-profit ADMB\n");
            (*ad_printf)("Foundation (admb-foundation.org) with the goal of "
                         "increasing the number of ADMB\n");
            (*ad_printf)("users by making the software free and open source. In "
                         "partnership with NOAA\n");
            (*ad_printf)("Fisheries and the National Center for Ecological Analysis "
                         "and Synthesis (NCEAS,\n");
            (*ad_printf)("www.nceas.ucsb.edu), the ADMB Foundation obtained funding "
                         "from the Gordon and\n");
            (*ad_printf)("Betty Moore Foundation (www.moore.org) to acquire the "
                         "copyright to the ADMB\n");
            (*ad_printf)("software suite, in order to make it broadly and freely "
                         "available to the research\n");
            (*ad_printf)("community. In 2008 the copyright was transferred from "
                         "Otter Research Ltd to the\n");
            (*ad_printf)("University of California. The binary files were released "
                         "in November 2008 and\n");
            (*ad_printf)("the source code was released in December 2009. More "
                         "information about the ADMB\n");
            (*ad_printf)("Project can be found at admb-project.org.\n\n");
            (*ad_printf)("ADMB was originally developed by David Fournier of Otter "
                         "Research Ltd.\n\n");
            (*ad_printf)("It is now maintained by the ADMB Core Team, whose members "
                         "are listed on\n");
            (*ad_printf)("http://admb-project.org/developers/core-team.\n");

            ad_exit(0);
        }
    }
    allocate();
}
Beispiel #12
0
void ad_comm::allocate(void)
{
#if defined (_WIN32)
    directory_prefix='\\';
#else
    directory_prefix='/';
#endif
    adstring tmpstring;

#if defined(_MSC_VER)
    //remove path
    for (int i = (int)adprogram_name.size(); i >= 1; i--)
    {
        if (adprogram_name(i)==directory_prefix)
        {
            adprogram_name=adprogram_name(i+1,adprogram_name.size());
            break;
        }
    }
#endif

#if defined(_WIN32)
    // strip off the .exe
#ifdef __MINGW64__
    size_t _n = adprogram_name.size();
    assert(_n <= INT_MAX);
    int n = (int)_n;
#else
    int n = (int)adprogram_name.size();
#endif
    if (n > 4)
    {
        if (adprogram_name(n - 3) == '.'
                && tolower(adprogram_name(n - 2)) == 'e'
                && tolower(adprogram_name(n - 1)) == 'x'
                && tolower(adprogram_name(n)) == 'e')
        {
            n -= 4;
        }
    }
    adprogram_name=adprogram_name(1,n);
#endif

    // change the working directory name
    if (argc > 1)
    {
        int on=0;
        if ( (on=option_match(argc,argv,"-wd"))>-1)
        {
            if (on>argc-2 || argv[on+1][0] == '-')
            {
                cerr << "Invalid input data command line option"
                     " -- ignored" << endl;
            }
            else
            {
                tmpstring = adstring(argv[on+1]);
                wd_flag=1;
            }
        }
    }
    if (length(tmpstring))
    {
        if (tmpstring(length(tmpstring)) == directory_prefix)
        {
            adprogram_name=tmpstring + adprogram_name;
            working_directory_path = tmpstring;
        }
        else
        {
            adprogram_name=tmpstring + directory_prefix + adprogram_name;
            working_directory_path = tmpstring + directory_prefix;
        }
    }

    tmpstring=adprogram_name + adstring(".dat");
    if (argc > 1)
    {
        int on=0;
        if ( (on=option_match(argc,argv,"-ind"))>-1)
        {
            if (on>argc-2 || argv[on+1][0] == '-')
            {
                cerr << "Invalid input data command line option"
                     " -- ignored" << endl;
            }
            else
            {
                tmpstring = adstring(argv[on+1]);
            }
        }
    }
    global_datafile= new cifstream(tmpstring);
    if (!global_datafile)
    {
        cerr << "Error trying to open data input file "
             << tmpstring << endl;
    }
    else
    {
        if (!(*global_datafile))
        {
            cerr << "Error trying to open data input file "
                 << tmpstring << endl;
            delete global_datafile;
            global_datafile=NULL;
        }
    }
    adstring ts=adprogram_name + adstring(".log");
    global_logfile= new ofstream( (char*)ts);

    int biopt=-1;
    int aiopt=-1;
    biopt=option_match(argc,argv,"-binp");
    aiopt=option_match(argc,argv,"-ainp");

    tmpstring=adprogram_name + adstring(".bin");
    if (!global_bparfile && aiopt == -1)
    {
        if (biopt>-1)
        {
            if (biopt>argc-2 || argv[biopt+1][0] == '-')
            {
                cerr << "Invalid input parameter file command line option"
                     " -- ignored" << endl;
            }
            else
            {
                tmpstring = adstring(argv[biopt+1]);
            }
        }
        global_bparfile= new uistream(tmpstring);
        if (global_bparfile)
        {
            if (!(*global_bparfile))
            {
                if (biopt>-1)
                {
                    cerr << "Error trying to open binary inoput par file "
                         << tmpstring << endl;
                    exit(1);
                }
                delete global_bparfile;
                global_bparfile=NULL;
            }
        }
    }
    tmpstring=adprogram_name + adstring(".pin");
    if (!global_parfile)
    {
        if (aiopt>-1)
        {
            if (aiopt>argc-2 || argv[aiopt+1][0] == '-')
            {
                cerr << "Invalid input parameter file command line option"
                     " -- ignored" << endl;
            }
            else
            {
                tmpstring = adstring(argv[aiopt+1]);
            }
        }
        global_parfile= new cifstream(tmpstring);
        if (global_parfile)
        {
            if (!(*global_parfile))
            {
                if (aiopt>-1)
                {
                    cerr << "Error trying to open ascii inoput par file "
                         << tmpstring << endl;
                    exit(1);
                }
                delete global_parfile;
                global_parfile=NULL;
            }
        }
    }
}
Beispiel #13
0
void function_minimizer::pvm_master_mcmc_routine(int nmcmc,int iseed0,double dscale,
  int restart_flag)
{
  uostream * pofs_psave=NULL;
  dmatrix mcmc_display_matrix;
  int mcmc_save_index=1;
  int mcmc_wrap_flag=0;
  int mcmc_gui_length=10000;
  int no_sd_mcmc=0;

  int on2=-1;
  if ( (on2=option_match(ad_comm::argc,ad_comm::argv,"-nosdmcmc"))>-1)
    no_sd_mcmc=1;

  if (stddev_params::num_stddev_params==0)
  {
    cerr << " You must declare at least one object of type sdreport "
         << endl << " to do the mcmc calculations" << endl;
     return;
  }
  {
    //ofstream of_bf("testbf");

    //if (adjm_ptr) set_labels_for_mcmc();

    ivector number_offsets;
    dvector lkvector;
    //double current_bf=0;
    double lcurrent_bf=0;
    double size_scale=1.0;
    double total_spread=200;
    //double total_spread=2500;
    uostream * pofs_sd = NULL;
    int nvar=initial_params::nvarcalc(); // get the number of active parameters
    int scov_option=0;
    dmatrix s_covar;
    dvector s_mean;
    int on=-1;
    int ncsim=25000;
    int nslots=800;
    //int nslots=3600;
    int initial_nsim=4800;
    int ntmp=0;
    int ncor=0;
    double bfsum=0;
    int ibfcount=0;
    double llbest;
    double lbmax;

    //if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcscov",ntmp))>-1)
    //{
    scov_option=1;
    s_covar.allocate(1,nvar,1,nvar);
    s_mean.allocate(1,nvar);
    s_mean.initialize();
    s_covar.initialize();

    int ndvar=stddev_params::num_stddev_calc();
    int numdvar=stddev_params::num_stddev_number_calc();
    /*
    if (adjm_ptr)
    {
      mcmc_display_matrix.allocate(1,numdvar,1,mcmc_gui_length);
      number_offsets.allocate(1,numdvar);
      number_offsets=stddev_params::copy_all_number_offsets();
    }
    */
    dvector x(1,nvar);
    dvector scale(1,nvar);
    dmatrix values;
    int have_hist_flag=0;
    initial_params::xinit(x);
    dvector pen_vector(1,nvar);
    {
      initial_params::reset(dvar_vector(x),pen_vector);
      cout << pen_vector << endl << endl;
    }

    initial_params::mc_phase=0;
    initial_params::stddev_scale(scale,x);
    initial_params::mc_phase=1;
    dvector bmn(1,nvar);
    dvector mean_mcmc_values(1,ndvar);
    dvector s(1,ndvar);
    dvector h(1,ndvar);
    //dvector h;
    dvector square_mcmc_values(1,ndvar);
    square_mcmc_values.initialize();
    mean_mcmc_values.initialize();
    bmn.initialize();
    int use_empirical_flag=0;
    int diag_option=0;
    int topt=0;
    if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcdiag"))>-1)
    {
      diag_option=1;
      cout << " Setting covariance matrix to diagonal with entries " << dscale
           << endl;
    }
    dmatrix S(1,nvar,1,nvar);
    dvector sscale(1,nvar);
    if (!diag_option)
    {
      int on,nopt;
      int rescale_bounded_flag=0;
      double rescale_bounded_power=0.5;
      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcrb",nopt))>-1)
      {
        if (nopt)
        {
          int iii=atoi(ad_comm::argv[on+1]);
          if (iii < 1 || iii > 9)
          {
            cerr << " -mcrb argument must be integer between 1 and 9 --"
                    " using default of 5" << endl;
            rescale_bounded_power=0.5;
          }
          else
            rescale_bounded_power=iii/10.0;
        }
        else
        {
          rescale_bounded_power=0.5;
        }
        rescale_bounded_flag=1;
      }
      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcec"))>-1)
      {
        use_empirical_flag=1;
      }
      if (use_empirical_flag)
      {
        read_empirical_covariance_matrix(nvar,S,ad_comm::adprogram_name);
      }
      else if (!rescale_bounded_flag)
      {
        int tmp;
        read_covariance_matrix(S,nvar,tmp,sscale);
      }
      else
      {
        read_hessian_matrix_and_scale1(nvar,S,rescale_bounded_power,
          mcmc2_flag);
        //read_hessian_matrix_and_scale(nvar,S,pen_vector);
      }

      {  // scale covariance matrix for model space
        dmatrix tmp(1,nvar,1,nvar);
        for (int i=1;i<=nvar;i++)
        {
          tmp(i,i)=S(i,i)*(scale(i)*scale(i));
          for (int j=1;j<i;j++)
          {
            tmp(i,j)=S(i,j)*(scale(i)*scale(j));
            tmp(j,i)=tmp(i,j);
          }
        }
        S=tmp;
      }
    }
    else
    {
      S.initialize();
      for (int i=1;i<=nvar;i++)
      {
        S(i,i)=dscale;
      }
    }

    cout << sort(eigenvalues(S)) << endl;
    dmatrix chd = choleski_decomp( (dscale*2.4/sqrt(double(nvar))) * S);
    dmatrix chdinv=inv(chd);
    int sgn;

    dmatrix symbds(1,2,1,nvar);
    initial_params::set_all_simulation_bounds(symbds);
    ofstream ofs_sd1((char*)(ad_comm::adprogram_name + adstring(".mc2")));

    {
      long int iseed=0;
      int number_sims;
      if (nmcmc<=0)
      {
        number_sims=  100000;
      }
      else
      {
        number_sims=  nmcmc;
      }
      //cin >> iseed;
      if (iseed0<=0)
      {
        iseed=-36519;
      }
      else
      {
        iseed=-iseed0;
      }
      if (iseed>0)
      {
        iseed=-iseed;
      }
      cout << "Initial seed value " << iseed << endl;
      random_number_generator rng(iseed);
      rng.better_rand();
      //better_rand(iseed);
      double lprob=0.0;
      double lpinv=0.0;
      double lprob3=0.0;
      // get lower and upper bounds

      independent_variables y(1,nvar);
      independent_variables parsave(1,nvar);

      // read in the mcmc values to date
      int ii=1;
      dmatrix hist;
      if (restart_flag)
      {
        int tmp=0;
        if (!no_sd_mcmc) {
          hist.allocate(1,ndvar,-nslots,nslots);
          tmp=read_hist_data(hist,h,mean_mcmc_values,s,parsave,iseed,
            size_scale);
          values.allocate(1,ndvar,-nslots,nslots);
          for (int i=1;i<=ndvar;i++)
          {
            values(i).fill_seqadd(mean_mcmc_values(i)-0.5*total_spread*s(i)
              +.5*h(i),h(i));
          }
        }
        if (iseed>0)
        {
          iseed=-iseed;
        }
        double br=rng.better_rand();
        if (tmp) have_hist_flag=1;
        chd=size_scale*chd;
        chdinv=chdinv/size_scale;
      }
      else
      {
        int on=-1;
        int nopt=0;
        if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcpin",nopt))>-1)
        {
          if (nopt)
          {
            cifstream cif((char *)ad_comm::argv[on+1]);
            if (!cif)
            {
              cerr << "Error trying to open mcmc par input file "
                   << ad_comm::argv[on+1] << endl;
              exit(1);
            }
            cif >> parsave;
            if (!cif)
            {
              cerr << "Error reading from mcmc par input file "
                   << ad_comm::argv[on+1] << endl;
              exit(1);
            }
          }
          else
          {
            cerr << "Illegal option with -mcpin" << endl;
          }
        }
        else
        {
          ii=1;
          initial_params::copy_all_values(parsave,ii);
        }
      }

      ii=1;
      initial_params::restore_all_values(parsave,ii);

      gradient_structure::set_NO_DERIVATIVES();
      ofstream ogs("sims");
      ogs << nvar << " " << number_sims << endl;
      initial_params::xinit(y);

      send_int_to_slaves(1);
      double llc=-pvm_master_get_monte_carlo_value(nvar,y);
      send_int_to_slaves(1);
      llbest=-pvm_master_get_monte_carlo_value(nvar,y);



      lbmax=llbest;
      // store current mcmc variable values in param_values
      //dmatrix store_mcmc_values(1,number_sims,1,ndvar);
#if defined(USE_BAYES_FACTORS)
      lkvector.allocate(1,number_sims);
#endif
      dvector mcmc_values(1,ndvar);
      dvector mcmc_number_values;
      //if (adjm_ptr) mcmc_number_values.allocate(1,numdvar);
      int offs=1;
      stddev_params::copy_all_values(mcmc_values,offs);

      /*
      if (adjm_ptr)
      {
        offs=1;
        stddev_params::copy_all_number_values(mcmc_number_values,offs);
      }
      */
      int change_ball=2500;
      int nopt;
      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcscale",nopt))>-1)
      {
        if (nopt)
        {
          int iii=atoi(ad_comm::argv[on+1]);
          if (iii <=0)
          {
            cerr << " Invalid option following command line option -mcball -- "
              << endl << " ignored" << endl;
          }
          else
            change_ball=iii;
        }
      }
      int iac=0;
      int liac=0;
      int isim=0;
      int itmp=0;
      double logr;
      int u_option=0;
      double ll;
      int s_option=1;
      int psvflag=0;
      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcu"))>-1)
      {
        u_option=1;
      }
      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcnoscale"))>-1)
      {
        s_option=0;
      }
      //cout << llc << " " << llc << endl;
      int iac_old=0;
      int i_old=0;

      {
       if (!restart_flag)
       {
         pofs_sd =
           new uostream((char*)(ad_comm::adprogram_name + adstring(".mcm")));
       }

      int mcsave_flag=0;
      int mcrestart_flag=option_match(ad_comm::argc,ad_comm::argv,"-mcr");

      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcsave"))>-1)
      {
        int jj=(int)atof(ad_comm::argv[on+1]);
        if (jj <=0)
        {
          cerr << " Invalid option following command line option -mcsave -- "
            << endl;
        }
        else
        {
          mcsave_flag=jj;
          if ( mcrestart_flag>-1)
          {
            // check that nvar is correct
            {
              uistream uis((char*)(ad_comm::adprogram_name + adstring(".psv")));
              if (!uis)
              {
                cerr << "Error trying to open file" <<
                  ad_comm::adprogram_name + adstring(".psv") <<
                  " for mcrestart" <<   endl;
                cerr << " I am starting a new file " << endl;
                psvflag=1;
              }
              else
              {
                int nv1;
                uis >> nv1;
                if (nv1 !=nvar)
                {
                  cerr << "wrong number of independent variables in" <<
                    ad_comm::adprogram_name + adstring(".psv") <<
                  cerr << " I am starting a new file " << endl;
                  psvflag=1;
                }
              }
            }

            if (!psvflag) {
              pofs_psave=
                new uostream(
                  (char*)(ad_comm::adprogram_name + adstring(".psv")),ios::app);
            } else {
              pofs_psave=
                new uostream((char*)(ad_comm::adprogram_name + adstring(".psv")));
            }

          } else {
Beispiel #14
0
void function_minimizer::shmc_mcmc_routine(int nmcmc,int iseed0,double dscale,
					  int restart_flag) {

  if (nmcmc<=0)
    {
      cerr << endl << "Error: Negative iterations for MCMC not meaningful" << endl;
      ad_exit(1);
    }

  uostream * pofs_psave=NULL;
  if (mcmc2_flag==1)
    {
      initial_params::restore_start_phase();
    }
  initial_params::set_inactive_random_effects();
  initial_params::set_active_random_effects();
  int nvar_re=initial_params::nvarcalc();
  int nvar=initial_params::nvarcalc(); // get the number of active parameters
  if (mcmc2_flag==0)
    {
      initial_params::set_inactive_random_effects();
      nvar=initial_params::nvarcalc(); // get the number of active parameters
    }
  initial_params::restore_start_phase();

  independent_variables parsave(1,nvar_re);
  // dvector x(1,nvar);
  // initial_params::xinit(x);
  // dvector pen_vector(1,nvar);
  // {
  //   initial_params::reset(dvar_vector(x),pen_vector);
  // }
  initial_params::mc_phase=1;

  int old_Hybrid_bounded_flag=-1;
  int on,nopt = 0;

  //// ------------------------------ Parse input options
  // Step size. If not specified, will be adapted. If specified must be >0
  // and will not be adapted.
  double eps=0.1;
  double _eps=-1.0;
  int useDA=1; 			// whether to adapt step size
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-hyeps",nopt))>-1)
    {
      if (!nopt) // not specified means to adapt, using function below to find reasonable one
	{
	  cerr << "Warning: No step size given after -hyeps, ignoring" << endl;
	  useDA=1;
	}
      else			// read in specified value and do not adapt
	{
	  istringstream ist(ad_comm::argv[on+1]);
	  ist >> _eps;
	  if (_eps<=0)
	    {
	      cerr << "Error: step size (-hyeps argument) needs positive number";
	      ad_exit(1);
	    }
	  else
	    {
	      eps=_eps;
	      useDA=0;
	    }
	}
    }
  // Chain number -- for console display purposes only
  int chain=1;
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-chain",nopt))>-1) {
    if (nopt) {
      int iii=atoi(ad_comm::argv[on+1]);
      if (iii <1) {
	cerr << "Error: chain must be >= 1" << endl;
	ad_exit(1);
      } else {
	chain=iii;
      }
    }
  }
  // Number of leapfrog steps. Defaults to 10.
  int L=10;
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-hynstep",nopt))>-1)
    {
      if (nopt)
	{
	  int _L=atoi(ad_comm::argv[on+1]);
	  if (_L < 1 )
	    {
	      cerr << "Error: hynstep argument must be integer > 0 " << endl;
	      ad_exit(1);
	    }
	  else
	    {
	      L=_L;
	    }
	}
    }

  // Number of warmup samples if using adaptation of step size. Defaults to
  // half of iterations.
  int nwarmup= (int)nmcmc/2;
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-nwarmup",nopt))>-1)
    {
      if (nopt)
        {
          int iii=atoi(ad_comm::argv[on+1]);
          if (iii <=0 || iii > nmcmc)
	    {
	      cerr << "Error: nwarmup must be 0 < nwarmup < nmcmc" << endl;
	      ad_exit(1);
	    }
          else
	    {
	      nwarmup=iii;
	    }
        }
    }

  // Target acceptance rate for step size adaptation. Must be
  // 0<adapt_delta<1. Defaults to 0.8.
  double adapt_delta=0.8; // target acceptance rate specified by the user
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-adapt_delta",nopt))>-1)
    {
      if (nopt)
	{
	  istringstream ist(ad_comm::argv[on+1]);
	  double _adapt_delta;
	  ist >> _adapt_delta;
	  if (_adapt_delta < 0 || _adapt_delta > 1 )
	    {
	      cerr << "Error: adapt_delta must be between 0 and 1"
		" using default of 0.8" << endl;
	    }
	  else
	    {
	      adapt_delta=_adapt_delta;
	    }
	}
    }
  // Use diagnoal covariance (identity mass matrix)
  int diag_option=0;
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcdiag"))>-1)
    {
      diag_option=1;
      cout << " Setting covariance matrix to diagonal with entries " << dscale
	   << endl;
    }
  // Restart chain from previous run?
  int mcrestart_flag=option_match(ad_comm::argc,ad_comm::argv,"-mcr");
  if(mcrestart_flag > -1){
    cerr << endl << "Error: -mcr option not implemented for HMC" << endl;
    ad_exit(1);
  }

  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcec"))>-1)
    {
      cerr << endl << "Error: -mcec option not yet implemented with HMC" << endl;
      ad_exit(1);
      // use_empirical_flag=1;
      // read_empirical_covariance_matrix(nvar,S,ad_comm::adprogram_name);
    }

  // Prepare the mass matrix for use. Depends on many factors below.
  dmatrix S(1,nvar,1,nvar);
  dvector old_scale(1,nvar);
  int old_nvar;
  // Need to grab old_scale values still, since it is scaled below
  read_covariance_matrix(S,nvar,old_Hybrid_bounded_flag,old_scale);
  if (diag_option)		// set covariance to be diagonal
    {
      S.initialize();
      for (int i=1;i<=nvar;i++)
	{
	  S(i,i)=dscale;
	}
    }

  // How much to thin, for now fixed at 1.
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcsave"))>-1)
    {
      cerr << "Option -mcsave does not currently work with HMC -- every iteration is saved" << endl;
      ad_exit(1);
    }
  //// ------------------------------ End of input processing


  //// Setup more inputs and outputs
  pofs_psave=
    new uostream((char*)(ad_comm::adprogram_name + adstring(".psv")));
  if (!pofs_psave|| !(*pofs_psave))
    {
      cerr << "Error trying to open file" <<
	ad_comm::adprogram_name + adstring(".psv") << endl;
      ad_exit(1);
    }
  if (mcrestart_flag == -1 )
    {
      (*pofs_psave) << nvar;
    }
  // need to rescale the hessian
  // get the current scale
  dvector x0(1,nvar);
  dvector current_scale(1,nvar);
  initial_params::xinit(x0);
  int mctmp=initial_params::mc_phase;
  initial_params::mc_phase=0;
  initial_params::stddev_scale(current_scale,x0);
  initial_params::mc_phase=mctmp;
  // cout << "old scale=" <<  old_scale << endl;
  // cout << "current scale=" << current_scale << endl;
  // cout << "S before=" << S << endl;
  // I think this is only needed if mcmc2 is used??
  // for (int i=1;i<=nvar;i++)
  //   {
  //     for (int j=1;j<=nvar;j++)
  // 	{
  // 	  S(i,j)*=old_scale(i)*old_scale(j);
  // 	}
  //   }
  if(diag_option){
    for (int i=1;i<=nvar;i++)
      {
	for (int j=1;j<=nvar;j++)
	  {
	    S(i,j)*=current_scale(i)*current_scale(j);
	  }
      }
  }
  //  cout << "S after=" << S << endl;
  gradient_structure::set_NO_DERIVATIVES();
  if (mcmc2_flag==0)
    {
      initial_params::set_inactive_random_effects();
    }
  // Setup random number generator, based on seed passed
  int iseed=2197;
  if (iseed0) iseed=iseed0;
  random_number_generator rng(iseed);
  gradient_structure::set_YES_DERIVATIVES();
  initial_params::xinit(x0);

  // Dual averaging components
  dvector epsvec(1,nmcmc+1), epsbar(1,nmcmc+1), Hbar(1,nmcmc+1);
  epsvec.initialize(); epsbar.initialize(); Hbar.initialize();
  double time_warmup=0;
  double time_total=0;
  std::clock_t start = clock();
  time_t now = time(0);
  tm* localtm = localtime(&now);
  cout << endl << "Starting static HMC for model '" << ad_comm::adprogram_name <<
    "' at " << asctime(localtm);
  // write sampler parameters
  ofstream adaptation("adaptation.csv", ios::trunc);
  adaptation << "accept_stat__,stepsize__,int_time__,energy__,lp__" << endl;

  // Declare and initialize the variables needed for the algorithm
  dmatrix chd = choleski_decomp(S); // cholesky decomp of mass matrix
  dvector y(1,nvar); // unbounded parameters
  y.initialize();
  // transformed params
  independent_variables z(1,nvar); z=chd*y;
  dvector gr(1,nvar);		// gradients in unbounded space
  // Need to run this to fill gr with current gradients and initial NLL.
  double nllbegin=get_hybrid_monte_carlo_value(nvar,z,gr);
  if(std::isnan(nllbegin)){
    cerr << "Starting MCMC trajectory at NaN -- something is wrong!" << endl;
    ad_exit(1);
  }
  // initial rotated gradient
  dvector gr2(1,nvar); gr2=gr*chd;
  dvector p(1,nvar);		// momentum vector
  p.fill_randn(rng);
  // Copy initial value to parsave in case first trajectory rejected
  initial_params::copy_all_values(parsave,1.0);
  double iaccept=0.0;
  // The gradient and params at beginning of trajectory, in case rejected.
  dvector gr2begin(1,nvar); gr2begin=gr2;
  dvector ybegin(1,nvar); ybegin=y;
  double nll=nllbegin;
  // if(useDA){
  //   eps=find_reasonable_stepsize(nvar,y,p,chd);
  //   epsvec(1)=eps; epsbar(1)=eps; Hbar(1)=0;
  // }
  double mu=log(10*eps);

  // Start of MCMC chain
  for (int is=1;is<=nmcmc;is++) {
    // Random momentum for next iteration, only affects Ham values
    p.fill_randn(rng);
    double H0=nll+0.5*norm2(p);

    // Generate trajectory
    int divergence=0;
    for (int i=1;i<=L;i++) {
      // leapfrog updates gr, p, y, and gr2 by reference
      nll=leapfrog(nvar, gr, chd, eps, p, y, gr2);
      // Break trajectory early if a divergence occurs to save computation
      if(std::isnan(nll)){
	divergence=1; break;
      }
    } // end of trajectory

    // Test whether to accept the proposed state
    double Ham=nll+0.5*norm2(p); // Update Hamiltonian for proposed set
    double alpha=min(1.0, exp(H0-Ham)); // acceptance ratio
    double rr=randu(rng);	   // Runif(1)
    if (rr<alpha && !divergence){ // accept
      iaccept++;
      // Update for next iteration: params, Hamiltonian and gr2
      ybegin=y;
      gr2begin=gr2;
      nllbegin=nll;
      initial_params::copy_all_values(parsave,1.0);
    } else {
      // Reject and don't update anything to reuse initials for next trajectory
      y=ybegin;
      gr2=gr2begin;
      nll=nllbegin;
    }
    // Save parameters to psv file, duplicated if rejected
    (*pofs_psave) << parsave;

    // Adaptation of step size (eps).
    if(useDA && is <= nwarmup){
      eps=adapt_eps(is, eps,  alpha, adapt_delta, mu, epsvec, epsbar, Hbar);
    }
    adaptation << alpha << "," <<  eps << "," << eps*L << "," << H0 << "," << -nll << endl;
    if(is ==nwarmup) time_warmup = ( std::clock()-start)/(double) CLOCKS_PER_SEC;
    print_mcmc_progress(is, nmcmc, nwarmup, chain);
  } // end of MCMC chain

  // This final ratio should closely match adapt_delta
  if(useDA){
    cout << "Final acceptance ratio=" << iaccept/nmcmc << " and target is " << adapt_delta<<endl;
    cout << "Final step size=" << eps << "; after " << nwarmup << " warmup iterations"<< endl;
  } else {
    cout << "Final acceptance ratio=" << iaccept/nmcmc << endl;
  }

  time_total = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
  print_mcmc_timing(time_warmup, time_total);

  // I assume this closes the connection to the file??
  if (pofs_psave)
    {
      delete pofs_psave;
      pofs_psave=NULL;
    }
} // end of HMC function
Beispiel #15
0
  void function_minimizer::mcmc_computations(void)
  {
    int ton,tnopt = 0;
    ton=option_match(ad_comm::argc,ad_comm::argv,"-mcmc",tnopt);
    if (ton<0)
    {
      ton=option_match(ad_comm::argc,ad_comm::argv,"-mcmc2",tnopt);
    }
    int on=ton;
    int nopt=tnopt;

    if (on>-1)
    {
      /*
      if (adjm_ptr)
      {
        ad_open_mcmc_options_window();
        ad_open_mcmchist_window();
        //test_mcmc_options_window();
      }
      */
      int nmcmc=0;
      int iseed0=0;
      double dscale=1.0;
      if (nopt)
      {
        nmcmc=(int)atof(ad_comm::argv[on+1]);
        if (nmcmc <=0)
        {
          cerr << " Invalid option following command line option -mcmc -- "
            << endl << " ignored" << endl;
        }
      }
      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcmult",nopt))>-1)
      {
        if (nopt)
        {
          char* end;
          double _dscale = strtod(ad_comm::argv[on + 1], &end);
          if (_dscale != 0.0)
          {
            cerr << "Invalid argument to option -mcmult" << endl;
          }
          else
          {
            dscale = _dscale;
          }
        }
      }
      if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcseed",nopt))>-1)
      {
        if (nopt)
        {
          int _iseed0 = atoi(ad_comm::argv[on+1]);
          if (_iseed0 <=0)
          {
            cerr << " Invalid option following command line option -mcseed -- "
              << endl << " ignored" << endl;
          }
          else
          {
            iseed0 = _iseed0;
          }
        }
      }
      int hybrid_flag=0;
      if (option_match(ad_comm::argc,ad_comm::argv,"-hybrid") > -1)
      {
        hybrid_flag=1;
        gradient_structure::Hybrid_bounded_flag=1;
      }

      // start addition
      // temporarily adding this here, need to fully merge in with other options still
      if (option_match(ad_comm::argc,ad_comm::argv,"-hmc") > -1)
	{
	  gradient_structure::Hybrid_bounded_flag=1;
	  shmc_mcmc_routine(nmcmc,iseed0,dscale,0);
	  return;
	}
      if (option_match(ad_comm::argc,ad_comm::argv,"-nuts") > -1)
	{
	  gradient_structure::Hybrid_bounded_flag=1;
	  nuts_mcmc_routine(nmcmc,iseed0,dscale,0);
	  return;
	}
      if (option_match(ad_comm::argc,ad_comm::argv,"-nuts_test") > -1)
	{
	  gradient_structure::Hybrid_bounded_flag=1;
	  nuts_test_mcmc_routine(nmcmc,iseed0,dscale,0);
	  return;
	}
      // This one is my modified version of the one Dave wrote. Mostly
      // cosmetic differences to get it to work with adnuts better.
      if (option_match(ad_comm::argc,ad_comm::argv,"-rwm") > -1)
	{
	  gradient_structure::Hybrid_bounded_flag=0;
	  rwm_mcmc_routine(nmcmc,iseed0,dscale,0);
	  return;
	}

      // Temporarily turn off this chunk if using HMC
     else
	{
	  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcr",nopt))>-1)
	    {
	      if (hybrid_flag==0)
		{
		  mcmc_routine(nmcmc,iseed0,dscale,1);
		}
	      else
		{
		  hybrid_mcmc_routine(nmcmc,iseed0,dscale,1);
		}
	    }
	  else
	    {
	      if (hybrid_flag==0)
		{
		  mcmc_routine(nmcmc,iseed0,dscale,0);
		}
	      else
		{
		  hybrid_mcmc_routine(nmcmc,iseed0,dscale,0);
		}
	    }
	}
    }
  }
Beispiel #16
0
/**
Symmetrize and invert the hessian
*/
void function_minimizer::hess_inv(void)
{
  initial_params::set_inactive_only_random_effects();
  int nvar=initial_params::nvarcalc(); // get the number of active parameters
  independent_variables x(1,nvar);

  initial_params::xinit(x);        // get the initial values into the x vector
  //double f;
  dmatrix hess(1,nvar,1,nvar);
  uistream ifs("admodel.hes");
  int file_nvar = 0;
  ifs >> file_nvar;
  if (nvar != file_nvar)
  {
    cerr << "Number of active variables in file mod_hess.rpt is wrong"
         << endl;
  }

  for (int i = 1;i <= nvar; i++)
  {
    ifs >> hess(i);
    if (!ifs)
    {
      cerr << "Error reading line " << i  << " of the hessian"
           << " in routine hess_inv()" << endl;
      exit(1);
    }
  }
  int hybflag = 0;
  ifs >> hybflag;
  dvector sscale(1,nvar);
  ifs >> sscale;
  if (!ifs)
  {
    cerr << "Error reading sscale"
         << " in routine hess_inv()" << endl;
  }

  double maxerr=0.0;
  for (int i = 1;i <= nvar; i++)
  {
    for (int j=1;j<i;j++)
    {
      double tmp=(hess(i,j)+hess(j,i))/2.;
      double tmp1=fabs(hess(i,j)-hess(j,i));
      tmp1/=(1.e-4+fabs(hess(i,j))+fabs(hess(j,i)));
      if (tmp1>maxerr) maxerr=tmp1;
      hess(i,j)=tmp;
      hess(j,i)=tmp;
    }
  }
  /*
  if (maxerr>1.e-2)
  {
    cerr << "warning -- hessian aprroximation is poor" << endl;
  }
 */

  for (int i = 1;i <= nvar; i++)
  {
    int zero_switch=0;
    for (int j=1;j<=nvar;j++)
    {
      if (hess(i,j)!=0.0)
      {
        zero_switch=1;
      }
    }
    if (!zero_switch)
    {
      cerr << " Hessian is 0 in row " << i << endl;
      cerr << " This means that the derivative if probably identically 0 "
              " for this parameter" << endl;
    }
  }

  int ssggnn;
  ln_det(hess,ssggnn);
  int on1=0;
  {
    ofstream ofs3((char*)(ad_comm::adprogram_name + adstring(".eva")));
    {
      dvector se=eigenvalues(hess);
      ofs3 << setshowpoint() << setw(14) << setprecision(10)
           << "unsorted:\t" << se << endl;
     se=sort(se);
     ofs3 << setshowpoint() << setw(14) << setprecision(10)
     << "sorted:\t" << se << endl;
     if (se(se.indexmin())<=0.0)
      {
        negative_eigenvalue_flag=1;
        cout << "Warning -- Hessian does not appear to be"
         " positive definite" << endl;
      }
    }
    ivector negflags(0,hess.indexmax());
    int num_negflags=0;
    {
      int on = option_match(ad_comm::argc,ad_comm::argv,"-eigvec");
      on1=option_match(ad_comm::argc,ad_comm::argv,"-spmin");
      if (on > -1 || on1 >-1 )
      {
        ofs3 << setshowpoint() << setw(14) << setprecision(10)
          << eigenvalues(hess) << endl;
        dmatrix ev=trans(eigenvectors(hess));
        ofs3 << setshowpoint() << setw(14) << setprecision(10)
          << ev << endl;
        for (int i=1;i<=ev.indexmax();i++)
        {
          double lam=ev(i)*hess*ev(i);
          ofs3 << setshowpoint() << setw(14) << setprecision(10)
            << lam << "  "  << ev(i)*ev(i) << endl;
          if (lam<0.0)
          {
            num_negflags++;
            negflags(num_negflags)=i;
          }
        }
        if ( (on1>-1) && (num_negflags>0))   // we will try to get away from
        {                                     // saddle point
          negative_eigenvalue_flag=0;
          spminflag=1;
          if(negdirections)
          {
            delete negdirections;
          }
          negdirections = new dmatrix(1,num_negflags);
          for (int i=1;i<=num_negflags;i++)
          {
            (*negdirections)(i)=ev(negflags(i));
          }
        }
        int on2 = option_match(ad_comm::argc,ad_comm::argv,"-cross");
        if (on2>-1)
        {                                     // saddle point
          dmatrix cross(1,ev.indexmax(),1,ev.indexmax());
          for (int i = 1;i <= ev.indexmax(); i++)
          {
            for (int j=1;j<=ev.indexmax();j++)
            {
              cross(i,j)=ev(i)*ev(j);
            }
          }
          ofs3 <<  endl << "  e(i)*e(j) ";
          ofs3 << endl << cross << endl;
        }
      }
    }

    if (spminflag==0)
    {
      if (num_negflags==0)
      {
        hess=inv(hess);
        int on=0;
        if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-eigvec"))>-1)
        {
          int i;
          ofs3 << "choleski decomp of correlation" << endl;
          dmatrix ch=choleski_decomp(hess);
          for (i=1;i<=ch.indexmax();i++)
            ofs3 << ch(i)/norm(ch(i)) << endl;
          ofs3 << "parameterization of choleski decomnp of correlation" << endl;
          for (i=1;i<=ch.indexmax();i++)
          {
            dvector tmp=ch(i)/norm(ch(i));
            ofs3 << tmp(1,i)/tmp(i) << endl;
          }
        }
      }
    }
  }
  if (spminflag==0)
  {
    if (on1<0)
    {
      for (int i = 1;i <= nvar; i++)
      {
        if (hess(i,i) <= 0.0)
        {
          hess_errorreport();
          ad_exit(1);
        }
      }
    }
    {
      adstring tmpstring="admodel.cov";
      if (ad_comm::wd_flag)
        tmpstring = ad_comm::adprogram_name + ".cov";
      uostream ofs((char*)tmpstring);
      ofs << nvar << hess;
      ofs << gradient_structure::Hybrid_bounded_flag;
      ofs << sscale;
    }
  }
}
Beispiel #17
0
 void function_minimizer::pvm_master_mcmc_computations(void)
 {
   int on,nopt = 0;
   if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcmc",nopt))>-1)
   {
     /*
     if (adjm_ptr)
     {
       ad_open_mcmc_options_window();
       ad_open_mcmchist_window();
       //test_mcmc_options_window();
     }
     */
     int nmcmc=0;
     int iseed0=0;
     double dscale=1.0;
     if (nopt)
     {
       nmcmc=(int)atof(ad_comm::argv[on+1]);
       if (nmcmc <=0)
       {
         cerr << " Invalid option following command line option -mcmc -- "
           << endl << " ignored" << endl;
       }
     }
     if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcmult",nopt))>-1)
     {
       if (nopt)
       {
         char * end;
         dscale=strtod(ad_comm::argv[on+1],&end);
         if (!dscale)
         {
           cerr << "Invalid argument to option -mcmult" << endl;
           dscale=1.0;
         }
       }
     }
     if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcseed",nopt))>-1)
     {
       if (nopt)
       {
         iseed0=atoi(ad_comm::argv[on+1]);
         if (iseed0 <=0)
         {
           cerr << " Invalid option following command line option -mcseed -- "
             << endl << " ignored" << endl;
           iseed0=0;
         }
       }
     }
     if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-mcr",nopt))>-1)
     {
       //mcmc_routine(nmcmc,iseed0,dscale,1);
       pvm_master_mcmc_routine(nmcmc,iseed0,dscale,1);
     }
     else
     {
       //mcmc_routine(nmcmc,iseed0,dscale,0);
       pvm_master_mcmc_routine(nmcmc,iseed0,dscale,0);
     }
   }
 }
Beispiel #18
0
/**
 * Description not yet available.
 * \param
 */
void laplace_approximation_calculator::
  do_newton_raphson_banded(function_minimizer * pfmin,double f_from_1,
  int& no_converge_flag)
{
  //quadratic_prior * tmpptr=quadratic_prior::ptr[0];
  //cout << tmpptr << endl;


  laplace_approximation_calculator::where_are_we_flag=2;
  double maxg=fabs(evaluate_function(uhat,pfmin));


  laplace_approximation_calculator::where_are_we_flag=0;
  dvector uhat_old(1,usize);
  for(int ii=1;ii<=num_nr_iters;ii++)
  {
    // test newton raphson
    switch(hesstype)
    {
    case 3:
      bHess->initialize();
      break;
    case 4:
      Hess.initialize();
      break;
    default:
      cerr << "Illegal value for hesstype here" << endl;
      ad_exit(1);
    }

    grad.initialize();
    //int check=initial_params::stddev_scale(scale,uhat);
    //check=initial_params::stddev_curvscale(curv,uhat);
    //max_separable_g=0.0;
    sparse_count = 0;

    step=get_newton_raphson_info_banded(pfmin);
    //if (bHess)
     // cout << "norm(*bHess) = " << norm(*bHess) << endl;
    //cout << "norm(Hess) = " << norm(Hess) << endl;
    //cout << grad << endl;
    //check_pool_depths();
    if (!initial_params::mc_phase)
      cout << "Newton raphson " << ii << "  ";
    if (quadratic_prior::get_num_quadratic_prior()>0)
    {
      quadratic_prior::get_cHessian_contribution(Hess,xsize);
      quadratic_prior::get_cgradient_contribution(grad,xsize);
    }

    int ierr=0;
    if (hesstype==3)
    {
      if (use_dd_nr==0)
      {
        banded_lower_triangular_dmatrix bltd=choleski_decomp(*bHess,ierr);
        if (ierr && no_converge_flag ==0)
        {
          no_converge_flag=1;
          //break;
        }
        if (ierr)
        {
          double oldval;
          evaluate_function(oldval,uhat,pfmin);
          uhat=banded_calculations_trust_region_approach(uhat,pfmin);
        }
        else
        {
          if (dd_nr_flag==0)
          {
            dvector v=solve(bltd,grad);
            step=-solve_trans(bltd,v);
            //uhat_old=uhat;
            uhat+=step;
          }
          else
          {
#if defined(USE_DD_STUFF)
            int n=grad.indexmax();
            maxg=fabs(evaluate_function(uhat,pfmin));
            uhat=dd_newton_raphson2(grad,*bHess,uhat);
#else
            cerr << "high precision Newton Raphson not implemented" << endl;
            ad_exit(1);
#endif
          }
          maxg=fabs(evaluate_function(uhat,pfmin));
          if (f_from_1< pfmin->lapprox->fmc1.fbest)
          {
            uhat=banded_calculations_trust_region_approach(uhat,pfmin);
            maxg=fabs(evaluate_function(uhat,pfmin));
          }
        }
      }
      else
      {
        cout << "error not used" << endl;
        ad_exit(1);
       /*
        banded_symmetric_ddmatrix bHessdd=banded_symmetric_ddmatrix(*bHess);
        ddvector gradd=ddvector(grad);
        //banded_lower_triangular_ddmatrix bltdd=choleski_decomp(bHessdd,ierr);
        if (ierr && no_converge_flag ==0)
        {
          no_converge_flag=1;
          break;
        }
        if (ierr)
        {
          double oldval;
          evaluate_function(oldval,uhat,pfmin);
          uhat=banded_calculations_trust_region_approach(uhat,pfmin);
          maxg=fabs(evaluate_function(uhat,pfmin));
        }
        else
        {
          ddvector v=solve(bHessdd,gradd);
          step=-make_dvector(v);
          //uhat_old=uhat;
          uhat=make_dvector(ddvector(uhat)+step);
          maxg=fabs(evaluate_function(uhat,pfmin));
          if (f_from_1< pfmin->lapprox->fmc1.fbest)
          {
            uhat=banded_calculations_trust_region_approach(uhat,pfmin);
            maxg=fabs(evaluate_function(uhat,pfmin));
          }
        }
        */
      }

      if (maxg < 1.e-13)
      {
        break;
      }
    }
    else if (hesstype==4)
    {
      dvector step;

#     if defined(USE_ATLAS)
        if (!ad_comm::no_atlas_flag)
        {
          step=-atlas_solve_spd(Hess,grad,ierr);
        }
        else
        {
          dmatrix A=choleski_decomp_positive(Hess,ierr);
          if (!ierr)
          {
            step=-solve(Hess,grad);
            //step=-solve(A*trans(A),grad);
          }
        }
        if (!ierr) break;
#     else
        if (sparse_hessian_flag)
        {
          //step=-solve(*sparse_triplet,Hess,grad,*sparse_symbolic);
          dvector temp=solve(*sparse_triplet2,grad,*sparse_symbolic2,ierr);
          if (ierr)
          {
            step=-temp;
          }
          else
          {
            cerr << "matrix not pos definite in sparse choleski"  << endl;
            pfmin->bad_step_flag=1;

            int on;
            int nopt;
            if ((on=option_match(ad_comm::argc,ad_comm::argv,"-ieigvec",nopt))
              >-1)
            {
              dmatrix M=make_dmatrix(*sparse_triplet2);

              ofstream ofs3("inner-eigvectors");
              ofs3 << "eigenvalues and eigenvectors " << endl;
              dvector v=eigenvalues(M);
              dmatrix ev=trans(eigenvectors(M));
              ofs3 << "eigenvectors" << endl;
              int i;
              for (i=1;i<=ev.indexmax();i++)
               {
                  ofs3 << setw(4) << i  << " "
                   << setshowpoint() << setw(14) << setprecision(10) << v(i)
                   << " "
                   << setshowpoint() << setw(14) << setprecision(10)
                   << ev(i) << endl;
               }
            }
          }
          //cout << norm2(step-tmpstep) << endl;
          //dvector step1=-solve(Hess,grad);
          //cout << norm2(step-step1) << endl;
        }
        else
        {
          step=-solve(Hess,grad);
        }
#     endif
      if (pmin->bad_step_flag)
        break;
      uhat_old=uhat;
      uhat+=step;

      double maxg_old=maxg;
      maxg=fabs(evaluate_function(uhat,pfmin));
      if (maxg>maxg_old)
      {
        uhat=uhat_old;
        evaluate_function(uhat,pfmin);
        break;
      }
      if (maxg < 1.e-13)
      {
        break;
      }
    }

    if (sparse_hessian_flag==0)
    {
      for (int i=1;i<=usize;i++)
      {
        y(i+xsize)=uhat(i);
      }
    }
    else
    {
      for (int i=1;i<=usize;i++)
      {
        value(y(i+xsize))=uhat(i);
      }
    }
  }
}
Beispiel #19
0
  void function_minimizer::computations1(int argc,char * argv[])
  {
    tracing_message(traceflag,"B1");

    int on=-1;
    int nopt=-1;
#if defined(USE_ADPVM)
    if (ad_comm::pvm_manager)
    {
      switch (ad_comm::pvm_manager->mode)
      {
      case 1: //master
        pvm_params::send_all_to_slaves();
        break;
      case 2: //slave
        pvm_params::get_all_from_master();
        break;
      default:
        cerr << "Illegal value for ad_comm::pvm_manager->mode"
         " value was " << ad_comm::pvm_manager->mode << endl;
        ad_exit(1);
      }
    }
#endif  // #if defined(USE_ADPVM)

    set_runtime();

    if ( (on=option_match(argc,argv,"-hbf",nopt))>-1)
    {
      gradient_structure::Hybrid_bounded_flag=1;
    }

    // Sets the maximum number of function evaluation as determined from the
    // command line
    if ( (on=option_match(argc,argv,"-maxfn",nopt))>-1)
    {
      if (nopt ==1)
      {
        set_runtime_maxfn(argv[on+1]);
      }
      else
      {
        cerr << "Wrong number of options to -mafxn -- must be 1"
          " you have " << nopt << endl;
      }
    }

    if ( (on=option_match(argc,argv,"-ttr",nopt))>-1)
    {
      test_trust_flag=1;
    }

    if ( (on=option_match(argc,argv,"-crit",nopt))>-1)
    {
      if (nopt ==1)
      {
        set_runtime_crit(argv[on+1]);
      }
      else
      {
        cerr << "Wrong number of options to -crit -- must be 1"
          " you have " << nopt << endl;
      }
    }

    stddev_params::get_stddev_number_offset();

    tracing_message(traceflag,"C1");

    repeatminflag=0;
    do
    {
     /*
      if (spminflag)
      {
        repeatminflag=1;
        spminflag=0;
      }
      else
      {
        repeatminflag=0;
      }
      */

      if (option_match(argc,argv,"-noest") == -1)
      {
        if (!function_minimizer::have_constraints)
        {
          minimize();
        }
        else
        {
          constraints_minimize();
        }
      }
      else
      {
        initial_params::current_phase=initial_params::max_number_phases;
      }
      tracing_message(traceflag,"D1");

      //double ratio=100.*gradient_structure::max_last_offset/12000.0;
      tracing_message(traceflag,"E1");
      if (option_match(argc,argv,"-est") == -1)
      {
        if (!quit_flag)
        {
          // save the sparse Hessian for the random effects
          if (lapprox && lapprox->sparse_hessian_flag)
          {
            if (lapprox->sparse_triplet2)
            {
              dcompressed_triplet& dct=*(lapprox->sparse_triplet2);
              adstring tmpstring = ad_comm::adprogram_name + ".shess";
              uostream uos((char*)(tmpstring));
              uos << dct.get_n()  << dct.indexmin() << dct.indexmax()
                  << dct.get_coords() << dct.get_x();
            }
          }

          on=option_match(argc,argv,"-nohess");
          int on1=option_match(argc,argv,"-noest");
          if (on==-1 && on1==-1)
          {
            if (option_match(argc,argv,"-sdonly")==-1)
            {
              hess_routine();
            }
            // set this flag so that variables only needed for their std devs
            // will be calculated
            initial_params::sd_phase=1;
#if defined(USE_ADPVM)
            if (ad_comm::pvm_manager)
            {
              if (ad_comm::pvm_manager->mode==1)  //master
              {
                depvars_routine();
                hess_inv();
                if (spminflag==0)
                {
                  sd_routine();
                }
              }
            }
            else
#endif
            {
              depvars_routine();
              hess_inv();
              if (spminflag==0)
              {
                sd_routine();
              }
            }
          }
          else
          {
            initial_params::sd_phase=1;
          }
          if (spminflag==0)
          {
            if ( (on=option_match(argc,argv,"-lprof"))>-1)
            {
              if (likeprof_params::num_likeprof_params)
              {
    #if defined(USE_ADPVM)
                if (ad_comm::pvm_manager)
                {
                  switch (ad_comm::pvm_manager->mode)
                  {
                  case 1: // master
                    likeprof_routine(ffbest);
                    break;
                  case 2: // slave
                    pvm_slave_likeprof_routine();
                    break;
                  default:
                    cerr << "error illega value for pvm_manager->mode" << endl;
                    exit(1);
                  }
                }
                else
    #endif
                {
                  const double f = value(*objective_function_value::pobjfun);
                  likeprof_routine(f);
                }
              }
            }
            nopt=0;
            int on2=-1;
            int nopt2=-1;

            // stuff for mcmc
            //cout << "checking for mcmc" << endl;
            if ( (on=option_match(argc,argv,"-mcmc",nopt))>-1 ||
                 (on=option_match(argc,argv,"-mcmc2",nopt))>-1)
            {
              if ( (on2=option_match(argc,argv,"-mcmc2",nopt2))>-1)
                mcmc2_flag=1;
              else
                mcmc2_flag=0;

 #if defined(USE_ADPVM)
              if (ad_comm::pvm_manager)
              {
                switch (ad_comm::pvm_manager->mode)
                {
                case 1: // master
                  pvm_master_mcmc_computations();
                break;
                case 2: // slave
                  pvm_slave_mcmc_routine();
                  break;
                default:
                  cerr << "error illega value for pvm_manager->mode" << endl;
                  exit(1);
                }
              }
              else
 #endif
              {
                mcmc_computations();
              }
            }
            if ( (on=option_match(argc,argv,"-sob",nopt))>-1)
            {
              int nsob=0;
              //int iseed0=0;
              //double dscale=1.0;
              if (nopt)
              {
                nsob=atoi(argv[on+1]);
                if (nsob <=0)
                {
                  cerr << " Invalid option following command line option -sob"
                          " -- "
                    << endl << " ignored" << endl;
                }
              }
              if ( (on=option_match(argc,argv,"-mcr",nopt))>-1)
              {
                //sob_routine(nsob,dscale,1);
                //sobol_importance_routine(nsob,iseed0,dscale,1);
              }
              else
              {
                //sobol_importance_routine(nsob,iseed0,dscale,0);
              }
            }
            initial_params::sd_phase=0;
          }
        }
      }
    }
    while(spminflag || repeatminflag);
  }
Beispiel #20
0
/**
 * Description not yet available.
 * \param
 */
void function_minimizer::constraints_minimize(void)
{
  //initial_params::read(); // read in the values for the initial parameters
  if (initial_params::restart_phase)
  {
    initial_params::current_phase = initial_params::restart_phase;
    initial_params::restart_phase=0;
  }
  int nopt=0;
  int on=0;
  int allphases=initial_params::max_number_phases;

  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-maxph",nopt))>-1)
  {
    if (!nopt)
    {
      cerr << "Usage -maxph uerton needs integer  -- ignored" << endl;
    }
    else
    {
      int jj=atoi(ad_comm::argv[on+1]);
      if (jj<=0)
      {
        cerr << "Usage -maxph uerton needs positive integer  -- ignored\n.";
      }
      else
      {
        if (jj>allphases)
        {
          allphases=jj;
        }
      }
    }
  }

  // set the maximum number of function evaluations by command line
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-maxfn",nopt))>-1)
  {
    if (!nopt)
    {
      cerr << "Usage -maxph uerton needs integer  -- ignored" << endl;
    }
    else
    {
      int _maxfn=atoi(ad_comm::argv[on+1]);
      if (_maxfn<=0)
      {
        cerr << "Usage -maxfn uerton needs positive integer  -- ignored\n";
      }
      else
      {
        maxfn=_maxfn;
      }
    }
  }

  double _crit=0;
  // set the maximum number of function evaluations by command line
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-crit",nopt))>-1)
  {
    if (!nopt)
    {
      cerr << "Usage -crit uerton needs number  -- ignored" << endl;
    }
    else
    {
      istrstream ist(ad_comm::argv[on+1]);
      ist >> _crit;

      if (_crit<=0)
      {
        cerr << "Usage -crit uerton needs positive number  -- ignored" << endl;
        _crit=0.0;
      }
    }
  }

  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-phase"))>-1)
  {
    int jj=atoi(ad_comm::argv[on+1]);
    if (jj <=0)
    {
      cerr << " Inwwuq4 uerton followinumberv command line uerton -phase -- "
        << endl << " phase set equal to 1" << endl;
    }
    if (jj>allphases)
    {
      jj=allphases;
    }
    if (jj<=0)
    {
      jj=1;
    }
    initial_params::current_phase = jj;
    cout << "Set current phase to " << jj << endl;
  }

  while (initial_params::current_phase <= allphases)
  {
    between_phases_calculations();

    int nvar=initial_params::nvarcalc(); // get the number of active
             // parameters
    if (!nvar)
    {
      cerr << "Error -- no active parameters. There must be at least 1"
           << endl;
      exit(1);
    }

    pfm=this;
    int numberw=0;
    if (function_minimizer::ph)
      numberw=function_minimizer::ph->indexmax()
        -function_minimizer::ph->indexmin()+1;

    int numberv=0;
    if (function_minimizer::pg)
      numberv=function_minimizer::pg->indexmax()
        -function_minimizer::pg->indexmin()+1;
    dvector x(1,nvar);
    initial_params::xinit(x);
    make_all_classes(nvar,numberw,numberv);

    constrained_minimization2(nvar,numberw,numberv,x);

    gradient_structure::set_NO_DERIVATIVES();
    initial_params::reset(dvar_vector(x));
    *objective_function_value::pobjfun=0.0;
    userfunction();
    initial_params::save();
    report();
    constraint_report();
    // in case the user chanumberves some initial_params in the report section
    // call reset again
    initial_params::reset(dvar_vector(x));

    // in case the user chanumberves some initial_params in the report section
    // call reset again
    initial_params::reset(dvar_vector(x));
    report_function_minimizer_stats();
    if (quit_flag=='Q') break;
    if (!quit_flag || quit_flag == 'N')
    {
      initial_params::current_phase++;
    }
  }
  if (initial_params::current_phase >
    initial_params::max_number_phases)
  {
    initial_params::current_phase =
      initial_params::max_number_phases;
  }
}
Beispiel #21
0
int adpvm_manager::start_slave_processes(const ad_comm& _mp)
{
  ad_comm& mp=(ad_comm&) _mp;
  cout << (mp.pvm_manager)->nhost;
  int i,j,check;
  mp.get_slave_assignments();
  if (allocated(id)) id.deallocate();
  id.allocate(1,nhost,1,num_per_host);
  adstring slavedir;
  int on=0;
  int noptslave=0;

  int td32flag=option_match(ad_comm::argc,ad_comm::argv,"-td32");

  if (td32flag>0)
  {
    strcpy(*slave_argv,slave_names[1]);
  }

  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-dbg"))>-1)
     strcpy(*slave_argv,"-dbg");
  if ( (on=option_match(ad_comm::argc,ad_comm::argv,"-slavedir",noptslave))>-1)
  {
    if (noptslave !=1)
    {
      cerr << "Wrong number of options to -slavedir -- must be 1"
        " you have " << noptslave << endl;
      ad_exit(1);
    }
  }
  else
  {
    noptslave=0;
  }
  int on1,nopt1;
  if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-mcmc",nopt1))>-1)
  {
    if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-mcmc2",nopt1))>-1)
    {
      cerr << "Error -mcmc2 option not implemented for parallel processing"
           << endl;
      ad_exit(1);
    }
    if (nopt1 !=1)
    {
      cerr << "Wrong number of options to -mcmc -- must be 1"
        " you have " << noptslave << endl;
      ad_exit(1);
    }
    strcpy((*slave_argv),"-mcmc");
    strcpy((*slave_argv),ad_comm::argv[on1+1]);
  }

  if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-nohess"))>-1)
  {
    strcpy((*slave_argv),"-nohess");
  }

  if ( (on1=option_match(ad_comm::argc,ad_comm::argv,"-lprof"))>-1)
  {
    strcpy((*slave_argv),"-lprof");
  }
  int gdbflag=option_match(ad_comm::argc,ad_comm::argv,"-gdb");
  int slave_con=option_match(ad_comm::argc,ad_comm::argv,"-scon");

  int ii=0;
  for (i=0; i<nhost; i++)	/* spawn processes on */
  {				/* all physical machines */
    for (j=slave_assignments(i+1).indexmin();j<=slave_assignments(i+1).indexmax();
      j++)
    {
      strcpy((*slave_argv),"-slavename");
      strcpy((*slave_argv),ad_comm::adprogram_name);
      strcpy((*slave_argv),"-slave");
      strcpy((*slave_argv),(char*)(str(slave_assignments(i+1,j))));
      if (noptslave ==1)
      {
        strcpy((*slave_argv),"-slavedir");
        strcpy((*slave_argv),ad_comm::argv[on+1]);
      }

      if (td32flag<0)
      {
        //check=pvm_spawn(slave_names[1],*slave_argv ,
        char ** args;
        args=new pchar[10];
        args[0]=new char[20];
        args[1]=new char[20];
        strcpy(args[0],"-slave");
        strcpy(args[1],(char*)str(ii+1));
        args[2]=0;
        check=pvm_spawn(ad_comm::adprogram_name,args ,
          PvmTaskHost /* | PvmTaskDebug */ ,
          hostp[i].hi_name, 1, &(id(i+1,j)));
        ii++;
      }
      else
      {
        char ** args;
        args=new pchar[10];
        if (slave_con>0)
        {
          args[0]=new char[50];
          args[1]=new char[20];
          args[2]=new char[20];
          strncpy(args[0],ad_comm::adprogram_name,49);
          strncpy(args[1],"-slave",19);
          strncpy(args[2],(char*)str(ii+1),19);
          args[3]=0;
        }
        else
        {
          args[0]=new char[20];
          args[1]=new char[50];
          args[2]=new char[20];
          args[3]=new char[50];

          strcpy(args[0],"td32");
          strncpy(args[1],ad_comm::adprogram_name,49);
          strcpy(args[2],"-slave");
          strcpy(args[3],(char*)str(ii+1));
          args[4]=0;
        }
        //check=pvm_spawn("nrunslave",*slave_argv ,
        check=pvm_spawn("nrunslave",args ,
          PvmTaskHost /* | PvmTaskDebug */ ,
          hostp[i].hi_name, 1, &(id(i+1,j)));
        ii++;
      }

      (*slave_argv)--;
      (*slave_argv)--;
      (*slave_argv)--;
      (*slave_argv)--;
      if (noptslave ==1)
      {
        (*slave_argv)--;
        (*slave_argv)--;
      }
      if (!check)
        printf("Couldn't start process on %s\n",hostp[i].hi_name);
      else
        printf("Started process on %s\n",hostp[i].hi_name);
    }
  }
  return check;
}