ROL
ROL_TruncatedMeanQuadrangle.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_TRUNCATEDMEANQUAD_HPP
45#define ROL_TRUNCATEDMEANQUAD_HPP
46
48
49namespace ROL {
50
51template<class Real>
53private:
54
55 Real beta_;
56
57 void parseParameterList(ROL::ParameterList &parlist) {
58 std::string type = parlist.sublist("SOL").get("Type","Risk Averse");
59 ROL::ParameterList list;
60 if (type == "Risk Averse") {
61 list = parlist.sublist("SOL").sublist("Risk Measure").sublist("Truncated Mean");
62 }
63 else if (type == "Error") {
64 list = parlist.sublist("SOL").sublist("Error Measure").sublist("Huber");
65 }
66 else if (type == "Deviation") {
67 list = parlist.sublist("SOL").sublist("Deviation Measure").sublist("Truncated Mean");
68 }
69 else if (type == "Regret") {
70 list = parlist.sublist("SOL").sublist("Regret Measure").sublist("Truncated Mean");
71 }
72 beta_ = list.get<Real>("Threshold");
73 }
74
75 void checkInputs(void) const {
76 Real zero(0);
77 ROL_TEST_FOR_EXCEPTION((beta_ <= zero), std::invalid_argument,
78 ">>> ERROR (ROL::TruncatedMeanQuadrangle): Threshold must be positive!");
79 }
80
81public:
82
84 : ExpectationQuad<Real>(), beta_(beta) {
86 }
87
88 TruncatedMeanQuadrangle(ROL::ParameterList &parlist)
89 : ExpectationQuad<Real>() {
90 parseParameterList(parlist);
92 }
93
94 Real error(Real x, int deriv = 0) {
95 bool inside = ( std::abs(x) < beta_ ? true : false );
96 Real err(0), zero(0), half(0.5), one(1), two(2);
97 if (deriv==0) {
98 err = (inside ? half*std::pow(x,two)/beta_ : std::abs(x)-half*beta_);
99 }
100 else if (deriv==1) {
101 err = (inside ? x/beta_ : ((zero < x) - (x < zero)));
102 }
103 else {
104 err = (inside ? one/beta_ : zero);
105 }
106 return err;
107 }
108
109 Real regret(Real x, int deriv = 0) {
110 Real zero(0), one(1);
111 Real X = ((deriv==0) ? x : ((deriv==1) ? one : zero));
112 Real reg = error(x,deriv) + X;
113 return reg;
114 }
115
116 void check(void) {
118 // Check v'(beta)
119 Real x = beta_, zero(0), one(1), two(2), p1(0.1);
120 Real vx = zero, vy = zero;
121 Real dv = regret(x,1);
122 Real t = one;
123 Real diff = zero;
124 Real err = zero;
125 std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(beta) is correct? \n";
126 std::cout << std::right << std::setw(20) << "t"
127 << std::setw(20) << "v'(x)"
128 << std::setw(20) << "(v(x+t)-v(x-t))/2t"
129 << std::setw(20) << "Error"
130 << "\n";
131 for (int i = 0; i < 13; i++) {
132 vy = regret(x+t,0);
133 vx = regret(x-t,0);
134 diff = (vy-vx)/(two*t);
135 err = std::abs(diff-dv);
136 std::cout << std::scientific << std::setprecision(11) << std::right
137 << std::setw(20) << t
138 << std::setw(20) << dv
139 << std::setw(20) << diff
140 << std::setw(20) << err
141 << "\n";
142 t *= p1;
143 }
144 std::cout << "\n";
145 // Check v'(-beta)
146 x = -beta_;
147 vx = zero;
148 vy = zero;
149 dv = regret(x,1);
150 t = one;
151 diff = zero;
152 err = zero;
153 std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-beta) is correct? \n";
154 std::cout << std::right << std::setw(20) << "t"
155 << std::setw(20) << "v'(x)"
156 << std::setw(20) << "(v(x+t)-v(x-t))/2t"
157 << std::setw(20) << "Error"
158 << "\n";
159 for (int i = 0; i < 13; i++) {
160 vy = regret(x+t,0);
161 vx = regret(x-t,0);
162 diff = (vy-vx)/(two*t);
163 err = std::abs(diff-dv);
164 std::cout << std::scientific << std::setprecision(11) << std::right
165 << std::setw(20) << t
166 << std::setw(20) << dv
167 << std::setw(20) << diff
168 << std::setw(20) << err
169 << "\n";
170 t *= p1;
171 }
172 std::cout << "\n";
173 }
174
175};
176
177}
178#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0 zero)()
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.
Real regret(Real x, int deriv=0)
Evaluate the scalar regret function at x.
void check(void)
Run default derivative tests for the scalar regret function.
void parseParameterList(ROL::ParameterList &parlist)
Real error(Real x, int deriv=0)
Evaluate the scalar error function at x.
TruncatedMeanQuadrangle(ROL::ParameterList &parlist)