ROL
ROL_ExpectationQuad.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_EXPECTATIONQUAD_HPP
11 #define ROL_EXPECTATIONQUAD_HPP
12 
13 #include "ROL_Types.hpp"
14 
50 namespace ROL {
51 
52 template<class Real>
54 public:
55  virtual ~ExpectationQuad(void) {}
56  ExpectationQuad(void) {}
57 
65  virtual Real regret(Real x, int deriv = 0) = 0;
66 
74  virtual Real error(Real x, int deriv = 0) {
75  const Real one(1), zero(0);
76  Real X = (deriv==0 ? x : (deriv==1 ? one : zero));
77  return regret(x,deriv) - X;
78  }
79 
82  virtual void check(void) {
83  Real zero(0), half(0.5), two(2), one(1), oem3(1.e-3), fem4(5.e-4), p1(0.1);
84  // Check v(0) = 0
85  Real x = zero;
86  Real vx = regret(x,0);
87  std::cout << std::right << std::setw(20) << "CHECK REGRET: v(0) = 0? \n";
88  std::cout << std::right << std::setw(20) << "v(0)" << "\n";
89  std::cout << std::scientific << std::setprecision(11) << std::right
90  << std::setw(20) << std::abs(vx)
91  << "\n";
92  std::cout << "\n";
93  // Check v(x) > x
94  Real scale = two;
95  std::cout << std::right << std::setw(20) << "CHECK REGRET: x < v(x) for |x| > 0? \n";
96  std::cout << std::right << std::setw(20) << "x"
97  << std::right << std::setw(20) << "v(x)"
98  << "\n";
99  for (int i = 0; i < 10; i++) {
100  x = scale*(Real)rand()/(Real)RAND_MAX - scale*half;
101  vx = regret(x,0);
102  std::cout << std::scientific << std::setprecision(11) << std::right
103  << std::setw(20) << x
104  << std::setw(20) << vx
105  << "\n";
106  scale *= two;
107  }
108  std::cout << "\n";
109  // Check v(x) is convex
110  Real y = zero;
111  Real vy = zero;
112  Real z = zero;
113  Real vz = zero;
114  Real l = zero;
115  scale = two;
116  std::cout << std::right << std::setw(20) << "CHECK REGRET: v(x) is convex? \n";
117  std::cout << std::right << std::setw(20) << "v(l*x+(1-l)*y)"
118  << std::setw(20) << "l*v(x)+(1-l)*v(y)"
119  << "\n";
120  for (int i = 0; i < 10; i++) {
121  x = scale*(Real)rand()/(Real)RAND_MAX - scale*half;
122  vx = regret(x,0);
123  y = scale*(Real)rand()/(Real)RAND_MAX - scale*half;
124  vy = regret(y,0);
125  l = (Real)rand()/(Real)RAND_MAX;
126  z = l*x + (one-l)*y;
127  vz = regret(z,0);
128  std::cout << std::scientific << std::setprecision(11) << std::right
129  << std::setw(20) << vz
130  << std::setw(20) << l*vx + (one-l)*vy
131  << "\n";
132  scale *= two;
133  }
134  std::cout << "\n";
135  // Check v'(x)
136  x = oem3*(Real)rand()/(Real)RAND_MAX - fem4;
137  vx = regret(x,0);
138  Real dv = regret(x,1);
139  Real t = one;
140  Real diff = zero;
141  Real err = zero;
142  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(x) is correct? \n";
143  std::cout << std::right << std::setw(20) << "t"
144  << std::setw(20) << "v'(x)"
145  << std::setw(20) << "(v(x+t)-v(x))/t"
146  << std::setw(20) << "Error"
147  << "\n";
148  for (int i = 0; i < 13; i++) {
149  y = x + t;
150  vy = regret(y,0);
151  diff = (vy-vx)/t;
152  err = std::abs(diff-dv);
153  std::cout << std::scientific << std::setprecision(11) << std::right
154  << std::setw(20) << t
155  << std::setw(20) << dv
156  << std::setw(20) << diff
157  << std::setw(20) << err
158  << "\n";
159  t *= p1;
160  }
161  std::cout << "\n";
162  // Check v''(x)
163  x = oem3*(Real)rand()/(Real)RAND_MAX - fem4;
164  vx = regret(x,1);
165  dv = regret(x,2);
166  t = one;
167  diff = zero;
168  err = zero;
169  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(x) is correct? \n";
170  std::cout << std::right << std::setw(20) << "t"
171  << std::setw(20) << "v''(x)"
172  << std::setw(20) << "(v'(x+t)-v'(x))/t"
173  << std::setw(20) << "Error"
174  << "\n";
175  for (int i = 0; i < 13; i++) {
176  y = x + t;
177  vy = regret(y,1);
178  diff = (vy-vx)/t;
179  err = std::abs(diff-dv);
180  std::cout << std::scientific << std::setprecision(11) << std::right
181  << std::setw(20) << t
182  << std::setw(20) << dv
183  << std::setw(20) << diff
184  << std::setw(20) << err
185  << "\n";
186  t *= p1;
187  }
188  std::cout << "\n";
189  }
190 };
191 
192 }
193 
194 #endif
Provides a general interface for risk and error measures generated through the expectation risk quadr...
Contains definitions of custom data types in ROL.
virtual void check(void)
Run default derivative tests for the scalar regret function.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
virtual Real error(Real x, int deriv=0)
Evaluate the scalar error function at x.
virtual Real regret(Real x, int deriv=0)=0
Evaluate the scalar regret function at x.