ROL
ROL_SimulatedObjectiveCVaR.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_SIMULATED_OBJECTIVE_CVAR_H
45 #define ROL_SIMULATED_OBJECTIVE_CVAR_H
46 
47 #include "ROL_SimulatedVector.hpp"
48 #include "ROL_PlusFunction.hpp"
49 #include "ROL_RiskVector.hpp"
50 #include "ROL_Objective_SimOpt.hpp"
51 
52 namespace ROL {
53 
54 template <class Real>
55 class SimulatedObjectiveCVaR : public Objective<Real> {
56 private:
57  const Teuchos::RCP<SampleGenerator<Real> > sampler_;
58  const Teuchos::RCP<Objective_SimOpt<Real> > pobj_;
59  const Teuchos::RCP<PlusFunction<Real> > pfunc_;
60  const Real alpha_;
61 
62 public:
63 
65 
66  SimulatedObjectiveCVaR(const Teuchos::RCP<SampleGenerator<Real> > & sampler,
67  const Teuchos::RCP<Objective_SimOpt<Real> > & pobj,
68  const Teuchos::RCP<PlusFunction<Real> > & pfunc,
69  const Real & alpha)
70  : sampler_(sampler), pobj_(pobj), pfunc_(pfunc), alpha_(alpha) {}
71 
72  Real value(const Vector<Real> &x,
73  Real &tol) {
74  const Vector_SimOpt<Real> &uz = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(x);
75  Teuchos::RCP<const Vector<Real> > uptr = uz.get_1();
76  Teuchos::RCP<const Vector<Real> > zptr = uz.get_2();
77  const SimulatedVector<Real> &pu = Teuchos::dyn_cast<const SimulatedVector<Real> >(*uptr);
78  const RiskVector<Real> &rz = Teuchos::dyn_cast<const RiskVector<Real> >(*zptr);
79  Real t = rz.getStatistic(0);
80  Teuchos::RCP<const Vector<Real> > z = rz.getVector();
81 
82  std::vector<Real> param;
83  Real weight(0), one(1);
84  Real val = 0;
85  Real tmpval = 0;
86  Real tmpsum = 0;
87  Real tmpplus = 0;
88  for (typename std::vector<SimulatedVector<Real> >::size_type i=0; i<pu.numVectors(); ++i) {
89  param = sampler_->getMyPoint(static_cast<int>(i));
90  weight = sampler_->getMyWeight(static_cast<int>(i));
91  pobj_->setParameter(param);
92  //tmpval = pobj_->value(*(pu.get(i)), *zptr, tol);
93  tmpval = pobj_->value(*(pu.get(i)), *z, tol);
94  tmpplus = pfunc_->evaluate(tmpval-t, 0);
95  tmpsum += tmpplus*weight;
96  }
97  sampler_->sumAll(&tmpsum, &val, 1);
98  val *= (one/(one-alpha_));
99  val += t;
100  return val;
101  }
102 
103  virtual void gradient(Vector<Real> &g,
104  const Vector<Real> &x,
105  Real &tol) {
106  g.zero();
107  // split x
108  const Vector_SimOpt<Real> &xuz = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(x);
109  Teuchos::RCP<const Vector<Real> > xuptr = xuz.get_1();
110  Teuchos::RCP<const Vector<Real> > xzptr = xuz.get_2();
111  const SimulatedVector<Real> &pxu = Teuchos::dyn_cast<const SimulatedVector<Real> >(*xuptr);
112  const RiskVector<Real> &rxz = Teuchos::dyn_cast<const RiskVector<Real> >(*xzptr);
113  Real xt = rxz.getStatistic(0);
114  Teuchos::RCP<const Vector<Real> > xz = rxz.getVector();
115  // split g
116  Vector_SimOpt<Real> &guz = Teuchos::dyn_cast<Vector_SimOpt<Real> >(g);
117  Teuchos::RCP<Vector<Real> > guptr = guz.get_1();
118  Teuchos::RCP<Vector<Real> > gzptr = guz.get_2();
119  SimulatedVector<Real> &pgu = Teuchos::dyn_cast<SimulatedVector<Real> >(*guptr);
120  RiskVector<Real> &rgz = Teuchos::dyn_cast<RiskVector<Real> >(*gzptr);
121  Teuchos::RCP<Vector<Real> > gz = rgz.getVector();
122 
123  std::vector<Real> param;
124  Real weight(0), one(1), sum(0), tmpsum(0), tmpval(0), tmpplus(0);
125  //Teuchos::RCP<Vector<Real> > tmp1 = gzptr->clone();
126  //Teuchos::RCP<Vector<Real> > tmp2 = gzptr->clone();
127  Teuchos::RCP<Vector<Real> > tmp1 = gz->clone();
128  Teuchos::RCP<Vector<Real> > tmp2 = gz->clone();
129  for (typename std::vector<SimulatedVector<Real> >::size_type i=0; i<pgu.numVectors(); ++i) {
130  param = sampler_->getMyPoint(static_cast<int>(i));
131  weight = sampler_->getMyWeight(static_cast<int>(i));
132  pobj_->setParameter(param);
133  //tmpval = pobj_->value(*(pxu.get(i)), *xzptr, tol);
134  tmpval = pobj_->value(*(pxu.get(i)), *xz, tol);
135  tmpplus = pfunc_->evaluate(tmpval-xt, 1);
136  tmpsum += weight*tmpplus;
137  //Vector_SimOpt<Real> xi(Teuchos::rcp_const_cast<Vector<Real> >(pxu.get(i)), Teuchos::rcp_const_cast<Vector<Real> >(xzptr));
138  Vector_SimOpt<Real> xi(Teuchos::rcp_const_cast<Vector<Real> >(pxu.get(i)), Teuchos::rcp_const_cast<Vector<Real> >(xz));
139  Vector_SimOpt<Real> gi(pgu.get(i), tmp1);
140  pobj_->gradient(gi, xi, tol);
141  gi.scale(weight*tmpplus);
142  tmp2->plus(*tmp1);
143  pgu.get(i)->scale(one/(one-alpha_));
144  }
145  //sampler_->sumAll(*tmp2, *gzptr);
146  //gzptr->scale(one/(one-alpha_));
147  sampler_->sumAll(*tmp2, *gz);
148  gz->scale(one/(one-alpha_));
149  sampler_->sumAll(&tmpsum, &sum, 1);
150  rgz.setStatistic(one - (one/(one-alpha_))*sum);
151  }
152 
153 /*
154  virtual void hessVec(Vector<Real> &hv,
155  const Vector<Real> &v,
156  const Vector<Real> &x,
157  Real &tol) {
158  hv.zero();
159  // split x
160  const Vector_SimOpt<Real> &xuz = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(x);
161  Teuchos::RCP<const Vector<Real> > xuptr = xuz.get_1();
162  Teuchos::RCP<const Vector<Real> > xzptr = xuz.get_2();
163  const SimulatedVector<Real> &pxu = Teuchos::dyn_cast<const SimulatedVector<Real> >(*xuptr);
164  // split v
165  const Vector_SimOpt<Real> &vuz = Teuchos::dyn_cast<const Vector_SimOpt<Real> >(v);
166  Teuchos::RCP<const Vector<Real> > vuptr = vuz.get_1();
167  Teuchos::RCP<const Vector<Real> > vzptr = vuz.get_2();
168  const SimulatedVector<Real> &pvu = Teuchos::dyn_cast<const SimulatedVector<Real> >(*vuptr);
169  // split hv
170  Vector_SimOpt<Real> &hvuz = Teuchos::dyn_cast<Vector_SimOpt<Real> >(hv);
171  Teuchos::RCP<Vector<Real> > hvuptr = hvuz.get_1();
172  Teuchos::RCP<Vector<Real> > hvzptr = hvuz.get_2();
173  SimulatedVector<Real> &phvu = Teuchos::dyn_cast<SimulatedVector<Real> >(*hvuptr);
174 
175  std::vector<Real> param;
176  Real weight(0);
177  Teuchos::RCP<Vector<Real> > tmp1 = hvzptr->clone();
178  Teuchos::RCP<Vector<Real> > tmp2 = hvzptr->clone();
179  for (typename std::vector<SimulatedVector<Real> >::size_type i=0; i<phvu.numVectors(); ++i) {
180  param = sampler_->getMyPoint(static_cast<int>(i));
181  weight = sampler_->getMyWeight(static_cast<int>(i));
182  pobj_->setParameter(param);
183  Vector_SimOpt<Real> xi(Teuchos::rcp_const_cast<Vector<Real> >(pxu.get(i)), Teuchos::rcp_const_cast<Vector<Real> >(xzptr));
184  Vector_SimOpt<Real> vi(Teuchos::rcp_const_cast<Vector<Real> >(pvu.get(i)), Teuchos::rcp_const_cast<Vector<Real> >(vzptr));
185  Vector_SimOpt<Real> hvi(phvu.get(i), tmp1);
186  pobj_->hessVec(hvi, vi, xi, tol);
187  hvi.scale(weight);
188  tmp2->plus(*tmp1);
189  }
190  sampler_->sumAll(*tmp2, *hvzptr);
191  }
192 */
193 
194 }; // class SimulatedObjective
195 
196 } // namespace ROL
197 
198 #endif
Provides the interface to evaluate objective functions.
Provides the interface to evaluate simulation-based objective functions.
Defines the linear algebra or vector space interface for simulation-based optimization.
size_type numVectors() const
Teuchos::RCP< const Vector< Real > > get_2() const
const Teuchos::RCP< SampleGenerator< Real > > sampler_
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< const Vector< Real > > getVector(void) const
Defines the linear algebra of a vector space on a generic partitioned vector where the individual vec...
void setStatistic(const Real stat)
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Teuchos::RCP< const Vector< Real > > get_1() const
const Teuchos::RCP< Objective_SimOpt< Real > > pobj_
Real value(const Vector< Real > &x, Real &tol)
Compute value.
SimulatedObjectiveCVaR(const Teuchos::RCP< SampleGenerator< Real > > &sampler, const Teuchos::RCP< Objective_SimOpt< Real > > &pobj, const Teuchos::RCP< PlusFunction< Real > > &pfunc, const Real &alpha)
Teuchos::RCP< const StdVector< Real > > getStatistic(void) const
const Teuchos::RCP< PlusFunction< Real > > pfunc_
Teuchos::RCP< const Vector< Real > > get(size_type i) const