ROL
ROL_StochasticProblem_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_STOCHASTICPROBLEM_DEF_HPP
11 #define ROL_STOCHASTICPROBLEM_DEF_HPP
12 
13 namespace ROL {
14 
15 template<typename Real>
17  const Ptr<Vector<Real>> &x,
18  const Ptr<Vector<Real>> &g)
19  : Problem<Real>(obj,x,g), needRiskLessObj_(true) {}
20 
21 template<typename Real>
23  const Ptr<SampleGenerator<Real>> &fsampler,
24  const Ptr<SampleGenerator<Real>> &gsampler,
25  const Ptr<SampleGenerator<Real>> &hsampler) {
26  // Throw an exception if problem has been finalized
27  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
28  ">>> ROL::StochasticProblem::makeObjectiveStochastic: Cannot set stochastic objective after problem has been finalized!");
29  // Throw an exception if the value sampler is null
30  ROL_TEST_FOR_EXCEPTION(fsampler == nullPtr,std::invalid_argument,
31  ">>> ROL::StochasticProblem::makeObjectiveStochastic: Objective function value sampler is null!");
32  // Store original objective function for reuse later
33  ORIGINAL_obj_ = INPUT_obj_;
34  // Check samplers
35  Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
36  _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
37  _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
38  // Determine Stochastic Objective Type
39  std::string type = list.sublist("SOL").sublist("Objective").get("Type","Risk Neutral");
40  if ( type == "Risk Neutral" ) {
41  needRiskLessObj_ = true;
42  objList_ = nullPtr;
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);
45  }
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);
52  }
53  else if ( type == "Mean Value" ) {
54  needRiskLessObj_ = true;
55  objList_ = nullPtr;
56  INPUT_obj_ = makePtr<MeanValueObjective<Real>>(ORIGINAL_obj_,fsampler);
57  }
58  else {
59  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
60  ">>> ROL::StochasticProblem::makeObjectiveStochastic: Invalid stochastic optimization type!");
61  }
62 }
63 
64 template<typename Real>
66  ParameterList &list,
67  const Ptr<SampleGenerator<Real>> &fsampler,
68  const Ptr<SampleGenerator<Real>> &gsampler,
69  const Ptr<SampleGenerator<Real>> &hsampler) {
70  // Throw an exception if problem has been finalized
71  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
72  ">>> ROL::StochasticProblem::makeObjectiveStochastic: Cannot set stochastic objective after problem has been finalized!");
73  // Throw an exception if the value sampler is null
74  ROL_TEST_FOR_EXCEPTION(fsampler == nullPtr,std::invalid_argument,
75  ">>> ROL::StochasticProblem::makeObjectiveStochastic: Objective function value sampler is null!");
76  // Throw an exception if the value sampler is null
77  ROL_TEST_FOR_EXCEPTION(rvf == nullPtr,std::invalid_argument,
78  ">>> ROL::StochasticProblem::makeObjectiveStochastic: Risk measure is null!");
79  // Store original objective function for reuse later
80  ORIGINAL_obj_ = INPUT_obj_;
81  // Check samplers
82  Ptr<SampleGenerator<Real>> _gsampler, _hsampler;
83  _gsampler = (gsampler == nullPtr ? fsampler : gsampler);
84  _hsampler = (hsampler == nullPtr ? _gsampler : hsampler);
85  // Determine Stochastic Objective Type
86  needRiskLessObj_ = false;
87  objList_ = makePtr<ParameterList>();
88  *objList_ = list;
89  //objList_->sublist("SOL") = list.sublist("SOL").sublist("Objective");
90  INPUT_obj_ = makePtr<StochasticObjective<Real>>(ORIGINAL_obj_,rvf,fsampler,_gsampler,_hsampler);
91 }
92 
93 template<typename Real>
95  ParameterList &list,
96  const Ptr<SampleGenerator<Real>> &sampler,
97  const Ptr<BatchManager<Real>> &bman) {
98  // Throw an exception if problem has been finalized
99  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
100  ">>> ROL::StochasticProblem::makeConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
101  // Throw an exception if the value sampler is null
102  ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
103  ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint sampler is null!");
104  // Store original constraint for reuse later
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});
111  // Determine Stochastic Constraint Type
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);
122  }
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);
131  }
132  mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
133  res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
134  if (bnd != nullPtr)
135  bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
136  }
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);
145  }
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);
149  }
150  else {
151  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
152  ">>> ROL::StochasticProblem::makeConstraintStochastic: Invalid stochastic optimization type!");
153  }
155  if(bnd != nullPtr) Problem<Real>::addConstraint(name,con,mul,bnd,res);
156  else Problem<Real>::addConstraint(name,con,mul,res);
157 }
158 
159 template<typename Real>
161  ParameterList &list,
162  const Ptr<SampleGenerator<Real>> &sampler,
163  const Ptr<BatchManager<Real>> &bman) {
164  // Throw an exception if problem has been finalized
165  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
166  ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
167  // Throw an exception if the value sampler is null
168  ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
169  ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint sampler is null!");
170  // Store original constraint for reuse later
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});
177  // Determine Stochastic Constraint Type
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);
187  }
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);
195  }
196  mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
197  res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
198  if (bnd != nullPtr)
199  bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
200  }
201  else if ( type == "Mean Value" ) {
202  con = makePtr<MeanValueConstraint<Real>>(it->second.constraint,sampler);
203  }
204  else {
205  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
206  ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Invalid stochastic optimization type!");
207  }
209  if(bnd != nullPtr) Problem<Real>::addLinearConstraint(name,con,mul,bnd,res);
210  else Problem<Real>::addLinearConstraint(name,con,mul,res);
211 }
212 
213 template<typename Real>
215  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
216  ">>> ROL::StochasticProblem::resetStochasticObjective: Cannot reset stochastic objective after problem has been finalized!");
217  if (ORIGINAL_obj_ != nullPtr) {
218  INPUT_obj_ = ORIGINAL_obj_;
219  needRiskLessObj_ = true;
220  objList_ = nullPtr;
221  }
222  ORIGINAL_obj_ = nullPtr;
223 }
224 
225 template<typename Real>
227  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
228  ">>> ROL::StochasticProblem::resetStochasticConstraint: Cannot reset stochastic constraint after problem has been finalized!");
229  auto it = ORIGINAL_con_.find(name);
230  if (it != ORIGINAL_con_.end()) {
231  Ptr<Constraint<Real>> con = it->second.constraint;
232  Ptr<Vector<Real>> mul = it->second.multiplier;
233  Ptr<Vector<Real>> res = it->second.residual;
234  Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
236  if (bnd != nullPtr) Problem<Real>::addConstraint(name,con,mul,bnd,res);
237  else Problem<Real>::addConstraint(name,con,mul,res);
238  conList_.erase(conList_.find(name));
239  ORIGINAL_con_.erase(it);
240  }
241 }
242 
243 template<typename Real>
245  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
246  ">>> ROL::StochasticProblem::resetStochasticLinearConstraint: Cannot reset stochastic constraint after problem has been finalized!");
247  auto it = ORIGINAL_linear_con_.find(name);
248  if (it != ORIGINAL_linear_con_.end()) {
249  Ptr<Constraint<Real>> con = it->second.constraint;
250  Ptr<Vector<Real>> mul = it->second.multiplier;
251  Ptr<Vector<Real>> res = it->second.residual;
252  Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
254  if (bnd != nullPtr) Problem<Real>::addLinearConstraint(name,con,mul,bnd,res);
255  else Problem<Real>::addLinearConstraint(name,con,mul,res);
256  ORIGINAL_linear_con_.erase(it);
257  }
258 }
259 
260 template<typename Real>
262  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
263  ">>> ROL::StochasticProblem::reset: Cannot reset stochastic problem after problem has been finalized!");
264  // Reset objective
265  resetStochasticObjective();
266  // Reset general constraints
267  std::vector<std::string> names;
268  for (auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
269  names.push_back(it->first);
270  }
271  for (auto it = names.begin(); it != names.end(); ++it) {
272  resetStochasticConstraint(*it);
273  }
274  // Reset linear constraints
275  names.clear();
276  for (auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
277  names.push_back(it->first);
278  }
279  for (auto it = names.begin(); it != names.end(); ++it) {
280  resetStochasticLinearConstraint(*it);
281  }
282  // Reset primal optimization variables
283  if (ORIGINAL_xprim_ != nullPtr) {
284  INPUT_xprim_ = ORIGINAL_xprim_;
285  ORIGINAL_xprim_ = nullPtr;
286  }
287  // Reset dual optimization variables
288  if (ORIGINAL_xdual_ != nullPtr) {
289  INPUT_xdual_ = ORIGINAL_xdual_;
290  ORIGINAL_xdual_ = nullPtr;
291  }
292  // Reset bound constraint
293  if (ORIGINAL_bnd_ != nullPtr) {
294  INPUT_bnd_ = ORIGINAL_bnd_;
295  ORIGINAL_bnd_ = nullPtr;
296  }
297 }
298 
299 template<typename Real>
300 std::vector<Real> StochasticProblem<Real>::getObjectiveStatistic(void) const {
301  ROL_TEST_FOR_EXCEPTION(!isFinalized(),std::invalid_argument,
302  ">>> ROL::StochasticProblem::getObjectiveStatistic: Cannot get statistic if problem has not been finalized!");
303  try {
304  Ptr<std::vector<Real>> stat
305  = dynamicPtrCast<RiskVector<Real>>(INPUT_xprim_)->getStatistic();
306  if (stat != nullPtr) return *stat;
307  else return std::vector<Real>();
308  }
309  catch (std::exception &e) {
310  return std::vector<Real>();
311  }
312 }
313 
314 template<typename Real>
315 std::vector<Real> StochasticProblem<Real>::getConstraintStatistic(std::string name) const {
316  ROL_TEST_FOR_EXCEPTION(!isFinalized(),std::invalid_argument,
317  ">>> ROL::StochasticProblem::getConstraintStatistic: Cannot get statistic if problem has not been finalized!");
318  auto it = statMap_.find(name);
319  ROL_TEST_FOR_EXCEPTION(it==statMap_.end(),std::invalid_argument,
320  ">>> ROL::StochasticProblem::getConstraintStatistic: Constraint does not exist!");
321  try {
322  Ptr<std::vector<Real>> stat
323  = dynamicPtrCast<RiskVector<Real>>(INPUT_xprim_)->getStatistic(1,it->second);
324  if (stat != nullPtr) return *stat;
325  else return std::vector<Real>();
326  }
327  catch (std::exception &e) {
328  return std::vector<Real>();
329  }
330 }
331 
332 template<typename Real>
333 Real StochasticProblem<Real>::getSolutionStatistic(int comp, std::string name) const {
334  ROL_TEST_FOR_EXCEPTION(!isFinalized(),std::invalid_argument,
335  ">>> ROL::StochasticProblem::getConstraintStatistic: Cannot get statistic if problem has not been finalized!");
336  ROL_TEST_FOR_EXCEPTION(comp>1||comp<0,std::invalid_argument,
337  ">>> ROL::StochasticProblem::getSolutionStatistic: Component must be either 0 or 1!");
338  Real val(0);
339  if (comp == 0) {
340  try {
341  val = dynamicPtrCast<StochasticObjective<Real>>(INPUT_obj_)->computeStatistic(*INPUT_xprim_);
342  }
343  catch (std::exception &e) {
344  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
345  ">>> ROL::StochasticProblem::getSolutionStatistic: Objective does not have a computeStatistic function!");
346  }
347  }
348  else {
349  auto it = statMap_.find(name);
350  ROL_TEST_FOR_EXCEPTION(it==statMap_.end(),std::invalid_argument,
351  ">>> ROL::StochasticProblem::getSolutionStatistic: Constraint does not exist!");
352  try {
353  auto it2 = INPUT_con_.find(name);
354  val = dynamicPtrCast<StochasticConstraint<Real>>(it2->second.constraint)->computeStatistic(*INPUT_xprim_);
355  }
356  catch (std::exception &e) {
357  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
358  ">>> ROL::StochasticProblem::getSolutionStatistic: Constraint does not have a computeStatistic function!");
359  }
360  }
361  return val;
362 }
363 
364 template<typename Real>
365 void StochasticProblem<Real>::finalize(bool lumpConstraints, bool printToStream, std::ostream &outStream) {
367  std::vector<Ptr<ParameterList>> conList;
368  bool flag(true);
369  risk_ = !needRiskLessObj_;
370  size_t cnt(0);
371  needRiskLessCon_.clear();
372  statMap_.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);
378  }
379  else {
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);
383  if (!flag) {
384  dynamicPtrCast<StochasticConstraint<Real>>(it->second.constraint)->setIndex(cnt);
385  risk_ = true;
386  }
387  }
388  statMap_.insert({it->first,cnt});
389  cnt++;
390  }
391  // Set objective function
392  if (risk_) {
393  if (needRiskLessObj_) {
394  Ptr<Objective<Real>> obj = INPUT_obj_;
395  INPUT_obj_ = makePtr<RiskLessObjective<Real>>(obj);
396  }
397  // Set risk vector
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);
405  }
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);
410  }
411  }
412  // Set risk bound constraint
413  if (INPUT_bnd_ != nullPtr) {
414  ORIGINAL_bnd_ = INPUT_bnd_;
415  INPUT_bnd_ = makePtr<RiskBoundConstraint<Real>>(objList_,conList,ORIGINAL_bnd_);
416  }
417  // Set appropriate general constraints to be risk less
418  cnt = 0;
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;
426  riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
427  if (ORIGINAL_con_.count(it->first) == size_t(0))
428  ORIGINAL_con_.insert({it->first,ConstraintData<Real>(it->second.constraint,mul,res,bnd)});
429  }
430  cnt++;
431  }
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;
438  if (bnd != nullPtr) Problem<Real>::addConstraint(it->first,con,mul,bnd,res);
439  else Problem<Real>::addConstraint(it->first,con,mul,res);
440  }
441  // Set all linear constraints to be risk less
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;
448  riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
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)});
451  }
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;
458  if (bnd != nullPtr) Problem<Real>::addLinearConstraint(it->first,con,mul,bnd,res);
459  else Problem<Real>::addLinearConstraint(it->first,con,mul,res);
460  }
461  }
462  // Call default finalize
463  Problem<Real>::finalize(lumpConstraints,printToStream,outStream);
464  }
465 }
466 
467 template<typename Real>
470 
471  if (risk_) {
472  if (needRiskLessObj_ && ORIGINAL_obj_ != nullPtr) {
473  INPUT_obj_ = ORIGINAL_obj_;
474  ORIGINAL_obj_ = nullPtr;
475  }
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;
482  size_t cnt = 0;
483 
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;
491  riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
492  }
493  cnt++;
494  }
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;
503  if (bnd != nullPtr) Problem<Real>::addConstraint(it2->first,con,mul,bnd,res);
504  else Problem<Real>::addConstraint(it2->first,con,mul,res);
505  ORIGINAL_con_.erase(it2);
506  }
507  }
508  // Set all linear constraints to be risk less
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;
515  riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
516  }
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;
525  if (bnd != nullPtr) Problem<Real>::addLinearConstraint(it2->first,con,mul,bnd,res);
526  else Problem<Real>::addLinearConstraint(it2->first,con,mul,res);
527  ORIGINAL_linear_con_.erase(it2);
528  }
529  }
530  }
531  risk_ = false;
532  needRiskLessCon_.clear();
533  statMap_.clear();
534 }
535 
536 } // namespace ROL
537 
538 #endif // ROL_STOCHASTICPROBLEM_DEF_HPP
Provides the interface to evaluate objective functions.
void makeConstraintStochastic(std::string name, ParameterList &list, const Ptr< SampleGenerator< Real >> &sampler, const Ptr< BatchManager< Real >> &bman=nullPtr)
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 resetStochasticLinearConstraint(std::string name)
std::vector< Real > getConstraintStatistic(std::string name) const
void removeLinearConstraint(std::string name)
Remove an existing linear constraint.
StochasticProblem(const Ptr< Objective< Real >> &obj, const Ptr< Vector< Real >> &x, const Ptr< Vector< Real >> &g=nullPtr)
Default constructor for StochasticProblem.
Real getSolutionStatistic(int comp=0, std::string name="") const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
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...
std::vector< Real > getObjectiveStatistic(void) const
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.
void resetStochasticConstraint(std::string name)
void removeConstraint(std::string name)
Remove an existing constraint.
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
void edit(void) override
Resume editting optimization problem after finalize has been called.
virtual void edit()
Resume editting optimization problem after finalize has been called.
void makeObjectiveStochastic(ParameterList &list, const Ptr< SampleGenerator< Real >> &fsampler, const Ptr< SampleGenerator< Real >> &gsampler=nullPtr, const Ptr< SampleGenerator< Real >> &hsampler=nullPtr)
void makeLinearConstraintStochastic(std::string name, ParameterList &list, const Ptr< SampleGenerator< Real >> &sampler, const Ptr< BatchManager< Real >> &bman=nullPtr)
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...