ROL
ROL_MoreauYosidaCVaR.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_MOREAUYOSIDACVAR_HPP
45 #define ROL_MOREAUYOSIDACVAR_HPP
46 
47 #include "ROL_ExpectationQuad.hpp"
48 
102 namespace ROL {
103 
104 template<class Real>
105 class MoreauYosidaCVaR : public ExpectationQuad<Real> {
106 private:
107 
108  Real prob_;
109  Real eps_;
110 
111  Real omp_;
112  Real ub_;
113 
114  void checkInputs(void) const {
115  Real zero(0), one(1);
116  TEUCHOS_TEST_FOR_EXCEPTION((prob_ <= zero) || (prob_ >= one), std::invalid_argument,
117  ">>> ERROR (ROL::MoreauYosidaCVaR): Confidence level must be between 0 and 1!");
118  TEUCHOS_TEST_FOR_EXCEPTION((eps_ <= zero), std::invalid_argument,
119  ">>> ERROR (ROL::MoreauYosidaCVaR): Smoothing parameter must be positive!");
120  }
121 
122  void setParameters(void) {
123  Real one(1);
124  omp_ = one-prob_;
125  ub_ = eps_/omp_;
126  }
127 
128 public:
134  MoreauYosidaCVaR(Real prob, Real eps )
135  : ExpectationQuad<Real>(), prob_(prob), eps_(eps) {
136  checkInputs();
137  setParameters();
138  }
139 
149  MoreauYosidaCVaR(Teuchos::ParameterList &parlist)
150  : ExpectationQuad<Real>() {
151  Teuchos::ParameterList& list
152  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Moreau-Yosida CVaR");
153  prob_ = list.get<Real>("Confidence Level");
154  eps_ = list.get<Real>("Smoothing Parameter");
155  checkInputs();
156  setParameters();
157  }
158 
159  Real error(Real x, int deriv = 0) {
160  Real zero(0), one(1);
161  Real X = ((deriv==0) ? x : ((deriv==1) ? one : zero));
162  return regret(x,deriv) - X;
163  }
164 
165  Real regret(Real x, int deriv = 0) {
166  Real zero(0), half(0.5), one(1), reg(0);
167  if ( x <= zero ) {
168  reg = 0;
169  }
170  else if ( x >= ub_ ) {
171  reg = ((deriv == 0) ? (x-half*ub_)/omp_
172  : ((deriv == 1) ? one/omp_ : zero));
173  }
174  else {
175  reg = ((deriv == 0) ? half/eps_*x*x
176  : ((deriv == 1) ? x/eps_ : one/eps_));
177  }
178  return reg;
179  }
180 
181  void checkRegret(void) {
183  Real zero(0), one(1), two(2), p1(0.1);
184  // Check v'(eps)
185  Real x = eps_;
186  Real vx = zero, vy = zero;
187  Real dv = regret(x,1);
188  Real t = one;
189  Real diff = zero;
190  Real err = zero;
191  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(eps) is correct? \n";
192  std::cout << std::right << std::setw(20) << "t"
193  << std::setw(20) << "v'(x)"
194  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
195  << std::setw(20) << "Error"
196  << "\n";
197  for (int i = 0; i < 13; i++) {
198  vy = regret(x+t,0);
199  vx = regret(x-t,0);
200  diff = (vy-vx)/(two*t);
201  err = std::abs(diff-dv);
202  std::cout << std::scientific << std::setprecision(11) << std::right
203  << std::setw(20) << t
204  << std::setw(20) << dv
205  << std::setw(20) << diff
206  << std::setw(20) << err
207  << "\n";
208  t *= p1;
209  }
210  std::cout << "\n";
211  // check v''(eps)
212  vx = zero;
213  vy = zero;
214  dv = regret(x,2);
215  t = one;
216  diff = zero;
217  err = zero;
218  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(eps) is correct? \n";
219  std::cout << std::right << std::setw(20) << "t"
220  << std::setw(20) << "v''(x)"
221  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
222  << std::setw(20) << "Error"
223  << "\n";
224  for (int i = 0; i < 13; i++) {
225  vy = regret(x+t,1);
226  vx = regret(x-t,1);
227  diff = (vy-vx)/(two*t);
228  err = std::abs(diff-dv);
229  std::cout << std::scientific << std::setprecision(11) << std::right
230  << std::setw(20) << t
231  << std::setw(20) << dv
232  << std::setw(20) << diff
233  << std::setw(20) << err
234  << "\n";
235  t *= p1;
236  }
237  std::cout << "\n";
238  // Check v'(0)
239  x = zero;
240  vx = zero;
241  vy = zero;
242  dv = regret(x,1);
243  t = one;
244  diff = zero;
245  err = zero;
246  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(0) is correct? \n";
247  std::cout << std::right << std::setw(20) << "t"
248  << std::setw(20) << "v'(x)"
249  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
250  << std::setw(20) << "Error"
251  << "\n";
252  for (int i = 0; i < 13; i++) {
253  vy = regret(x+t,0);
254  vx = regret(x-t,0);
255  diff = (vy-vx)/(two*t);
256  err = std::abs(diff-dv);
257  std::cout << std::scientific << std::setprecision(11) << std::right
258  << std::setw(20) << t
259  << std::setw(20) << dv
260  << std::setw(20) << diff
261  << std::setw(20) << err
262  << "\n";
263  t *= p1;
264  }
265  std::cout << "\n";
266  // check v''(eps)
267  vx = zero;
268  vy = zero;
269  dv = regret(x,2);
270  t = one;
271  diff = zero;
272  err = zero;
273  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(0) is correct? \n";
274  std::cout << std::right << std::setw(20) << "t"
275  << std::setw(20) << "v''(x)"
276  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
277  << std::setw(20) << "Error"
278  << "\n";
279  for (int i = 0; i < 13; i++) {
280  vy = regret(x+t,1);
281  vx = regret(x-t,1);
282  diff = (vy-vx)/(two*t);
283  err = std::abs(diff-dv);
284  std::cout << std::scientific << std::setprecision(11) << std::right
285  << std::setw(20) << t
286  << std::setw(20) << dv
287  << std::setw(20) << diff
288  << std::setw(20) << err
289  << "\n";
290  t *= p1;
291  }
292  std::cout << "\n";
293  // Check v'(0)
294  x = -eps_;
295  vx = zero;
296  vy = zero;
297  dv = regret(x,1);
298  t = one;
299  diff = zero;
300  err = zero;
301  std::cout << std::right << std::setw(20) << "CHECK REGRET: v'(-eps) is correct? \n";
302  std::cout << std::right << std::setw(20) << "t"
303  << std::setw(20) << "v'(x)"
304  << std::setw(20) << "(v(x+t)-v(x-t))/2t"
305  << std::setw(20) << "Error"
306  << "\n";
307  for (int i = 0; i < 13; i++) {
308  vy = regret(x+t,0);
309  vx = regret(x-t,0);
310  diff = (vy-vx)/(two*t);
311  err = std::abs(diff-dv);
312  std::cout << std::scientific << std::setprecision(11) << std::right
313  << std::setw(20) << t
314  << std::setw(20) << dv
315  << std::setw(20) << diff
316  << std::setw(20) << err
317  << "\n";
318  t *= p1;
319  }
320  std::cout << "\n";
321  // check v''(eps)
322  vx = zero;
323  vy = zero;
324  dv = regret(x,2);
325  t = one;
326  diff = zero;
327  err = zero;
328  std::cout << std::right << std::setw(20) << "CHECK REGRET: v''(-eps) is correct? \n";
329  std::cout << std::right << std::setw(20) << "t"
330  << std::setw(20) << "v''(x)"
331  << std::setw(20) << "(v'(x+t)-v'(x-t))/2t"
332  << std::setw(20) << "Error"
333  << "\n";
334  for (int i = 0; i < 13; i++) {
335  vy = regret(x+t,1);
336  vx = regret(x-t,1);
337  diff = (vy-vx)/(two*t);
338  err = std::abs(diff-dv);
339  std::cout << std::scientific << std::setprecision(11) << std::right
340  << std::setw(20) << t
341  << std::setw(20) << dv
342  << std::setw(20) << diff
343  << std::setw(20) << err
344  << "\n";
345  t *= p1;
346  }
347  std::cout << "\n";
348  }
349 
350 };
351 
352 }
353 #endif
Provides a general interface for risk measures generated through the expectation risk quadrangle...
virtual void checkRegret(void)
Run default derivative tests for the scalar regret function.
MoreauYosidaCVaR(Teuchos::ParameterList &parlist)
Constructor.
Provides an interface for a smooth approximation of the conditional value-at-risk.
void checkRegret(void)
Run default derivative tests for the scalar regret function.
MoreauYosidaCVaR(Real prob, Real eps)
Constructor.
Real error(Real x, int deriv=0)
Real regret(Real x, int deriv=0)
Evaluate the scalar regret function at x.