ROL
ROL_PH_bPOEObjective.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_BPOEOBJECTIVE_H
45 #define PH_BPOEOBJECTIVE_H
46 
47 #include "ROL_Objective.hpp"
48 
55 namespace ROL {
56 
57 template <class Real>
58 class PH_bPOEObjective : public Objective<Real> {
59 private:
60  const Ptr<Objective<Real>> obj_;
61  Real threshold_;
62  Real order_;
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  Ptr<const Vector<Real>> getConstVector(const Vector<Real> &x) const {
90  const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
91  return xrv.getVector();
92  }
93 
94  Ptr<Vector<Real>> getVector(Vector<Real> &x) const {
95  RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
96  return xrv.getVector();
97  }
98 
99  Ptr<const std::vector<Real>> getConstStat(const Vector<Real> &x) const {
100  const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
101  Ptr<const std::vector<Real>> xstat = xrv.getStatistic();
102  if (xstat == nullPtr) {
103  xstat = makePtr<const std::vector<Real>>(0);
104  }
105  return xstat;
106  }
107 
108  Ptr<std::vector<Real>> getStat(Vector<Real> &x) const {
109  RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
110  Ptr<std::vector<Real>> xstat = xrv.getStatistic();
111  if (xstat == nullPtr) {
112  xstat = makePtr<std::vector<Real>>(0);
113  }
114  return xstat;
115  }
116 
117  // pth power of the positive part function
118  Real pplus(const Real x, const int deriv = 0) const {
119  const Real zero(0), one(1), two(2), three(3);
120  Real val(0);
121  if (x > zero) {
122  if (deriv==0) {
123  val = (order_==one ? x
124  : std::pow(x,order_));
125  }
126  else if (deriv==1) {
127  val = order_*(order_==one ? one
128  : (order_==two ? x
129  : std::pow(x,order_-one)));
130  }
131  else if (deriv==2) {
132  val = order_*(order_-one)*(order_==one ? zero
133  : (order_==two ? one
134  : (order_==three ? x
135  : std::pow(x,order_-two))));
136  }
137  }
138  return val;
139  }
140 
141  Real bPOEobjective(const Real t, const Real x, const int deriv = 0) const {
142  const Real one(1);
143  Real arg = t*(x-threshold_)+one;
144  return pplus(arg,deriv);
145  }
146 
147 public:
148 
150  ParameterList &parlist)
151  : obj_(obj),
152  isValueComputed_(false),
153  isGradientInitialized_(false),
154  isGradientComputed_(false) {
155  ParameterList &list = parlist.sublist("SOL").sublist("Probability").sublist("bPOE");
156  threshold_ = list.get<Real>("Threshold");
157  order_ = list.get<Real>("Moment Order");
158  }
159 
160  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
161  Ptr<const Vector<Real>> xvec = getConstVector(x);
162  obj_->update(*xvec,flag,iter);
163  isValueComputed_ = false;
164  isGradientComputed_ = false;
165  }
166 
167  Real value( const Vector<Real> &x, Real &tol ) {
168  Ptr<const Vector<Real>> xvec = getConstVector(x);
169  Ptr<const std::vector<Real>> xstat = getConstStat(x);
170  getValue(*xvec,tol);
171  Real xt = (*xstat)[0];
172  Real prob = bPOEobjective(xt,val_,0);
173  return prob;
174  }
175 
176  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
177  Ptr<Vector<Real>> gvec = getVector(g);
178  Ptr<std::vector<Real>> gstat = getStat(g);
179  Ptr<const Vector<Real>> xvec = getConstVector(x);
180  Ptr<const std::vector<Real>> xstat = getConstStat(x);
181  getValue(*xvec,tol);
182  Real xt = (*xstat)[0], diff = val_-threshold_;
183  Real prob = bPOEobjective(xt,val_,1);
184  getGradient(*xvec,tol);
185  gvec->set(*g_);
186  gvec->scale(prob*xt);
187  (*gstat)[0] = prob*diff;
188  }
189 
190  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
191  Ptr<Vector<Real>> hvec = getVector(hv);
192  Ptr<std::vector<Real>> hstat = getStat(hv);
193  Ptr<const Vector<Real>> vvec = getConstVector(v);
194  Ptr<const std::vector<Real>> vstat = getConstStat(v);
195  Ptr<const Vector<Real>> xvec = getConstVector(x);
196  Ptr<const std::vector<Real>> xstat = getConstStat(x);
197  getValue(*xvec,tol);
198  Real xt = (*xstat)[0], vt = (*vstat)[0], diff = val_-threshold_;
199  Real prob1 = bPOEobjective(xt,val_,1);
200  Real prob2 = bPOEobjective(xt,val_,2);
201  getGradient(*xvec,tol);
202  Real gv = vvec->dot(g_->dual());
203  obj_->hessVec(*hvec,*vvec,*xvec,tol);
204  hvec->scale(prob1*xt);
205  hvec->axpy(prob2*xt*(vt*diff+xt*gv)+vt*prob1,*g_);
206  (*hstat)[0] = prob2*std::pow(diff,2)*vt+(prob2*diff*xt+prob1)*gv;
207  }
208 
209  void setParameter(const std::vector<Real> &param) {
210  obj_->setParameter(param);
212  }
213 
214 };
215 
216 }
217 #endif
Provides the interface to evaluate objective functions.
ROL::Ptr< std::vector< Real > > getStatistic(const int comp=0, const int index=0)
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
Ptr< Vector< Real > > g_
void getValue(const Vector< Real > &x, Real &tol)
ROL::Ptr< const Vector< Real > > getVector(void) const
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Real pplus(const Real x, const int deriv=0) const
void setParameter(const std::vector< Real > &param)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Provides the interface for the progressive hedging probability objective.
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Ptr< Vector< Real > > getVector(Vector< Real > &x) const
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
Ptr< std::vector< Real > > getStat(Vector< Real > &x) const
PH_bPOEObjective(const Ptr< Objective< Real >> &obj, ParameterList &parlist)
Real value(const Vector< Real > &x, Real &tol)
Compute value.
void getGradient(const Vector< Real > &x, Real &tol)
Real bPOEobjective(const Real t, const Real x, const int deriv=0) const
virtual void setParameter(const std::vector< Real > &param)
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Ptr< const Vector< Real > > getConstVector(const Vector< Real > &x) const
const Ptr< Objective< Real > > obj_
Ptr< const std::vector< Real > > getConstStat(const Vector< Real > &x) const