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_RiskVector.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 
57  Teuchos::RCP<Vector<Real> > dualVector_;
58 
59  Real xstat_;
60  Real vstat_;
61 
63 
64 public:
65  ExpectationQuad(void) : RiskMeasure<Real>(), xstat_(0.0), vstat_(0.0), firstReset_(true) {}
66 
67  virtual Real regret(Real x, int deriv = 0) = 0;
68 
69  virtual void checkRegret(void) {
70  // Check v(0) = 0
71  Real x = 0.0;
72  Real vx = regret(x,0);
73  std::cout << std::right << std::setw(20) << "CHECK REGRET: v(0) = 0? \n";
74  std::cout << std::right << std::setw(20) << "v(0)" << "\n";
75  std::cout << std::scientific << std::setprecision(11) << std::right
76  << std::setw(20) << std::abs(vx)
77  << "\n";
78  std::cout << "\n";
79  // Check v(x) > x
80  Real scale = 2.0;
81  std::cout << std::right << std::setw(20) << "CHECK REGRET: x < v(x) for |x| > 0? \n";
82  std::cout << std::right << std::setw(20) << "x"
83  << std::right << std::setw(20) << "v(x)"
84  << "\n";
85  for (int i = 0; i < 10; i++) {
86  x = scale*(Real)rand()/(Real)RAND_MAX - scale*0.5;
87  vx = regret(x,0);
88  std::cout << std::scientific << std::setprecision(11) << std::right
89  << std::setw(20) << x
90  << std::setw(20) << vx
91  << "\n";
92  scale *= 2.0;
93  }
94  std::cout << "\n";
95  // Check v(x) is convex
96  Real y = 0.0;
97  Real vy = 0.0;
98  Real z = 0.0;
99  Real vz = 0.0;
100  Real l = 0.0;
101  scale = 2.0;
102  std::cout << std::right << std::setw(20) << "CHECK REGRET: v(x) is convex? \n";
103  std::cout << std::right << std::setw(20) << "v(l*x+(1-l)*y)"
104  << std::setw(20) << "l*v(x)+(1-l)*v(y)"
105  << "\n";
106  for (int i = 0; i < 10; i++) {
107  x = scale*(Real)rand()/(Real)RAND_MAX - scale*0.5;
108  vx = regret(x,0);
109  y = scale*(Real)rand()/(Real)RAND_MAX - scale*0.5;
110  vy = regret(y,0);
111  l = (Real)rand()/(Real)RAND_MAX;
112  z = l*x + (1.0-l)*y;
113  vz = regret(z,0);
114  std::cout << std::scientific << std::setprecision(11) << std::right
115  << std::setw(20) << vz
116  << std::setw(20) << l*vx + (1.0-l)*vy
117  << "\n";
118  scale *= 2.0;
119  }
120  std::cout << "\n";
121  // Check v'(x)
122  x = 0.001*(Real)rand()/(Real)RAND_MAX - 0.0005;
123  vx = regret(x,0);
124  Real dv = regret(x,1);
125  Real t = 1.0;
126  Real diff = 0.0;
127  Real err = 0.0;
128  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(x) is correct? \n";
129  std::cout << std::right << std::setw(20) << "t"
130  << std::setw(20) << "v'(x)"
131  << std::setw(20) << "(v(x+t)-v(x))/t"
132  << std::setw(20) << "Error"
133  << "\n";
134  for (int i = 0; i < 13; i++) {
135  y = x + t;
136  vy = regret(y,0);
137  diff = (vy-vx)/t;
138  err = std::abs(diff-dv);
139  std::cout << std::scientific << std::setprecision(11) << std::right
140  << std::setw(20) << t
141  << std::setw(20) << dv
142  << std::setw(20) << diff
143  << std::setw(20) << err
144  << "\n";
145  t *= 0.1;
146  }
147  std::cout << "\n";
148  // Check v''(x)
149  x = 0.001*(Real)rand()/(Real)RAND_MAX - 0.0005;
150  vx = regret(x,1);
151  dv = regret(x,2);
152  t = 1.0;
153  diff = 0.0;
154  err = 0.0;
155  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(x) is correct? \n";
156  std::cout << std::right << std::setw(20) << "t"
157  << std::setw(20) << "v''(x)"
158  << std::setw(20) << "(v'(x+t)-v'(x))/t"
159  << std::setw(20) << "Error"
160  << "\n";
161  for (int i = 0; i < 13; i++) {
162  y = x + t;
163  vy = regret(y,1);
164  diff = (vy-vx)/t;
165  err = std::abs(diff-dv);
166  std::cout << std::scientific << std::setprecision(11) << std::right
167  << std::setw(20) << t
168  << std::setw(20) << dv
169  << std::setw(20) << diff
170  << std::setw(20) << err
171  << "\n";
172  t *= 0.1;
173  }
174  std::cout << "\n";
175  }
176 
177  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
179  xstat_ = Teuchos::dyn_cast<const RiskVector<Real> >(
180  Teuchos::dyn_cast<const Vector<Real> >(x)).getStatistic();
181  if (firstReset_) {
182  dualVector_ = (x0->dual()).clone();
183  firstReset_ = false;
184  }
185  dualVector_->zero();
186  }
187 
188  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
189  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
190  reset(x0,x);
191  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(
192  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
193  vstat_ = Teuchos::dyn_cast<const RiskVector<Real> >(
194  Teuchos::dyn_cast<const Vector<Real> >(v)).getStatistic();
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  RiskVector<Real> &gs = Teuchos::dyn_cast<RiskVector<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.setStatistic(gstat);
232 
233  sampler.sumAll(*(RiskMeasure<Real>::g_),*dualVector_);
234  gs.setVector(*dualVector_);
235  }
236 
238  RiskVector<Real> &hs = Teuchos::dyn_cast<RiskVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(hv));
239  Real stat = RiskMeasure<Real>::val_;
240  Real gstat = 0.0;
241  sampler.sumAll(&stat,&gstat,1);
242  hs.setStatistic(gstat);
243 
244  sampler.sumAll(*(RiskMeasure<Real>::hv_),*dualVector_);
245  hs.setVector(*dualVector_);
246  }
247 };
248 
249 }
250 
251 #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.
const Real getStatistic(const int i=0) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void sumAll(Real *input, Real *output, int dim) const
void setVector(const Vector< Real > &vec)
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 setStatistic(const Real stat)
virtual Real regret(Real x, int deriv=0)=0
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
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)
Teuchos::RCP< Vector< Real > > dualVector_