Skip to content

Commit

Permalink
Granular construction of a few more Options classes
Browse files Browse the repository at this point in the history
Refactor in support of libqueso#569 to enable various layered
phases of constructing OptimizerOptions, MLSamplingLeveloptions, and
InfiniteDimensionalMCMCSamplerOptions.  This mirrors work done in
other *OptionsValues.
  • Loading branch information
briadam committed Oct 16, 2017
1 parent 2d089e6 commit a746a80
Show file tree
Hide file tree
Showing 6 changed files with 464 additions and 469 deletions.
13 changes: 11 additions & 2 deletions src/core/inc/InfiniteDimensionalMCMCSamplerOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ class InfiniteDimensionalMCMCSamplerOptions
//! Given prefix, read the input file for parameters named prefix_*
InfiniteDimensionalMCMCSamplerOptions(const BaseEnvironment& env, const char* prefix);

//! Set default values for parameter options
void set_defaults();

//! Set parameter option names to begin with prefix
void set_prefix(const std::string& prefix);

//! Given prefix, read the input file for parameters named "prefix"+*
void parse(const BaseEnvironment& env, const std::string& prefix);

//! Destructor
virtual ~InfiniteDimensionalMCMCSamplerOptions();

Expand Down Expand Up @@ -86,10 +95,10 @@ class InfiniteDimensionalMCMCSamplerOptions

private:
#ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
BoostInputOptionsParser * m_parser;
ScopedPtr<BoostInputOptionsParser>::Type m_parser;
#endif // QUESO_DISABLE_BOOST_PROGRAM_OPTIONS

const BaseEnvironment& m_env;
const BaseEnvironment* m_env;

std::string m_option_help;
std::string m_option_dataOutputDirName;
Expand Down
13 changes: 12 additions & 1 deletion src/core/inc/OptimizerOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@ class OptimizerOptions
//! Copy constructor
OptimizerOptions(const OptimizerOptions & rhs);

//! Set default values for parameter options
void set_defaults();

//! Set parameter option names to begin with prefix
void set_prefix(const std::string& prefix);

//! Given prefix, read the input file for parameters named "prefix"+*
void parse(const BaseEnvironment& env, const std::string& prefix);

//! Destructor
virtual ~OptimizerOptions();
//@}
Expand Down Expand Up @@ -135,7 +144,9 @@ class OptimizerOptions
private:
const BaseEnvironment * m_env;

BoostInputOptionsParser * m_parser;
#ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
ScopedPtr<BoostInputOptionsParser>::Type m_parser;
#endif

