ROL
ROL_ExpectationQuad.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_EXPECTATIONQUAD_HPP
45 #define ROL_EXPECTATIONQUAD_HPP
46 
47 #include "ROL_CVaRVector.hpp"
48 #include "ROL_RiskMeasure.hpp"
49 #include "ROL_Types.hpp"
50 
51 namespace ROL {
52 
53 template<class Real>
54 class ExpectationQuad : public RiskMeasure<Real> {
55 private:
56  Real xstat_;
57  Real vstat_;
58 
59 public:
60  ExpectationQuad(void) : xstat_(0.0), vstat_(0.0) {}
61 
62  virtual Real regret(Real x, int deriv = 0) = 0;
63 
64  virtual void checkRegret(void) {
65  // Check v(0) = 0
66  Real x = 0.0;
67  Real vx = regret(x,0);
68  std::cout << std::right << std::setw(20) << "CHECK REGRET: v(0) = 0? \n";
69  std::cout << std::right << std::setw(20) << "v(0)" << "\n";
70  std::cout << std::scientific << std::setprecision(11) << std::right
71  << std::setw(20) << std::abs(vx)
72  << "\n";
73  std::cout << "\n";
74  // Check v(x) > x
75  Real scale = 2.0;
76  std::cout << std::right << std::setw(20) << "CHECK REGRET: x < v(x) for |x| > 0? \n";
77  std::cout << std::right << std::setw(20) << "x"
78  << std::right << std::setw(20) << "v(x)"
79  << "\n";
80  for (int i = 0; i < 10; i++) {
81  x = scale*(Real)rand()/(Real)RAND_MAX - scale*0.5;
82  vx = regret(x,0);
83  std::cout << std::scientific << std::setprecision(11) << std::right
84  << std::setw(20) << x
85  << std::setw(20) << vx
86  << "\n";
87  scale *= 2.0;
88  }
89  std::cout << "\n";
90  // Check v(x) is convex
91  Real y = 0.0;
92  Real vy = 0.0;
93  Real z = 0.0;
94  Real vz = 0.0;
95  Real l = 0.0;
96  scale = 2.0;
97  std::cout << std::right << std::setw(20) << "CHECK REGRET: v(x) is convex? \n";
98  std::cout << std::right << std::setw(20) << "v(l*x+(1-l)*y)"
99  << std::setw(20) << "l*v(x)+(1-l)*v(y)"
100  << "\n";
101  for (int i = 0; i < 10; i++) {
102  x = scale*(Real)rand()/(Real)RAND_MAX - scale*0.5;
103  vx = regret(x,0);
104  y = scale*(Real)rand()/(Real)RAND_MAX - scale*0.5;
105  vy = regret(y,0);
106  l = (Real)rand()/(Real)RAND_MAX;
107  z = l*x + (1.0-l)*y;
108  vz = regret(z,0);
109  std::cout << std::scientific << std::setprecision(11) << std::right
110  << std::setw(20) << vz
111  << std::setw(20) << l*vx + (1.0-l)*vy
112  << "\n";
113  scale *= 2.0;
114  }
115  std::cout << "\n";
116  // Check v'(x)
117  x = 0.001*(Real)rand()/(Real)RAND_MAX - 0.0005;
118  vx = regret(x,0);
119  Real dv = regret(x,1);
120  Real t = 1.0;
121  Real diff = 0.0;
122  Real err = 0.0;
123  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(x) is correct? \n";
124  std::cout << std::right << std::setw(20) << "t"
125  << std::setw(20) << "v'(x)"
126  << std::setw(20) << "(v(x+t)-v(x))/t"
127  << std::setw(20) << "Error"
128  << "\n";
129  for (int i = 0; i < 13; i++) {
130  y = x + t;
131  vy = regret(y,0);
132  diff = (vy-vx)/t;
133  err = std::abs(diff-dv);
134  std::cout << std::scientific << std::setprecision(11) << std::right
135  << std::setw(20) << t
136  << std::setw(20) << dv
137  << std::setw(20) << diff
138  << std::setw(20) << err
139  << "\n";
140  t *= 0.1;
141  }
142  std::cout << "\n";
143  // Check v''(x)
144  x = 0.001*(Real)rand()/(Real)RAND_MAX - 0.0005;
145  vx = regret(x,1);
146  dv = regret(x,2);
147  t = 1.0;
148  diff = 0.0;
149  err = 0.0;
150  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(x) is correct? \n";
151  std::cout << std::right << std::setw(20) << "t"
152  << std::setw(20) << "v''(x)"
153  << std::setw(20) << "(v'(x+t)-v'(x))/t"
154  << std::setw(20) << "Error"
155  << "\n";
156  for (int i = 0; i < 13; i++) {
157  y = x + t;
158  vy = regret(y,1);
159  diff = (vy-vx)/t;
160  err = std::abs(diff-dv);
161  std::cout << std::scientific << std::setprecision(11) << std::right
162  << std::setw(20) << t
163  << std::setw(20) << dv
164  << std::setw(20) << diff
165  << std::setw(20) << err
166  << "\n";
167  t *= 0.1;
168  }
169  std::cout << "\n";
170  }
171 
172  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
173  x0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const CVaRVector<Real> >(
174  Teuchos::dyn_cast<const Vector<Real> >(x)).getVector());
175  xstat_ = Teuchos::dyn_cast<const CVaRVector<Real> >(
176  Teuchos::dyn_cast<const Vector<Real> >(x)).getVaR();
178  RiskMeasure<Real>::g_ = x0->clone(); RiskMeasure<Real>::g_->zero();
179  RiskMeasure<Real>::hv_ = x0->clone(); RiskMeasure<Real>::hv_->zero();
180  }
181 
182  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
183  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
184  x0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const CVaRVector<Real> >(
185  Teuchos::dyn_cast<const Vector<Real> >(x)).getVector());
186  xstat_ = Teuchos::dyn_cast<const CVaRVector<Real> >(
187  Teuchos::dyn_cast<const Vector<Real> >(x)).getVaR();
188  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const CVaRVector<Real> >(
189  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
190  vstat_ = Teuchos::dyn_cast<const CVaRVector<Real> >(
191  Teuchos::dyn_cast<const Vector<Real> >(v)).getVaR();
193  RiskMeasure<Real>::g_ = x0->clone(); RiskMeasure<Real>::g_->zero();
194  RiskMeasure<Real>::hv_ = x0->clone(); RiskMeasure<Real>::hv_->zero();
195  }
196 
197  void update(const Real val, const Real weight) {
198  Real r = regret(val-xstat_,0);
199  RiskMeasure<Real>::val_ += weight * r;
200  }
201 
202  void update(const Real val, const Vector<Real> &g, const Real weight) {
203  Real r = regret(val-xstat_,1);
204  RiskMeasure<Real>::val_ -= weight * r;
205  RiskMeasure<Real>::g_->axpy(weight*r,g);
206  }
207 
208  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
209  const Real weight) {
210  Real r1 = regret(val-xstat_,1);
211  Real r2 = regret(val-xstat_,2);
212  RiskMeasure<Real>::val_ += weight * r2 * (vstat_ - gv);
213  RiskMeasure<Real>::hv_->axpy(weight*r2*(gv-vstat_),g);
214  RiskMeasure<Real>::hv_->axpy(weight*r1,hv);
215  }
216 
218  Real val = RiskMeasure<Real>::val_;
219  Real gval = 0.0;
220  sampler.sumAll(&val,&gval,1);
221  gval += xstat_;
222  return gval;
223  }
224 
226  CVaRVector<Real> &gs = Teuchos::dyn_cast<CVaRVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(g));
227  Real stat = RiskMeasure<Real>::val_;
228  Real gstat = 0.0;
229  sampler.sumAll(&stat,&gstat,1);
230  gstat += 1.0;
231  gs.setVaR(gstat);
232 
233  Teuchos::RCP<Vector<Real> > gz = RiskMeasure<Real>::g_->clone();
234  sampler.sumAll(*(RiskMeasure<Real>::g_),*gz);
235  gs.setVector(*gz);
236  }
237 
239  CVaRVector<Real> &hs = Teuchos::dyn_cast<CVaRVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(hv));
240  Real stat = RiskMeasure<Real>::val_;
241  Real gstat = 0.0;
242  sampler.sumAll(&stat,&gstat,1);
243  hs.setVaR(gstat);
244 
245  Teuchos::RCP<Vector<Real> > hz = RiskMeasure<Real>::hv_->clone();
246  sampler.sumAll(*(RiskMeasure<Real>::hv_),*hz);
247  hs.setVector(*hz);
248  }
249 };
250 
251 }
252 
253 #endif
void update(const Real val, const Vector< Real > &g, const Real weight)
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
virtual void checkRegret(void)
Real getValue(SampleGenerator< Real > &sampler)
Contains definitions of custom data types in ROL.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
void sumAll(Real *input, Real *output, int dim) const
const Real getVaR() const
Teuchos::RCP< const Vector< Real > > getVector() const
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
void setVaR(const Real var)
virtual Real regret(Real x, int deriv=0)=0
void setVector(const Vector< Real > &vec)
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
void update(const Real val, const Real weight)
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)