queso-0.55.0
StatisticalInverseProblemOptions.C
Go to the documentation of this file.
1 //-----------------------------------------------------------------------bl-
2 //--------------------------------------------------------------------------
3 //
4 // QUESO - a library to support the Quantification of Uncertainty
5 // for Estimation, Simulation and Optimization
6 //
7 // Copyright (C) 2008-2015 The PECOS Development Team
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the Version 2.1 GNU Lesser General
11 // Public License as published by the Free Software Foundation.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc. 51 Franklin Street, Fifth Floor,
21 // Boston, MA 02110-1301 USA
22 //
23 //-----------------------------------------------------------------------el-
24 
25 #include <boost/program_options.hpp>
26 
27 #include <queso/Defines.h>
28 #include <queso/StatisticalInverseProblemOptions.h>
29 #include <queso/Miscellaneous.h>
30 
31 // -------------------------------------------------
32 // SipOptionsValues--------------------------
33 // -------------------------------------------------
34 
35 namespace QUESO {
36 
37 // Default constructor -----------------------------
39  :
40  m_prefix("ip_"),
41  m_help(UQ_SIP_HELP),
42  m_computeSolution (UQ_SIP_COMPUTE_SOLUTION_ODV ),
43  m_dataOutputFileName (UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV),
44  m_seedWithMAPEstimator(UQ_SIP_SEEDWITHMAPESTIMATOR),
45  m_useOptimizerMonitor(UQ_SIP_USEOPTIMIZERMONITOR),
46 //m_dataOutputAllowedSet(),
47  m_parser(NULL),
48  m_option_help (m_prefix + "help" ),
49  m_option_computeSolution (m_prefix + "computeSolution" ),
50  m_option_dataOutputFileName (m_prefix + "dataOutputFileName" ),
51  m_option_dataOutputAllowedSet(m_prefix + "dataOutputAllowedSet"),
52 #ifdef UQ_SIP_READS_SOLVER_OPTION
53  m_option_solver (m_prefix + "solver" ),
54  m_solverString (UQ_SIP_SOLVER_ODV)
55 #endif
56  m_option_seedWithMAPEstimator(m_prefix + "seedWithMAPEstimator"),
57  m_option_useOptimizerMonitor(m_prefix + "useOptimizerMonitor")
58 {
59 }
60 
62  prefix)
63  :
64  m_prefix((std::string)(prefix) + "ip_"),
65  m_help(UQ_SIP_HELP),
66  m_computeSolution (UQ_SIP_COMPUTE_SOLUTION_ODV ),
67  m_dataOutputFileName (UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV),
68  m_seedWithMAPEstimator(UQ_SIP_SEEDWITHMAPESTIMATOR),
69  m_useOptimizerMonitor(UQ_SIP_USEOPTIMIZERMONITOR),
70 //m_dataOutputAllowedSet(),
71  m_parser(new BoostInputOptionsParser(env->optionsInputFileName())),
72  m_option_help (m_prefix + "help" ),
73  m_option_computeSolution (m_prefix + "computeSolution" ),
74  m_option_dataOutputFileName (m_prefix + "dataOutputFileName" ),
75  m_option_dataOutputAllowedSet(m_prefix + "dataOutputAllowedSet"),
76 #ifdef UQ_SIP_READS_SOLVER_OPTION
77  m_option_solver (m_prefix + "solver" ),
78  m_solverString (UQ_SIP_SOLVER_ODV)
79 #endif
80  m_option_seedWithMAPEstimator(m_prefix + "seedWithMAPEstimator"),
81  m_option_useOptimizerMonitor(m_prefix + "useOptimizerMonitor")
82 {
83  m_parser->registerOption<std::string>(m_option_help, UQ_SIP_HELP, "produce help message for statistical inverse problem");
84  m_parser->registerOption<bool >(m_option_computeSolution, UQ_SIP_COMPUTE_SOLUTION_ODV , "compute solution process" );
85  m_parser->registerOption<std::string>(m_option_dataOutputFileName, UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV , "name of data output file" );
86  m_parser->registerOption<std::string>(m_option_dataOutputAllowedSet, UQ_SIP_DATA_OUTPUT_ALLOWED_SET_ODV, "subEnvs that will write to data output file" );
87 #ifdef UQ_SIP_READS_SOLVER_OPTION
88  m_parser->registerOption<std::string>(m_option_solver, UQ_SIP_SOLVER_ODV , "algorithm for calibration" );
89 #endif
90  m_parser->registerOption<bool >(m_option_seedWithMAPEstimator, UQ_SIP_SEEDWITHMAPESTIMATOR , "toggle for seeding chain at MAP" );
91  m_parser->registerOption<bool >(m_option_useOptimizerMonitor, UQ_SIP_USEOPTIMIZERMONITOR , "toggle for using optimizer monitor (prints diagnostics");
92 
94 
95  m_parser->getOption<std::string>(m_option_help, m_help);
99 #ifdef UQ_SIP_READS_SOLVER_OPTION
100  m_parser->getOption<std::string>(m_option_solver, m_solver);
101 #endif
104 
105  checkOptions();
106 }
107 // Copy constructor - -----------------------------
109 {
110  this->copy(src);
111 }
112 // Destructor ---------------------------------------
114 {
115 }
116 
117 // Set methods --------------------------------------
120 {
121  this->copy(rhs);
122  return *this;
123 }
124 // Private methods-----------------------------------
125 void
127 {
128  // Nothing
129 }
130 
131 void
133 {
137 #ifdef UQ_SIP_READS_SOLVER_OPTION
138  m_solverString = src.m_solverString;
139 #endif
142 }
143 
144 std::ostream &
145 operator<<(std::ostream& os, const SipOptionsValues & obj)
146 {
147  os << "\n" << obj.m_option_computeSolution << " = " << obj.m_computeSolution
148  << "\n" << obj.m_option_dataOutputFileName << " = " << obj.m_dataOutputFileName;
149  os << "\n" << obj.m_option_dataOutputAllowedSet << " = ";
150  for (std::set<unsigned int>::iterator setIt = obj.m_dataOutputAllowedSet.begin(); setIt != obj.m_dataOutputAllowedSet.end(); ++setIt) {
151  os << *setIt << " ";
152  }
153 #ifdef UQ_SIP_READS_SOLVER_OPTION
154  << "\n" << obj.m_option_solver << " = " << obj.m_solverString
155 #endif
156  os << std::endl;
157  return os;
158 }
159 
160 // -------------------------------------------------
161 // StatisticalInverseProblemOptions----------
162 // -------------------------------------------------
163 
164 // Default constructor -----------------------------
166  const BaseEnvironment& env,
167  const char* prefix)
168 :
169  m_ov (),
170  m_prefix ((std::string)(prefix) + "ip_"),
171  m_env (env),
172  m_optionsDesc (new boost::program_options::options_description("Statistical Inverse Problem options")),
173  m_option_help (m_prefix + "help" ),
174  m_option_computeSolution (m_prefix + "computeSolution" ),
175  m_option_dataOutputFileName (m_prefix + "dataOutputFileName" ),
176  m_option_dataOutputAllowedSet(m_prefix + "dataOutputAllowedSet"),
177 #ifdef UQ_SIP_READS_SOLVER_OPTION
178  m_option_solver (m_prefix + "solver" )
179 #endif
180  m_option_seedWithMAPEstimator(m_prefix + "seedWithMAPEstimator"),
181  m_option_useOptimizerMonitor(m_prefix + "useOptimizerMonitor")
182 {
184  queso_require_not_equal_to_msg(m_env.optionsInputFileName(), "", "this constructor is incompatible with the absence of an options input file");
185 }
186 
187 // Constructor 2------------------------------------
189  const BaseEnvironment& env,
190  const char* prefix,
191  const SipOptionsValues& alternativeOptionsValues)
192 :
193  m_ov (alternativeOptionsValues),
194  m_prefix ((std::string)(prefix) + "ip_"),
195  m_env (env),
196  m_optionsDesc (NULL),
197  m_option_help (m_prefix + "help" ),
198  m_option_computeSolution (m_prefix + "computeSolution" ),
199  m_option_dataOutputFileName (m_prefix + "dataOutputFileName" ),
200  m_option_dataOutputAllowedSet(m_prefix + "dataOutputAllowedSet"),
201 #ifdef UQ_SIP_READS_SOLVER_OPTION
202  m_option_solver (m_prefix + "solver" )
203 #endif
204  m_option_seedWithMAPEstimator(m_prefix + "seedWithMAPEstimator"),
205  m_option_useOptimizerMonitor(m_prefix + "useOptimizerMonitor")
206 {
208 
209  queso_require_equal_to_msg(m_env.optionsInputFileName(), "", "this constructor is incompatible with the existence of an options input file");
210 
211  if (m_env.subDisplayFile() != NULL) {
212  *m_env.subDisplayFile() << "In StatisticalInverseProblemOptions::constructor(2)"
213  << ": after setting values of options with prefix '" << m_prefix
214  << "', state of object is:"
215  << "\n" << *this
216  << std::endl;
217  }
218 }
219 // Destructor --------------------------------------
221 {
223  if (m_optionsDesc) delete m_optionsDesc;
224 }
225 
226 // I/O methods --------------------------------------
227 void
229 {
230  queso_require_msg(m_optionsDesc, "m_optionsDesc variable is NULL");
231 
235 
236  if (m_env.subDisplayFile() != NULL) {
237  *m_env.subDisplayFile() << "In StatisticalInverseProblemOptions::scanOptionsValues()"
238  << ": after reading values of options with prefix '" << m_prefix
239  << "', state of object is:"
240  << "\n" << *this
241  << std::endl;
242  }
243 
244  return;
245 }
246 
247 // --------------------------------------------------
248 void
250 {
252 
253  os << "\n" << m_option_computeSolution << " = " << m_ov.m_computeSolution
255  os << "\n" << m_option_dataOutputAllowedSet << " = ";
256  for (std::set<unsigned int>::iterator setIt = m_ov.m_dataOutputAllowedSet.begin(); setIt != m_ov.m_dataOutputAllowedSet.end(); ++setIt) {
257  os << *setIt << " ";
258  }
259 #ifdef UQ_SIP_READS_SOLVER_OPTION
260  << "\n" << m_option_solver << " = " << m_ov.m_solverString
261 #endif
264  os << std::endl;
265 
266  return;
267 }
268 
269 void
270 StatisticalInverseProblemOptions::defineMyOptions(boost::program_options::options_description& optionsDesc) const
271 {
273 
274  optionsDesc.add_options()
275  (m_option_help.c_str(), "produce help message for statistical inverse problem")
276  (m_option_computeSolution.c_str(), boost::program_options::value<bool >()->default_value(UQ_SIP_COMPUTE_SOLUTION_ODV ), "compute solution process" )
277  (m_option_dataOutputFileName.c_str(), boost::program_options::value<std::string>()->default_value(UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV ), "name of data output file" )
278  (m_option_dataOutputAllowedSet.c_str(), boost::program_options::value<std::string>()->default_value(UQ_SIP_DATA_OUTPUT_ALLOWED_SET_ODV), "subEnvs that will write to data output file" )
279 #ifdef UQ_SIP_READS_SOLVER_OPTION
280  (m_option_solver.c_str(), boost::program_options::value<std::string>()->default_value(UQ_SIP_SOLVER_ODV ), "algorithm for calibration" )
281 #endif
282  (m_option_seedWithMAPEstimator.c_str(), boost::program_options::value<bool>()->default_value(UQ_SIP_SEEDWITHMAPESTIMATOR), "toggle optimisation of posterior pdf before sampling")
283  (m_option_useOptimizerMonitor.c_str(), boost::program_options::value<bool>()->default_value(UQ_SIP_USEOPTIMIZERMONITOR), "toggle printing of optimisation progress when seed with MAP estimator")
284  ;
285 }
286 //--------------------------------------------------
287 void
288 StatisticalInverseProblemOptions::getMyOptionValues(boost::program_options::options_description& optionsDesc)
289 {
291 
292  if (m_env.allOptionsMap().count(m_option_help)) {
293  if (m_env.subDisplayFile()) {
294  *m_env.subDisplayFile() << optionsDesc
295  << std::endl;
296  }
297  }
298 
300  m_ov.m_computeSolution = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_computeSolution]).as<bool>();
301  }
302 
304  m_ov.m_dataOutputFileName = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_dataOutputFileName]).as<std::string>();
305  }
306 
309  std::vector<double> tmpAllow(0,0.);
310  std::string inputString = m_env.allOptionsMap()[m_option_dataOutputAllowedSet].as<std::string>();
311  MiscReadDoublesFromString(inputString,tmpAllow);
312 
313  if (tmpAllow.size() > 0) {
314  for (unsigned int i = 0; i < tmpAllow.size(); ++i) {
315  m_ov.m_dataOutputAllowedSet.insert((unsigned int) tmpAllow[i]);
316  }
317  }
318  }
319 
320 #ifdef UQ_SIP_READS_SOLVER_OPTION
321  if (m_env.allOptionsMap().count(m_option_solver)) {
322  m_ov.m_solverString = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_solver]).as<std::string>();
323  }
324 #endif
325 
327  m_ov.m_seedWithMAPEstimator = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_seedWithMAPEstimator]).as<bool>();
328  }
329 
331  m_ov.m_useOptimizerMonitor = ((const boost::program_options::variable_value&) m_env.allOptionsMap()[m_option_useOptimizerMonitor]).as<bool>();
332  }
333 }
334 
335 // --------------------------------------------------
336 // Operator declared outside class definition ------
337 // --------------------------------------------------
338 
339 std::ostream& operator<<(std::ostream& os, const StatisticalInverseProblemOptions& obj)
340 {
342  obj.print(os);
343 
344  return os;
345 }
346 
347 } // End namespace QUESO
#define UQ_SIP_COMPUTE_SOLUTION_ODV
#define UQ_SIP_DATA_OUTPUT_FILE_NAME_ODV
void getMyOptionValues(boost::program_options::options_description &optionsDesc)
Gets the option values of the SIP.
void scanInputFileForMyOptions(const boost::program_options::options_description &optionsDesc) const
This method scans the input file provided by the user to QUESO.
Definition: Environment.C:349
void print(std::ostream &os) const
It prints the option values.
void defineMyOptions(boost::program_options::options_description &optionsDesc) const
Define my SIP options as the default options.
#define queso_require_not_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:86
#define queso_deprecated()
Definition: Defines.h:134
boost::program_options::variables_map & allOptionsMap() const
Definition: Environment.C:340
#define queso_require_msg(asserted, msg)
Definition: asserts.h:69
#define queso_require_equal_to_msg(expr1, expr2, msg)
Definition: asserts.h:85
void copy(const SipOptionsValues &src)
Copies the option values from src to this.
#define UQ_SIP_SEEDWITHMAPESTIMATOR
std::ostream & operator<<(std::ostream &os, const BaseEnvironment &obj)
std::string optionsInputFileName() const
Access to the attribute m_optionsInputFileName, which stores the name of the input file passed by the...
Definition: Environment.C:311
void scanInputFile()
This is the method that parses the input file.
SipOptionsValues & operator=(const SipOptionsValues &rhs)
Assignment operator; it copies rhs to this.
#define UQ_SIP_DATA_OUTPUT_ALLOWED_SET_ODV
std::ofstream * subDisplayFile() const
Access function for m_subDisplayFile (displays file on stream).
Definition: Environment.C:278
#define UQ_SIP_USEOPTIMIZERMONITOR
std::string m_help
If this string is non-empty, options are print to the output file.
This class provides options for a Statistical Inverse Problem if no input file is available...
This (virtual) class sets up the environment underlying the use of the QUESO library by an executable...
Definition: Environment.h:193
void registerOption(std::string name, T defaultValue, std::string description)
Call this to register an option with the parser.
void scanOptionsValues()
It scans the option values from the options input file.
This class reads option values for a Statistical Inverse Problem from an input file.
std::set< unsigned int > m_dataOutputAllowedSet
boost::program_options::options_description * m_optionsDesc
StatisticalInverseProblemOptions(const BaseEnvironment &env, const char *prefix)
Constructor: reads options from the input file.
void getOption(std::string &name, T &value)
Get option name from the parser and set value to the parsed value.
void MiscReadDoublesFromString(const std::string &inputString, std::vector< double > &outputDoubles)
Definition: Miscellaneous.C:40

Generated on Fri Jun 17 2016 14:17:42 for queso-0.55.0 by  doxygen 1.8.5