44#ifndef ROL_STOCHASTICPROBLEM_DEF_HPP
45#define ROL_STOCHASTICPROBLEM_DEF_HPP
49template<
typename Real>
55template<
typename Real>
61 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
62 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Cannot set stochastic objective after problem has been finalized!");
64 ROL_TEST_FOR_EXCEPTION(fsampler == nullPtr,std::invalid_argument,
65 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Objective function value sampler is null!");
69 Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
70 _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
71 _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
73 std::string type = list.sublist(
"SOL").sublist(
"Objective").get(
"Type",
"Risk Neutral");
74 if ( type ==
"Risk Neutral" ) {
77 bool storage = list.sublist(
"SOL").sublist(
"Objective").sublist(
"Risk Neutral").get(
"Use Storage",
true);
80 else if ( type ==
"Risk Averse" || type ==
"Deviation" || type ==
"Error" ||
81 type ==
"Regret" || type ==
"Probability" ) {
84 objList_->sublist(
"SOL") = list.sublist(
"SOL").sublist(
"Objective");
87 else if ( type ==
"Mean Value" ) {
93 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
94 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Invalid stochastic optimization type!");
98template<
typename Real>
105 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
106 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Cannot set stochastic objective after problem has been finalized!");
108 ROL_TEST_FOR_EXCEPTION(fsampler == nullPtr,std::invalid_argument,
109 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Objective function value sampler is null!");
111 ROL_TEST_FOR_EXCEPTION(rvf == nullPtr,std::invalid_argument,
112 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Risk measure is null!");
116 Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
117 _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
118 _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
121 objList_ = makePtr<ParameterList>();
127template<
typename Real>
133 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
134 ">>> ROL::StochasticProblem::makeConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
136 ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
137 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint sampler is null!");
140 ROL_TEST_FOR_EXCEPTION(it ==
INPUT_con_.end(),std::invalid_argument,
141 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint does not exist!");
143 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint already set!");
146 std::string type = list.sublist(
"SOL").sublist(name).get(
"Type",
"Risk Neutral");
147 Ptr<Constraint<Real>> con = it->second.constraint;
148 Ptr<Vector<Real>> mul = it->second.multiplier;
149 Ptr<Vector<Real>> res = it->second.residual;
150 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
151 if ( type ==
"Risk Neutral" ) {
152 ROL_TEST_FOR_EXCEPTION(bman == nullPtr,std::invalid_argument,
153 ">>> ROL::StochasticProblem::makeConstraintStochastic: Risk neutral constraints need a valid BatchManager!");
154 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(nullPtr,
true)});
155 con = makePtr<RiskNeutralConstraint<Real>>(it->second.constraint,sampler,bman);
157 else if ( type ==
"Almost Sure" ) {
158 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(nullPtr,
true)});
159 int nsamp = sampler->numMySamples();
160 con = makePtr<AlmostSureConstraint<Real>>(sampler,it->second.constraint);
161 std::vector<Ptr<Vector<Real>>> mvec(nsamp,nullPtr), rvec(nsamp,nullPtr);
162 for (
int j = 0; j < nsamp; ++j) {
163 mvec[j] = mul->clone(); mvec[j]->set(*mul);
164 rvec[j] = res->clone(); rvec[j]->set(*res);
166 mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
167 res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
169 bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
171 else if ( type ==
"Risk Averse" || type ==
"Deviation" || type ==
"Error" ||
172 type ==
"Regret" || type ==
"Probability" ) {
173 ROL_TEST_FOR_EXCEPTION(bnd == nullPtr,std::invalid_argument,
174 ">>> ROL::StochasticProblem::makeConstraintStochastic: Stochastic constraints must be inequalities!");
175 Ptr<ParameterList> clist = makePtr<ParameterList>();
176 clist->sublist(
"SOL") = list.sublist(
"SOL").sublist(name);
177 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(clist,
false)});
178 con = makePtr<StochasticConstraint<Real>>(it->second.constraint,sampler,*clist);
180 else if ( type ==
"Mean Value" ) {
181 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(nullPtr,
true)});
182 con = makePtr<MeanValueConstraint<Real>>(it->second.constraint,sampler);
185 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
186 ">>> ROL::StochasticProblem::makeConstraintStochastic: Invalid stochastic optimization type!");
193template<
typename Real>
199 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
200 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
202 ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
203 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint sampler is null!");
207 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint does not exist!");
209 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint already set!");
212 std::string type = list.sublist(
"SOL").sublist(name).get(
"Type",
"Risk Neutral");
213 Ptr<Constraint<Real>> con = it->second.constraint;
214 Ptr<Vector<Real>> mul = it->second.multiplier;
215 Ptr<Vector<Real>> res = it->second.residual;
216 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
217 if ( type ==
"Risk Neutral" ) {
218 ROL_TEST_FOR_EXCEPTION(bman == nullPtr,std::invalid_argument,
219 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Risk neutral constraints need a valid BatchManager!");
220 con = makePtr<RiskNeutralConstraint<Real>>(it->second.constraint,sampler,bman);
222 else if ( type ==
"Almost Sure" ) {
223 int nsamp = sampler->numMySamples();
224 con = makePtr<AlmostSureConstraint<Real>>(sampler,it->second.constraint);
225 std::vector<Ptr<Vector<Real>>> mvec(nsamp,nullPtr), rvec(nsamp,nullPtr);
226 for (
int j = 0; j < nsamp; ++j) {
227 mvec[j] = mul->clone(); mvec[j]->set(*mul);
228 rvec[j] = res->clone(); rvec[j]->set(*res);
230 mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
231 res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
233 bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
235 else if ( type ==
"Mean Value" ) {
236 con = makePtr<MeanValueConstraint<Real>>(it->second.constraint,sampler);
239 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
240 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Invalid stochastic optimization type!");
247template<
typename Real>
249 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
250 ">>> ROL::StochasticProblem::resetStochasticObjective: Cannot reset stochastic objective after problem has been finalized!");
259template<
typename Real>
261 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
262 ">>> ROL::StochasticProblem::resetStochasticConstraint: Cannot reset stochastic constraint after problem has been finalized!");
265 Ptr<Constraint<Real>> con = it->second.constraint;
266 Ptr<Vector<Real>> mul = it->second.multiplier;
267 Ptr<Vector<Real>> res = it->second.residual;
268 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
277template<
typename Real>
279 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
280 ">>> ROL::StochasticProblem::resetStochasticLinearConstraint: Cannot reset stochastic constraint after problem has been finalized!");
283 Ptr<Constraint<Real>> con = it->second.constraint;
284 Ptr<Vector<Real>> mul = it->second.multiplier;
285 Ptr<Vector<Real>> res = it->second.residual;
286 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
294template<
typename Real>
296 ROL_TEST_FOR_EXCEPTION(
isFinalized(),std::invalid_argument,
297 ">>> ROL::StochasticProblem::reset: Cannot reset stochastic problem after problem has been finalized!");
301 std::vector<std::string> names;
303 names.push_back(it->first);
305 for (
auto it = names.begin(); it != names.end(); ++it) {
311 names.push_back(it->first);
313 for (
auto it = names.begin(); it != names.end(); ++it) {
333template<
typename Real>
335 ROL_TEST_FOR_EXCEPTION(!
isFinalized(),std::invalid_argument,
336 ">>> ROL::StochasticProblem::getObjectiveStatistic: Cannot get statistic if problem has not been finalized!");
338 Ptr<std::vector<Real>> stat
339 = dynamicPtrCast<RiskVector<Real>>(
INPUT_xprim_)->getStatistic();
340 if (stat != nullPtr)
return *stat;
341 else return std::vector<Real>();
343 catch (std::exception &e) {
344 return std::vector<Real>();
348template<
typename Real>
350 ROL_TEST_FOR_EXCEPTION(!
isFinalized(),std::invalid_argument,
351 ">>> ROL::StochasticProblem::getConstraintStatistic: Cannot get statistic if problem has not been finalized!");
353 ROL_TEST_FOR_EXCEPTION(it==
statMap_.end(),std::invalid_argument,
354 ">>> ROL::StochasticProblem::getConstraintStatistic: Constraint does not exist!");
356 Ptr<std::vector<Real>> stat
357 = dynamicPtrCast<RiskVector<Real>>(
INPUT_xprim_)->getStatistic(1,it->second);
358 if (stat != nullPtr)
return *stat;
359 else return std::vector<Real>();
361 catch (std::exception &e) {
362 return std::vector<Real>();
366template<
typename Real>
368 ROL_TEST_FOR_EXCEPTION(!
isFinalized(),std::invalid_argument,
369 ">>> ROL::StochasticProblem::getConstraintStatistic: Cannot get statistic if problem has not been finalized!");
370 ROL_TEST_FOR_EXCEPTION(comp>1||comp<0,std::invalid_argument,
371 ">>> ROL::StochasticProblem::getSolutionStatistic: Component must be either 0 or 1!");
377 catch (std::exception &e) {
378 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
379 ">>> ROL::StochasticProblem::getSolutionStatistic: Objective does not have a computeStatistic function!");
384 ROL_TEST_FOR_EXCEPTION(it==
statMap_.end(),std::invalid_argument,
385 ">>> ROL::StochasticProblem::getSolutionStatistic: Constraint does not exist!");
388 val = dynamicPtrCast<StochasticConstraint<Real>>(it2->second.constraint)->computeStatistic(*
INPUT_xprim_);
390 catch (std::exception &e) {
391 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
392 ">>> ROL::StochasticProblem::getSolutionStatistic: Constraint does not have a computeStatistic function!");
398template<
typename Real>
401 std::vector<Ptr<ParameterList>> conList;
408 auto it2 =
conList_.find(it->first);
410 conList.push_back(nullPtr);
414 conList.push_back(std::get<0>(it2->second));
416 flag = std::get<1>(it2->second);
418 dynamicPtrCast<StochasticConstraint<Real>>(it->second.constraint)->setIndex(cnt);
429 INPUT_obj_ = makePtr<RiskLessObjective<Real>>(obj);
437 Real statObj =
objList_->sublist(
"SOL").get(
"Initial Statistic",1.0);
438 dynamicPtrCast<RiskVector<Real>>(
INPUT_xprim_)->setStatistic(statObj,0);
440 for (
size_t i = 0; i < conList.size(); ++i) {
441 if (conList[i] != nullPtr) {
442 Real statCon = conList[i]->sublist(
"SOL").get(
"Initial Statistic",1.0);
443 dynamicPtrCast<RiskVector<Real>>(
INPUT_xprim_)->setStatistic(statCon,1,i);
453 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
456 Ptr<Constraint<Real>> con = makePtr<RiskLessConstraint<Real>>(it->second.constraint);
457 Ptr<Vector<Real>> mul = it->second.multiplier;
458 Ptr<Vector<Real>> res = it->second.residual;
459 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
466 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
467 Ptr<Constraint<Real>> con = it->second.constraint;
468 Ptr<Vector<Real>> mul = it->second.multiplier;
469 Ptr<Vector<Real>> res = it->second.residual;
470 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
476 riskless_con.clear();
478 Ptr<Constraint<Real>> con = makePtr<RiskLessConstraint<Real>>(it->second.constraint);
479 Ptr<Vector<Real>> mul = it->second.multiplier;
480 Ptr<Vector<Real>> res = it->second.residual;
481 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
486 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
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;
501template<
typename Real>
518 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
521 Ptr<Constraint<Real>> con = it->second.constraint;
522 Ptr<Vector<Real>> mul = it->second.multiplier;
523 Ptr<Vector<Real>> res = it->second.residual;
524 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
529 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
532 Ptr<Constraint<Real>> con = it2->second.constraint;
533 Ptr<Vector<Real>> mul = it2->second.multiplier;
534 Ptr<Vector<Real>> res = it2->second.residual;
535 Ptr<BoundConstraint<Real>> bnd = it2->second.bounds;
543 riskless_con.clear();
545 Ptr<Constraint<Real>> con = it->second.constraint;
546 Ptr<Vector<Real>> mul = it->second.multiplier;
547 Ptr<Vector<Real>> res = it->second.residual;
548 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
551 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
554 Ptr<Constraint<Real>> con = it2->second.constraint;
555 Ptr<Vector<Real>> mul = it2->second.multiplier;
556 Ptr<Vector<Real>> res = it2->second.residual;
557 Ptr<BoundConstraint<Real>> bnd = it2->second.bounds;
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.