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!");
33 ORIGINAL_obj_ = INPUT_obj_;
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" ) {
41 needRiskLessObj_ =
true;
43 bool storage = list.sublist(
"SOL").sublist(
"Objective").sublist(
"Risk Neutral").get(
"Use Storage",
true);
44 INPUT_obj_ = makePtr<RiskNeutralObjective<Real>>(ORIGINAL_obj_,fsampler,_gsampler,_hsampler,storage);
46 else if ( type ==
"Risk Averse" || type ==
"Deviation" || type ==
"Error" ||
47 type ==
"Regret" || type ==
"Probability" ) {
48 needRiskLessObj_ =
false;
49 objList_ = makePtr<ParameterList>();
50 objList_->sublist(
"SOL") = list.sublist(
"SOL").sublist(
"Objective");
51 INPUT_obj_ = makePtr<StochasticObjective<Real>>(ORIGINAL_obj_,*objList_,fsampler,_gsampler,_hsampler);
53 else if ( type ==
"Mean Value" ) {
54 needRiskLessObj_ =
true;
56 INPUT_obj_ = makePtr<MeanValueObjective<Real>>(ORIGINAL_obj_,fsampler);
59 ROL_TEST_FOR_EXCEPTION(
true,std::invalid_argument,
60 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Invalid stochastic optimization type!");
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!");
80 ORIGINAL_obj_ = INPUT_obj_;
82 Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
83 _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
84 _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
86 needRiskLessObj_ =
false;
87 objList_ = makePtr<ParameterList>();
90 INPUT_obj_ = makePtr<StochasticObjective<Real>>(ORIGINAL_obj_,rvf,fsampler,_gsampler,_hsampler);
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!");
105 auto it = INPUT_con_.find(name);
106 ROL_TEST_FOR_EXCEPTION(it == INPUT_con_.end(),std::invalid_argument,
107 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint does not exist!");
108 ROL_TEST_FOR_EXCEPTION(ORIGINAL_con_.find(name) != ORIGINAL_con_.end(),std::invalid_argument,
109 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint already set!");
110 ORIGINAL_con_.insert({name,it->second});
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!");
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!");
171 auto it = INPUT_linear_con_.find(name);
172 ROL_TEST_FOR_EXCEPTION(it == INPUT_linear_con_.end(),std::invalid_argument,
173 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint does not exist!");
174 ROL_TEST_FOR_EXCEPTION(ORIGINAL_linear_con_.find(name) != ORIGINAL_linear_con_.end(),std::invalid_argument,
175 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint already set!");
176 ORIGINAL_linear_con_.insert({name,it->second});
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!");
262 ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
263 ">>> ROL::StochasticProblem::reset: Cannot reset stochastic problem after problem has been finalized!");
265 resetStochasticObjective();
267 std::vector<std::string> names;
268 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
269 names.push_back(it->first);
271 for (
auto it = names.begin(); it != names.end(); ++it) {
272 resetStochasticConstraint(*it);
276 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
277 names.push_back(it->first);
279 for (
auto it = names.begin(); it != names.end(); ++it) {
280 resetStochasticLinearConstraint(*it);
283 if (ORIGINAL_xprim_ != nullPtr) {
284 INPUT_xprim_ = ORIGINAL_xprim_;
285 ORIGINAL_xprim_ = nullPtr;
288 if (ORIGINAL_xdual_ != nullPtr) {
289 INPUT_xdual_ = ORIGINAL_xdual_;
290 ORIGINAL_xdual_ = nullPtr;
293 if (ORIGINAL_bnd_ != nullPtr) {
294 INPUT_bnd_ = ORIGINAL_bnd_;
295 ORIGINAL_bnd_ = nullPtr;
367 std::vector<Ptr<ParameterList>> conList;
369 risk_ = !needRiskLessObj_;
371 needRiskLessCon_.clear();
373 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
374 auto it2 = conList_.find(it->first);
375 if (it2==conList_.end()) {
376 conList.push_back(nullPtr);
377 needRiskLessCon_.push_back(
true);
380 conList.push_back(std::get<0>(it2->second));
381 needRiskLessCon_.push_back(std::get<1>(it2->second));
382 flag = std::get<1>(it2->second);
384 dynamicPtrCast<StochasticConstraint<Real>>(it->second.constraint)->setIndex(cnt);
388 statMap_.insert({it->first,cnt});
393 if (needRiskLessObj_) {
394 Ptr<Objective<Real>> obj = INPUT_obj_;
395 INPUT_obj_ = makePtr<RiskLessObjective<Real>>(obj);
398 ORIGINAL_xprim_ = INPUT_xprim_;
399 INPUT_xprim_ = makePtr<RiskVector<Real>>(objList_,conList,ORIGINAL_xprim_);
400 ORIGINAL_xdual_ = INPUT_xdual_;
401 INPUT_xdual_ = makePtr<RiskVector<Real>>(objList_,conList,ORIGINAL_xdual_);
402 if (objList_ != nullPtr) {
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);
413 if (INPUT_bnd_ != nullPtr) {
414 ORIGINAL_bnd_ = INPUT_bnd_;
415 INPUT_bnd_ = makePtr<RiskBoundConstraint<Real>>(objList_,conList,ORIGINAL_bnd_);
419 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
420 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
421 if (needRiskLessCon_[cnt]) {
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;
427 if (ORIGINAL_con_.count(it->first) == size_t(0))
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();
443 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
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;
449 if (ORIGINAL_linear_con_.count(it->first) == size_t(0))
450 ORIGINAL_linear_con_.insert({it->first,
ConstraintData<Real>(it->second.constraint,mul,res,bnd)});
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;
472 if (needRiskLessObj_ && ORIGINAL_obj_ != nullPtr) {
473 INPUT_obj_ = ORIGINAL_obj_;
474 ORIGINAL_obj_ = nullPtr;
476 if (ORIGINAL_xprim_ != nullPtr) INPUT_xprim_ = ORIGINAL_xprim_;
477 if (ORIGINAL_xdual_ != nullPtr) INPUT_xdual_ = ORIGINAL_xdual_;
478 if (ORIGINAL_bnd_ != nullPtr) INPUT_bnd_ = ORIGINAL_bnd_;
479 ORIGINAL_xprim_ = nullPtr;
480 ORIGINAL_xdual_ = nullPtr;
481 ORIGINAL_bnd_ = nullPtr;
484 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
485 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
486 if (needRiskLessCon_[cnt]) {
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) {
496 auto it2 = ORIGINAL_con_.find(it->first);
497 if (it2 != ORIGINAL_con_.end()) {
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;
505 ORIGINAL_con_.erase(it2);
509 riskless_con.clear();
510 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
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) {
518 auto it2 = ORIGINAL_linear_con_.find(it->first);
519 if (it2 != ORIGINAL_linear_con_.end()) {
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;
527 ORIGINAL_linear_con_.erase(it2);
532 needRiskLessCon_.clear();