ROL
ROL_SmoothedWorstCaseQuadrangle.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_SMOOTHEDWORSTCASEQUAD_HPP
11 #define ROL_SMOOTHEDWORSTCASEQUAD_HPP
12 
13 #include "ROL_ExpectationQuad.hpp"
14 
52 namespace ROL {
53 
54 template<class Real>
56 private:
57 
58  Real eps_;
59 
60  void parseParameterList(ROL::ParameterList &parlist) {
61  std::string type = parlist.sublist("SOL").get("Type","Risk Averse");
62  ROL::ParameterList list;
63  if (type == "Risk Averse") {
64  list = parlist.sublist("SOL").sublist("Risk Measure").sublist("Smoothed Worst Case");
65  }
66  else if (type == "Error") {
67  list = parlist.sublist("SOL").sublist("Error Measure").sublist("Smoothed Worst Case");
68  }
69  else if (type == "Deviation") {
70  list = parlist.sublist("SOL").sublist("Deviation Measure").sublist("Smoothed Upper Range");
71  }
72  else if (type == "Regret") {
73  list = parlist.sublist("SOL").sublist("Regret Measure").sublist("Smoothed Worst Case");
74  }
75  eps_ = list.get<Real>("Smoothing Parameter");
76  }
77 
78  void checkInputs(void) const {
79  Real zero(0);
80  ROL_TEST_FOR_EXCEPTION((eps_ <= zero), std::invalid_argument,
81  ">>> ERROR (ROL::SmoothedWorstCaseQuadrangle): Smoothing parameter must be positive!");
82  }
83 
84 public:
90  : ExpectationQuad<Real>(), eps_(eps) {
91  checkInputs();
92  }
93 
102  SmoothedWorstCaseQuadrangle(ROL::ParameterList &parlist) : ExpectationQuad<Real>() {
103  parseParameterList(parlist);
104  checkInputs();
105  }
106 
107  Real error(Real x, int deriv = 0) {
108  Real err(0), zero(0), half(0.5), one(1);
109  if (deriv == 0) {
110  err = (x <= -eps_) ? -(x+half*eps_) : half*x*x/eps_;
111  }
112  else if (deriv == 1) {
113  err = (x <= -eps_) ? -one : x/eps_;
114  }
115  else {
116  err = (x <= -eps_) ? zero : one/eps_;
117  }
118  return err;
119  }
120 
121  Real regret(Real x, int deriv = 0) {
122  Real zero(0), one(1);
123  Real X = ((deriv==0) ? x : ((deriv==1) ? one : zero));
124  Real reg = error(x,deriv) + X;
125  return reg;
126  }
127 
128  void check(void) {
130  // Check v'(-eps)
131  Real x = -eps_, zero(0), one(1), two(2), p1(0.1);
132  Real vx = zero, vy = zero;
133  Real dv = regret(x,1);
134  Real t = one;
135  Real diff = zero;
136  Real err = zero;
137  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-eps) 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  // check v''(-eps)
158  vx = zero;
159  vy = zero;
160  dv = regret(x,2);
161  t = one;
162  diff = zero;
163  err = zero;
164  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(-eps) is correct? \n";
165  std::cout << std::right << std::setw(20) << "t"
166  << std::setw(20) << "v''(x)"
167  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
168  << std::setw(20) << "Error"
169  << "\n";
170  for (int i = 0; i < 13; i++) {
171  vy = regret(x+t,1);
172  vx = regret(x-t,1);
173  diff = (vy-vx)/(two*t);
174  err = std::abs(diff-dv);
175  std::cout << std::scientific << std::setprecision(11) << std::right
176  << std::setw(20) << t
177  << std::setw(20) << dv
178  << std::setw(20) << diff
179  << std::setw(20) << err
180  << "\n";
181  t *= p1;
182  }
183  std::cout << "\n";
184  // Check v'(0)
185  x = zero;
186  vx = zero;
187  vy = zero;
188  dv = regret(x,1);
189  t = one;
190  diff = zero;
191  err = zero;
192  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(0) is correct? \n";
193  std::cout << std::right << std::setw(20) << "t"
194  << std::setw(20) << "v'(x)"
195  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
196  << std::setw(20) << "Error"
197  << "\n";
198  for (int i = 0; i < 13; i++) {
199  vy = regret(x+t,0);
200  vx = regret(x-t,0);
201  diff = (vy-vx)/(two*t);
202  err = std::abs(diff-dv);
203  std::cout << std::scientific << std::setprecision(11) << std::right
204  << std::setw(20) << t
205  << std::setw(20) << dv
206  << std::setw(20) << diff
207  << std::setw(20) << err
208  << "\n";
209  t *= p1;
210  }
211  std::cout << "\n";
212  // check v''(0)
213  vx = zero;
214  vy = zero;
215  dv = regret(x,2);
216  t = one;
217  diff = zero;
218  err = zero;
219  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(0) is correct? \n";
220  std::cout << std::right << std::setw(20) << "t"
221  << std::setw(20) << "v''(x)"
222  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
223  << std::setw(20) << "Error"
224  << "\n";
225  for (int i = 0; i < 13; i++) {
226  vy = regret(x+t,1);
227  vx = regret(x-t,1);
228  diff = (vy-vx)/(two*t);
229  err = std::abs(diff-dv);
230  std::cout << std::scientific << std::setprecision(11) << std::right
231  << std::setw(20) << t
232  << std::setw(20) << dv
233  << std::setw(20) << diff
234  << std::setw(20) << err
235  << "\n";
236  t *= p1;
237  }
238  std::cout << "\n";
239  // Check v'(-1-eps)
240  x = -eps_-one;
241  vx = zero;
242  vy = zero;
243  dv = regret(x,1);
244  t = one;
245  diff = zero;
246  err = zero;
247  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-1-eps) is correct? \n";
248  std::cout << std::right << std::setw(20) << "t"
249  << std::setw(20) << "v'(x)"
250  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
251  << std::setw(20) << "Error"
252  << "\n";
253  for (int i = 0; i < 13; i++) {
254  vy = regret(x+t,0);
255  vx = regret(x-t,0);
256  diff = (vy-vx)/(two*t);
257  err = std::abs(diff-dv);
258  std::cout << std::scientific << std::setprecision(11) << std::right
259  << std::setw(20) << t
260  << std::setw(20) << dv
261  << std::setw(20) << diff
262  << std::setw(20) << err
263  << "\n";
264  t *= p1;
265  }
266  std::cout << "\n";
267  // check v''(-1-eps)
268  vx = zero;
269  vy = zero;
270  dv = regret(x,2);
271  t = one;
272  diff = zero;
273  err = zero;
274  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(-1-eps) is correct? \n";
275  std::cout << std::right << std::setw(20) << "t"
276  << std::setw(20) << "v''(x)"
277  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
278  << std::setw(20) << "Error"
279  << "\n";
280  for (int i = 0; i < 13; i++) {
281  vy = regret(x+t,1);
282  vx = regret(x-t,1);
283  diff = (vy-vx)/(two*t);
284  err = std::abs(diff-dv);
285  std::cout << std::scientific << std::setprecision(11) << std::right
286  << std::setw(20) << t
287  << std::setw(20) << dv
288  << std::setw(20) << diff
289  << std::setw(20) << err
290  << "\n";
291  t *= p1;
292  }
293  std::cout << "\n";
294  }
295 
296 };
297 
298 }
299 #endif
Provides an interface for a smoothed version of the worst-case scenario risk measure using the expect...
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.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
SmoothedWorstCaseQuadrangle(ROL::ParameterList &parlist)
Constructor.
Real regret(Real x, int deriv=0)
Evaluate the scalar regret function at x.
SmoothedWorstCaseQuadrangle(const Real eps)
Constructor.
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.
void parseParameterList(ROL::ParameterList &parlist)