ROL
ROL_TruncatedMeanQuadrangle.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_TRUNCATEDMEANQUAD_HPP
11 #define ROL_TRUNCATEDMEANQUAD_HPP
12 
13 #include "ROL_ExpectationQuad.hpp"
14 
15 namespace ROL {
16 
17 template<class Real>
19 private:
20 
21  Real beta_;
22 
23  void parseParameterList(ROL::ParameterList &parlist) {
24  std::string type = parlist.sublist("SOL").get("Type","Risk Averse");
25  ROL::ParameterList list;
26  if (type == "Risk Averse") {
27  list = parlist.sublist("SOL").sublist("Risk Measure").sublist("Truncated Mean");
28  }
29  else if (type == "Error") {
30  list = parlist.sublist("SOL").sublist("Error Measure").sublist("Huber");
31  }
32  else if (type == "Deviation") {
33  list = parlist.sublist("SOL").sublist("Deviation Measure").sublist("Truncated Mean");
34  }
35  else if (type == "Regret") {
36  list = parlist.sublist("SOL").sublist("Regret Measure").sublist("Truncated Mean");
37  }
38  beta_ = list.get<Real>("Threshold");
39  }
40 
41  void checkInputs(void) const {
42  Real zero(0);
43  ROL_TEST_FOR_EXCEPTION((beta_ <= zero), std::invalid_argument,
44  ">>> ERROR (ROL::TruncatedMeanQuadrangle): Threshold must be positive!");
45  }
46 
47 public:
48 
50  : ExpectationQuad<Real>(), beta_(beta) {
51  checkInputs();
52  }
53 
54  TruncatedMeanQuadrangle(ROL::ParameterList &parlist)
55  : ExpectationQuad<Real>() {
56  parseParameterList(parlist);
57  checkInputs();
58  }
59 
60  Real error(Real x, int deriv = 0) {
61  bool inside = ( std::abs(x) < beta_ ? true : false );
62  Real err(0), zero(0), half(0.5), one(1), two(2);
63  if (deriv==0) {
64  err = (inside ? half*std::pow(x,two)/beta_ : std::abs(x)-half*beta_);
65  }
66  else if (deriv==1) {
67  err = (inside ? x/beta_ : ((zero < x) - (x < zero)));
68  }
69  else {
70  err = (inside ? one/beta_ : zero);
71  }
72  return err;
73  }
74 
75  Real regret(Real x, int deriv = 0) {
76  Real zero(0), one(1);
77  Real X = ((deriv==0) ? x : ((deriv==1) ? one : zero));
78  Real reg = error(x,deriv) + X;
79  return reg;
80  }
81 
82  void check(void) {
84  // Check v'(beta)
85  Real x = beta_, zero(0), one(1), two(2), p1(0.1);
86  Real vx = zero, vy = zero;
87  Real dv = regret(x,1);
88  Real t = one;
89  Real diff = zero;
90  Real err = zero;
91  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(beta) is correct? \n";
92  std::cout << std::right << std::setw(20) << "t"
93  << std::setw(20) << "v'(x)"
94  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
95  << std::setw(20) << "Error"
96  << "\n";
97  for (int i = 0; i < 13; i++) {
98  vy = regret(x+t,0);
99  vx = regret(x-t,0);
100  diff = (vy-vx)/(two*t);
101  err = std::abs(diff-dv);
102  std::cout << std::scientific << std::setprecision(11) << std::right
103  << std::setw(20) << t
104  << std::setw(20) << dv
105  << std::setw(20) << diff
106  << std::setw(20) << err
107  << "\n";
108  t *= p1;
109  }
110  std::cout << "\n";
111  // Check v'(-beta)
112  x = -beta_;
113  vx = zero;
114  vy = zero;
115  dv = regret(x,1);
116  t = one;
117  diff = zero;
118  err = zero;
119  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-beta) is correct? \n";
120  std::cout << std::right << std::setw(20) << "t"
121  << std::setw(20) << "v'(x)"
122  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
123  << std::setw(20) << "Error"
124  << "\n";
125  for (int i = 0; i < 13; i++) {
126  vy = regret(x+t,0);
127  vx = regret(x-t,0);
128  diff = (vy-vx)/(two*t);
129  err = std::abs(diff-dv);
130  std::cout << std::scientific << std::setprecision(11) << std::right
131  << std::setw(20) << t
132  << std::setw(20) << dv
133  << std::setw(20) << diff
134  << std::setw(20) << err
135  << "\n";
136  t *= p1;
137  }
138  std::cout << "\n";
139  }
140 
141 };
142 
143 }
144 #endif
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 error(Real x, int deriv=0)
Evaluate the scalar error function at x.
void check(void)
Run default derivative tests for the scalar regret function.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
TruncatedMeanQuadrangle(ROL::ParameterList &parlist)
Real regret(Real x, int deriv=0)
Evaluate the scalar regret function at x.
void parseParameterList(ROL::ParameterList &parlist)