ROL
ROL_PlusFunction.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_PLUSFUNCTION_HPP
45 #define ROL_PLUSFUNCTION_HPP
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_PositiveFunction.hpp"
49 #include "ROL_Distribution.hpp"
51 
52 namespace ROL {
53 
54 template<class Real>
55 class PlusFunction : public PositiveFunction<Real> {
56 private:
57  ROL::Ptr<Distribution<Real> > dist_;
58  Real param_;
59 
60 public:
61  PlusFunction(ROL::Ptr<Distribution<Real> > &dist, Real param = 1.) : dist_(dist) {
62  param_ = ((param <= 0) ? 1.e-2 : param);
63  }
64 
65  PlusFunction(ROL::ParameterList &parlist) {
66  Real param(1.e-1), zero(0), one(1);
67  ROL::ParameterList pfList;
68  if (parlist.isSublist("Plus Function")) {
69  param = parlist.sublist("Plus Function").get("Smoothing Parameter",1.);
70  pfList = parlist.sublist("Plus Function");
71  }
72  else {
73  param = parlist.get("Smoothing Parameter",1.);
74  pfList = parlist;
75  }
76  param_ = ((param <= zero) ? one : param);
77  dist_ = DistributionFactory<Real>(pfList);
78  }
79 
80  Real evaluate(Real input, int deriv) {
81  Real val = 0.0;
82  switch(deriv) {
83  case 0: val = param_*dist_->integrateCDF(input/param_); break;
84  case 1: val = dist_->evaluateCDF(input/param_); break;
85  case 2: val = dist_->evaluatePDF(input/param_)/param_; break;
86  }
87  return val;
88  }
89 
90  void test(Real x) {
91  // FIRST DERIVATIVE
92  Real vx = evaluate(x,0);
93  Real vy = 0.0;
94  Real dv = evaluate(x,1);
95  Real t = 1.0;
96  Real diff = 0.0;
97  Real err = 0.0;
98  std::cout << std::right << std::setw(20) << "CHECK PLUS FUNCTION: p'(x) with x = "
99  << x << " is correct?\n";
100  std::cout << std::right << std::setw(20) << "t"
101  << std::setw(20) << "p'(x)"
102  << std::setw(20) << "(p(x+t)-p(x))/t"
103  << std::setw(20) << "Error"
104  << "\n";
105  for (int i = 0; i < 13; i++) {
106  vy = evaluate(x+t,0);
107  diff = (vy-vx)/t;
108  err = std::abs(diff-dv);
109  std::cout << std::scientific << std::setprecision(11) << std::right
110  << std::setw(20) << t
111  << std::setw(20) << dv
112  << std::setw(20) << diff
113  << std::setw(20) << err
114  << "\n";
115  t *= 0.1;
116  }
117  std::cout << "\n";
118  // SECOND DERIVATIVE
119  vx = evaluate(x,1);
120  vy = 0.0;
121  dv = evaluate(x,2);
122  t = 1.0;
123  diff = 0.0;
124  err = 0.0;
125  std::cout << std::right << std::setw(20) << "CHECK PLUS FUNCTION: p''(x) with x = "
126  << x << " is correct?\n";
127  std::cout << std::right << std::setw(20) << "t"
128  << std::setw(20) << "p''(x)"
129  << std::setw(20) << "(p'(x+t)-p'(x))/t"
130  << std::setw(20) << "Error"
131  << "\n";
132  for (int i = 0; i < 13; i++) {
133  vy = evaluate(x+t,1);
134  diff = (vy-vx)/t;
135  err = std::abs(diff-dv);
136  std::cout << std::scientific << std::setprecision(11) << std::right
137  << std::setw(20) << t
138  << std::setw(20) << dv
139  << std::setw(20) << diff
140  << std::setw(20) << err
141  << "\n";
142  t *= 0.1;
143  }
144  std::cout << "\n";
145  }
146 };
147 
148 }
149 
150 #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.)