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  Teuchos::RCP<Distribution<Real> > dist_;
58  Real param_;
59 
60 public:
61  PlusFunction(Teuchos::RCP<Distribution<Real> > &dist, Real param = 1.) : dist_(dist) {
62  param_ = ((param <= 0) ? 1.e-2 : param);
63  }
64 
65  PlusFunction(Teuchos::ParameterList &parlist) {
66  Real param = parlist.get("Smoothing Parameter",1.);
67  param_ = ((param <= 0) ? 1. : param);
68  dist_ = DistributionFactory<Real>(parlist);
69  }
70 
71  Real evaluate(Real input, int deriv) {
72  Real val = 0.0;
73  switch(deriv) {
74  case 0: val = param_*dist_->integrateCDF(input/param_); break;
75  case 1: val = dist_->evaluateCDF(input/param_); break;
76  case 2: val = dist_->evaluatePDF(input/param_)/param_; break;
77  }
78  return val;
79  }
80 
81  void test(Real x) {
82  // FIRST DERIVATIVE
83  Real vx = evaluate(x,0);
84  Real vy = 0.0;
85  Real dv = evaluate(x,1);
86  Real t = 1.0;
87  Real diff = 0.0;
88  Real err = 0.0;
89  std::cout << std::right << std::setw(20) << "CHECK PLUS FUNCTION: p'(x) with x = "
90  << x << " is correct?\n";
91  std::cout << std::right << std::setw(20) << "t"
92  << std::setw(20) << "p'(x)"
93  << std::setw(20) << "(p(x+t)-p(x))/t"
94  << std::setw(20) << "Error"
95  << "\n";
96  for (int i = 0; i < 13; i++) {
97  vy = evaluate(x+t,0);
98  diff = (vy-vx)/t;
99  err = std::abs(diff-dv);
100  std::cout << std::scientific << std::setprecision(11) << std::right
101  << std::setw(20) << t
102  << std::setw(20) << dv
103  << std::setw(20) << diff
104  << std::setw(20) << err
105  << "\n";
106  t *= 0.1;
107  }
108  std::cout << "\n";
109  // SECOND DERIVATIVE
110  vx = evaluate(x,1);
111  vy = 0.0;
112  dv = evaluate(x,2);
113  t = 1.0;
114  diff = 0.0;
115  err = 0.0;
116  std::cout << std::right << std::setw(20) << "CHECK PLUS FUNCTION: p''(x) with x = "
117  << x << " is correct?\n";
118  std::cout << std::right << std::setw(20) << "t"
119  << std::setw(20) << "p''(x)"
120  << std::setw(20) << "(p'(x+t)-p'(x))/t"
121  << std::setw(20) << "Error"
122  << "\n";
123  for (int i = 0; i < 13; i++) {
124  vy = evaluate(x+t,1);
125  diff = (vy-vx)/t;
126  err = std::abs(diff-dv);
127  std::cout << std::scientific << std::setprecision(11) << std::right
128  << std::setw(20) << t
129  << std::setw(20) << dv
130  << std::setw(20) << diff
131  << std::setw(20) << err
132  << "\n";
133  t *= 0.1;
134  }
135  std::cout << "\n";
136  }
137 };
138 
139 }
140 
141 #endif
Contains definitions of custom data types in ROL.
Teuchos::RCP< Distribution< Real > > dist_
PlusFunction(Teuchos::ParameterList &parlist)
PlusFunction(Teuchos::RCP< Distribution< Real > > &dist, Real param=1.)
Real evaluate(Real input, int deriv)