ROL
ROL_StochasticProblem.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_HPP
45 #define ROL_STOCHASTICPROBLEM_HPP
46 
48 #include "ROL_SampleGenerator.hpp"
49 
50 // Risk-Neutral Includes
52 
53 // Risk-Averse Includes
55 #include "ROL_RiskVector.hpp"
57 
58 // Risk-Less Includes
60 
61 // BPOE Includes
62 #include "ROL_BPOEObjective.hpp"
63 
64 #include "Teuchos_ParameterList.hpp"
65 
66 namespace ROL {
67 
68 template<class Real>
69 class StochasticProblem : public OptimizationProblem<Real> {
70 private:
71  Teuchos::RCP<Teuchos::ParameterList> parlist_;
72 
73  Teuchos::RCP<Objective<Real> > ORIGINAL_obj_;
74  Teuchos::RCP<Vector<Real> > ORIGINAL_vec_;
75  Teuchos::RCP<BoundConstraint<Real> > ORIGINAL_bnd_;
76  Teuchos::RCP<EqualityConstraint<Real> > ORIGINAL_con_;
77 
78  Teuchos::RCP<Objective<Real> > obj_;
79  Teuchos::RCP<Vector<Real> > vec_;
80  Teuchos::RCP<BoundConstraint<Real> > bnd_;
81  Teuchos::RCP<EqualityConstraint<Real> > con_;
82 
83  Teuchos::RCP<SampleGenerator<Real> > vsampler_;
84  Teuchos::RCP<SampleGenerator<Real> > gsampler_;
85  Teuchos::RCP<SampleGenerator<Real> > hsampler_;
86 
87  bool setVector_;
88 
89 public:
91  : OptimizationProblem<Real>(),
92  parlist_(Teuchos::null),
93  ORIGINAL_obj_(Teuchos::null), ORIGINAL_vec_(Teuchos::null),
94  ORIGINAL_bnd_(Teuchos::null), ORIGINAL_con_(Teuchos::null),
95  obj_(Teuchos::null), vec_(Teuchos::null),
96  bnd_(Teuchos::null), con_(Teuchos::null),
97  vsampler_(Teuchos::null), gsampler_(Teuchos::null), hsampler_(Teuchos::null),
98  setVector_(false) {}
99 
100  StochasticProblem(Teuchos::ParameterList &parlist)
101  : OptimizationProblem<Real>(),
102  ORIGINAL_obj_(Teuchos::null), ORIGINAL_vec_(Teuchos::null),
103  ORIGINAL_bnd_(Teuchos::null), ORIGINAL_con_(Teuchos::null),
104  obj_(Teuchos::null), vec_(Teuchos::null),
105  bnd_(Teuchos::null), con_(Teuchos::null),
106  vsampler_(Teuchos::null), gsampler_(Teuchos::null), hsampler_(Teuchos::null),
107  setVector_(false) {
108  parlist_ = Teuchos::rcpFromRef(parlist);
109  }
110 
111  StochasticProblem(Teuchos::ParameterList &parlist,
112  const Teuchos::RCP<Objective<Real> > &obj,
113  const Teuchos::RCP<SampleGenerator<Real> > &sampler,
114  const Teuchos::RCP<Vector<Real> > &vec)
115  : OptimizationProblem<Real>(),
116  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
117  ORIGINAL_bnd_(Teuchos::null), ORIGINAL_con_(Teuchos::null),
118  obj_(Teuchos::null), vec_(Teuchos::null),
119  bnd_(Teuchos::null), con_(Teuchos::null),
120  vsampler_(sampler), gsampler_(sampler), hsampler_(sampler),
121  setVector_(false) {
122  parlist_ = Teuchos::rcpFromRef(parlist);
123  setObjective(obj);
124  setSolutionVector(vec);
125  setBoundConstraint(Teuchos::null);
126  setEqualityConstraint(Teuchos::null);
127  }
128 
129  StochasticProblem(Teuchos::ParameterList &parlist,
130  const Teuchos::RCP<Objective<Real> > &obj,
131  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
132  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
133  const Teuchos::RCP<Vector<Real> > &vec)
134  : OptimizationProblem<Real>(),
135  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
136  ORIGINAL_bnd_(Teuchos::null), ORIGINAL_con_(Teuchos::null),
137  obj_(Teuchos::null), vec_(Teuchos::null),
138  bnd_(Teuchos::null), con_(Teuchos::null),
139  vsampler_(vsampler), gsampler_(gsampler), hsampler_(gsampler),
140  setVector_(false) {
141  parlist_ = Teuchos::rcpFromRef(parlist);
142  setObjective(obj);
143  setSolutionVector(vec);
144  setBoundConstraint(Teuchos::null);
145  setEqualityConstraint(Teuchos::null);
146  }
147 
148  StochasticProblem(Teuchos::ParameterList &parlist,
149  const Teuchos::RCP<Objective<Real> > &obj,
150  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
151  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
152  const Teuchos::RCP<SampleGenerator<Real> > &hsampler,
153  const Teuchos::RCP<Vector<Real> > &vec)
154  : OptimizationProblem<Real>(),
155  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
156  ORIGINAL_bnd_(Teuchos::null), ORIGINAL_con_(Teuchos::null),
157  obj_(Teuchos::null), vec_(Teuchos::null),
158  bnd_(Teuchos::null), con_(Teuchos::null),
159  vsampler_(vsampler), gsampler_(gsampler), hsampler_(hsampler),
160  setVector_(false) {
161  parlist_ = Teuchos::rcpFromRef(parlist);
162  setObjective(obj);
163  setSolutionVector(vec);
164  setBoundConstraint(Teuchos::null);
165  setEqualityConstraint(Teuchos::null);
166  }
167 
168  StochasticProblem(Teuchos::ParameterList &parlist,
169  const Teuchos::RCP<Objective<Real> > &obj,
170  const Teuchos::RCP<SampleGenerator<Real> > &sampler,
171  const Teuchos::RCP<Vector<Real> > &vec,
172  const Teuchos::RCP<BoundConstraint<Real> > &bnd)
173  : OptimizationProblem<Real>(),
174  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
175  ORIGINAL_bnd_(bnd), ORIGINAL_con_(Teuchos::null),
176  obj_(Teuchos::null), vec_(Teuchos::null),
177  bnd_(Teuchos::null), con_(Teuchos::null),
178  vsampler_(sampler), gsampler_(sampler), hsampler_(sampler),
179  setVector_(false) {
180  parlist_ = Teuchos::rcpFromRef(parlist);
181  setObjective(obj);
182  setSolutionVector(vec);
183  setBoundConstraint(bnd);
184  setEqualityConstraint(Teuchos::null);
185  }
186 
187  StochasticProblem(Teuchos::ParameterList &parlist,
188  const Teuchos::RCP<Objective<Real> > &obj,
189  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
190  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
191  const Teuchos::RCP<Vector<Real> > &vec,
192  const Teuchos::RCP<BoundConstraint<Real> > &bnd)
193  : OptimizationProblem<Real>(),
194  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
195  ORIGINAL_bnd_(bnd), ORIGINAL_con_(Teuchos::null),
196  obj_(Teuchos::null), vec_(Teuchos::null),
197  bnd_(Teuchos::null), con_(Teuchos::null),
198  vsampler_(vsampler), gsampler_(gsampler), hsampler_(gsampler),
199  setVector_(false) {
200  parlist_ = Teuchos::rcpFromRef(parlist);
201  setObjective(obj);
202  setSolutionVector(vec);
203  setBoundConstraint(bnd);
204  setEqualityConstraint(Teuchos::null);
205  }
206 
207  StochasticProblem(Teuchos::ParameterList &parlist,
208  const Teuchos::RCP<Objective<Real> > &obj,
209  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
210  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
211  const Teuchos::RCP<SampleGenerator<Real> > &hsampler,
212  const Teuchos::RCP<Vector<Real> > &vec,
213  const Teuchos::RCP<BoundConstraint<Real> > &bnd)
214  : OptimizationProblem<Real>(),
215  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
216  ORIGINAL_bnd_(bnd), ORIGINAL_con_(Teuchos::null),
217  obj_(Teuchos::null), vec_(Teuchos::null),
218  bnd_(Teuchos::null), con_(Teuchos::null),
219  vsampler_(vsampler), gsampler_(gsampler), hsampler_(hsampler),
220  setVector_(false) {
221  parlist_ = Teuchos::rcpFromRef(parlist);
222  setObjective(obj);
223  setSolutionVector(vec);
224  setBoundConstraint(bnd);
225  setEqualityConstraint(Teuchos::null);
226  }
227 
228  StochasticProblem(Teuchos::ParameterList &parlist,
229  const Teuchos::RCP<Objective<Real> > &obj,
230  const Teuchos::RCP<SampleGenerator<Real> > &sampler,
231  const Teuchos::RCP<Vector<Real> > &vec,
232  const Teuchos::RCP<EqualityConstraint<Real> > &con)
233  : OptimizationProblem<Real>(),
234  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
235  ORIGINAL_bnd_(Teuchos::null), ORIGINAL_con_(con),
236  obj_(Teuchos::null), vec_(Teuchos::null),
237  bnd_(Teuchos::null), con_(Teuchos::null),
238  vsampler_(sampler), gsampler_(sampler), hsampler_(sampler),
239  setVector_(false) {
240  parlist_ = Teuchos::rcpFromRef(parlist);
241  setObjective(obj);
242  setSolutionVector(vec);
243  setBoundConstraint(Teuchos::null);
245  }
246 
247  StochasticProblem(Teuchos::ParameterList &parlist,
248  const Teuchos::RCP<Objective<Real> > &obj,
249  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
250  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
251  const Teuchos::RCP<Vector<Real> > &vec,
252  const Teuchos::RCP<EqualityConstraint<Real> > &con)
253  : OptimizationProblem<Real>(),
254  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
255  ORIGINAL_bnd_(Teuchos::null), ORIGINAL_con_(con),
256  obj_(Teuchos::null), vec_(Teuchos::null),
257  bnd_(Teuchos::null), con_(Teuchos::null),
258  vsampler_(vsampler), gsampler_(gsampler), hsampler_(gsampler),
259  setVector_(false) {
260  parlist_ = Teuchos::rcpFromRef(parlist);
261  setObjective(obj);
262  setSolutionVector(vec);
263  setBoundConstraint(Teuchos::null);
265  }
266 
267  StochasticProblem(Teuchos::ParameterList &parlist,
268  const Teuchos::RCP<Objective<Real> > &obj,
269  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
270  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
271  const Teuchos::RCP<SampleGenerator<Real> > &hsampler,
272  const Teuchos::RCP<Vector<Real> > &vec,
273  const Teuchos::RCP<EqualityConstraint<Real> > &con)
274  : OptimizationProblem<Real>(),
275  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
276  ORIGINAL_bnd_(Teuchos::null), ORIGINAL_con_(con),
277  obj_(Teuchos::null), vec_(Teuchos::null),
278  bnd_(Teuchos::null), con_(Teuchos::null),
279  vsampler_(vsampler), gsampler_(gsampler), hsampler_(hsampler),
280  setVector_(false) {
281  parlist_ = Teuchos::rcpFromRef(parlist);
282  setObjective(obj);
283  setSolutionVector(vec);
284  setBoundConstraint(Teuchos::null);
286  }
287 
288  StochasticProblem(Teuchos::ParameterList &parlist,
289  const Teuchos::RCP<Objective<Real> > &obj,
290  const Teuchos::RCP<SampleGenerator<Real> > &sampler,
291  const Teuchos::RCP<Vector<Real> > &vec,
292  const Teuchos::RCP<BoundConstraint<Real> > &bnd,
293  const Teuchos::RCP<EqualityConstraint<Real> > &con)
294  : OptimizationProblem<Real>(),
295  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
296  ORIGINAL_bnd_(bnd), ORIGINAL_con_(con),
297  obj_(Teuchos::null), vec_(Teuchos::null),
298  bnd_(Teuchos::null), con_(Teuchos::null),
299  vsampler_(sampler), gsampler_(sampler), hsampler_(sampler),
300  setVector_(false) {
301  parlist_ = Teuchos::rcpFromRef(parlist);
302  setObjective(obj);
303  setSolutionVector(vec);
304  setBoundConstraint(bnd);
306  }
307 
308  StochasticProblem(Teuchos::ParameterList &parlist,
309  const Teuchos::RCP<Objective<Real> > &obj,
310  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
311  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
312  const Teuchos::RCP<Vector<Real> > &vec,
313  const Teuchos::RCP<BoundConstraint<Real> > &bnd,
314  const Teuchos::RCP<EqualityConstraint<Real> > &con)
315  : OptimizationProblem<Real>(),
316  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
317  ORIGINAL_bnd_(bnd), ORIGINAL_con_(con),
318  obj_(Teuchos::null), vec_(Teuchos::null),
319  bnd_(Teuchos::null), con_(Teuchos::null),
320  vsampler_(vsampler), gsampler_(gsampler), hsampler_(gsampler),
321  setVector_(false) {
322  parlist_ = Teuchos::rcpFromRef(parlist);
323  setObjective(obj);
324  setSolutionVector(vec);
325  setBoundConstraint(bnd);
327  }
328 
329  StochasticProblem(Teuchos::ParameterList &parlist,
330  const Teuchos::RCP<Objective<Real> > &obj,
331  const Teuchos::RCP<SampleGenerator<Real> > &vsampler,
332  const Teuchos::RCP<SampleGenerator<Real> > &gsampler,
333  const Teuchos::RCP<SampleGenerator<Real> > &hsampler,
334  const Teuchos::RCP<Vector<Real> > &vec,
335  const Teuchos::RCP<BoundConstraint<Real> > &bnd,
336  const Teuchos::RCP<EqualityConstraint<Real> > &con)
337  : OptimizationProblem<Real>(),
338  ORIGINAL_obj_(obj), ORIGINAL_vec_(vec),
339  ORIGINAL_bnd_(bnd), ORIGINAL_con_(con),
340  obj_(Teuchos::null), vec_(Teuchos::null),
341  bnd_(Teuchos::null), con_(Teuchos::null),
342  vsampler_(vsampler), gsampler_(gsampler), hsampler_(hsampler),
343  setVector_(false) {
344  parlist_ = Teuchos::rcpFromRef(parlist);
345  setObjective(obj);
346  setSolutionVector(vec);
347  setBoundConstraint(bnd);
349  }
350 
351  void setParameterList(Teuchos::ParameterList &parlist) {
352  parlist_ = Teuchos::rcpFromRef(parlist);
353  if (ORIGINAL_obj_ != Teuchos::null) {
354  setObjective(ORIGINAL_obj_);
355  }
356  if (ORIGINAL_vec_ != Teuchos::null) {
357  setSolutionVector(ORIGINAL_vec_);
358  }
359  if (ORIGINAL_bnd_ != Teuchos::null) {
360  setBoundConstraint(ORIGINAL_bnd_);
361  }
362  }
363 
364  void setValueSampleGenerator(const Teuchos::RCP<SampleGenerator<Real> > &vsampler) {
365  vsampler_ = vsampler;
366  if ( gsampler_ == Teuchos::null ) {
367  gsampler_ = vsampler_;
368  }
369  if ( hsampler_ == Teuchos::null ) {
370  hsampler_ = gsampler_;
371  }
372  }
373 
374  void setGradientSampleGenerator(const Teuchos::RCP<SampleGenerator<Real> > &gsampler) {
375  gsampler_ = gsampler;
376  if ( hsampler_ == Teuchos::null ) {
377  hsampler_ = gsampler_;
378  }
379  }
380 
381  void setHessVecSampleGenerator(const Teuchos::RCP<SampleGenerator<Real> > &hsampler) {
382  hsampler_ = hsampler;
383  }
384 
385  void setObjective(const Teuchos::RCP<Objective<Real> > &obj) {
386  if ( parlist_ == Teuchos::null ) {
387  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
388  ">>> ERROR (ROL::StochasticProblem): parameter list not set!");
389  }
390  else {
391  ORIGINAL_obj_ = obj;
392  if ( vsampler_ == Teuchos::null ) {
393  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
394  ">>> ERROR (ROL::StochasticProblem): value sampler not set!");
395  }
396  else {
397  // Determine Stochastic Optimization Type
398  std::string type = parlist_->sublist("SOL").get("Stochastic Optimization Type","Risk Neutral");
399  if ( type == "Risk Neutral" ) {
400  bool storage = parlist_->sublist("SOL").get("Store Sampled Value and Gradient",true);
401  obj_ = Teuchos::rcp(new RiskNeutralObjective<Real>(obj,vsampler_,gsampler_,hsampler_,storage));
402  }
403  else if ( type == "Risk Averse" ) {
404  obj_ = Teuchos::rcp(new RiskAverseObjective<Real>(obj,*parlist_,vsampler_,gsampler_,hsampler_));
405  }
406  else if ( type == "BPOE" ) {
407  Real order = parlist_->sublist("SOL").sublist("BPOE").get("Moment Order",1.);
408  Real threshold = parlist_->sublist("SOL").sublist("BPOE").get("Threshold",0.);
409  obj_ = Teuchos::rcp(new BPOEObjective<Real>(obj,order,threshold,vsampler_,gsampler_,hsampler_));
410  }
411  else if ( type == "Mean Value" ) {
412  std::vector<Real> mean = computeSampleMean(vsampler_);
413  obj->setParameter(mean);
414  obj_ = obj;
415  }
416  else {
417  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
418  "Invalid stochastic optimization type" << type);
419  }
420  // Set OptimizationProblem data
422  }
423  }
424  }
425 
426  void setSolutionVector(const Teuchos::RCP<Vector<Real> > &vec) {
427  if ( parlist_ == Teuchos::null ) {
428  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
429  ">>> ERROR (ROL::StochasticProblem): parameter list not set!");
430  }
431  else {
432  ORIGINAL_vec_ = vec;
433  // Determine Stochastic Optimization Type
434  std::string type = parlist_->sublist("SOL").get("Stochastic Optimization Type","Risk Neutral");
435  if ( type == "Risk Neutral" || type == "Mean Value" ) {
436  vec_ = vec;
437  }
438  else if ( type == "Risk Averse" ) {
439  vec_ = Teuchos::rcp(new RiskVector<Real>(*parlist_,vec));
440  }
441  else if ( type == "BPOE" ) {
442  std::vector<Real> stat(1,1);
443  vec_ = Teuchos::rcp(new RiskVector<Real>(vec,stat,true));
444  }
445  else {
446  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
447  "Invalid stochastic optimization type" << type);
448  }
449  // Set OptimizationProblem data
451  setVector_ = true;
452  }
453  }
454 
455  void setSolutionStatistic(const Real stat) {
456  if ( parlist_ == Teuchos::null ) {
457  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
458  ">>> ERROR (ROL::StochasticProblem): parameter list not set!");
459  }
460  else {
461  if ( setVector_ ) {
462  // Determine Stochastic Optimization Type
463  std::string type = parlist_->sublist("SOL").get("Stochastic Optimization Type","Risk Neutral");
464  if ( type == "Risk Averse" || type == "BPOE" ) {
465  RiskVector<Real> &x = Teuchos::dyn_cast<RiskVector<Real> >(*vec_);
466  x.setStatistic(stat);
467  }
468  // Set OptimizationProblem data
470  }
471  }
472  }
473 
474  void setBoundConstraint(const Teuchos::RCP<BoundConstraint<Real> > &bnd) {
475  if ( parlist_ == Teuchos::null ) {
476  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
477  ">>> ERROR (ROL::StochasticProblem): parameter list not set!");
478  }
479  else {
480  ORIGINAL_bnd_ = bnd;
481  // Determine Stochastic Optimization Type
482  std::string type = parlist_->sublist("SOL").get("Stochastic Optimization Type","Risk Neutral");
483  if ( type == "Risk Neutral" || type == "Mean Value" ) {
484  bnd_ = bnd;
485  }
486  else if ( type == "Risk Averse" || type == "BPOE" ) {
487  bnd_ = Teuchos::rcp(new RiskBoundConstraint<Real>(*parlist_,bnd));
488  }
489  else {
490  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
491  "Invalid stochastic optimization type" << type);
492  }
493  // Set OptimizationProblem data
495  }
496  }
497 
498  void setEqualityConstraint(const Teuchos::RCP<EqualityConstraint<Real> > &con) {
499  if ( parlist_ == Teuchos::null ) {
500  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
501  ">>> ERROR (ROL::StochasticProblem): parameter list not set!");
502  }
503  else {
504  ORIGINAL_con_ = con;
505  if ( con != Teuchos::null ) {
506  // Determine Stochastic Optimization Type
507  std::string type = parlist_->sublist("SOL").get("Stochastic Optimization Type","Risk Neutral");
508  if ( type == "Risk Neutral" || type == "Mean Value" ) {
509  con_ = con;
510  }
511  else if ( type == "Risk Averse" || type == "BPOE" ) {
512  con_ = Teuchos::rcp(new RiskLessEqualityConstraint<Real>(con));
513  }
514  else {
515  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
516  "Invalid stochastic optimization type" << type);
517  }
518  }
519  else {
520  con_ = con;
521  }
522  // Set OptimizationProblem data
524  }
525  }
526 
527 // Real getSolutionStatistic(void) {
528 // try {
529 // return Teuchos::dyn_cast<const RiskVector<Real> >(
530 // Teuchos::dyn_cast<const Vector<Real> >(*vec_)).getStatistic(0);
531 // }
532 // catch (std::exception &e) {
533 // return 0.;
534 // }
535 // }
536 
537  Real getSolutionStatistic(void) {
538  if ( parlist_ == Teuchos::null ) {
539  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
540  ">>> ERROR (ROL::StochasticProblem): parameter list not set!");
541  }
542  else {
543  try {
544  const RiskVector<Real> x = Teuchos::dyn_cast<const RiskVector<Real> >(
545  Teuchos::dyn_cast<const Vector<Real> >(*vec_));
546  std::string type = parlist_->sublist("SOL").get("Stochastic Optimization Type","Risk Neutral");
547  Real val(0);
548  if ( type == "Risk Averse" ) {
549  Teuchos::ParameterList &list
550  = parlist_->sublist("SOL").sublist("Risk Measure");
551  std::string risk = list.get("Name","CVaR");
552  if ( risk == "Mixed-Quantile Quadrangle" ) {
553  Teuchos::ParameterList &MQQlist = list.sublist("Mixed-Quantile Quadrangle");
554  Teuchos::Array<Real> coeff
555  = Teuchos::getArrayFromStringParameter<Real>(MQQlist,"Coefficient Array");
556  for (int i = 0; i < coeff.size(); i++) {
557  val += coeff[i]*x.getStatistic(i);
558  }
559  }
560  else if ( risk == "Super Quantile Quadrangle" ) {
561  SuperQuantileQuadrangle<Real> sqq(*parlist_);
562  val = sqq.computeStatistic(*vec_);
563  }
564  else if ( risk == "Chebyshev-Kusuoka" ) {
565  ChebyshevKusuoka<Real> sqq(*parlist_);
566  val = static_cast<SpectralRisk<Real> >(sqq).computeStatistic(*vec_);
567  }
568  else if ( risk == "Spectral Risk" ) {
569  SpectralRisk<Real> sqq(*parlist_);
570  val = sqq.computeStatistic(*vec_);
571  }
572  else if ( risk == "Quantile-Radius Quadrangle" ) {
573  Real half(0.5);
574  val = half*(x.getStatistic(0) + x.getStatistic(1));
575  }
576  else {
577  val = x.getStatistic(0);
578  }
579  }
580  else {
581  val = x.getStatistic(0);
582  }
583  return val;
584  }
585  catch (std::exception &e) {
586  return 0;
587  }
588  }
589  }
590 
591  std::vector<std::vector<Real> > checkObjectiveGradient( const Vector<Real> &d,
592  const bool printToStream = true,
593  std::ostream & outStream = std::cout,
594  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
595  const int order = 1 ) {
596  Teuchos::RCP<Vector<Real> > dp = d.clone();
597  dp->set(d);
598  Real stat(5.1264386);
599  Teuchos::RCP<Vector<Real> > D = createVector(dp,stat);
600  return OptimizationProblem<Real>::checkObjectiveGradient(*D,printToStream,outStream,numSteps,order);
601  }
602 
603  std::vector<std::vector<Real> > checkObjectiveHessVec( const Vector<Real> &v,
604  const bool printToStream = true,
605  std::ostream & outStream = std::cout,
606  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
607  const int order = 1 ) {
608  Teuchos::RCP<Vector<Real> > vp = v.clone();
609  vp->set(v);
610  Real stat(3.223468906);
611  Teuchos::RCP<Vector<Real> > V = createVector(vp,stat);
612  return OptimizationProblem<Real>::checkObjectiveHessVec(*V,printToStream,outStream,numSteps,order);
613  }
614 
615  void printObjectiveSamples(const std::string &filename = "objectiveSamples",
616  const int prec = 12,
617  const Teuchos::RCP<SampleGenerator<Real> > &sampler = Teuchos::null) {
618  Teuchos::RCP<SampleGenerator<Real> > samp = sampler;
619  if ( sampler == Teuchos::null ) {
620  samp = vsampler_;
621  }
622 
623  int width = prec + 5 + 4;
624  std::stringstream name;
625  name << filename << "_" << samp->batchID() << ".txt";
626  std::ofstream file(name.str().c_str());
627  if (file.is_open()) {
628  Real val(0), tol(ROL_EPSILON<Real>());
629  std::vector<Real> sample;
630  file << std::scientific << std::setprecision(prec);
631  for (int i = 0; i < samp->numMySamples(); ++i) {
632  sample = samp->getMyPoint(i);
633  obj_->setParameter(sample);
634  val = ORIGINAL_obj_->value(*ORIGINAL_vec_,tol);
635  for (int j = 0; j < static_cast<int>(sample.size()); ++j) {
636  file << std::setw(width) << std::left << sample[j];
637  }
638  file << std::setw(width) << std::left << val << std::endl;
639  }
640  file.close();
641  }
642  else {
643  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
644  ">>> (ROL::StochasticProblem::printObjectiveDistribution): Unable to open file!");
645  }
646  }
647 
648 private:
649  std::vector<Real> computeSampleMean(Teuchos::RCP<SampleGenerator<Real> > &sampler) {
650  // Compute mean value of inputs and set parameter in objective
651  int dim = sampler->getMyPoint(0).size(), nsamp = sampler->numMySamples();
652  std::vector<Real> loc(dim), mean(dim), pt(dim);
653  Real wt(0);
654  for (int i = 0; i < nsamp; i++) {
655  pt = sampler->getMyPoint(i);
656  wt = sampler->getMyWeight(i);
657  for (int j = 0; j < dim; j++) {
658  loc[j] += wt*pt[j];
659  }
660  }
661  sampler->sumAll(&loc[0],&mean[0],dim);
662  return mean;
663  }
664 
665  Teuchos::RCP<Vector<Real> > createVector(Teuchos::RCP<Vector<Real> > &vec, Real stat = 1) {
666  if ( parlist_ == Teuchos::null ) {
667  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
668  ">>> ERROR (ROL::StochasticProblem): parameter list not set!");
669  }
670  else {
671  // Determine Stochastic Optimization Type
672  std::string type = parlist_->sublist("SOL").get("Stochastic Optimization Type","Risk Neutral");
673  if ( type == "Risk Neutral" || type == "Mean Value" ) {
674  return vec;
675  }
676  else if ( type == "Risk Averse" ) {
677  return Teuchos::rcp(new RiskVector<Real>(*parlist_,vec,stat));
678  }
679  else if ( type == "BPOE" ) {
680  std::vector<Real> statistic(1,stat);
681  return Teuchos::rcp(new RiskVector<Real>(vec,statistic,true));
682  }
683  else {
684  TEUCHOS_TEST_FOR_EXCEPTION(true,std::logic_error,
685  "Invalid stochastic optimization type" << type);
686  }
687  }
688  }
689 };
690 }
691 #endif
Provides the interface to evaluate objective functions.
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< SampleGenerator< Real > > &hsampler, const Teuchos::RCP< Vector< Real > > &vec)
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &sampler, const Teuchos::RCP< Vector< Real > > &vec, const Teuchos::RCP< EqualityConstraint< Real > > &con)
virtual std::vector< std::vector< Real > > checkObjectiveGradient(const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Teuchos::RCP< EqualityConstraint< Real > > con_
std::vector< Real > computeSampleMean(Teuchos::RCP< SampleGenerator< Real > > &sampler)
void setGradientSampleGenerator(const Teuchos::RCP< SampleGenerator< Real > > &gsampler)
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
Real computeStatistic(const Vector< Real > &x) const
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< SampleGenerator< Real > > &hsampler, const Teuchos::RCP< Vector< Real > > &vec, const Teuchos::RCP< BoundConstraint< Real > > &bnd)
Teuchos::RCP< BoundConstraint< Real > > bnd_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
void setHessVecSampleGenerator(const Teuchos::RCP< SampleGenerator< Real > > &hsampler)
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &sampler, const Teuchos::RCP< Vector< Real > > &vec)
Teuchos::RCP< SampleGenerator< Real > > vsampler_
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &sampler, const Teuchos::RCP< Vector< Real > > &vec, const Teuchos::RCP< BoundConstraint< Real > > &bnd)
Teuchos::RCP< EqualityConstraint< Real > > ORIGINAL_con_
void setObjective(const Teuchos::RCP< Objective< Real > > &obj)
Teuchos::RCP< BoundConstraint< Real > > ORIGINAL_bnd_
void setObjective(const Teuchos::RCP< Objective< Real > > &obj)
Defines the equality constraint operator interface.
std::vector< std::vector< Real > > checkObjectiveGradient(const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
void setSolutionVector(const Teuchos::RCP< Vector< Real > > &sol)
void setStatistic(const Real stat)
Teuchos::RCP< Vector< Real > > ORIGINAL_vec_
void setParameterList(Teuchos::ParameterList &parlist)
Provides an interface for the risk measure associated with the super quantile quadrangle.
Provides an interface for spectral risk measures.
std::vector< std::vector< Real > > checkObjectiveHessVec(const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< Vector< Real > > &vec, const Teuchos::RCP< BoundConstraint< Real > > &bnd)
Provides the interface to apply upper and lower bound constraints.
Teuchos::RCP< SampleGenerator< Real > > gsampler_
Teuchos::RCP< Vector< Real > > vec_
#define ROL_NUM_CHECKDERIV_STEPS
Number of steps for derivative checks.
Definition: ROL_Types.hpp:73
Teuchos::RCP< Objective< Real > > ORIGINAL_obj_
Teuchos::RCP< Teuchos::ParameterList > parlist_
void setEqualityConstraint(const Teuchos::RCP< EqualityConstraint< Real > > &con)
Teuchos::RCP< const StdVector< Real > > getStatistic(void) const
void setSolutionStatistic(const Real stat)
Teuchos::RCP< Vector< Real > > createVector(Teuchos::RCP< Vector< Real > > &vec, Real stat=1)
void setValueSampleGenerator(const Teuchos::RCP< SampleGenerator< Real > > &vsampler)
void setBoundConstraint(const Teuchos::RCP< BoundConstraint< Real > > &bnd)
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &sampler, const Teuchos::RCP< Vector< Real > > &vec, const Teuchos::RCP< BoundConstraint< Real > > &bnd, const Teuchos::RCP< EqualityConstraint< Real > > &con)
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< SampleGenerator< Real > > &hsampler, const Teuchos::RCP< Vector< Real > > &vec, const Teuchos::RCP< BoundConstraint< Real > > &bnd, const Teuchos::RCP< EqualityConstraint< Real > > &con)
void setEqualityConstraint(const Teuchos::RCP< EqualityConstraint< Real > > &con)
void setBoundConstraint(const Teuchos::RCP< BoundConstraint< Real > > &bnd)
StochasticProblem(Teuchos::ParameterList &parlist)
void printObjectiveSamples(const std::string &filename="objectiveSamples", const int prec=12, const Teuchos::RCP< SampleGenerator< Real > > &sampler=Teuchos::null)
void setSolutionVector(const Teuchos::RCP< Vector< Real > > &vec)
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< SampleGenerator< Real > > &hsampler, const Teuchos::RCP< Vector< Real > > &vec, const Teuchos::RCP< EqualityConstraint< Real > > &con)
virtual std::vector< std::vector< Real > > checkObjectiveHessVec(const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Teuchos::RCP< Objective< Real > > obj_
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< Vector< Real > > &vec, const Teuchos::RCP< EqualityConstraint< Real > > &con)
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< Vector< Real > > &vec)
StochasticProblem(Teuchos::ParameterList &parlist, const Teuchos::RCP< Objective< Real > > &obj, const Teuchos::RCP< SampleGenerator< Real > > &vsampler, const Teuchos::RCP< SampleGenerator< Real > > &gsampler, const Teuchos::RCP< Vector< Real > > &vec, const Teuchos::RCP< BoundConstraint< Real > > &bnd, const Teuchos::RCP< EqualityConstraint< Real > > &con)
Provides an interface for the Chebyshev-Kusuoka risk measure.
Teuchos::RCP< SampleGenerator< Real > > hsampler_