ROL
ROL_RiskMeasure.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 ROL_RISKMEASURE_HPP
11 #define ROL_RISKMEASURE_HPP
12 
13 #include "ROL_RiskVector.hpp"
14 
59 namespace ROL {
60 
61 template<class Real>
62 class RiskMeasure {
63 protected:
64  Real val_;
65  Real gv_;
66  ROL::Ptr<Vector<Real> > g_;
67  ROL::Ptr<Vector<Real> > hv_;
68  ROL::Ptr<Vector<Real> > dualVector_;
70 
71  int comp_;
72  int index_;
73 
74 public:
75  virtual ~RiskMeasure() {}
76 
77  RiskMeasure(void) : val_(0), gv_(0), firstReset_(true),
78  comp_(0), index_(0) {}
79 
80  void setRiskVectorInfo(const int comp, const int index) {
81  comp_ = comp;
82  index_ = index;
83  }
84 
85  int getComponent(void) const {
86  return comp_;
87  }
88 
89  int getIndex(void) const {
90  return index_;
91  }
92 
102  virtual void reset(ROL::Ptr<Vector<Real> > &x0, const Vector<Real> &x) {
103  x0 = ROL::constPtrCast<Vector<Real> >(
104  dynamic_cast<const RiskVector<Real>&>(x).getVector());
105  // Create memory for class members
106  if ( firstReset_ ) {
107  g_ = (x0->dual()).clone();
108  hv_ = (x0->dual()).clone();
109  dualVector_ = (x0->dual()).clone();
110  firstReset_ = false;
111  }
112  // Zero member variables
113  const Real zero(0);
114  val_ = zero; gv_ = zero;
115  g_->zero(); hv_->zero(); dualVector_->zero();
116  }
117 
131  virtual void reset(ROL::Ptr<Vector<Real> > &x0, const Vector<Real> &x,
132  ROL::Ptr<Vector<Real> > &v0, const Vector<Real> &v) {
133  reset(x0,x);
134  // Get vector component of v. This is important for CVaR.
135  v0 = ROL::constPtrCast<Vector<Real> >(
136  dynamic_cast<const RiskVector<Real>&>(v).getVector());
137  }
138 
146  virtual void update(const Real val, const Real weight) {
147  val_ += weight * val;
148  }
149 
159  virtual void update(const Real val, const Vector<Real> &g, const Real weight) {
160  g_->axpy(weight,g);
161  }
162 
178  virtual void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
179  const Real weight) {
180  hv_->axpy(weight,hv);
181  }
182 
191  virtual Real getValue(SampleGenerator<Real> &sampler) {
192  Real val(0);
193  sampler.sumAll(&val_,&val,1);
194  return val;
195  }
196 
208  virtual void getGradient(Vector<Real> &g, SampleGenerator<Real> &sampler) {
209  sampler.sumAll(*g_,*dualVector_);
210  (dynamic_cast<RiskVector<Real>&>(g)).setVector(*dualVector_);
211  }
212 
224  virtual void getHessVec(Vector<Real> &hv, SampleGenerator<Real> &sampler) {
225  sampler.sumAll(*hv_,*dualVector_);
226  (dynamic_cast<RiskVector<Real>&>(hv)).setVector(*dualVector_);
227  }
228 };
229 
230 }
231 
232 #endif
virtual void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
virtual Real getValue(SampleGenerator< Real > &sampler)
Return risk measure value.
ROL::Ptr< Vector< Real > > hv_
virtual void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
void sumAll(Real *input, Real *output, int dim) const
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
int getComponent(void) const
ROL::Ptr< Vector< Real > > dualVector_
virtual void reset(ROL::Ptr< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
ROL::Ptr< Vector< Real > > g_
void setRiskVectorInfo(const int comp, const int index)
virtual void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
Update internal risk measure storage for Hessian-time-a-vector computation.
virtual void reset(ROL::Ptr< Vector< Real > > &x0, const Vector< Real > &x, ROL::Ptr< Vector< Real > > &v0, const Vector< Real > &v)
Reset internal risk measure storage. Called for Hessian-times-a-vector computation.
int getIndex(void) const
virtual void update(const Real val, const Real weight)
Update internal risk measure storage for value computation.
virtual void update(const Real val, const Vector< Real > &g, const Real weight)
Update internal risk measure storage for gradient computation.
Provides the interface to implement risk measures.