ROL
ROL_PH_ProbObjective.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 PH_PROBOBJECTIVE_H
11 #define PH_PROBOBJECTIVE_H
12 
13 #include "ROL_Objective.hpp"
14 
21 namespace ROL {
22 
23 template <class Real>
24 class PH_ProbObjective : public Objective<Real> {
25 private:
26  const Ptr<Objective<Real>> obj_;
27  Real threshold_;
28  Real eps_;
29 
31  Real val_;
32 
35  Ptr<Vector<Real>> g_;
36 
37  void getValue(const Vector<Real> &x, Real &tol) {
38  if (!isValueComputed_) {
39  val_ = obj_->value(x,tol);
40  isValueComputed_ = true;
41  }
42  }
43 
44  void getGradient(const Vector<Real> &x, Real &tol) {
46  g_ = x.dual().clone();
48  }
49  if (!isGradientComputed_) {
50  obj_->gradient(*g_,x,tol);
51  isGradientComputed_ = true;
52  }
53  }
54 
55  Real smoothHeaviside(const Real x, const int deriv = 0) const {
56  const Real one(1), two(2);
57  Real val(0);
58  if (deriv == 0) {
59  Real ex = std::exp(-two*x/eps_);
60  val = one/(one+ex);
61  }
62  else if (deriv == 1) {
63  Real ex = std::exp(-two*x/eps_);
64  val = (two/eps_)*ex/std::pow(one+ex,2);
65  }
66  else if (deriv == 2) {
67  Real ex = std::exp(two*x/eps_);
68  val = std::pow(two/eps_,2)*ex*(one-ex)/std::pow(one+ex,3);
69  }
70  return val;
71  }
72 
73 public:
74 
76  ParameterList &parlist)
77  : obj_(obj),
78  isValueComputed_(false),
80  isGradientComputed_(false) {
81  ParameterList &list = parlist.sublist("SOL").sublist("Probability").sublist("Smoothed POE");
82  threshold_ = list.get<Real>("Threshold");
83  eps_ = list.get<Real>("Smoothing Parameter");
84  }
85 
86  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
87  obj_->update(x,flag,iter);
88  isValueComputed_ = false;
89  isGradientComputed_ = false;
90  }
91 
92  Real value( const Vector<Real> &x, Real &tol ) {
93  getValue(x,tol);
94  Real prob = smoothHeaviside(val_-threshold_,0);
95  if (std::abs(prob) > ROL_EPSILON<Real>()) {
96  return prob;
97  }
98  return static_cast<Real>(0);
99  }
100 
101  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
102  getValue(x,tol);
103  Real prob = smoothHeaviside(val_-threshold_,1);
104  if (std::abs(prob) > ROL_EPSILON<Real>()) {
105  getGradient(x,tol);
106  g.set(*g_); g.scale(prob);
107  }
108  else {
109  g.zero();
110  }
111  }
112 
113  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
114  getValue(x,tol);
115  Real prob1 = smoothHeaviside(val_-threshold_,1);
116  Real prob2 = smoothHeaviside(val_-threshold_,2);
117  if (std::abs(prob1) > ROL_EPSILON<Real>()) {
118  obj_->hessVec(hv,v,x,tol);
119  hv.scale(prob1);
120  }
121  else {
122  hv.zero();
123  }
124  if (std::abs(prob2) > ROL_EPSILON<Real>()) {
125  getGradient(x,tol);
126  //Real gv = v.dot(g_->dual());
127  Real gv = v.apply(*g_);
128  hv.axpy(prob2*gv,*g_);
129  }
130  }
131 
132  void setParameter(const std::vector<Real> &param) {
133  obj_->setParameter(param);
135  }
136 
137 };
138 
139 }
140 #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:192
virtual void scale(const Real alpha)=0
Compute where .
virtual Real apply(const Vector< Real > &x) const
Apply to a dual vector. This is equivalent to the call .
Definition: ROL_Vector.hpp:204
Provides the interface for the progressive hedging probability objective.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:119
Ptr< Vector< Real > > g_
const Ptr< Objective< Real > > obj_
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:133
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
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:175
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.