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!");
67 ORIGINAL_obj_ = INPUT_obj_;
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" ) {
75 needRiskLessObj_ =
true;
77 bool storage = list.sublist(
"SOL").sublist(
"Objective").sublist(
"Risk Neutral").get(
"Use Storage",
true);
78 INPUT_obj_ = makePtr<RiskNeutralObjective<Real>>(ORIGINAL_obj_,fsampler,_gsampler,_hsampler,storage);
80 else if ( type ==
"Risk Averse" || type ==
"Deviation" || type ==
"Error" ||
81 type ==
"Regret" || type ==
"Probability" ) {
82 needRiskLessObj_ =
false;
83 objList_ = makePtr<ParameterList>();
84 objList_->sublist(
"SOL") = list.sublist(
"SOL").sublist(
"Objective");
85 INPUT_obj_ = makePtr<StochasticObjective<Real>>(ORIGINAL_obj_,*objList_,fsampler,_gsampler,_hsampler);
87 else if ( type ==
"Mean Value" ) {
88 needRiskLessObj_ =
true;
90 INPUT_obj_ = makePtr<MeanValueObjective<Real>>(ORIGINAL_obj_,fsampler);
93 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
94 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Invalid stochastic optimization type!");
104 ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
105 ">>> ROL::StochasticProblem::makeConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
107 ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
108 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint sampler is null!");
110 auto it = INPUT_con_.find(name);
111 ROL_TEST_FOR_EXCEPTION(it == INPUT_con_.end(),std::invalid_argument,
112 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint does not exist!");
113 ROL_TEST_FOR_EXCEPTION(ORIGINAL_con_.find(name) != ORIGINAL_con_.end(),std::invalid_argument,
114 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint already set!");
115 ORIGINAL_con_.insert({name,it->second});
117 std::string type = list.sublist(
"SOL").sublist(name).get(
"Type",
"Risk Neutral");
118 Ptr<Constraint<Real>> con = it->second.constraint;
119 Ptr<Vector<Real>> mul = it->second.multiplier;
120 Ptr<Vector<Real>> res = it->second.residual;
121 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
122 if ( type ==
"Risk Neutral" ) {
123 ROL_TEST_FOR_EXCEPTION(bman == nullPtr,std::invalid_argument,
124 ">>> ROL::StochasticProblem::makeConstraintStochastic: Risk neutral constraints need a valid BatchManager!");
125 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(nullPtr,
true)});
126 con = makePtr<RiskNeutralConstraint<Real>>(it->second.constraint,sampler,bman);
128 else if ( type ==
"Almost Sure" ) {
129 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(nullPtr,
true)});
130 int nsamp = sampler->numMySamples();
131 con = makePtr<AlmostSureConstraint<Real>>(sampler,it->second.constraint);
132 std::vector<Ptr<Vector<Real>>> mvec(nsamp,nullPtr), rvec(nsamp,nullPtr);
133 for (
int j = 0; j < nsamp; ++j) {
134 mvec[j] = mul->clone(); mvec[j]->set(*mul);
135 rvec[j] = res->clone(); rvec[j]->set(*res);
137 mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
138 res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
140 bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
142 else if ( type ==
"Risk Averse" || type ==
"Deviation" || type ==
"Error" ||
143 type ==
"Regret" || type ==
"Probability" ) {
144 ROL_TEST_FOR_EXCEPTION(bnd == nullPtr,std::invalid_argument,
145 ">>> ROL::StochasticProblem::makeConstraintStochastic: Stochastic constraints must be inequalities!");
146 Ptr<ParameterList> clist = makePtr<ParameterList>();
147 clist->sublist(
"SOL") = list.sublist(
"SOL").sublist(name);
148 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(clist,
false)});
149 con = makePtr<StochasticConstraint<Real>>(it->second.constraint,sampler,*clist);
151 else if ( type ==
"Mean Value" ) {
152 conList_.insert({name,std::pair<Ptr<ParameterList>,
bool>(nullPtr,
true)});
153 con = makePtr<MeanValueConstraint<Real>>(it->second.constraint,sampler);
156 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
157 ">>> ROL::StochasticProblem::makeConstraintStochastic: Invalid stochastic optimization type!");
170 ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
171 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
173 ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
174 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint sampler is null!");
176 auto it = INPUT_linear_con_.find(name);
177 ROL_TEST_FOR_EXCEPTION(it == INPUT_linear_con_.end(),std::invalid_argument,
178 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint does not exist!");
179 ROL_TEST_FOR_EXCEPTION(ORIGINAL_linear_con_.find(name) != ORIGINAL_linear_con_.end(),std::invalid_argument,
180 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint already set!");
181 ORIGINAL_linear_con_.insert({name,it->second});
183 std::string type = list.sublist(
"SOL").sublist(name).get(
"Type",
"Risk Neutral");
184 Ptr<Constraint<Real>> con = it->second.constraint;
185 Ptr<Vector<Real>> mul = it->second.multiplier;
186 Ptr<Vector<Real>> res = it->second.residual;
187 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
188 if ( type ==
"Risk Neutral" ) {
189 ROL_TEST_FOR_EXCEPTION(bman == nullPtr,std::invalid_argument,
190 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Risk neutral constraints need a valid BatchManager!");
191 con = makePtr<RiskNeutralConstraint<Real>>(it->second.constraint,sampler,bman);
193 else if ( type ==
"Almost Sure" ) {
194 int nsamp = sampler->numMySamples();
195 con = makePtr<AlmostSureConstraint<Real>>(sampler,it->second.constraint);
196 std::vector<Ptr<Vector<Real>>> mvec(nsamp,nullPtr), rvec(nsamp,nullPtr);
197 for (
int j = 0; j < nsamp; ++j) {
198 mvec[j] = mul->clone(); mvec[j]->set(*mul);
199 rvec[j] = res->clone(); rvec[j]->set(*res);
201 mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
202 res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
204 bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
206 else if ( type ==
"Mean Value" ) {
207 con = makePtr<MeanValueConstraint<Real>>(it->second.constraint,sampler);
210 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
211 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Invalid stochastic optimization type!");
267 ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
268 ">>> ROL::StochasticProblem::reset: Cannot reset stochastic problem after problem has been finalized!");
270 resetStochasticObjective();
272 std::vector<std::string> names;
273 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
274 names.push_back(it->first);
276 for (
auto it = names.begin(); it != names.end(); ++it) {
277 resetStochasticConstraint(*it);
281 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
282 names.push_back(it->first);
284 for (
auto it = names.begin(); it != names.end(); ++it) {
285 resetStochasticLinearConstraint(*it);
288 if (ORIGINAL_xprim_ != nullPtr) {
289 INPUT_xprim_ = ORIGINAL_xprim_;
290 ORIGINAL_xprim_ = nullPtr;
293 if (ORIGINAL_xdual_ != nullPtr) {
294 INPUT_xdual_ = ORIGINAL_xdual_;
295 ORIGINAL_xdual_ = nullPtr;
298 if (ORIGINAL_bnd_ != nullPtr) {
299 INPUT_bnd_ = ORIGINAL_bnd_;
300 ORIGINAL_bnd_ = nullPtr;
372 std::vector<Ptr<ParameterList>> conList;
374 risk_ = !needRiskLessObj_;
376 needRiskLessCon_.clear();
378 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
379 auto it2 = conList_.find(it->first);
380 if (it2==conList_.end()) {
381 conList.push_back(nullPtr);
382 needRiskLessCon_.push_back(
true);
385 conList.push_back(std::get<0>(it2->second));
386 needRiskLessCon_.push_back(std::get<1>(it2->second));
387 flag = std::get<1>(it2->second);
389 dynamicPtrCast<StochasticConstraint<Real>>(it->second.constraint)->setIndex(cnt);
393 statMap_.insert({it->first,cnt});
398 if (needRiskLessObj_) {
399 Ptr<Objective<Real>> obj = INPUT_obj_;
400 INPUT_obj_ = makePtr<RiskLessObjective<Real>>(obj);
403 ORIGINAL_xprim_ = INPUT_xprim_;
404 INPUT_xprim_ = makePtr<RiskVector<Real>>(objList_,conList,ORIGINAL_xprim_);
405 ORIGINAL_xdual_ = INPUT_xdual_;
406 INPUT_xdual_ = makePtr<RiskVector<Real>>(objList_,conList,ORIGINAL_xdual_);
407 if (objList_ != nullPtr) {
408 Real statObj = objList_->sublist(
"SOL").get(
"Initial Statistic",1.0);
409 dynamicPtrCast<RiskVector<Real>>(INPUT_xprim_)->setStatistic(statObj,0);
411 for (
size_t i = 0; i < conList.size(); ++i) {
412 if (conList[i] != nullPtr) {
413 Real statCon = conList[i]->sublist(
"SOL").get(
"Initial Statistic",1.0);
414 dynamicPtrCast<RiskVector<Real>>(INPUT_xprim_)->setStatistic(statCon,1,i);
418 if (INPUT_bnd_ != nullPtr) {
419 ORIGINAL_bnd_ = INPUT_bnd_;
420 INPUT_bnd_ = makePtr<RiskBoundConstraint<Real>>(objList_,conList,ORIGINAL_bnd_);
424 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
425 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
426 if (needRiskLessCon_[cnt]) {
427 Ptr<Constraint<Real>> con = makePtr<RiskLessConstraint<Real>>(it->second.constraint);
428 Ptr<Vector<Real>> mul = it->second.multiplier;
429 Ptr<Vector<Real>> res = it->second.residual;
430 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
432 if (ORIGINAL_con_.count(it->first) == size_t(0))
437 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
438 Ptr<Constraint<Real>> con = it->second.constraint;
439 Ptr<Vector<Real>> mul = it->second.multiplier;
440 Ptr<Vector<Real>> res = it->second.residual;
441 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
447 riskless_con.clear();
448 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
449 Ptr<Constraint<Real>> con = makePtr<RiskLessConstraint<Real>>(it->second.constraint);
450 Ptr<Vector<Real>> mul = it->second.multiplier;
451 Ptr<Vector<Real>> res = it->second.residual;
452 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
454 if (ORIGINAL_linear_con_.count(it->first) == size_t(0))
455 ORIGINAL_linear_con_.insert({it->first,
ConstraintData<Real>(it->second.constraint,mul,res,bnd)});
457 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
458 Ptr<Constraint<Real>> con = it->second.constraint;
459 Ptr<Vector<Real>> mul = it->second.multiplier;
460 Ptr<Vector<Real>> res = it->second.residual;
461 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
477 if (needRiskLessObj_ && ORIGINAL_obj_ != nullPtr) {
478 INPUT_obj_ = ORIGINAL_obj_;
479 ORIGINAL_obj_ = nullPtr;
481 if (ORIGINAL_xprim_ != nullPtr) INPUT_xprim_ = ORIGINAL_xprim_;
482 if (ORIGINAL_xdual_ != nullPtr) INPUT_xdual_ = ORIGINAL_xdual_;
483 if (ORIGINAL_bnd_ != nullPtr) INPUT_bnd_ = ORIGINAL_bnd_;
484 ORIGINAL_xprim_ = nullPtr;
485 ORIGINAL_xdual_ = nullPtr;
486 ORIGINAL_bnd_ = nullPtr;
489 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
490 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
491 if (needRiskLessCon_[cnt]) {
492 Ptr<Constraint<Real>> con = it->second.constraint;
493 Ptr<Vector<Real>> mul = it->second.multiplier;
494 Ptr<Vector<Real>> res = it->second.residual;
495 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
500 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
501 auto it2 = ORIGINAL_con_.find(it->first);
502 if (it2 != ORIGINAL_con_.end()) {
503 Ptr<Constraint<Real>> con = it2->second.constraint;
504 Ptr<Vector<Real>> mul = it2->second.multiplier;
505 Ptr<Vector<Real>> res = it2->second.residual;
506 Ptr<BoundConstraint<Real>> bnd = it2->second.bounds;
510 ORIGINAL_con_.erase(it2);
514 riskless_con.clear();
515 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
516 Ptr<Constraint<Real>> con = it->second.constraint;
517 Ptr<Vector<Real>> mul = it->second.multiplier;
518 Ptr<Vector<Real>> res = it->second.residual;
519 Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
522 for (
auto it = riskless_con.begin(); it != riskless_con.end(); ++it) {
523 auto it2 = ORIGINAL_linear_con_.find(it->first);
524 if (it2 != ORIGINAL_linear_con_.end()) {
525 Ptr<Constraint<Real>> con = it2->second.constraint;
526 Ptr<Vector<Real>> mul = it2->second.multiplier;
527 Ptr<Vector<Real>> res = it2->second.residual;
528 Ptr<BoundConstraint<Real>> bnd = it2->second.bounds;
532 ORIGINAL_linear_con_.erase(it2);
537 needRiskLessCon_.clear();