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_Types.hpp"
48
84namespace ROL {
85
86template<class Real>
88public:
89 virtual ~ExpectationQuad(void) {}
91
99 virtual Real regret(Real x, int deriv = 0) = 0;
100
108 virtual Real error(Real x, int deriv = 0) {
109 const Real one(1), zero(0);
110 Real X = (deriv==0 ? x : (deriv==1 ? one : zero));
111 return regret(x,deriv) - X;
112 }
113
116 virtual void check(void) {
117 Real zero(0), half(0.5), two(2), one(1), oem3(1.e-3), fem4(5.e-4), p1(0.1);
118 // Check v(0) = 0
119 Real x = zero;
120 Real vx = regret(x,0);
121 std::cout << std::right << std::setw(20) << "CHECK REGRET: v(0) = 0? \n";
122 std::cout << std::right << std::setw(20) << "v(0)" << "\n";
123 std::cout << std::scientific << std::setprecision(11) << std::right
124 << std::setw(20) << std::abs(vx)
125 << "\n";
126 std::cout << "\n";
127 // Check v(x) > x
128 Real scale = two;
129 std::cout << std::right << std::setw(20) << "CHECK REGRET: x < v(x) for |x| > 0? \n";
130 std::cout << std::right << std::setw(20) << "x"
131 << std::right << std::setw(20) << "v(x)"
132 << "\n";
133 for (int i = 0; i < 10; i++) {
134 x = scale*(Real)rand()/(Real)RAND_MAX - scale*half;
135 vx = regret(x,0);
136 std::cout << std::scientific << std::setprecision(11) << std::right
137 << std::setw(20) << x
138 << std::setw(20) << vx
139 << "\n";
140 scale *= two;
141 }
142 std::cout << "\n";
143 // Check v(x) is convex
144 Real y = zero;
145 Real vy = zero;
146 Real z = zero;
147 Real vz = zero;
148 Real l = zero;
149 scale = two;
150 std::cout << std::right << std::setw(20) << "CHECK REGRET: v(x) is convex? \n";
151 std::cout << std::right << std::setw(20) << "v(l*x+(1-l)*y)"
152 << std::setw(20) << "l*v(x)+(1-l)*v(y)"
153 << "\n";
154 for (int i = 0; i < 10; i++) {
155 x = scale*(Real)rand()/(Real)RAND_MAX - scale*half;
156 vx = regret(x,0);
157 y = scale*(Real)rand()/(Real)RAND_MAX - scale*half;
158 vy = regret(y,0);
159 l = (Real)rand()/(Real)RAND_MAX;
160 z = l*x + (one-l)*y;
161 vz = regret(z,0);
162 std::cout << std::scientific << std::setprecision(11) << std::right
163 << std::setw(20) << vz
164 << std::setw(20) << l*vx + (one-l)*vy
165 << "\n";
166 scale *= two;
167 }
168 std::cout << "\n";
169 // Check v'(x)
170 x = oem3*(Real)rand()/(Real)RAND_MAX - fem4;
171 vx = regret(x,0);
172 Real dv = regret(x,1);
173 Real t = one;
174 Real diff = zero;
175 Real err = zero;
176 std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(x) is correct? \n";
177 std::cout << std::right << std::setw(20) << "t"
178 << std::setw(20) << "v'(x)"
179 << std::setw(20) << "(v(x+t)-v(x))/t"
180 << std::setw(20) << "Error"
181 << "\n";
182 for (int i = 0; i < 13; i++) {
183 y = x + t;
184 vy = regret(y,0);
185 diff = (vy-vx)/t;
186 err = std::abs(diff-dv);
187 std::cout << std::scientific << std::setprecision(11) << std::right
188 << std::setw(20) << t
189 << std::setw(20) << dv
190 << std::setw(20) << diff
191 << std::setw(20) << err
192 << "\n";
193 t *= p1;
194 }
195 std::cout << "\n";
196 // Check v''(x)
197 x = oem3*(Real)rand()/(Real)RAND_MAX - fem4;
198 vx = regret(x,1);
199 dv = regret(x,2);
200 t = one;
201 diff = zero;
202 err = zero;
203 std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(x) is correct? \n";
204 std::cout << std::right << std::setw(20) << "t"
205 << std::setw(20) << "v''(x)"
206 << std::setw(20) << "(v'(x+t)-v'(x))/t"
207 << std::setw(20) << "Error"
208 << "\n";
209 for (int i = 0; i < 13; i++) {
210 y = x + t;
211 vy = regret(y,1);
212 diff = (vy-vx)/t;
213 err = std::abs(diff-dv);
214 std::cout << std::scientific << std::setprecision(11) << std::right
215 << std::setw(20) << t
216 << std::setw(20) << dv
217 << std::setw(20) << diff
218 << std::setw(20) << err
219 << "\n";
220 t *= p1;
221 }
222 std::cout << "\n";
223 }
224};
225
226}
227
228#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Contains definitions of custom data types in ROL.
Provides a general interface for risk and error measures generated through the expectation risk quadr...
virtual void check(void)
Run default derivative tests for the scalar regret function.
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.