ROL
ROL_PH_ProbObjective.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 PH_PROBOBJECTIVE_H
45 #define PH_PROBOBJECTIVE_H
46 
47 #include "ROL_Objective.hpp"
48 
55 namespace ROL {
56 
57 template <class Real>
58 class PH_ProbObjective : public Objective<Real> {
59 private:
60  const Ptr<Objective<Real>> obj_;
61  Real threshold_;
62  Real eps_;
63 
65  Real val_;
66 
69  Ptr<Vector<Real>> g_;
70 
71  void getValue(const Vector<Real> &x, Real &tol) {
72  if (!isValueComputed_) {
73  val_ = obj_->value(x,tol);
74  isValueComputed_ = true;
75  }
76  }
77 
78  void getGradient(const Vector<Real> &x, Real &tol) {
80  g_ = x.dual().clone();
82  }
83  if (!isGradientComputed_) {
84  obj_->gradient(*g_,x,tol);
85  isGradientComputed_ = true;
86  }
87  }
88 
89  Real smoothHeaviside(const Real x, const int deriv = 0) const {
90  const Real one(1), two(2);
91  Real val(0);
92  if (deriv == 0) {
93  Real ex = std::exp(-two*x/eps_);
94  val = one/(one+ex);
95  }
96  else if (deriv == 1) {
97  Real ex = std::exp(-two*x/eps_);
98  val = (two/eps_)*ex/std::pow(one+ex,2);
99  }
100  else if (deriv == 2) {
101  Real ex = std::exp(two*x/eps_);
102  val = std::pow(two/eps_,2)*ex*(one-ex)/std::pow(one+ex,3);
103  }
104  return val;
105  }
106 
107 public:
108 
110  ParameterList &parlist)
111  : obj_(obj),
112  isValueComputed_(false),
113  isGradientInitialized_(false),
114  isGradientComputed_(false) {
115  ParameterList &list = parlist.sublist("SOL").sublist("Probability").sublist("Smoothed POE");
116  threshold_ = list.get<Real>("Threshold");
117  eps_ = list.get<Real>("Smoothing Parameter");
118  }
119 
120  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
121  obj_->update(x,flag,iter);
122  isValueComputed_ = false;
123  isGradientComputed_ = false;
124  }
125 
126  Real value( const Vector<Real> &x, Real &tol ) {
127  getValue(x,tol);
128  Real prob = smoothHeaviside(val_-threshold_,0);
129  if (std::abs(prob) > ROL_EPSILON<Real>()) {
130  return prob;
131  }
132  return static_cast<Real>(0);
133  }
134 
135  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
136  getValue(x,tol);
137  Real prob = smoothHeaviside(val_-threshold_,1);
138  if (std::abs(prob) > ROL_EPSILON<Real>()) {
139  getGradient(x,tol);
140  g.set(*g_); g.scale(prob);
141  }
142  else {
143  g.zero();
144  }
145  }
146 
147  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
148  getValue(x,tol);
149  Real prob1 = smoothHeaviside(val_-threshold_,1);
150  Real prob2 = smoothHeaviside(val_-threshold_,2);
151  if (std::abs(prob1) > ROL_EPSILON<Real>()) {
152  obj_->hessVec(hv,v,x,tol);
153  hv.scale(prob1);
154  }
155  else {
156  hv.zero();
157  }
158  if (std::abs(prob2) > ROL_EPSILON<Real>()) {
159  getGradient(x,tol);
160  Real gv = v.dot(g_->dual());
161  hv.axpy(prob2*gv,*g_);
162  }
163  }
164 
165  void setParameter(const std::vector<Real> &param) {
166  obj_->setParameter(param);
168  }
169 
170 };
171 
172 }
173 #endif
Provides the interface to evaluate objective functions.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:226
virtual void scale(const Real alpha)=0
Compute where .
Provides the interface for the progressive hedging probability objective.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
Ptr< Vector< Real > > g_
const Ptr< Objective< Real > > obj_
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual Real dot(const Vector &x) const =0
Compute where .
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
void getGradient(const Vector< Real > &x, Real &tol)
void getValue(const Vector< Real > &x, Real &tol)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
PH_ProbObjective(const Ptr< Objective< Real >> &obj, ParameterList &parlist)
Real value(const Vector< Real > &x, Real &tol)
Compute value.
virtual void setParameter(const std::vector< Real > &param)
void setParameter(const std::vector< Real > &param)
Real smoothHeaviside(const Real x, const int deriv=0) const
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.