// The input options as strings so we can parse the input file later
std::string m_option_help;
Expand Down
105 changes: 63 additions & 42 deletions src/core/src/InfiniteDimensionalMCMCSamplerOptions.C
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,11 @@ namespace QUESO {
InfiniteDimensionalMCMCSamplerOptions::InfiniteDimensionalMCMCSamplerOptions(
const BaseEnvironment& env,
const char * prefix)
: m_prefix((std::string)(prefix) + "infmcmc_"),
m_dataOutputDirName(UQ_INF_DATA_OUTPUT_DIR_NAME_ODV),
m_dataOutputFileName(UQ_INF_DATA_OUTPUT_FILE_NAME_ODV),
m_num_iters(UQ_INF_NUM_ITERS_ODV),
m_save_freq(UQ_INF_SAVE_FREQ_ODV),
m_rwmh_step(UQ_INF_RWMH_STEP_ODV),
#ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
m_parser(new BoostInputOptionsParser(env.optionsInputFileName())),
#endif // QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
m_env(env),
m_option_help(m_prefix + "help"),
m_option_dataOutputDirName(m_prefix + "dataOutputDirName"),
m_option_dataOutputFileName(m_prefix + "dataOutputFileName"),
m_option_num_iters(m_prefix + "num_iters"),
m_option_save_freq(m_prefix + "save_freq"),
m_option_rwmh_step(m_prefix + "rwmh_step")
{
queso_require_not_equal_to_msg(m_env.optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the abscense of an options input file"));

#ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
m_parser->registerOption(m_option_help, "produce help message for infinite dimensional sampler");
m_parser->registerOption<std::string>(m_option_dataOutputDirName, UQ_INF_DATA_OUTPUT_DIR_NAME_ODV, "name of data output dir");
m_parser->registerOption<std::string>(m_option_dataOutputFileName, UQ_INF_DATA_OUTPUT_FILE_NAME_ODV, "name of data output file (HDF5)");
m_parser->registerOption<unsigned int>(m_option_num_iters, UQ_INF_NUM_ITERS_ODV, "number of mcmc iterations to do");
m_parser->registerOption<unsigned int>(m_option_save_freq, UQ_INF_SAVE_FREQ_ODV, "the frequency at which to save the chain state");
m_parser->registerOption<double >(m_option_rwmh_step, UQ_INF_RWMH_STEP_ODV, "the step-size in the random-walk Metropolis proposal");;

m_parser->scanInputFile();

m_parser->getOption<std::string>(m_option_dataOutputDirName, m_dataOutputDirName);
m_parser->getOption<std::string>(m_option_dataOutputFileName, m_dataOutputFileName);
m_parser->getOption<unsigned int>(m_option_num_iters, m_num_iters);
m_parser->getOption<unsigned int>(m_option_save_freq, m_save_freq);
m_parser->getOption<double >(m_option_rwmh_step, m_rwmh_step);
#else
m_dataOutputDirName = m_env.input()(m_option_dataOutputDirName, UQ_INF_DATA_OUTPUT_DIR_NAME_ODV);
m_dataOutputFileName = m_env.input()(m_option_dataOutputFileName, UQ_INF_DATA_OUTPUT_FILE_NAME_ODV);
m_num_iters = m_env.input()(m_option_num_iters, UQ_INF_NUM_ITERS_ODV);
m_save_freq = m_env.input()(m_option_save_freq, UQ_INF_SAVE_FREQ_ODV);
m_rwmh_step = m_env.input()(m_option_rwmh_step, UQ_INF_RWMH_STEP_ODV);;
#endif // QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
queso_require_not_equal_to_msg(m_env->optionsInputFileName(), std::string(""), std::string("this constructor is incompatible with the abscense of an options input file"));

checkOptions();
this->set_defaults();
this->parse(env, prefix);
}

void
Expand Down Expand Up @@ -116,7 +78,66 @@ std::ostream & operator<<(std::ostream & os,
const BaseEnvironment&
InfiniteDimensionalMCMCSamplerOptions::env() const
{
return this->m_env;
return *(this->m_env);
}


void InfiniteDimensionalMCMCSamplerOptions::set_defaults()
{
m_dataOutputDirName = UQ_INF_DATA_OUTPUT_DIR_NAME_ODV;
m_dataOutputFileName = UQ_INF_DATA_OUTPUT_FILE_NAME_ODV;
m_num_iters = UQ_INF_NUM_ITERS_ODV;
m_save_freq = UQ_INF_SAVE_FREQ_ODV;
m_rwmh_step = UQ_INF_RWMH_STEP_ODV;
}


void InfiniteDimensionalMCMCSamplerOptions::set_prefix(const std::string& prefix)
{
m_prefix = prefix+ "infmcmc_";

m_option_help = m_prefix + "help";
m_option_dataOutputDirName = m_prefix + "dataOutputDirName";
m_option_dataOutputFileName = m_prefix + "dataOutputFileName";
m_option_num_iters = m_prefix + "num_iters";
m_option_save_freq = m_prefix + "save_freq";
m_option_rwmh_step = m_prefix + "rwmh_step";
}


void InfiniteDimensionalMCMCSamplerOptions::parse(const BaseEnvironment& env, const std::string& prefix)
{
m_env = &env;

this->set_prefix(prefix);

#ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS

m_parser.reset(new BoostInputOptionsParser(env.optionsInputFileName()));

m_parser->registerOption(m_option_help, "produce help message for infinite dimensional sampler");
m_parser->registerOption<std::string>(m_option_dataOutputDirName, m_dataOutputDirName, "name of data output dir");
m_parser->registerOption<std::string>(m_option_dataOutputFileName, m_dataOutputFileName, "name of data output file (HDF5)");
m_parser->registerOption<unsigned int>(m_option_num_iters, m_num_iters, "number of mcmc iterations to do");
m_parser->registerOption<unsigned int>(m_option_save_freq, m_save_freq, "the frequency at which to save the chain state");
m_parser->registerOption<double >(m_option_rwmh_step, m_rwmh_step, "the step-size in the random-walk Metropolis proposal");;

m_parser->scanInputFile();

m_parser->getOption<std::string>(m_option_dataOutputDirName, m_dataOutputDirName);
m_parser->getOption<std::string>(m_option_dataOutputFileName, m_dataOutputFileName);
m_parser->getOption<unsigned int>(m_option_num_iters, m_num_iters);
m_parser->getOption<unsigned int>(m_option_save_freq, m_save_freq);
m_parser->getOption<double >(m_option_rwmh_step, m_rwmh_step);
#else
m_dataOutputDirName = m_env->input()(m_option_dataOutputDirName, m_dataOutputDirName);
m_dataOutputFileName = m_env->input()(m_option_dataOutputFileName, m_dataOutputFileName);
m_num_iters = m_env->input()(m_option_num_iters, m_num_iters);
m_save_freq = m_env->input()(m_option_save_freq, m_save_freq);
m_rwmh_step = m_env->input()(m_option_rwmh_step, m_rwmh_step);
#endif // QUESO_DISABLE_BOOST_PROGRAM_OPTIONS

checkOptions();
}

} // End namespace QUESO
185 changes: 96 additions & 89 deletions src/core/src/OptimizerOptions.C
Original file line number Diff line number Diff line change
Expand Up @@ -38,98 +38,18 @@
namespace QUESO {

OptimizerOptions::OptimizerOptions()
: m_prefix("ip_"),
m_help(UQ_OPT_HELP),
m_maxIterations(UQ_OPT_MAX_ITERATIONS),
m_tolerance(UQ_OPT_TOLERANCE),
m_finiteDifferenceStepSize(UQ_OPT_FINITE_DIFFERENCE_STEP_SIZE),
m_solverType(UQ_OPT_SOLVER_TYPE),
m_fstepSize(UQ_OPT_FSTEP_SIZE),
m_fdfstepSize(UQ_OPT_FDFSTEP_SIZE),
m_lineTolerance(UQ_OPT_LINE_TOLERANCE),
m_env(NULL),
#ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
m_parser(NULL),
#endif // QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
m_option_help(m_prefix + "help"),
m_option_maxIterations(m_prefix + "maxIterations"),
m_option_tolerance(m_prefix + "tolerance"),
m_option_finiteDifferenceStepSize(m_prefix + "finiteDifferenceStepSize"),
m_option_solverType(m_prefix + "solverType"),
m_option_fstepSize(m_prefix + "fstepSize"),
m_option_fdfstepSize(m_prefix + "fdfStepSize"),
m_option_lineTolerance(m_prefix + "lineTolerance")
:
m_env(NULL)
{
this->set_defaults();
this->set_prefix("");
}

OptimizerOptions::OptimizerOptions(const BaseEnvironment * env, const char *
prefix)
: m_prefix((std::string)(prefix) + "optimizer_"),
m_help(UQ_OPT_HELP),
m_maxIterations(UQ_OPT_MAX_ITERATIONS),
m_tolerance(UQ_OPT_TOLERANCE),
m_finiteDifferenceStepSize(UQ_OPT_FINITE_DIFFERENCE_STEP_SIZE),
m_solverType(UQ_OPT_SOLVER_TYPE),
m_fstepSize(UQ_OPT_FSTEP_SIZE),
m_fdfstepSize(UQ_OPT_FDFSTEP_SIZE),
m_lineTolerance(UQ_OPT_LINE_TOLERANCE),
m_env(env),
#ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
m_parser(new BoostInputOptionsParser(env->optionsInputFileName())),
#endif // QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
m_option_help(m_prefix + "help"),
m_option_maxIterations(m_prefix + "maxIterations"),
m_option_tolerance(m_prefix + "tolerance"),
m_option_finiteDifferenceStepSize(m_prefix + "finiteDifferenceStepSize"),
m_option_solverType(m_prefix + "solverType"),
m_option_fstepSize(m_prefix + "fstepSize"),
m_option_fdfstepSize(m_prefix + "fdfStepSize"),
m_option_lineTolerance(m_prefix + "lineTolerance")
OptimizerOptions::
OptimizerOptions(const BaseEnvironment* env, const char* prefix)
{
#ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
m_parser->registerOption<std::string>(m_option_help, UQ_OPT_HELP,
"produce help message for statistical inverse problem");

m_parser->registerOption<unsigned int>(m_option_maxIterations,
UQ_OPT_MAX_ITERATIONS,
"max number of optimizer iterations to do");
m_parser->registerOption<double>(m_option_tolerance, UQ_OPT_TOLERANCE,
"optimize until gradient is less than tolerance");
m_parser->registerOption<double>(m_option_finiteDifferenceStepSize,
UQ_OPT_FINITE_DIFFERENCE_STEP_SIZE,
"if no deriv is given, do finite difference with this step size");
m_parser->registerOption<std::string>(m_option_solverType, UQ_OPT_SOLVER_TYPE,
"which optimisation algorithm to use");
m_parser->registerOption<double>(m_option_fstepSize, UQ_OPT_FSTEP_SIZE,
"sets the step size used in gradient-free solvers");
m_parser->registerOption<double>(m_option_fdfstepSize, UQ_OPT_FDFSTEP_SIZE,
"sets the step size used in gradient-based solvers");
m_parser->registerOption<double>(m_option_lineTolerance, UQ_OPT_LINE_TOLERANCE,
"sets the line minimisation tolerance");

m_parser->scanInputFile();

m_parser->getOption<std::string>(m_option_help, m_help);
m_parser->getOption<unsigned int>(m_option_maxIterations, m_maxIterations);
m_parser->getOption<double>(m_option_tolerance, m_tolerance);
m_parser->getOption<double>(m_option_finiteDifferenceStepSize,
m_finiteDifferenceStepSize);
m_parser->getOption<std::string>(m_option_solverType, m_solverType);
m_parser->getOption<double>(m_option_fstepSize, m_fstepSize);
m_parser->getOption<double>(m_option_fdfstepSize, m_fdfstepSize);
m_parser->getOption<double>(m_option_lineTolerance, m_lineTolerance);
#else
m_help = m_env->input()(m_option_help, UQ_OPT_HELP);
m_maxIterations = m_env->input()(m_option_maxIterations, UQ_OPT_MAX_ITERATIONS);
m_tolerance = m_env->input()(m_option_tolerance, UQ_OPT_TOLERANCE);
m_finiteDifferenceStepSize = m_env->input()(m_option_finiteDifferenceStepSize, UQ_OPT_FINITE_DIFFERENCE_STEP_SIZE);
m_solverType = m_env->input()(m_option_solverType, UQ_OPT_SOLVER_TYPE);
m_fstepSize = m_env->input()(m_option_fstepSize, UQ_OPT_FSTEP_SIZE);
m_fdfstepSize = m_env->input()(m_option_fdfstepSize, UQ_OPT_FDFSTEP_SIZE);
m_lineTolerance = m_env->input()(m_option_lineTolerance, UQ_OPT_LINE_TOLERANCE);
#endif // QUESO_DISABLE_BOOST_PROGRAM_OPTIONS

checkOptions();
this->set_defaults();
this->parse(*env, prefix);
}

OptimizerOptions::OptimizerOptions(const OptimizerOptions & rhs)
Expand All @@ -143,7 +63,9 @@ OptimizerOptions::OptimizerOptions(const OptimizerOptions & rhs)
m_fstepSize(rhs.m_fstepSize),
m_fdfstepSize(rhs.m_fdfstepSize),
m_lineTolerance(rhs.m_lineTolerance),
m_parser(rhs.m_parser), // We'll never touch the input file in a copied object
// #ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS
// m_parser(rhs.m_parser), // We'll never touch the input file in a copied object
// #endif
m_option_help(rhs.m_option_help),
m_option_maxIterations(rhs.m_option_maxIterations),
m_option_tolerance(rhs.m_option_tolerance),
Expand Down Expand Up @@ -185,4 +107,89 @@ operator<<(std::ostream& os, const OptimizerOptions & obj)
return os;
}


void OptimizerOptions::set_defaults()
{
m_help = UQ_OPT_HELP;
m_maxIterations = UQ_OPT_MAX_ITERATIONS;
m_tolerance = UQ_OPT_TOLERANCE;
m_finiteDifferenceStepSize = UQ_OPT_FINITE_DIFFERENCE_STEP_SIZE;
m_solverType = UQ_OPT_SOLVER_TYPE;
m_fstepSize = UQ_OPT_FSTEP_SIZE;
m_fdfstepSize = UQ_OPT_FDFSTEP_SIZE;
m_lineTolerance = UQ_OPT_LINE_TOLERANCE;
}


void OptimizerOptions::set_prefix(const std::string& prefix)
{
// NOTE: previously default ctor was setting to "ip_"
m_prefix = prefix + "optimizer_";

m_option_help = m_prefix + "help";
m_option_maxIterations = m_prefix + "maxIterations";
m_option_tolerance = m_prefix + "tolerance";
m_option_finiteDifferenceStepSize = m_prefix + "finiteDifferenceStepSize";
m_option_solverType = m_prefix + "solverType";
m_option_fstepSize = m_prefix + "fstepSize";
m_option_fdfstepSize = m_prefix + "fdfStepSize";
m_option_lineTolerance = m_prefix + "lineTolerance";
}


void OptimizerOptions::
parse(const BaseEnvironment& env, const std::string& prefix)
{
m_env = &env;

this->set_prefix(prefix);

#ifndef QUESO_DISABLE_BOOST_PROGRAM_OPTIONS

m_parser.reset(new BoostInputOptionsParser(env.optionsInputFileName()));

m_parser->registerOption<std::string>(m_option_help, m_help,
"produce help message for statistical inverse problem");
m_parser->registerOption<unsigned int>(m_option_maxIterations,
m_maxIterations,
"max number of optimizer iterations to do");
m_parser->registerOption<double>(m_option_tolerance, m_tolerance,
"optimize until gradient is less than tolerance");
m_parser->registerOption<double>(m_option_finiteDifferenceStepSize,
m_finiteDifferenceStepSize,
"if no deriv is given, do finite difference with this step size");
m_parser->registerOption<std::string>(m_option_solverType, m_solverType,
"which optimisation algorithm to use");
m_parser->registerOption<double>(m_option_fstepSize, m_fstepSize,
"sets the step size used in gradient-free solvers");
m_parser->registerOption<double>(m_option_fdfstepSize, m_fdfstepSize,
"sets the step size used in gradient-based solvers");
m_parser->registerOption<double>(m_option_lineTolerance, m_lineTolerance,
"sets the line minimisation tolerance");

m_parser->scanInputFile();

m_parser->getOption<std::string>(m_option_help, m_help);
m_parser->getOption<unsigned int>(m_option_maxIterations, m_maxIterations);
m_parser->getOption<double>(m_option_tolerance, m_tolerance);
m_parser->getOption<double>(m_option_finiteDifferenceStepSize,
m_finiteDifferenceStepSize);
m_parser->getOption<std::string>(m_option_solverType, m_solverType);
m_parser->getOption<double>(m_option_fstepSize, m_fstepSize);
m_parser->getOption<double>(m_option_fdfstepSize, m_fdfstepSize);
m_parser->getOption<double>(m_option_lineTolerance, m_lineTolerance);
#else
m_help = m_env->input()(m_option_help, m_help);
m_maxIterations = m_env->input()(m_option_maxIterations, m_maxIterations);
m_tolerance = m_env->input()(m_option_tolerance, m_tolerance);
m_finiteDifferenceStepSize = m_env->input()(m_option_finiteDifferenceStepSize, m_finiteDifferenceStepSize);
m_solverType = m_env->input()(m_option_solverType, m_solverType);
m_fstepSize = m_env->input()(m_option_fstepSize, m_fstepSize);
m_fdfstepSize = m_env->input()(m_option_fdfstepSize, m_fdfstepSize);
m_lineTolerance = m_env->input()(m_option_lineTolerance, m_lineTolerance);
#endif // QUESO_DISABLE_BOOST_PROGRAM_OPTIONS

checkOptions();
}

} // End namespace QUESO
Loading

0 comments on commit a746a80

Please sign in to comment.