ROL
ROL_PH_DeviationObjective.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_DEVIATIONOBJECTIVE_H
11 #define PH_DEVIATIONOBJECTIVE_H
12 
13 #include "ROL_Objective.hpp"
15 
22 namespace ROL {
23 
24 template <class Real>
25 class PH_DeviationObjective : public Objective<Real> {
26 private:
27  const Ptr<Objective<Real>> obj_;
28  Ptr<ExpectationQuad<Real>> quad_;
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  Ptr<const Vector<Real>> getConstVector(const Vector<Real> &x) const {
56  const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
57  return xrv.getVector();
58  }
59 
60  Ptr<Vector<Real>> getVector(Vector<Real> &x) const {
61  RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
62  return xrv.getVector();
63  }
64 
65  Ptr<const std::vector<Real>> getConstStat(const Vector<Real> &x) const {
66  const RiskVector<Real> &xrv = dynamic_cast<const RiskVector<Real>&>(x);
67  Ptr<const std::vector<Real>> xstat = xrv.getStatistic();
68  if (xstat == nullPtr) {
69  xstat = makePtr<const std::vector<Real>>(0);
70  }
71  return xstat;
72  }
73 
74  Ptr<std::vector<Real>> getStat(Vector<Real> &x) const {
75  RiskVector<Real> &xrv = dynamic_cast<RiskVector<Real>&>(x);
76  Ptr<std::vector<Real>> xstat = xrv.getStatistic();
77  if (xstat == nullPtr) {
78  xstat = makePtr<std::vector<Real>>(0);
79  }
80  return xstat;
81  }
82 
83 public:
84 
86  ParameterList &parlist)
87  : obj_(obj),
88  isValueComputed_(false),
90  isGradientComputed_(false) {
91  std::string risk = parlist.sublist("SOL").sublist("Deviation Measure").get("Name","Variance");
93  switch(ed) {
95  quad_ = makePtr<MeanVarianceQuadrangle<Real>>(parlist); break;
97  quad_ = makePtr<TruncatedMeanQuadrangle<Real>>(parlist); break;
99  quad_ = makePtr<QuantileQuadrangle<Real>>(parlist); break;
101  quad_ = makePtr<MoreauYosidaCVaR<Real>>(parlist); break;
103  quad_ = makePtr<GenMoreauYosidaCVaR<Real>>(parlist); break;
105  quad_ = makePtr<LogExponentialQuadrangle<Real>>(parlist); break;
107  quad_ = makePtr<LogQuantileQuadrangle<Real>>(parlist); break;
109  quad_ = makePtr<SmoothedWorstCaseQuadrangle<Real>>(parlist); break;
110  default:
111  ROL_TEST_FOR_EXCEPTION(true,std::invalid_argument,
112  "Invalid deviation measure type " << risk << "!");
113  }
114  }
115 
116  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
117  Ptr<const Vector<Real>> xvec = getConstVector(x);
118  obj_->update(*xvec,flag,iter);
119  isValueComputed_ = false;
120  isGradientComputed_ = false;
121  }
122 
123  Real value( const Vector<Real> &x, Real &tol ) {
124  Ptr<const Vector<Real>> xvec = getConstVector(x);
125  Ptr<const std::vector<Real>> xstat = getConstStat(x);
126  getValue(*xvec,tol);
127  Real err = quad_->error(val_-(*xstat)[0],0);
128  return err;
129  }
130 
131  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
132  Ptr<Vector<Real>> gvec = getVector(g);
133  Ptr<std::vector<Real>> gstat = getStat(g);
134  Ptr<const Vector<Real>> xvec = getConstVector(x);
135  Ptr<const std::vector<Real>> xstat = getConstStat(x);
136  getValue(*xvec,tol);
137  Real err = quad_->error(val_-(*xstat)[0],1);
138  getGradient(*xvec,tol);
139  gvec->set(*g_); gvec->scale(err);
140  (*gstat)[0] = -err;
141  }
142 
143  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
144  Ptr<Vector<Real>> hvec = getVector(hv);
145  Ptr<std::vector<Real>> hstat = getStat(hv);
146  Ptr<const Vector<Real>> vvec = getConstVector(v);
147  Ptr<const std::vector<Real>> vstat = getConstStat(v);
148  Ptr<const Vector<Real>> xvec = getConstVector(x);
149  Ptr<const std::vector<Real>> xstat = getConstStat(x);
150  getValue(*xvec,tol);
151  Real err1 = quad_->error(val_-(*xstat)[0],1);
152  Real err2 = quad_->error(val_-(*xstat)[0],2);
153  getGradient(*xvec,tol);
154  //Real gv = vvec->dot(g_->dual());
155  Real gv = vvec->apply(*g_);
156  obj_->hessVec(*hvec,*vvec,*xvec,tol);
157  hvec->scale(err1); hvec->axpy(err2*(gv-(*vstat)[0]),*g_);
158  (*hstat)[0] = err2*((*vstat)[0]-gv);
159  }
160 
161  void setParameter(const std::vector<Real> &param) {
162  obj_->setParameter(param);
164  }
165 
166 };
167 
168 }
169 #endif
const Ptr< Objective< Real > > obj_
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
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
Provides the interface for the progressive hedging deviation objective.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
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
void getGradient(const Vector< Real > &x, Real &tol)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
void setParameter(const std::vector< Real > &param)
Ptr< std::vector< Real > > getStatistic(const int comp=0, const int index=0)
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Ptr< const std::vector< Real > > getConstStat(const Vector< Real > &x) const
void getValue(const Vector< Real > &x, Real &tol)
virtual void setParameter(const std::vector< Real > &param)
Ptr< const Vector< Real > > getVector(void) const
Ptr< Vector< Real > > getVector(Vector< Real > &x) const
PH_DeviationObjective(const Ptr< Objective< Real >> &obj, ParameterList &parlist)
Ptr< ExpectationQuad< Real > > quad_
Ptr< std::vector< Real > > getStat(Vector< Real > &x) const
EDeviationMeasure StringToEDeviationMeasure(std::string s)