ROL
ROL_ConvexCombinationRiskMeasure.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 ROL_CONVEXCOMBINATIONRISKMEASURE_HPP
45 #define ROL_CONVEXCOMBINATIONRISKMEASURE_HPP
46 
48 
65 namespace ROL {
66 
67 template<class Real>
69 private:
71 
72  std::vector<Real> lambda_;
73  std::vector<ROL::Ptr<RandVarFunctional<Real> > > risk_;
75  std::vector<int> statVec_;
76 
77  Ptr<SampledScalar<Real>> values_;
78  Ptr<SampledScalar<Real>> gradvecs_;
79  Ptr<SampledVector<Real>> gradients_;
80  Ptr<SampledVector<Real>> hessvecs_;
81 
84 
85  void initializeCCRM(void) {
86  values_ = makePtr<SampledScalar<Real>>();
87  gradvecs_ = makePtr<SampledScalar<Real>>();
88  gradients_ = makePtr<SampledVector<Real>>();
89  hessvecs_ = makePtr<SampledVector<Real>>();
90 
92  RandVarFunctional<Real>::setHessVecStorage(gradvecs_,hessvecs_);
93  for (uint i = 0; i < size_; ++i) {
94  risk_[i]->setStorage(values_,gradients_);
95  risk_[i]->setHessVecStorage(gradvecs_,hessvecs_);
96  }
97  }
98 
99  void checkInputs(void) {
100  uint lSize = lambda_.size(), rSize = risk_.size();
101  ROL_TEST_FOR_EXCEPTION((lSize!=rSize),std::invalid_argument,
102  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Convex combination parameter and risk measure arrays have different sizes!");
103  Real sum(0), zero(0), one(1);
104  for (uint i = 0; i < lSize; ++i) {
105  ROL_TEST_FOR_EXCEPTION((lambda_[i]>one || lambda_[i]<zero), std::invalid_argument,
106  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Element of convex combination parameter array out of range!");
107  ROL_TEST_FOR_EXCEPTION(risk_[i] == ROL::nullPtr, std::invalid_argument,
108  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Risk measure pointer is null!");
109  sum += lambda_[i];
110  }
111  ROL_TEST_FOR_EXCEPTION((std::abs(sum-one) > std::sqrt(ROL_EPSILON<Real>())),std::invalid_argument,
112  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Coefficients do not sum to one!");
113  initializeCCRM();
114  }
115 
116 public:
126  ConvexCombinationRiskMeasure(ROL::ParameterList &parlist)
127  : RandVarFunctional<Real>(), size_(0) {
128  ROL::ParameterList &list
129  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Convex Combination Risk Measure");
130  // Get convex combination parameters
131  lambda_ = ROL::getArrayFromStringParameter<Real>(list,"Convex Combination Parameters");
132 
133  size_ = lambda_.size();
134  // Build risk measures
135  statVec_.clear();
136  risk_.clear(); risk_.resize(size_,ROL::nullPtr);
137  for (uint i = 0; i < size_; ++i) {
138  std::ostringstream convert;
139  convert << i;
140  std::string si = convert.str();
141  ROL::ParameterList &ilist = list.sublist(si);
142  std::string name = ilist.get<std::string>("Name");
143  ROL::ParameterList riskList;
144  riskList.sublist("SOL").sublist("Risk Measure").set("Name",name);
145  riskList.sublist("SOL").sublist("Risk Measure").sublist(name) = ilist;
146  risk_[i] = RiskMeasureFactory<Real>(riskList);
147  // Get statistic information
148  int nstat;
149  std::vector<Real> lower, upper;
150  bool isBound;
151  RiskMeasureInfo(riskList,name,nstat,lower,upper,isBound);
152  statVec_.push_back(nstat);
153  }
154  // Check inputs
155  checkInputs();
156  }
157 
158  void setSample(const std::vector<Real> &point, const Real weight) {
160  for (uint i = 0; i < size_; ++i) {
161  risk_[i]->setSample(point,weight);
162  }
163  }
164 
165  void resetStorage(bool flag = true) {
167  for (uint i = 0; i < size_; ++i) {
168  risk_[i]->resetStorage(flag);
169  }
170  }
171 
172  void initialize(const Vector<Real> &x) {
174  for (uint i = 0; i < size_; ++i) {
175  risk_[i]->initialize(x);
176  }
177  }
178 
180  const Vector<Real> &x,
181  const std::vector<Real> &xstat,
182  Real &tol) {
183  std::vector<Real> statx;
184  int offset(0);
185  for (uint i = 0; i < size_; ++i) {
186  statx.resize(statVec_[i]);
187  for (int j = 0; j < statVec_[i]; ++j) {
188  statx[j] = xstat[offset+j];
189  }
190  risk_[i]->updateValue(obj,x,statx,tol);
191  offset += statVec_[i];
192  }
193  }
194 
195  Real getValue(const Vector<Real> &x,
196  const std::vector<Real> &xstat,
197  SampleGenerator<Real> &sampler) {
198  Real val(0);
199  std::vector<Real> statx;
200  int offset(0);
201  for (uint i = 0; i < size_; ++i) {
202  statx.resize(statVec_[i]);
203  for (int j = 0; j < statVec_[i]; ++j) {
204  statx[j] = xstat[offset+j];
205  }
206  val += lambda_[i]*risk_[i]->getValue(x,statx,sampler);
207  offset += statVec_[i];
208  }
209  return val;
210  }
211 
213  const Vector<Real> &x,
214  const std::vector<Real> &xstat,
215  Real &tol) {
216  std::vector<Real> statx;
217  int offset(0);
218  for (uint i = 0; i < size_; ++i) {
219  statx.resize(statVec_[i]);
220  for (int j = 0; j < statVec_[i]; ++j) {
221  statx[j] = xstat[offset+j];
222  }
223  risk_[i]->updateGradient(obj,x,statx,tol);
224  offset += statVec_[i];
225  }
226  }
227 
229  std::vector<Real> &gstat,
230  const Vector<Real> &x,
231  const std::vector<Real> &xstat,
232  SampleGenerator<Real> &sampler) {
233  std::vector<Real> statg, statx;
234  int offset(0);
235  for (uint i = 0; i < size_; ++i) {
236  statg.resize(statVec_[i]);
237  statx.resize(statVec_[i]);
238  for (int j = 0; j < statVec_[i]; ++j) {
239  statg[j] = static_cast<Real>(0);
240  statx[j] = xstat[offset+j];
241  }
242  g_->zero();
243  risk_[i]->getGradient(*g_,statg,x,statx,sampler);
244  g.axpy(lambda_[i],*g_);
245  for (int j = 0; j < statVec_[i]; ++j) {
246  gstat[offset+j] = lambda_[i]*statg[j];
247  }
248  offset += statVec_[i];
249  }
250  }
251 
253  const Vector<Real> &v,
254  const std::vector<Real> &vstat,
255  const Vector<Real> &x,
256  const std::vector<Real> &xstat,
257  Real &tol) {
258  std::vector<Real> statx, statv;
259  int offset(0);
260  for (uint i = 0; i < size_; ++i) {
261  statx.resize(statVec_[i]);
262  statv.resize(statVec_[i]);
263  for (int j = 0; j < statVec_[i]; ++j) {
264  statx[j] = xstat[offset+j];
265  statv[j] = vstat[offset+j];
266  }
267  risk_[i]->updateHessVec(obj,v,statv,x,statx,tol);
268  offset += statVec_[i];
269  }
270  }
271 
273  std::vector<Real> &hvstat,
274  const Vector<Real> &v,
275  const std::vector<Real> &vstat,
276  const Vector<Real> &x,
277  const std::vector<Real> &xstat,
278  SampleGenerator<Real> &sampler) {
279  std::vector<Real> stath, statx, statv;
280  int offset(0);
281  for (uint i = 0; i < size_; ++i) {
282  stath.resize(statVec_[i]);
283  statx.resize(statVec_[i]);
284  statv.resize(statVec_[i]);
285  for (int j = 0; j < statVec_[i]; ++j) {
286  stath[j] = static_cast<Real>(0);
287  statx[j] = xstat[offset+j];
288  statv[j] = vstat[offset+j];
289  }
290  hv_->zero();
291  risk_[i]->getHessVec(*hv_,stath,v,statv,x,statx,sampler);
292  hv.axpy(lambda_[i],*hv_);
293  for (int j = 0; j < statVec_[i]; ++j) {
294  hvstat[offset+j] = lambda_[i]*stath[j];
295  }
296  offset += statVec_[i];
297  }
298  }
299 };
300 
301 }
302 
303 #endif
virtual void setHessVecStorage(const Ptr< SampledScalar< Real >> &gradvec_storage, const Ptr< SampledVector< Real >> &hessvec_storage)
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:153
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.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
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.
virtual void setStorage(const Ptr< SampledScalar< Real >> &value_storage, const Ptr< SampledVector< Real >> &gradient_storage)
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.
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.