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 
47 #include "ROL_ExpectationQuad.hpp"
48 
49 namespace ROL {
50 
51 template<class Real>
53 private:
54 
55  Real beta_;
56 
57  void checkInputs(void) const {
58  Real zero(0);
59  TEUCHOS_TEST_FOR_EXCEPTION((beta_ <= zero), std::invalid_argument,
60  ">>> ERROR (ROL::TruncatedMeanQuadrangle): Threshold must be positive!");
61  }
62 
63 public:
64 
66  : ExpectationQuad<Real>(), beta_(beta) {
67  checkInputs();
68  }
69 
70  TruncatedMeanQuadrangle(Teuchos::ParameterList &parlist)
71  : ExpectationQuad<Real>() {
72  Teuchos::ParameterList &list
73  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Truncated Mean Quadrangle");
74  beta_ = list.get<Real>("Threshold");
75  checkInputs();
76  }
77 
78  Real error(Real x, int deriv = 0) {
79  bool inside = ( std::abs(x) ? true : false );
80  Real err(0), zero(0), half(0.5), one(1), two(2);
81  if (deriv==0) {
82  err = (inside ? half*std::pow(x,two)/beta_ : std::abs(x)-half*beta_);
83  }
84  else if (deriv==1) {
85  err = (inside ? x/beta_ : ((zero < x) - (x < zero)));
86  }
87  else {
88  err = (inside ? one/beta_ : zero);
89  }
90  return err;
91  }
92 
93  Real regret(Real x, int deriv = 0) {
94  Real zero(0), one(1);
95  Real X = ((deriv==0) ? x : ((deriv==1) ? one : zero));
96  Real reg = error(x,deriv) + X;
97  return reg;
98  }
99 
100  void checkRegret(void) {
102  // Check v'(beta)
103  Real x = beta_, zero(0), one(1), two(2), p1(0.1);
104  Real vx = zero, vy = zero;
105  Real dv = regret(x,1);
106  Real t = one;
107  Real diff = zero;
108  Real err = zero;
109  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(beta) is correct? \n";
110  std::cout << std::right << std::setw(20) << "t"
111  << std::setw(20) << "v'(x)"
112  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
113  << std::setw(20) << "Error"
114  << "\n";
115  for (int i = 0; i < 13; i++) {
116  vy = regret(x+t,0);
117  vx = regret(x-t,0);
118  diff = (vy-vx)/(two*t);
119  err = std::abs(diff-dv);
120  std::cout << std::scientific << std::setprecision(11) << std::right
121  << std::setw(20) << t
122  << std::setw(20) << dv
123  << std::setw(20) << diff
124  << std::setw(20) << err
125  << "\n";
126  t *= p1;
127  }
128  std::cout << "\n";
129  // Check v'(-beta)
130  x = -beta_;
131  vx = zero;
132  vy = zero;
133  dv = regret(x,1);
134  t = one;
135  diff = zero;
136  err = zero;
137  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-beta) is correct? \n";
138  std::cout << std::right << std::setw(20) << "t"
139  << std::setw(20) << "v'(x)"
140  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
141  << std::setw(20) << "Error"
142  << "\n";
143  for (int i = 0; i < 13; i++) {
144  vy = regret(x+t,0);
145  vx = regret(x-t,0);
146  diff = (vy-vx)/(two*t);
147  err = std::abs(diff-dv);
148  std::cout << std::scientific << std::setprecision(11) << std::right
149  << std::setw(20) << t
150  << std::setw(20) << dv
151  << std::setw(20) << diff
152  << std::setw(20) << err
153  << "\n";
154  t *= p1;
155  }
156  std::cout << "\n";
157  }
158 
159 };
160 
161 }
162 #endif
TruncatedMeanQuadrangle(Teuchos::ParameterList &parlist)
Provides a general interface for risk measures generated through the expectation risk quadrangle...
virtual void checkRegret(void)
Run default derivative tests for the scalar regret function.
void checkRegret(void)
Run default derivative tests for the scalar regret function.
Real regret(Real x, int deriv=0)
Evaluate the scalar regret function at x.