ROL
ROL_StochasticProblem_Def.hpp
Go to the documentation of this file.
1// @HEADER
2// ************************************************************************
3//
4// Rapid Optimization Library (ROL) Package
5// Copyright (2014) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact lead developers:
38// Drew Kouri (dpkouri@sandia.gov) and
39// Denis Ridzal (dridzal@sandia.gov)
40//
41// ************************************************************************
42// @HEADER
43
44#ifndef ROL_STOCHASTICPROBLEM_DEF_HPP
45#define ROL_STOCHASTICPROBLEM_DEF_HPP
46
47namespace ROL {
48
49template<typename Real>
51 const Ptr<Vector<Real>> &x,
52 const Ptr<Vector<Real>> &g)
53 : Problem<Real>(obj,x,g), needRiskLessObj_(true) {}
54
55template<typename Real>
57 const Ptr<SampleGenerator<Real>> &fsampler,
58 const Ptr<SampleGenerator<Real>> &gsampler,
59 const Ptr<SampleGenerator<Real>> &hsampler) {
60 // Throw an exception if problem has been finalized
61 ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
62 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Cannot set stochastic objective after problem has been finalized!");
63 // Throw an exception if the value sampler is null
64 ROL_TEST_FOR_EXCEPTION(fsampler == nullPtr,std::invalid_argument,
65 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Objective function value sampler is null!");
66 // Store original objective function for reuse later
67 ORIGINAL_obj_ = INPUT_obj_;
68 // Check samplers
69 Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
70 _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
71 _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
72 // Determine Stochastic Objective Type
73 std::string type = list.sublist("SOL").sublist("Objective").get("Type","Risk Neutral");
74 if ( type == "Risk Neutral" ) {
75 needRiskLessObj_ = true;
76 objList_ = nullPtr;
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);
79 }
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);
86 }
87 else if ( type == "Mean Value" ) {
88 needRiskLessObj_ = true;
89 objList_ = nullPtr;
90 INPUT_obj_ = makePtr<MeanValueObjective<Real>>(ORIGINAL_obj_,fsampler);
91 }
92 else {
93 ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
94 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Invalid stochastic optimization type!");
95 }
96}
97
98template<typename Real>
100 ParameterList &list,
101 const Ptr<SampleGenerator<Real>> &fsampler,
102 const Ptr<SampleGenerator<Real>> &gsampler,
103 const Ptr<SampleGenerator<Real>> &hsampler) {
104 // Throw an exception if problem has been finalized
105 ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
106 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Cannot set stochastic objective after problem has been finalized!");
107 // Throw an exception if the value sampler is null
108 ROL_TEST_FOR_EXCEPTION(fsampler == nullPtr,std::invalid_argument,
109 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Objective function value sampler is null!");
110 // Throw an exception if the value sampler is null
111 ROL_TEST_FOR_EXCEPTION(rvf == nullPtr,std::invalid_argument,
112 ">>> ROL::StochasticProblem::makeObjectiveStochastic: Risk measure is null!");
113 // Store original objective function for reuse later
114 ORIGINAL_obj_ = INPUT_obj_;
115 // Check samplers
116 Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
117 _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
118 _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
119 // Determine Stochastic Objective Type
120 needRiskLessObj_ = false;
121 objList_ = makePtr<ParameterList>();
122 *objList_ = list;
123 //objList_->sublist("SOL") = list.sublist("SOL").sublist("Objective");
124 INPUT_obj_ = makePtr<StochasticObjective<Real>>(ORIGINAL_obj_,rvf,fsampler,_gsampler,_hsampler);
125}
126
127template<typename Real>
129 ParameterList &list,
130 const Ptr<SampleGenerator<Real>> &sampler,
131 const Ptr<BatchManager<Real>> &bman) {
132 // Throw an exception if problem has been finalized
133 ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
134 ">>> ROL::StochasticProblem::makeConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
135 // Throw an exception if the value sampler is null
136 ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
137 ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint sampler is null!");
138 // Store original constraint for reuse later
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});
145 // Determine Stochastic Constraint Type
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);
156 }
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);
165 }
166 mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
167 res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
168 if (bnd != nullPtr)
169 bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
170 }
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);
179 }
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);
183 }
184 else {
185 ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
186 ">>> ROL::StochasticProblem::makeConstraintStochastic: Invalid stochastic optimization type!");
187 }
189 if(bnd != nullPtr) Problem<Real>::addConstraint(name,con,mul,bnd,res);
190 else Problem<Real>::addConstraint(name,con,mul,res);
191}
192
193template<typename Real>
195 ParameterList &list,
196 const Ptr<SampleGenerator<Real>> &sampler,
197 const Ptr<BatchManager<Real>> &bman) {
198 // Throw an exception if problem has been finalized
199 ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
200 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
201 // Throw an exception if the value sampler is null
202 ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
203 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint sampler is null!");
204 // Store original constraint for reuse later
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});
211 // Determine Stochastic Constraint Type
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);
221 }
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);
229 }
230 mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
231 res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
232 if (bnd != nullPtr)
233 bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
234 }
235 else if ( type == "Mean Value" ) {
236 con = makePtr<MeanValueConstraint<Real>>(it->second.constraint,sampler);
237 }
238 else {
239 ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
240 ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Invalid stochastic optimization type!");
241 }
243 if(bnd != nullPtr) Problem<Real>::addLinearConstraint(name,con,mul,bnd,res);
244 else Problem<Real>::addLinearConstraint(name,con,mul,res);
245}
246
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!");
251 if (ORIGINAL_obj_ != nullPtr) {
252 INPUT_obj_ = ORIGINAL_obj_;
253 needRiskLessObj_ = true;
254 objList_ = nullPtr;
255 }
256 ORIGINAL_obj_ = nullPtr;
257}
258
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!");
263 auto it = ORIGINAL_con_.find(name);
264 if (it != ORIGINAL_con_.end()) {
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;
270 if (bnd != nullPtr) Problem<Real>::addConstraint(name,con,mul,bnd,res);
271 else Problem<Real>::addConstraint(name,con,mul,res);
272 conList_.erase(conList_.find(name));
273 ORIGINAL_con_.erase(it);
274 }
275}
276
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!");
281 auto it = ORIGINAL_linear_con_.find(name);
282 if (it != ORIGINAL_linear_con_.end()) {
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;
288 if (bnd != nullPtr) Problem<Real>::addLinearConstraint(name,con,mul,bnd,res);
289 else Problem<Real>::addLinearConstraint(name,con,mul,res);
290 ORIGINAL_linear_con_.erase(it);
291 }
292}
293
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!");
298 // Reset objective
299 resetStochasticObjective();
300 // Reset general constraints
301 std::vector<std::string> names;
302 for (auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
303 names.push_back(it->first);
304 }
305 for (auto it = names.begin(); it != names.end(); ++it) {
306 resetStochasticConstraint(*it);
307 }
308 // Reset linear constraints
309 names.clear();
310 for (auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
311 names.push_back(it->first);
312 }
313 for (auto it = names.begin(); it != names.end(); ++it) {
314 resetStochasticLinearConstraint(*it);
315 }
316 // Reset primal optimization variables
317 if (ORIGINAL_xprim_ != nullPtr) {
318 INPUT_xprim_ = ORIGINAL_xprim_;
319 ORIGINAL_xprim_ = nullPtr;
320 }
321 // Reset dual optimization variables
322 if (ORIGINAL_xdual_ != nullPtr) {
323 INPUT_xdual_ = ORIGINAL_xdual_;
324 ORIGINAL_xdual_ = nullPtr;
325 }
326 // Reset bound constraint
327 if (ORIGINAL_bnd_ != nullPtr) {
328 INPUT_bnd_ = ORIGINAL_bnd_;
329 ORIGINAL_bnd_ = nullPtr;
330 }
331}
332
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!");
337 try {
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>();
342 }
343 catch (std::exception &e) {
344 return std::vector<Real>();
345 }
346}
347
348template<typename Real>
349std::vector<Real> StochasticProblem<Real>::getConstraintStatistic(std::string name) const {
350 ROL_TEST_FOR_EXCEPTION(!isFinalized(),std::invalid_argument,
351 ">>> ROL::StochasticProblem::getConstraintStatistic: Cannot get statistic if problem has not been finalized!");
352 auto it = statMap_.find(name);
353 ROL_TEST_FOR_EXCEPTION(it==statMap_.end(),std::invalid_argument,
354 ">>> ROL::StochasticProblem::getConstraintStatistic: Constraint does not exist!");
355 try {
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>();
360 }
361 catch (std::exception &e) {
362 return std::vector<Real>();
363 }
364}
365
366template<typename Real>
367Real StochasticProblem<Real>::getSolutionStatistic(int comp, std::string name) const {
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!");
372 Real val(0);
373 if (comp == 0) {
374 try {
375 val = dynamicPtrCast<StochasticObjective<Real>>(INPUT_obj_)->computeStatistic(*INPUT_xprim_);
376 }
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!");
380 }
381 }
382 else {
383 auto it = statMap_.find(name);
384 ROL_TEST_FOR_EXCEPTION(it==statMap_.end(),std::invalid_argument,
385 ">>> ROL::StochasticProblem::getSolutionStatistic: Constraint does not exist!");
386 try {
387 auto it2 = INPUT_con_.find(name);
388 val = dynamicPtrCast<StochasticConstraint<Real>>(it2->second.constraint)->computeStatistic(*INPUT_xprim_);
389 }
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!");
393 }
394 }
395 return val;
396}
397
398template<typename Real>
399void StochasticProblem<Real>::finalize(bool lumpConstraints, bool printToStream, std::ostream &outStream) {
401 std::vector<Ptr<ParameterList>> conList;
402 bool flag(true);
403 risk_ = !needRiskLessObj_;
404 size_t cnt(0);
405 needRiskLessCon_.clear();
406 statMap_.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);
412 }
413 else {
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);
417 if (!flag) {
418 dynamicPtrCast<StochasticConstraint<Real>>(it->second.constraint)->setIndex(cnt);
419 risk_ = true;
420 }
421 }
422 statMap_.insert({it->first,cnt});
423 cnt++;
424 }
425 // Set objective function
426 if (risk_) {
427 if (needRiskLessObj_) {
428 Ptr<Objective<Real>> obj = INPUT_obj_;
429 INPUT_obj_ = makePtr<RiskLessObjective<Real>>(obj);
430 }
431 // Set risk vector
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);
439 }
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);
444 }
445 }
446 // Set risk bound constraint
447 if (INPUT_bnd_ != nullPtr) {
448 ORIGINAL_bnd_ = INPUT_bnd_;
449 INPUT_bnd_ = makePtr<RiskBoundConstraint<Real>>(objList_,conList,ORIGINAL_bnd_);
450 }
451 // Set appropriate general constraints to be risk less
452 cnt = 0;
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;
460 riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
461 if (ORIGINAL_con_.count(it->first) == size_t(0))
462 ORIGINAL_con_.insert({it->first,ConstraintData<Real>(it->second.constraint,mul,res,bnd)});
463 }
464 cnt++;
465 }
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;
472 if (bnd != nullPtr) Problem<Real>::addConstraint(it->first,con,mul,bnd,res);
473 else Problem<Real>::addConstraint(it->first,con,mul,res);
474 }
475 // Set all linear constraints to be risk less
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;
482 riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
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)});
485 }
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;
492 if (bnd != nullPtr) Problem<Real>::addLinearConstraint(it->first,con,mul,bnd,res);
493 else Problem<Real>::addLinearConstraint(it->first,con,mul,res);
494 }
495 }
496 // Call default finalize
497 Problem<Real>::finalize(lumpConstraints,printToStream,outStream);
498 }
499}
500
501template<typename Real>
504
505 if (risk_) {
506 if (needRiskLessObj_ && ORIGINAL_obj_ != nullPtr) {
507 INPUT_obj_ = ORIGINAL_obj_;
508 ORIGINAL_obj_ = nullPtr;
509 }
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;
516 size_t cnt = 0;
517
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;
525 riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
526 }
527 cnt++;
528 }
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;
537 if (bnd != nullPtr) Problem<Real>::addConstraint(it2->first,con,mul,bnd,res);
538 else Problem<Real>::addConstraint(it2->first,con,mul,res);
539 ORIGINAL_con_.erase(it2);
540 }
541 }
542 // Set all linear constraints to be risk less
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;
549 riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
550 }
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;
559 if (bnd != nullPtr) Problem<Real>::addLinearConstraint(it2->first,con,mul,bnd,res);
560 else Problem<Real>::addLinearConstraint(it2->first,con,mul,res);
561 ORIGINAL_linear_con_.erase(it2);
562 }
563 }
564 }
565 risk_ = false;
566 needRiskLessCon_.clear();
567 statMap_.clear();
568}
569
570} // namespace ROL
571
572#endif // ROL_STOCHASTICPROBLEM_DEF_HPP
Provides the interface to evaluate objective functions.
void removeConstraint(std::string name)
Remove an existing constraint.
void removeLinearConstraint(std::string name)
Remove an existing linear constraint.
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.
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.
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...
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)
void resetStochasticLinearConstraint(std::string name)
void resetStochasticConstraint(std::string name)
void makeLinearConstraintStochastic(std::string name, ParameterList &list, const Ptr< SampleGenerator< Real > > &sampler, const Ptr< BatchManager< Real > > &bman=nullPtr)
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.
void makeObjectiveStochastic(ParameterList &list, const Ptr< SampleGenerator< Real > > &fsampler, const Ptr< SampleGenerator< Real > > &gsampler=nullPtr, const Ptr< SampleGenerator< Real > > &hsampler=nullPtr)
Defines the linear algebra or vector space interface.