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 public:
58 
60  : ExpectationQuad<Real>() {
61  beta_ = ((beta > 0.) ? beta : 1.);
62  }
63 
64  TruncatedMeanQuadrangle(Teuchos::ParameterList &parlist)
65  : ExpectationQuad<Real>() {
66  Teuchos::ParameterList &list
67  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Truncated Mean Quadrangle");
68  // Check inputs
69  Real beta = list.get("Threshold",1.);
70  beta_ = ((beta > 0.) ? beta : 1.);
71  }
72 
73  Real error(Real x, int deriv = 0) {
74  bool inside = ( std::abs(x) ? true : false );
75  Real err = 0.0;
76  if (deriv==0) {
77  err = (inside ? 0.5*std::pow(x,2.0)/beta_ : std::abs(x)-0.5*beta_);
78  }
79  else if (deriv==1) {
80  err = (inside ? x/beta_ : ((0.0 < x) - (x < 0.0)));
81  }
82  else {
83  err = (inside ? 1.0/beta_ : 0.0);
84  }
85  return err;
86  }
87 
88  Real regret(Real x, int deriv = 0) {
89  Real X = ((deriv==0) ? x : ((deriv==1) ? 1.0 : 0.0));
90  Real reg = error(x,deriv) + X;
91  return reg;
92  }
93 
94  void checkRegret(void) {
96  // Check v'(beta)
97  Real x = beta_;
98  Real vx = 0.0, vy = 0.0;
99  Real dv = regret(x,1);
100  Real t = 1.0;
101  Real diff = 0.0;
102  Real err = 0.0;
103  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(beta) is correct? \n";
104  std::cout << std::right << std::setw(20) << "t"
105  << std::setw(20) << "v'(x)"
106  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
107  << std::setw(20) << "Error"
108  << "\n";
109  for (int i = 0; i < 13; i++) {
110  vy = regret(x+t,0);
111  vx = regret(x-t,0);
112  diff = (vy-vx)/(2.0*t);
113  err = std::abs(diff-dv);
114  std::cout << std::scientific << std::setprecision(11) << std::right
115  << std::setw(20) << t
116  << std::setw(20) << dv
117  << std::setw(20) << diff
118  << std::setw(20) << err
119  << "\n";
120  t *= 0.1;
121  }
122  std::cout << "\n";
123  // Check v'(-beta)
124  x = -beta_;
125  vx = 0.0;
126  vy = 0.0;
127  dv = regret(x,1);
128  t = 1.0;
129  diff = 0.0;
130  err = 0.0;
131  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-beta) is correct? \n";
132  std::cout << std::right << std::setw(20) << "t"
133  << std::setw(20) << "v'(x)"
134  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
135  << std::setw(20) << "Error"
136  << "\n";
137  for (int i = 0; i < 13; i++) {
138  vy = regret(x+t,0);
139  vx = regret(x-t,0);
140  diff = (vy-vx)/(2.0*t);
141  err = std::abs(diff-dv);
142  std::cout << std::scientific << std::setprecision(11) << std::right
143  << std::setw(20) << t
144  << std::setw(20) << dv
145  << std::setw(20) << diff
146  << std::setw(20) << err
147  << "\n";
148  t *= 0.1;
149  }
150  std::cout << "\n";
151  }
152 
153 };
154 
155 }
156 #endif
TruncatedMeanQuadrangle(Teuchos::ParameterList &parlist)
virtual void checkRegret(void)