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!");
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!");
114 ORIGINAL_obj_ = INPUT_obj_;
116 Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
117 _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
118 _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
120 needRiskLessObj_ =
false;
121 objList_ = makePtr<ParameterList>();
124 INPUT_obj_ = makePtr<StochasticObjective<Real>>(ORIGINAL_obj_,rvf,fsampler,_gsampler,_hsampler);
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!");
139 auto it = INPUT_con_.find(name);
140 ROL_TEST_FOR_EXCEPTION(it == INPUT_con_.end(),std::invalid_argument,
141 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint does not exist!");
142 ROL_TEST_FOR_EXCEPTION(ORIGINAL_con_.find(name) != ORIGINAL_con_.end(),std::invalid_argument,
143 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint already set!");
144 ORIGINAL_con_.insert({name,it->second});
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!");
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!");
205 auto it = INPUT_linear_con_.find(name);
206 ROL_TEST_FOR_EXCEPTION(it == INPUT_linear_con_.end(),std::invalid_argument,
207 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint does not exist!");
208 ROL_TEST_FOR_EXCEPTION(ORIGINAL_linear_con_.find(name) != ORIGINAL_linear_con_.end(),std::invalid_argument,
209 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint already set!");
210 ORIGINAL_linear_con_.insert({name,it->second});
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!");
296 ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
297 ">>> ROL::StochasticProblem::reset: Cannot reset stochastic problem after problem has been finalized!");
299 resetStochasticObjective();
301 std::vector<std::string> names;
302 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
303 names.push_back(it->first);
305 for (
auto it = names.begin(); it != names.end(); ++it) {
306 resetStochasticConstraint(*it);
310 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
311 names.push_back(it->first);
313 for (
auto it = names.begin(); it != names.end(); ++it) {
314 resetStochasticLinearConstraint(*it);
317 if (ORIGINAL_xprim_ != nullPtr) {
318 INPUT_xprim_ = ORIGINAL_xprim_;
319 ORIGINAL_xprim_ = nullPtr;
322 if (ORIGINAL_xdual_ != nullPtr) {
323 INPUT_xdual_ = ORIGINAL_xdual_;
324 ORIGINAL_xdual_ = nullPtr;
327 if (ORIGINAL_bnd_ != nullPtr) {
328 INPUT_bnd_ = ORIGINAL_bnd_;
329 ORIGINAL_bnd_ = nullPtr;
401 std::vector<Ptr<ParameterList>> conList;
403 risk_ = !needRiskLessObj_;
405 needRiskLessCon_.clear();
407 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
408 auto it2 = conList_.find(it->first);
409 if (it2==conList_.end()) {
410 conList.push_back(nullPtr);
411 needRiskLessCon_.push_back(
true);
414 conList.push_back(std::get<0>(it2->second));
415 needRiskLessCon_.push_back(std::get<1>(it2->second));
416 flag = std::get<1>(it2->second);
418 dynamicPtrCast<StochasticConstraint<Real>>(it->second.constraint)->setIndex(cnt);
422 statMap_.insert({it->first,cnt});
427 if (needRiskLessObj_) {
428 Ptr<Objective<Real>> obj = INPUT_obj_;
429 INPUT_obj_ = makePtr<RiskLessObjective<Real>>(obj);
432 ORIGINAL_xprim_ = INPUT_xprim_;
433 INPUT_xprim_ = makePtr<RiskVector<Real>>(objList_,conList,ORIGINAL_xprim_);
434 ORIGINAL_xdual_ = INPUT_xdual_;
435 INPUT_xdual_ = makePtr<RiskVector<Real>>(objList_,conList,ORIGINAL_xdual_);
436 if (objList_ != nullPtr) {
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);
447 if (INPUT_bnd_ != nullPtr) {
448 ORIGINAL_bnd_ = INPUT_bnd_;
449 INPUT_bnd_ = makePtr<RiskBoundConstraint<Real>>(objList_,conList,ORIGINAL_bnd_);
453 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
454 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
455 if (needRiskLessCon_[cnt]) {
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;
461 if (ORIGINAL_con_.count(it->first) == size_t(0))
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();
477 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
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;
483 if (ORIGINAL_linear_con_.count(it->first) == size_t(0))
484 ORIGINAL_linear_con_.insert({it->first,
ConstraintData<Real>(it->second.constraint,mul,res,bnd)});
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;
506 if (needRiskLessObj_ && ORIGINAL_obj_ != nullPtr) {
507 INPUT_obj_ = ORIGINAL_obj_;
508 ORIGINAL_obj_ = nullPtr;
510 if (ORIGINAL_xprim_ != nullPtr) INPUT_xprim_ = ORIGINAL_xprim_;
511 if (ORIGINAL_xdual_ != nullPtr) INPUT_xdual_ = ORIGINAL_xdual_;
512 if (ORIGINAL_bnd_ != nullPtr) INPUT_bnd_ = ORIGINAL_bnd_;
513 ORIGINAL_xprim_ = nullPtr;
514 ORIGINAL_xdual_ = nullPtr;
515 ORIGINAL_bnd_ = nullPtr;
518 std::unordered_map<std::string,ConstraintData<Real>> riskless_con;
519 for (
auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
520 if (needRiskLessCon_[cnt]) {
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) {
530 auto it2 = ORIGINAL_con_.find(it->first);
531 if (it2 != ORIGINAL_con_.end()) {
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;
539 ORIGINAL_con_.erase(it2);
543 riskless_con.clear();
544 for (
auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
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) {
552 auto it2 = ORIGINAL_linear_con_.find(it->first);
553 if (it2 != ORIGINAL_linear_con_.end()) {
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;
561 ORIGINAL_linear_con_.erase(it2);
566 needRiskLessCon_.clear();