ROL
ROL_PlusFunction.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_PLUSFUNCTION_HPP
11 #define ROL_PLUSFUNCTION_HPP
12 
13 #include "ROL_Types.hpp"
14 #include "ROL_PositiveFunction.hpp"
15 #include "ROL_Distribution.hpp"
17 
18 namespace ROL {
19 
20 template<class Real>
21 class PlusFunction : public PositiveFunction<Real> {
22 private:
23  ROL::Ptr<Distribution<Real> > dist_;
24  Real param_;
25 
26 public:
27  PlusFunction(ROL::Ptr<Distribution<Real> > &dist, Real param = 1.) : dist_(dist) {
28  param_ = ((param <= 0) ? 1.e-2 : param);
29  }
30 
31  PlusFunction(ROL::ParameterList &parlist) {
32  Real param(1.e-1), zero(0), one(1);
33  ROL::ParameterList pfList;
34  if (parlist.isSublist("Plus Function")) {
35  param = parlist.sublist("Plus Function").get("Smoothing Parameter",1.);
36  pfList = parlist.sublist("Plus Function");
37  }
38  else {
39  param = parlist.get("Smoothing Parameter",1.);
40  pfList = parlist;
41  }
42  param_ = ((param <= zero) ? one : param);
43  dist_ = DistributionFactory<Real>(pfList);
44  }
45 
46  Real evaluate(Real input, int deriv) {
47  Real val = 0.0;
48  switch(deriv) {
49  case 0: val = param_*dist_->integrateCDF(input/param_); break;
50  case 1: val = dist_->evaluateCDF(input/param_); break;
51  case 2: val = dist_->evaluatePDF(input/param_)/param_; break;
52  }
53  return val;
54  }
55 
56  void test(Real x) {
57  // FIRST DERIVATIVE
58  Real vx = evaluate(x,0);
59  Real vy = 0.0;
60  Real dv = evaluate(x,1);
61  Real t = 1.0;
62  Real diff = 0.0;
63  Real err = 0.0;
64  std::cout << std::right << std::setw(20) << "CHECK PLUS FUNCTION: p'(x) with x = "
65  << x << " is correct?\n";
66  std::cout << std::right << std::setw(20) << "t"
67  << std::setw(20) << "p'(x)"
68  << std::setw(20) << "(p(x+t)-p(x))/t"
69  << std::setw(20) << "Error"
70  << "\n";
71  for (int i = 0; i < 13; i++) {
72  vy = evaluate(x+t,0);
73  diff = (vy-vx)/t;
74  err = std::abs(diff-dv);
75  std::cout << std::scientific << std::setprecision(11) << std::right
76  << std::setw(20) << t
77  << std::setw(20) << dv
78  << std::setw(20) << diff
79  << std::setw(20) << err
80  << "\n";
81  t *= 0.1;
82  }
83  std::cout << "\n";
84  // SECOND DERIVATIVE
85  vx = evaluate(x,1);
86  vy = 0.0;
87  dv = evaluate(x,2);
88  t = 1.0;
89  diff = 0.0;
90  err = 0.0;
91  std::cout << std::right << std::setw(20) << "CHECK PLUS FUNCTION: p''(x) with x = "
92  << x << " is correct?\n";
93  std::cout << std::right << std::setw(20) << "t"
94  << std::setw(20) << "p''(x)"
95  << std::setw(20) << "(p'(x+t)-p'(x))/t"
96  << std::setw(20) << "Error"
97  << "\n";
98  for (int i = 0; i < 13; i++) {
99  vy = evaluate(x+t,1);
100  diff = (vy-vx)/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 *= 0.1;
109  }
110  std::cout << "\n";
111  }
112 };
113 
114 }
115 
116 #endif
ROL::Ptr< Distribution< Real > > dist_
Contains definitions of custom data types in ROL.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
PlusFunction(ROL::ParameterList &parlist)
Real evaluate(Real input, int deriv)
PlusFunction(ROL::Ptr< Distribution< Real > > &dist, Real param=1.)