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