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 
47 namespace ROL {
48 
49 template<typename Real>
51  const Ptr<Vector<Real>> &x,
52  const Ptr<Vector<Real>> &g)
53  : Problem<Real>(obj,x,g), needRiskLessObj_(true) {}
54 
55 template<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 
98 template<typename Real>
100  ParameterList &list,
101  const Ptr<SampleGenerator<Real>> &sampler,
102  const Ptr<BatchManager<Real>> &bman) {
103  // Throw an exception if problem has been finalized
104  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
105  ">>> ROL::StochasticProblem::makeConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
106  // Throw an exception if the value sampler is null
107  ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
108  ">>> ROL::StochasticProblem::makeConstraintStochastic: Constraint sampler is null!");
109  // Store original constraint for reuse later
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});
116  // Determine Stochastic Constraint Type
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);
127  }
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);
136  }
137  mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
138  res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
139  if (bnd != nullPtr)
140  bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
141  }
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);
150  }
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);
154  }
155  else {
156  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
157  ">>> ROL::StochasticProblem::makeConstraintStochastic: Invalid stochastic optimization type!");
158  }
160  if(bnd != nullPtr) Problem<Real>::addConstraint(name,con,mul,bnd,res);
161  else Problem<Real>::addConstraint(name,con,mul,res);
162 }
163 
164 template<typename Real>
166  ParameterList &list,
167  const Ptr<SampleGenerator<Real>> &sampler,
168  const Ptr<BatchManager<Real>> &bman) {
169  // Throw an exception if problem has been finalized
170  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
171  ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Cannot set stochastic constraint after problem has been finalized!");
172  // Throw an exception if the value sampler is null
173  ROL_TEST_FOR_EXCEPTION(sampler == nullPtr,std::invalid_argument,
174  ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Constraint sampler is null!");
175  // Store original constraint for reuse later
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});
182  // Determine Stochastic Constraint Type
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);
192  }
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);
200  }
201  mul = makePtr<DualSimulatedVector<Real>>(mvec,sampler->getBatchManager(),sampler);
202  res = makePtr<PrimalSimulatedVector<Real>>(rvec,sampler->getBatchManager(),sampler);
203  if (bnd != nullPtr)
204  bnd = makePtr<SimulatedBoundConstraint<Real>>(sampler, bnd);
205  }
206  else if ( type == "Mean Value" ) {
207  con = makePtr<MeanValueConstraint<Real>>(it->second.constraint,sampler);
208  }
209  else {
210  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
211  ">>> ROL::StochasticProblem::makeLinearConstraintStochastic: Invalid stochastic optimization type!");
212  }
214  if(bnd != nullPtr) Problem<Real>::addLinearConstraint(name,con,mul,bnd,res);
215  else Problem<Real>::addLinearConstraint(name,con,mul,res);
216 }
217 
218 template<typename Real>
220  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
221  ">>> ROL::StochasticProblem::resetStochasticObjective: Cannot reset stochastic objective after problem has been finalized!");
222  if (ORIGINAL_obj_ != nullPtr) {
223  INPUT_obj_ = ORIGINAL_obj_;
224  needRiskLessObj_ = true;
225  objList_ = nullPtr;
226  }
227  ORIGINAL_obj_ = nullPtr;
228 }
229 
230 template<typename Real>
232  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
233  ">>> ROL::StochasticProblem::resetStochasticConstraint: Cannot reset stochastic constraint after problem has been finalized!");
234  auto it = ORIGINAL_con_.find(name);
235  if (it != ORIGINAL_con_.end()) {
236  Ptr<Constraint<Real>> con = it->second.constraint;
237  Ptr<Vector<Real>> mul = it->second.multiplier;
238  Ptr<Vector<Real>> res = it->second.residual;
239  Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
241  if (bnd != nullPtr) Problem<Real>::addConstraint(name,con,mul,bnd,res);
242  else Problem<Real>::addConstraint(name,con,mul,res);
243  conList_.erase(conList_.find(name));
244  ORIGINAL_con_.erase(it);
245  }
246 }
247 
248 template<typename Real>
250  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
251  ">>> ROL::StochasticProblem::resetStochasticLinearConstraint: Cannot reset stochastic constraint after problem has been finalized!");
252  auto it = ORIGINAL_linear_con_.find(name);
253  if (it != ORIGINAL_linear_con_.end()) {
254  Ptr<Constraint<Real>> con = it->second.constraint;
255  Ptr<Vector<Real>> mul = it->second.multiplier;
256  Ptr<Vector<Real>> res = it->second.residual;
257  Ptr<BoundConstraint<Real>> bnd = it->second.bounds;
259  if (bnd != nullPtr) Problem<Real>::addLinearConstraint(name,con,mul,bnd,res);
260  else Problem<Real>::addLinearConstraint(name,con,mul,res);
261  ORIGINAL_linear_con_.erase(it);
262  }
263 }
264 
265 template<typename Real>
267  ROL_TEST_FOR_EXCEPTION(isFinalized(),std::invalid_argument,
268  ">>> ROL::StochasticProblem::reset: Cannot reset stochastic problem after problem has been finalized!");
269  // Reset objective
270  resetStochasticObjective();
271  // Reset general constraints
272  std::vector<std::string> names;
273  for (auto it = INPUT_con_.begin(); it != INPUT_con_.end(); ++it) {
274  names.push_back(it->first);
275  }
276  for (auto it = names.begin(); it != names.end(); ++it) {
277  resetStochasticConstraint(*it);
278  }
279  // Reset linear constraints
280  names.clear();
281  for (auto it = INPUT_linear_con_.begin(); it != INPUT_linear_con_.end(); ++it) {
282  names.push_back(it->first);
283  }
284  for (auto it = names.begin(); it != names.end(); ++it) {
285  resetStochasticLinearConstraint(*it);
286  }
287  // Reset primal optimization variables
288  if (ORIGINAL_xprim_ != nullPtr) {
289  INPUT_xprim_ = ORIGINAL_xprim_;
290  ORIGINAL_xprim_ = nullPtr;
291  }
292  // Reset dual optimization variables
293  if (ORIGINAL_xdual_ != nullPtr) {
294  INPUT_xdual_ = ORIGINAL_xdual_;
295  ORIGINAL_xdual_ = nullPtr;
296  }
297  // Reset bound constraint
298  if (ORIGINAL_bnd_ != nullPtr) {
299  INPUT_bnd_ = ORIGINAL_bnd_;
300  ORIGINAL_bnd_ = nullPtr;
301  }
302 }
303 
304 template<typename Real>
305 std::vector<Real> StochasticProblem<Real>::getObjectiveStatistic(void) const {
306  ROL_TEST_FOR_EXCEPTION(!isFinalized(),std::invalid_argument,
307  ">>> ROL::StochasticProblem::getObjectiveStatistic: Cannot get statistic if problem has not been finalized!");
308  try {
309  Ptr<std::vector<Real>> stat
310  = dynamicPtrCast<RiskVector<Real>>(INPUT_xprim_)->getStatistic();
311  if (stat != nullPtr) return *stat;
312  else return std::vector<Real>();
313  }
314  catch (std::exception &e) {
315  return std::vector<Real>();
316  }
317 }
318 
319 template<typename Real>
320 std::vector<Real> StochasticProblem<Real>::getConstraintStatistic(std::string name) const {
321  ROL_TEST_FOR_EXCEPTION(!isFinalized(),std::invalid_argument,
322  ">>> ROL::StochasticProblem::getConstraintStatistic: Cannot get statistic if problem has not been finalized!");
323  auto it = statMap_.find(name);
324  ROL_TEST_FOR_EXCEPTION(it==statMap_.end(),std::invalid_argument,
325  ">>> ROL::StochasticProblem::getConstraintStatistic: Constraint does not exist!");
326  try {
327  Ptr<std::vector<Real>> stat
328  = dynamicPtrCast<RiskVector<Real>>(INPUT_xprim_)->getStatistic(1,it->second);
329  if (stat != nullPtr) return *stat;
330  else return std::vector<Real>();
331  }
332  catch (std::exception &e) {
333  return std::vector<Real>();
334  }
335 }
336 
337 template<typename Real>
338 Real StochasticProblem<Real>::getSolutionStatistic(int comp, std::string name) const {
339  ROL_TEST_FOR_EXCEPTION(!isFinalized(),std::invalid_argument,
340  ">>> ROL::StochasticProblem::getConstraintStatistic: Cannot get statistic if problem has not been finalized!");
341  ROL_TEST_FOR_EXCEPTION(comp>1||comp<0,std::invalid_argument,
342  ">>> ROL::StochasticProblem::getSolutionStatistic: Component must be either 0 or 1!");
343  Real val(0);
344  if (comp == 0) {
345  try {
346  val = dynamicPtrCast<StochasticObjective<Real>>(INPUT_obj_)->computeStatistic(*INPUT_xprim_);
347  }
348  catch (std::exception &e) {
349  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
350  ">>> ROL::StochasticProblem::getSolutionStatistic: Objective does not have a computeStatistic function!");
351  }
352  }
353  else {
354  auto it = statMap_.find(name);
355  ROL_TEST_FOR_EXCEPTION(it==statMap_.end(),std::invalid_argument,
356  ">>> ROL::StochasticProblem::getSolutionStatistic: Constraint does not exist!");
357  try {
358  auto it2 = INPUT_con_.find(name);
359  val = dynamicPtrCast<StochasticConstraint<Real>>(it2->second.constraint)->computeStatistic(*INPUT_xprim_);
360  }
361  catch (std::exception &e) {
362  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
363  ">>> ROL::StochasticProblem::getSolutionStatistic: Constraint does not have a computeStatistic function!");
364  }
365  }
366  return val;
367 }
368 
369 template<typename Real>
370 void StochasticProblem<Real>::finalize(bool lumpConstraints, bool printToStream, std::ostream &outStream) {
372  std::vector<Ptr<ParameterList>> conList;
373  bool flag(true);
374  risk_ = !needRiskLessObj_;
375  size_t cnt(0);
376  needRiskLessCon_.clear();
377  statMap_.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);
383  }
384  else {
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);
388  if (!flag) {
389  dynamicPtrCast<StochasticConstraint<Real>>(it->second.constraint)->setIndex(cnt);
390  risk_ = true;
391  }
392  }
393  statMap_.insert({it->first,cnt});
394  cnt++;
395  }
396  // Set objective function
397  if (risk_) {
398  if (needRiskLessObj_) {
399  Ptr<Objective<Real>> obj = INPUT_obj_;
400  INPUT_obj_ = makePtr<RiskLessObjective<Real>>(obj);
401  }
402  // Set risk vector
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);
410  }
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);
415  }
416  }
417  // Set risk bound constraint
418  if (INPUT_bnd_ != nullPtr) {
419  ORIGINAL_bnd_ = INPUT_bnd_;
420  INPUT_bnd_ = makePtr<RiskBoundConstraint<Real>>(objList_,conList,ORIGINAL_bnd_);
421  }
422  // Set appropriate general constraints to be risk less
423  cnt = 0;
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;
431  riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
432  if (ORIGINAL_con_.count(it->first) == size_t(0))
433  ORIGINAL_con_.insert({it->first,ConstraintData<Real>(it->second.constraint,mul,res,bnd)});
434  }
435  cnt++;
436  }
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;
443  if (bnd != nullPtr) Problem<Real>::addConstraint(it->first,con,mul,bnd,res);
444  else Problem<Real>::addConstraint(it->first,con,mul,res);
445  }
446  // Set all linear constraints to be risk less
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;
453  riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
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)});
456  }
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;
463  if (bnd != nullPtr) Problem<Real>::addLinearConstraint(it->first,con,mul,bnd,res);
464  else Problem<Real>::addLinearConstraint(it->first,con,mul,res);
465  }
466  }
467  // Call default finalize
468  Problem<Real>::finalize(lumpConstraints,printToStream,outStream);
469  }
470 }
471 
472 template<typename Real>
475 
476  if (risk_) {
477  if (needRiskLessObj_ && ORIGINAL_obj_ != nullPtr) {
478  INPUT_obj_ = ORIGINAL_obj_;
479  ORIGINAL_obj_ = nullPtr;
480  }
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;
487  size_t cnt = 0;
488 
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;
496  riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
497  }
498  cnt++;
499  }
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;
508  if (bnd != nullPtr) Problem<Real>::addConstraint(it2->first,con,mul,bnd,res);
509  else Problem<Real>::addConstraint(it2->first,con,mul,res);
510  ORIGINAL_con_.erase(it2);
511  }
512  }
513  // Set all linear constraints to be risk less
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;
520  riskless_con.insert({it->first,ConstraintData<Real>(con,mul,res,bnd)});
521  }
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;
530  if (bnd != nullPtr) Problem<Real>::addLinearConstraint(it2->first,con,mul,bnd,res);
531  else Problem<Real>::addLinearConstraint(it2->first,con,mul,res);
532  ORIGINAL_linear_con_.erase(it2);
533  }
534  }
535  }
536  risk_ = false;
537  needRiskLessCon_.clear();
538  statMap_.clear();
539 }
540 
541 } // namespace ROL
542 
543 #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)
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.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
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...
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.
std::vector< Real > getObjectiveStatistic(void) const
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...
std::vector< Real > getConstraintStatistic(std::string name) const
Real getSolutionStatistic(int comp=0, std::string name="") const