ROL
ROL_MoreauYosidaCVaR.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_MOREAUYOSIDACVAR_HPP
11 #define ROL_MOREAUYOSIDACVAR_HPP
12 
13 #include "ROL_ExpectationQuad.hpp"
14 
68 namespace ROL {
69 
70 template<class Real>
71 class MoreauYosidaCVaR : public ExpectationQuad<Real> {
72 private:
73 
74  Real prob_;
75  Real eps_;
76 
77  Real omp_;
78  Real ub_;
79 
80  void parseParameterList(ROL::ParameterList &parlist) {
81  std::string type = parlist.sublist("SOL").get("Type","Risk Averse");
82  ROL::ParameterList list;
83  if (type == "Risk Averse") {
84  list = parlist.sublist("SOL").sublist("Risk Measure").sublist("Moreau-Yosida CVaR");
85  }
86  else if (type == "Error") {
87  list = parlist.sublist("SOL").sublist("Error Measure").sublist("Moreau-Yosida-Koenker-Bassett");
88  }
89  else if (type == "Deviation") {
90  list = parlist.sublist("SOL").sublist("Deviation Measure").sublist("Moreau-Yosida CVaR");
91  }
92  else if (type == "Regret") {
93  list = parlist.sublist("SOL").sublist("Regret Measure").sublist("Moreau-Yosida Mean Absolute Loss");
94  }
95  prob_ = list.get<Real>("Confidence Level");
96  eps_ = list.get<Real>("Smoothing Parameter");
97  }
98 
99  void checkInputs(void) const {
100  Real zero(0), one(1);
101  ROL_TEST_FOR_EXCEPTION((prob_ <= zero) || (prob_ >= one), std::invalid_argument,
102  ">>> ERROR (ROL::MoreauYosidaCVaR): Confidence level must be between 0 and 1!");
103  ROL_TEST_FOR_EXCEPTION((eps_ <= zero), std::invalid_argument,
104  ">>> ERROR (ROL::MoreauYosidaCVaR): Smoothing parameter must be positive!");
105  }
106 
107  void setParameters(void) {
108  Real one(1);
109  omp_ = one-prob_;
110  ub_ = eps_/omp_;
111  }
112 
113 public:
119  MoreauYosidaCVaR(Real prob, Real eps )
120  : ExpectationQuad<Real>(), prob_(prob), eps_(eps) {
121  checkInputs();
122  setParameters();
123  }
124 
134  MoreauYosidaCVaR(ROL::ParameterList &parlist)
135  : ExpectationQuad<Real>() {
136  parseParameterList(parlist);
137  checkInputs();
138  setParameters();
139  }
140 
141  Real error(Real x, int deriv = 0) {
142  Real zero(0), one(1);
143  Real X = ((deriv==0) ? x : ((deriv==1) ? one : zero));
144  return regret(x,deriv) - X;
145  }
146 
147  Real regret(Real x, int deriv = 0) {
148  Real zero(0), half(0.5), one(1), reg(0);
149  if ( x <= zero ) {
150  reg = 0;
151  }
152  else if ( x >= ub_ ) {
153  reg = ((deriv == 0) ? (x-half*ub_)/omp_
154  : ((deriv == 1) ? one/omp_ : zero));
155  }
156  else {
157  reg = ((deriv == 0) ? half/eps_*x*x
158  : ((deriv == 1) ? x/eps_ : one/eps_));
159  }
160  return reg;
161  }
162 
163  void check(void) {
165  Real zero(0), one(1), two(2), p1(0.1);
166  // Check v'(eps)
167  Real x = eps_;
168  Real vx = zero, vy = zero;
169  Real dv = regret(x,1);
170  Real t = one;
171  Real diff = zero;
172  Real err = zero;
173  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(eps) is correct? \n";
174  std::cout << std::right << std::setw(20) << "t"
175  << std::setw(20) << "v'(x)"
176  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
177  << std::setw(20) << "Error"
178  << "\n";
179  for (int i = 0; i < 13; i++) {
180  vy = regret(x+t,0);
181  vx = regret(x-t,0);
182  diff = (vy-vx)/(two*t);
183  err = std::abs(diff-dv);
184  std::cout << std::scientific << std::setprecision(11) << std::right
185  << std::setw(20) << t
186  << std::setw(20) << dv
187  << std::setw(20) << diff
188  << std::setw(20) << err
189  << "\n";
190  t *= p1;
191  }
192  std::cout << "\n";
193  // check v''(eps)
194  vx = zero;
195  vy = zero;
196  dv = regret(x,2);
197  t = one;
198  diff = zero;
199  err = zero;
200  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(eps) is correct? \n";
201  std::cout << std::right << std::setw(20) << "t"
202  << std::setw(20) << "v''(x)"
203  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
204  << std::setw(20) << "Error"
205  << "\n";
206  for (int i = 0; i < 13; i++) {
207  vy = regret(x+t,1);
208  vx = regret(x-t,1);
209  diff = (vy-vx)/(two*t);
210  err = std::abs(diff-dv);
211  std::cout << std::scientific << std::setprecision(11) << std::right
212  << std::setw(20) << t
213  << std::setw(20) << dv
214  << std::setw(20) << diff
215  << std::setw(20) << err
216  << "\n";
217  t *= p1;
218  }
219  std::cout << "\n";
220  // Check v'(0)
221  x = zero;
222  vx = zero;
223  vy = zero;
224  dv = regret(x,1);
225  t = one;
226  diff = zero;
227  err = zero;
228  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(0) is correct? \n";
229  std::cout << std::right << std::setw(20) << "t"
230  << std::setw(20) << "v'(x)"
231  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
232  << std::setw(20) << "Error"
233  << "\n";
234  for (int i = 0; i < 13; i++) {
235  vy = regret(x+t,0);
236  vx = regret(x-t,0);
237  diff = (vy-vx)/(two*t);
238  err = std::abs(diff-dv);
239  std::cout << std::scientific << std::setprecision(11) << std::right
240  << std::setw(20) << t
241  << std::setw(20) << dv
242  << std::setw(20) << diff
243  << std::setw(20) << err
244  << "\n";
245  t *= p1;
246  }
247  std::cout << "\n";
248  // check v''(eps)
249  vx = zero;
250  vy = zero;
251  dv = regret(x,2);
252  t = one;
253  diff = zero;
254  err = zero;
255  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(0) is correct? \n";
256  std::cout << std::right << std::setw(20) << "t"
257  << std::setw(20) << "v''(x)"
258  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
259  << std::setw(20) << "Error"
260  << "\n";
261  for (int i = 0; i < 13; i++) {
262  vy = regret(x+t,1);
263  vx = regret(x-t,1);
264  diff = (vy-vx)/(two*t);
265  err = std::abs(diff-dv);
266  std::cout << std::scientific << std::setprecision(11) << std::right
267  << std::setw(20) << t
268  << std::setw(20) << dv
269  << std::setw(20) << diff
270  << std::setw(20) << err
271  << "\n";
272  t *= p1;
273  }
274  std::cout << "\n";
275  // Check v'(0)
276  x = -eps_;
277  vx = zero;
278  vy = zero;
279  dv = regret(x,1);
280  t = one;
281  diff = zero;
282  err = zero;
283  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-eps) is correct? \n";
284  std::cout << std::right << std::setw(20) << "t"
285  << std::setw(20) << "v'(x)"
286  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
287  << std::setw(20) << "Error"
288  << "\n";
289  for (int i = 0; i < 13; i++) {
290  vy = regret(x+t,0);
291  vx = regret(x-t,0);
292  diff = (vy-vx)/(two*t);
293  err = std::abs(diff-dv);
294  std::cout << std::scientific << std::setprecision(11) << std::right
295  << std::setw(20) << t
296  << std::setw(20) << dv
297  << std::setw(20) << diff
298  << std::setw(20) << err
299  << "\n";
300  t *= p1;
301  }
302  std::cout << "\n";
303  // check v''(eps)
304  vx = zero;
305  vy = zero;
306  dv = regret(x,2);
307  t = one;
308  diff = zero;
309  err = zero;
310  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(-eps) is correct? \n";
311  std::cout << std::right << std::setw(20) << "t"
312  << std::setw(20) << "v''(x)"
313  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
314  << std::setw(20) << "Error"
315  << "\n";
316  for (int i = 0; i < 13; i++) {
317  vy = regret(x+t,1);
318  vx = regret(x-t,1);
319  diff = (vy-vx)/(two*t);
320  err = std::abs(diff-dv);
321  std::cout << std::scientific << std::setprecision(11) << std::right
322  << std::setw(20) << t
323  << std::setw(20) << dv
324  << std::setw(20) << diff
325  << std::setw(20) << err
326  << "\n";
327  t *= p1;
328  }
329  std::cout << "\n";
330  }
331 
332 };
333 
334 }
335 #endif
void check(void)
Run default derivative tests for the scalar regret function.
Provides a general interface for risk and error measures generated through the expectation risk quadr...
Provides an interface for a smooth approximation of the conditional value-at-risk.
virtual void check(void)
Run default derivative tests for the scalar regret function.
void checkInputs(void) const
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
void parseParameterList(ROL::ParameterList &parlist)
MoreauYosidaCVaR(ROL::ParameterList &parlist)
Constructor.
MoreauYosidaCVaR(Real prob, Real eps)
Constructor.
Real error(Real x, int deriv=0)
Evaluate the scalar error function at x.
Real regret(Real x, int deriv=0)
Evaluate the scalar regret function at x.