ROL
ROL_AbsoluteValue.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_ABSOLUTEVALUE_HPP
45 #define ROL_ABSOLUTEVALUE_HPP
46 
47 #include "ROL_Types.hpp"
48 #include "ROL_PositiveFunction.hpp"
49 #include "ROL_ParameterList.hpp"
50 
51 namespace ROL {
52 
59 };
60 
61 template<class Real>
62 class AbsoluteValue : public PositiveFunction<Real> {
63 private:
64  Real param_;
66 
67 public:
69  : param_(1./param), eav_(eav) {
70  if ( eav != ABSOLUTEVALUE_TRUE && std::abs(param) < ROL_EPSILON<Real>() ) { param_ = 1.e2; }
71  }
72 
73  AbsoluteValue(ROL::ParameterList &parlist) {
74  Real param = parlist.get("Smoothing Parameter",1.);
75  param_ = 1./((param > 0.) ? param : 1.);
76  std::string type = parlist.get("Absolute Value Approximation","true");
78  if ( type == "Square Root" ) { eav_ = ABSOLUTEVALUE_SQUAREROOT; }
79  else if ( type == "Square Root Denominator" ) { eav_ = ABSOLUTEVALUE_SQRTDENOM; }
80  else if ( type == "C2") { eav_ = ABSOLUTEVALUE_C2; }
81  else if ( type == "true") { eav_ = ABSOLUTEVALUE_TRUE; }
82  }
83 
84  Real evaluate(Real input, int deriv) {
85  Real val = 0.0;
86  switch(eav_) {
87  case ABSOLUTEVALUE_TRUE: val = true_absolute_value(input,deriv); break;
88  case ABSOLUTEVALUE_SQUAREROOT: val = sqrt_absolute_value(input,deriv); break;
89  case ABSOLUTEVALUE_SQRTDENOM: val = sqrtd_absolute_value(input,deriv); break;
90  case ABSOLUTEVALUE_C2: val = c2_absolute_value(input,deriv); break;
91  default:
92  ROL_TEST_FOR_EXCEPTION( true, std::invalid_argument,
93  ">>> ERROR (ROL::AbsoluteValue): Absolute value approximation not defined!");
94  }
95  return val;
96  }
97 
98 private:
99  Real true_absolute_value( Real input, int deriv ) {
100  Real output = 0.0, e = 0.0;
101  if ( std::abs(param_) > ROL_EPSILON<Real>() ) { e = 0.5/param_; }
102 
103  int region = 0;
104  if ( input < -e ) { region = -1; }
105  else if ( input > e ) { region = 1; }
106 
107  if ( deriv == 0 ) { output = std::abs(input); }
108  else if ( deriv == 1 ) { output = (input < 0.0 ? -1.0 : 1.0); }
109  else if ( deriv == 2 ) {
110  if ( region == -1 ) { output = 0.0; }
111  else if ( region == 0 ) { output = e*std::exp( -1.0/(1.0 - std::pow(std::abs(input/e),2.0)) ); }
112  else if ( region == 1 ) { output = 0.0; }
113  }
114  return output;
115  }
116 
117  Real sqrt_absolute_value( Real input, int deriv ) {
118  Real output = 0.0;
119  if ( deriv == 0 ) { output = std::sqrt(input*input + 1.0/param_); }
120  else if ( deriv == 1 ) { output = input/std::sqrt(input*input+1.0/param_); }
121  else if ( deriv == 2 ) { output = (1.0/param_)/std::pow(input*input+1.0/param_,1.5); }
122  return output;
123  }
124 
125  Real sqrtd_absolute_value( Real input, int deriv ) {
126  Real output = 0.0;
127  if ( deriv == 0 ) { output = input*input/std::sqrt(input*input + 1.0/param_); }
128  else if ( deriv == 1 ) { output = (2.0/param_*input+std::pow(input,3.0)) /
129  std::pow(input*input+1.0/param_,1.5); }
130  else if ( deriv == 2 ) { output = ((2.0/param_-input*input)/param_) /
131  std::pow(input*input+1.0/param_,2.5); }
132  return output;
133  }
134 
135  Real c2_absolute_value( Real input, int deriv ) {
136  Real output = 0.0, e = 1.0;
137  if ( std::abs(param_) > ROL_EPSILON<Real>() ) { e = 0.5/param_; }
138 
139  int region = 0;
140  if ( input < -e ) { region = -1; }
141  else if ( input > e ) { region = 1; }
142 
143  if ( deriv == 0 ) {
144  if ( std::abs(region) == 1 ) { output = std::abs(input); }
145  else if ( region == 0 ) { output = 1.875*std::pow(input*e,2.0) -
146  1.25 *std::pow(input*e,4.0) +
147  0.375*std::pow(input*e,6.0); }
148  }
149  else if ( deriv == 1 ) {
150  if ( std::abs(region) == 1 ) { output = (input < 0.0 ? -1.0 : 1.0); }
151  else if ( region == 0 ) { output = e*2.0*1.875*input*e -
152  e*4.0*1.25 *std::pow(input*e,3.0) +
153  e*6.0*0.375*std::pow(input*e,5.0); }
154  }
155  else if ( deriv == 2 ) {
156  if ( std::abs(region) == 1 ) { output = 0.0; }
157  else if ( region == 0 ) { output = e* 2.0*1.875 -
158  e*e*12.0*1.25 *std::pow(input*e,2.0) +
159  e*e*30.0*0.375*std::pow(input*e,4.0); }
160  }
161  return output;
162  }
163 };
164 
165 }
166 
167 #endif
Real sqrtd_absolute_value(Real input, int deriv)
Contains definitions of custom data types in ROL.
Real c2_absolute_value(Real input, int deriv)
AbsoluteValue(Real param=1., EAbsoluteValue eav=ABSOLUTEVALUE_TRUE)
Real sqrt_absolute_value(Real input, int deriv)
AbsoluteValue(ROL::ParameterList &parlist)
Real evaluate(Real input, int deriv)
Real true_absolute_value(Real input, int deriv)