10#ifndef ROL_STOCHASTICPROBLEM_DEF_HPP
11#define ROL_STOCHASTICPROBLEM_DEF_HPP
15template<
typename Real>
21template<
typename Real>
27 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
28 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Cannot set stochastic objective after problem has been finalized!");
30 ROL_TEST_FOR_EXCEPTION(fsampler == nullPtr,std::invalid_argument,
31 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Objective function value sampler is null!");
35 Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
36 _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
37 _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
39 std::string type = list.sublist(
"SOL").sublist(
"Objective").get(
"Type",
"Risk Neutral");
40 if ( type ==
"Risk Neutral" ) {
43 bool storage = list.sublist(
"SOL").sublist(
"Objective").sublist(
"Risk Neutral").get(
"Use Storage",
true);
46 else if ( type ==
"Risk Averse" || type ==
"Deviation" || type ==
"Error" ||
47 type ==
"Regret" || type ==
"Probability" ) {
50 objList_->sublist(
"SOL") = list.sublist(
"SOL").sublist(
"Objective");
53 else if ( type ==
"Mean Value" ) {
59 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
60 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Invalid stochastic optimization type!");
64template<
typename Real>
71 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
72 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Cannot set stochastic objective after problem has been finalized!");
74 ROL_TEST_FOR_EXCEPTION(fsampler == nullPtr,std::invalid_argument,
75 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Objective function value sampler is null!");
77 ROL_TEST_FOR_EXCEPTION(rvf == nullPtr,std::invalid_argument,
78 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Risk measure is null!");
82 Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
83 _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
84 _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
93template<
typename Real>
99 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
100 ">>> ROL::StochasticProblem::makeConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
102 ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
103 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint sampler is null!");
106 ROL_TEST_FOR_EXCEPTION(it ==
INPUT_con_.end(),std::invalid_argument,
107 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint does not exist!");
109 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint already set!");
112 std::string type = list.sublist(
"SOL").sublist(name).get(
"Type",
"Risk Neutral");
113 Ptr<Constraint<Real>> con = it->second.constraint;
114 Ptr<Vector<Real>> mul = it->second.multiplier;
115 Ptr<Vector<Real>> res = it->second.residual;
116 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
117 if ( type ==
"Risk Neutral" ) {
118 ROL_TEST_FOR_EXCEPTION(bman == nullPtr,std::invalid_argument,
119 ">>> ROL::StochasticProblem::makeConstraintStochastic: Risk neutral constraints need a valid BatchManager!");
120 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(nullPtr,
true)});
121 con = makePtr<RiskNeutralConstraint<Real>>(it->second.constraint,sampler,bman);
123 else if ( type ==
"Almost Sure" ) {
124 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(nullPtr,
true)});
125 int nsamp = sampler->numMySamples();
126 con = makePtr<AlmostSureConstraint<Real>>(sampler,it->second.constraint);
127 std::vector<Ptr<Vector<Real>>> mvec(nsamp,nullPtr), rvec(nsamp,nullPtr);
128 for (
int j = 0; j < nsamp; ++j) {
129 mvec[j] = mul->clone(); mvec[j]->set(*mul);
130 rvec[j] = res->clone(); rvec[j]->set(*res);
132 mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
133 res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
135 bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
137 else if ( type ==
"Risk Averse" || type ==
"Deviation" || type ==
"Error" ||
138 type ==
"Regret" || type ==
"Probability" ) {
139 ROL_TEST_FOR_EXCEPTION(bnd == nullPtr,std::invalid_argument,
140 ">>> ROL::StochasticProblem::makeConstraintStochastic: Stochastic constraints must be inequalities!");
141 Ptr<ParameterList> clist = makePtr<ParameterList>();
142 clist->sublist(
"SOL") = list.sublist(
"SOL").sublist(name);
143 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(clist,
false)});
144 con = makePtr<StochasticConstraint<Real>>(it->second.constraint,sampler,*clist);
146 else if ( type ==
"Mean Value" ) {
147 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(nullPtr,
true)});
148 con = makePtr<MeanValueConstraint<Real>>(it->second.constraint,sampler);
151 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
152 ">>> ROL::StochasticProblem::makeConstraintStochastic: Invalid stochastic optimization type!");
159template<
typename Real>
165 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
166 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
168 ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
169 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint sampler is null!");
173 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint does not exist!");
175 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint already set!");
178 std::string type = list.sublist(
"SOL").sublist(name).get(
"Type",
"Risk Neutral");
179 Ptr<Constraint<Real>> con = it->second.constraint;
180 Ptr<Vector<Real>> mul = it->second.multiplier;
181 Ptr<Vector<Real>> res = it->second.residual;
182 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
183 if ( type ==
"Risk Neutral" ) {
184 ROL_TEST_FOR_EXCEPTION(bman == nullPtr,std::invalid_argument,
185 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Risk neutral constraints need a valid BatchManager!");
186 con = makePtr<RiskNeutralConstraint<Real>>(it->second.constraint,sampler,bman);
188 else if ( type ==
"Almost Sure" ) {
189 int nsamp = sampler->numMySamples();
190 con = makePtr<AlmostSureConstraint<Real>>(sampler,it->second.constraint);
191 std::vector<Ptr<Vector<Real>>> mvec(nsamp,nullPtr), rvec(nsamp,nullPtr);
192 for (
int j = 0; j < nsamp; ++j) {
193 mvec[j] = mul->clone(); mvec[j]->set(*mul);
194 rvec[j] = res->clone(); rvec[j]->set(*res);
196 mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
197 res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
199 bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
201 else if ( type ==
"Mean Value" ) {
202 con = makePtr<MeanValueConstraint<Real>>(it->second.constraint,sampler);
205 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
206 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Invalid stochastic optimization type!");
213template<
typename Real>
215 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
216 ">>> ROL::StochasticProblem::resetStochasticObjective: Cannot reset stochastic objective after problem has been finalized!");
225template<
typename Real>
227 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
228 ">>> ROL::StochasticProblem::resetStochasticConstraint: Cannot reset stochastic constraint after problem has been finalized!");
231 Ptr<Constraint<Real>> con = it->second.constraint;
232 Ptr<Vector<Real>> mul = it->second.multiplier;
233 Ptr<Vector<Real>> res = it->second.residual;
234 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
243template<
typename Real>
245 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
246 ">>> ROL::StochasticProblem::resetStochasticLinearConstraint: Cannot reset stochastic constraint after problem has been finalized!");
249 Ptr<Constraint<Real>> con = it->second.constraint;
250 Ptr<Vector<Real>> mul = it->second.multiplier;
251 Ptr<Vector<Real>> res = it->second.residual;
252 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
260template<
typename Real>
262 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
263 ">>> ROL::StochasticProblem::reset: Cannot reset stochastic problem after problem has been finalized!");
267 std::vector<std::string> names;
269 names.push_back(it->first);
271 for (
auto it = names.begin(); it != names.end(); ++it) {
277 names.push_back(it->first);
279 for (
auto it = names.begin(); it != names.end(); ++it) {
299template<
typename Real>
301 ROL_TEST_FOR_EXCEPTION(!
isFinalized(),std::invalid_argument,
302 ">>> ROL::StochasticProblem::getObjectiveStatistic: Cannot get statistic if problem has not been finalized!");
304 Ptr<std::vector<Real>> stat
305 = dynamicPtrCast<RiskVector<Real>>(
INPUT_xprim_)->getStatistic();
306 if (stat != nullPtr)
return *stat;
307 else return std::vector<Real>();
309 catch (std::exception &e) {
310 return std::vector<Real>();
314template<
typename Real>
316 ROL_TEST_FOR_EXCEPTION(!
isFinalized(),std::invalid_argument,
317 ">>> ROL::StochasticProblem::getConstraintStatistic: Cannot get statistic if problem has not been finalized!");
319 ROL_TEST_FOR_EXCEPTION(it==
statMap_.end(),std::invalid_argument,
320 ">>> ROL::StochasticProblem::getConstraintStatistic: Constraint does not exist!");
322 Ptr<std::vector<Real>> stat
323 = dynamicPtrCast<RiskVector<Real>>(
INPUT_xprim_)->getStatistic(1,it->second);
324 if (stat != nullPtr)
return *stat;
325 else return std::vector<Real>();
327 catch (std::exception &e) {
328 return std::vector<Real>();
332template<
typename Real>
334 ROL_TEST_FOR_EXCEPTION(!
isFinalized(),std::invalid_argument,
335 ">>> ROL::StochasticProblem::getConstraintStatistic: Cannot get statistic if problem has not been finalized!");
336 ROL_TEST_FOR_EXCEPTION(comp>1||comp<0,std::invalid_argument,
337 ">>> ROL::StochasticProblem::getSolutionStatistic: Component must be either 0 or 1!");
343 catch (std::exception &e) {
344 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
345 ">>> ROL::StochasticProblem::getSolutionStatistic: Objective does not have a computeStatistic function!");
350 ROL_TEST_FOR_EXCEPTION(it==
statMap_.end(),std::invalid_argument,
351 ">>> ROL::StochasticProblem::getSolutionStatistic: Constraint does not exist!");
356 catch (std::exception &e) {
357 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
358 ">>> ROL::StochasticProblem::getSolutionStatistic: Constraint does not have a computeStatistic function!");
364template<
typename Real>
367 std::vector<Ptr<ParameterList>> conList;
374 auto it2 =
conList_.find(it->first);
376 conList.push_back(nullPtr);
380 conList.push_back(std::get<0>(it2->second));
382 flag = std::get<1>(it2->second);
384 dynamicPtrCast<StochasticConstraint<Real>>(it->second.constraint)->setIndex(cnt);
395 INPUT_obj_ = makePtr<RiskLessObjective<Real>>(obj);
403 Real statObj =
objList_->sublist(
"SOL").get(
"Initial Statistic",1.0);
404 dynamicPtrCast<RiskVector<Real>>(
INPUT_xprim_)->setStatistic(statObj,0);
406 for (
size_t i = 0; i < conList.size(); ++i) {
407 if (conList[i] != nullPtr) {
408 Real statCon = conList[i]->sublist(
"SOL").get(
"Initial Statistic",1.0);
409 dynamicPtrCast<RiskVector<Real>>(
INPUT_xprim_)->setStatistic(statCon,1,i);
419 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
422 Ptr<Constraint<Real>> con = makePtr<RiskLessConstraint<Real>>(it->second.constraint);
423 Ptr<Vector<Real>> mul = it->second.multiplier;
424 Ptr<Vector<Real>> res = it->second.residual;
425 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
432 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
433 Ptr<Constraint<Real>> con = it->second.constraint;
434 Ptr<Vector<Real>> mul = it->second.multiplier;
435 Ptr<Vector<Real>> res = it->second.residual;
436 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
442 riskless_con.clear();
444 Ptr<Constraint<Real>> con = makePtr<RiskLessConstraint<Real>>(it->second.constraint);
445 Ptr<Vector<Real>> mul = it->second.multiplier;
446 Ptr<Vector<Real>> res = it->second.residual;
447 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
452 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
453 Ptr<Constraint<Real>> con = it->second.constraint;
454 Ptr<Vector<Real>> mul = it->second.multiplier;
455 Ptr<Vector<Real>> res = it->second.residual;
456 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
467template<
typename Real>
484 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
487 Ptr<Constraint<Real>> con = it->second.constraint;
488 Ptr<Vector<Real>> mul = it->second.multiplier;
489 Ptr<Vector<Real>> res = it->second.residual;
490 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
495 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
498 Ptr<Constraint<Real>> con = it2->second.constraint;
499 Ptr<Vector<Real>> mul = it2->second.multiplier;
500 Ptr<Vector<Real>> res = it2->second.residual;
501 Ptr<BoundConstraint<Real>> bnd = it2->second.bounds;
509 riskless_con.clear();
511 Ptr<Constraint<Real>> con = it->second.constraint;
512 Ptr<Vector<Real>> mul = it->second.multiplier;
513 Ptr<Vector<Real>> res = it->second.residual;
514 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
517 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
520 Ptr<Constraint<Real>> con = it2->second.constraint;
521 Ptr<Vector<Real>> mul = it2->second.multiplier;
522 Ptr<Vector<Real>> res = it2->second.residual;
523 Ptr<BoundConstraint<Real>> bnd = it2->second.bounds;
virtual Real computeStatistic(const Ptr< const std::vector< Real > > &xstat) const
Compute statistic.
Provides the interface to evaluate objective functions.
Ptr< BoundConstraint< Real > > INPUT_bnd_
Ptr< Objective< Real > > INPUT_obj_
void removeConstraint(std::string name)
Remove an existing constraint.
void removeLinearConstraint(std::string name)
Remove an existing linear constraint.
bool isFinalized() const
Indicate whether or no finalize has been called.
std::unordered_map< std::string, ConstraintData< Real > > INPUT_linear_con_
void addLinearConstraint(std::string name, const Ptr< Constraint< Real > > &linear_econ, const Ptr< Vector< Real > > &linear_emul, const Ptr< Vector< Real > > &linear_eres=nullPtr, bool reset=false)
Add a linear equality constraint.
Ptr< Vector< Real > > INPUT_xdual_
std::unordered_map< std::string, ConstraintData< Real > > INPUT_con_
void addConstraint(std::string name, const Ptr< Constraint< Real > > &econ, const Ptr< Vector< Real > > &emul, const Ptr< Vector< Real > > &eres=nullPtr, bool reset=false)
Add an equality constraint.
Ptr< Vector< Real > > INPUT_xprim_
virtual void finalize(bool lumpConstraints=false, bool printToStream=false, std::ostream &outStream=std::cout)
Tranform user-supplied constraints to consist of only bounds and equalities. Optimization problem can...
virtual void edit()
Resume editting optimization problem after finalize has been called.
Problem(const Ptr< Objective< Real > > &obj, const Ptr< Vector< Real > > &x, const Ptr< Vector< Real > > &g=nullPtr)
Default constructor for OptimizationProblem.
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
std::vector< Real > getConstraintStatistic(std::string name) const
void makeConstraintStochastic(std::string name, ParameterList &list, const Ptr< SampleGenerator< Real > > &sampler, const Ptr< BatchManager< Real > > &bman=nullPtr)
std::unordered_map< std::string, ConstraintData< Real > > ORIGINAL_con_
void edit(void) override
Resume editting optimization problem after finalize has been called.
void resetStochasticLinearConstraint(std::string name)
Ptr< Vector< Real > > ORIGINAL_xdual_
std::unordered_map< std::string, std::pair< Ptr< ParameterList >, bool > > conList_
void resetStochasticConstraint(std::string name)
Ptr< ParameterList > objList_
void resetStochasticObjective(void)
std::unordered_map< std::string, size_t > statMap_
void makeLinearConstraintStochastic(std::string name, ParameterList &list, const Ptr< SampleGenerator< Real > > &sampler, const Ptr< BatchManager< Real > > &bman=nullPtr)
Ptr< Objective< Real > > ORIGINAL_obj_
Ptr< Vector< Real > > ORIGINAL_xprim_
std::vector< Real > getObjectiveStatistic(void) const
void finalize(bool lumpConstraints=false, bool printToStream=false, std::ostream &outStream=std::cout) override
Tranform user-supplied constraints to consist of only bounds and equalities. Optimization problem can...
Real getSolutionStatistic(int comp=0, std::string name="") const
StochasticProblem(const Ptr< Objective< Real > > &obj, const Ptr< Vector< Real > > &x, const Ptr< Vector< Real > > &g=nullPtr)
Default constructor for StochasticProblem.
Ptr< BoundConstraint< Real > > ORIGINAL_bnd_
void resetStochastic(void)
std::vector< bool > needRiskLessCon_
void makeObjectiveStochastic(ParameterList &list, const Ptr< SampleGenerator< Real > > &fsampler, const Ptr< SampleGenerator< Real > > &gsampler=nullPtr, const Ptr< SampleGenerator< Real > > &hsampler=nullPtr)
std::unordered_map< std::string, ConstraintData< Real > > ORIGINAL_linear_con_
Defines the linear algebra or vector space interface.