ROL
ROL_ConvexCombinationRiskMeasure.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_CONVEXCOMBINATIONRISKMEASURE_HPP
11 #define ROL_CONVEXCOMBINATIONRISKMEASURE_HPP
12 
14 
31 namespace ROL {
32 
33 template<class Real>
35 private:
37 
38  std::vector<Real> lambda_;
39  std::vector<ROL::Ptr<RandVarFunctional<Real> > > risk_;
41  std::vector<int> statVec_;
42 
43  Ptr<ScalarController<Real>> values_;
44  Ptr<ScalarController<Real>> gradvecs_;
45  Ptr<VectorController<Real>> gradients_;
46  Ptr<VectorController<Real>> hessvecs_;
47 
50 
51  void initializeCCRM(void) {
52  values_ = makePtr<ScalarController<Real>>();
53  gradvecs_ = makePtr<ScalarController<Real>>();
54  gradients_ = makePtr<VectorController<Real>>();
55  hessvecs_ = makePtr<VectorController<Real>>();
56 
58  RandVarFunctional<Real>::setHessVecStorage(gradvecs_,hessvecs_);
59  for (uint i = 0; i < size_; ++i) {
60  risk_[i]->setStorage(values_,gradients_);
61  risk_[i]->setHessVecStorage(gradvecs_,hessvecs_);
62  }
63  }
64 
65  void checkInputs(void) {
66  uint lSize = lambda_.size(), rSize = risk_.size();
67  ROL_TEST_FOR_EXCEPTION((lSize!=rSize),std::invalid_argument,
68  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Convex combination parameter and risk measure arrays have different sizes!");
69  Real sum(0), zero(0), one(1);
70  for (uint i = 0; i < lSize; ++i) {
71  ROL_TEST_FOR_EXCEPTION((lambda_[i]>one || lambda_[i]<zero), std::invalid_argument,
72  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Element of convex combination parameter array out of range!");
73  ROL_TEST_FOR_EXCEPTION(risk_[i] == ROL::nullPtr, std::invalid_argument,
74  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Risk measure pointer is null!");
75  sum += lambda_[i];
76  }
77  ROL_TEST_FOR_EXCEPTION((std::abs(sum-one) > std::sqrt(ROL_EPSILON<Real>())),std::invalid_argument,
78  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Coefficients do not sum to one!");
80  }
81 
82 public:
92  ConvexCombinationRiskMeasure(ROL::ParameterList &parlist)
93  : RandVarFunctional<Real>(), size_(0) {
94  ROL::ParameterList &list
95  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Convex Combination Risk Measure");
96  // Get convex combination parameters
97  lambda_ = ROL::getArrayFromStringParameter<Real>(list,"Convex Combination Parameters");
98 
99  size_ = lambda_.size();
100  // Build risk measures
101  statVec_.clear();
102  risk_.clear(); risk_.resize(size_,ROL::nullPtr);
103  for (uint i = 0; i < size_; ++i) {
104  std::ostringstream convert;
105  convert << i;
106  std::string si = convert.str();
107  ROL::ParameterList &ilist = list.sublist(si);
108  std::string name = ilist.get<std::string>("Name");
109  ROL::ParameterList riskList;
110  riskList.sublist("SOL").sublist("Risk Measure").set("Name",name);
111  riskList.sublist("SOL").sublist("Risk Measure").sublist(name) = ilist;
112  risk_[i] = RiskMeasureFactory<Real>(riskList);
113  // Get statistic information
114  int nstat;
115  std::vector<Real> lower, upper;
116  bool isBound;
117  RiskMeasureInfo(riskList,name,nstat,lower,upper,isBound);
118  statVec_.push_back(nstat);
119  }
120  // Check inputs
121  checkInputs();
122  }
123 
124  void setSample(const std::vector<Real> &point, const Real weight) {
126  for (uint i = 0; i < size_; ++i) {
127  risk_[i]->setSample(point,weight);
128  }
129  }
130 
131  void resetStorage(bool flag = true) {
133  for (uint i = 0; i < size_; ++i) {
134  risk_[i]->resetStorage(flag);
135  }
136  }
139  for (uint i = 0; i < size_; ++i) {
140  risk_[i]->resetStorage(type);
141  }
142 
143  }
144 
145  void initialize(const Vector<Real> &x) {
147  for (uint i = 0; i < size_; ++i) {
148  risk_[i]->initialize(x);
149  }
150  }
151 
153  const Vector<Real> &x,
154  const std::vector<Real> &xstat,
155  Real &tol) {
156  std::vector<Real> statx;
157  int offset(0);
158  for (uint i = 0; i < size_; ++i) {
159  statx.resize(statVec_[i]);
160  for (int j = 0; j < statVec_[i]; ++j) {
161  statx[j] = xstat[offset+j];
162  }
163  risk_[i]->updateValue(obj,x,statx,tol);
164  offset += statVec_[i];
165  }
166  }
167 
168  Real getValue(const Vector<Real> &x,
169  const std::vector<Real> &xstat,
170  SampleGenerator<Real> &sampler) {
171  Real val(0);
172  std::vector<Real> statx;
173  int offset(0);
174  for (uint i = 0; i < size_; ++i) {
175  statx.resize(statVec_[i]);
176  for (int j = 0; j < statVec_[i]; ++j) {
177  statx[j] = xstat[offset+j];
178  }
179  val += lambda_[i]*risk_[i]->getValue(x,statx,sampler);
180  offset += statVec_[i];
181  }
182  return val;
183  }
184 
186  const Vector<Real> &x,
187  const std::vector<Real> &xstat,
188  Real &tol) {
189  std::vector<Real> statx;
190  int offset(0);
191  for (uint i = 0; i < size_; ++i) {
192  statx.resize(statVec_[i]);
193  for (int j = 0; j < statVec_[i]; ++j) {
194  statx[j] = xstat[offset+j];
195  }
196  risk_[i]->updateGradient(obj,x,statx,tol);
197  offset += statVec_[i];
198  }
199  }
200 
202  std::vector<Real> &gstat,
203  const Vector<Real> &x,
204  const std::vector<Real> &xstat,
205  SampleGenerator<Real> &sampler) {
206  std::vector<Real> statg, statx;
207  int offset(0);
208  for (uint i = 0; i < size_; ++i) {
209  statg.resize(statVec_[i]);
210  statx.resize(statVec_[i]);
211  for (int j = 0; j < statVec_[i]; ++j) {
212  statg[j] = static_cast<Real>(0);
213  statx[j] = xstat[offset+j];
214  }
215  g_->zero();
216  risk_[i]->getGradient(*g_,statg,x,statx,sampler);
217  g.axpy(lambda_[i],*g_);
218  for (int j = 0; j < statVec_[i]; ++j) {
219  gstat[offset+j] = lambda_[i]*statg[j];
220  }
221  offset += statVec_[i];
222  }
223  }
224 
226  const Vector<Real> &v,
227  const std::vector<Real> &vstat,
228  const Vector<Real> &x,
229  const std::vector<Real> &xstat,
230  Real &tol) {
231  std::vector<Real> statx, statv;
232  int offset(0);
233  for (uint i = 0; i < size_; ++i) {
234  statx.resize(statVec_[i]);
235  statv.resize(statVec_[i]);
236  for (int j = 0; j < statVec_[i]; ++j) {
237  statx[j] = xstat[offset+j];
238  statv[j] = vstat[offset+j];
239  }
240  risk_[i]->updateHessVec(obj,v,statv,x,statx,tol);
241  offset += statVec_[i];
242  }
243  }
244 
246  std::vector<Real> &hvstat,
247  const Vector<Real> &v,
248  const std::vector<Real> &vstat,
249  const Vector<Real> &x,
250  const std::vector<Real> &xstat,
251  SampleGenerator<Real> &sampler) {
252  std::vector<Real> stath, statx, statv;
253  int offset(0);
254  for (uint i = 0; i < size_; ++i) {
255  stath.resize(statVec_[i]);
256  statx.resize(statVec_[i]);
257  statv.resize(statVec_[i]);
258  for (int j = 0; j < statVec_[i]; ++j) {
259  stath[j] = static_cast<Real>(0);
260  statx[j] = xstat[offset+j];
261  statv[j] = vstat[offset+j];
262  }
263  hv_->zero();
264  risk_[i]->getHessVec(*hv_,stath,v,statv,x,statx,sampler);
265  hv.axpy(lambda_[i],*hv_);
266  for (int j = 0; j < statVec_[i]; ++j) {
267  hvstat[offset+j] = lambda_[i]*stath[j];
268  }
269  offset += statVec_[i];
270  }
271  }
272 };
273 
274 }
275 
276 #endif
ConvexCombinationRiskMeasure(ROL::ParameterList &parlist)
Constructor.
Provides the interface to evaluate objective functions.
typename PV< Real >::size_type size_type
std::vector< ROL::Ptr< RandVarFunctional< Real > > > risk_
Ptr< Vector< Real > > g_
void RiskMeasureInfo(ROL::ParameterList &parlist, std::string &name, int &nStatistic, std::vector< Real > &lower, std::vector< Real > &upper, bool &isBoundActivated, const bool printToStream=false, std::ostream &outStream=std::cout)
virtual void setSample(const std::vector< Real > &point, const Real weight)
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:119
Ptr< Vector< Real > > hv_
void updateValue(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal storage for value computation.
void initialize(const Vector< Real > &x)
Initialize temporary variables.
virtual void setStorage(const Ptr< ScalarController< Real >> &value_storage, const Ptr< VectorController< Real >> &gradient_storage)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
virtual void resetStorage(bool flag=true)
Reset internal storage.
void updateGradient(Objective< Real > &obj, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for gradient computation.
void updateHessVec(Objective< Real > &obj, const Vector< Real > &v, const std::vector< Real > &vstat, const Vector< Real > &x, const std::vector< Real > &xstat, Real &tol)
Update internal risk measure storage for Hessian-time-a-vector computation.
void resetStorage(bool flag=true)
Reset internal storage.
Provides the interface to implement any functional that maps a random variable to a (extended) real n...
void getGradient(Vector< Real > &g, std::vector< Real > &gstat, const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
void setSample(const std::vector< Real > &point, const Real weight)
void getHessVec(Vector< Real > &hv, std::vector< Real > &hvstat, const Vector< Real > &v, const std::vector< Real > &vstat, const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
virtual void setHessVecStorage(const Ptr< ScalarController< Real >> &gradvec_storage, const Ptr< VectorController< Real >> &hessvec_storage)
Real getValue(const Vector< Real > &x, const std::vector< Real > &xstat, SampleGenerator< Real > &sampler)
Return risk measure value.
virtual void initialize(const Vector< Real > &x)
Initialize temporary variables.
Provides an interface for a convex combination of risk measures.