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<ScalarController<Real>> values_;
78  Ptr<ScalarController<Real>> gradvecs_;
79  Ptr<VectorController<Real>> gradients_;
80  Ptr<VectorController<Real>> hessvecs_;
81 
84 
85  void initializeCCRM(void) {
86  values_ = makePtr<ScalarController<Real>>();
87  gradvecs_ = makePtr<ScalarController<Real>>();
88  gradients_ = makePtr<VectorController<Real>>();
89  hessvecs_ = makePtr<VectorController<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  }
173  for (uint i = 0; i < size_; ++i) {
174  risk_[i]->resetStorage(type);
175  }
176 
177  }
178 
179  void initialize(const Vector<Real> &x) {
181  for (uint i = 0; i < size_; ++i) {
182  risk_[i]->initialize(x);
183  }
184  }
185 
187  const Vector<Real> &x,
188  const std::vector<Real> &xstat,
189  Real &tol) {
190  std::vector<Real> statx;
191  int offset(0);
192  for (uint i = 0; i < size_; ++i) {
193  statx.resize(statVec_[i]);
194  for (int j = 0; j < statVec_[i]; ++j) {
195  statx[j] = xstat[offset+j];
196  }
197  risk_[i]->updateValue(obj,x,statx,tol);
198  offset += statVec_[i];
199  }
200  }
201 
202  Real getValue(const Vector<Real> &x,
203  const std::vector<Real> &xstat,
204  SampleGenerator<Real> &sampler) {
205  Real val(0);
206  std::vector<Real> statx;
207  int offset(0);
208  for (uint i = 0; i < size_; ++i) {
209  statx.resize(statVec_[i]);
210  for (int j = 0; j < statVec_[i]; ++j) {
211  statx[j] = xstat[offset+j];
212  }
213  val += lambda_[i]*risk_[i]->getValue(x,statx,sampler);
214  offset += statVec_[i];
215  }
216  return val;
217  }
218 
220  const Vector<Real> &x,
221  const std::vector<Real> &xstat,
222  Real &tol) {
223  std::vector<Real> statx;
224  int offset(0);
225  for (uint i = 0; i < size_; ++i) {
226  statx.resize(statVec_[i]);
227  for (int j = 0; j < statVec_[i]; ++j) {
228  statx[j] = xstat[offset+j];
229  }
230  risk_[i]->updateGradient(obj,x,statx,tol);
231  offset += statVec_[i];
232  }
233  }
234 
236  std::vector<Real> &gstat,
237  const Vector<Real> &x,
238  const std::vector<Real> &xstat,
239  SampleGenerator<Real> &sampler) {
240  std::vector<Real> statg, statx;
241  int offset(0);
242  for (uint i = 0; i < size_; ++i) {
243  statg.resize(statVec_[i]);
244  statx.resize(statVec_[i]);
245  for (int j = 0; j < statVec_[i]; ++j) {
246  statg[j] = static_cast<Real>(0);
247  statx[j] = xstat[offset+j];
248  }
249  g_->zero();
250  risk_[i]->getGradient(*g_,statg,x,statx,sampler);
251  g.axpy(lambda_[i],*g_);
252  for (int j = 0; j < statVec_[i]; ++j) {
253  gstat[offset+j] = lambda_[i]*statg[j];
254  }
255  offset += statVec_[i];
256  }
257  }
258 
260  const Vector<Real> &v,
261  const std::vector<Real> &vstat,
262  const Vector<Real> &x,
263  const std::vector<Real> &xstat,
264  Real &tol) {
265  std::vector<Real> statx, statv;
266  int offset(0);
267  for (uint i = 0; i < size_; ++i) {
268  statx.resize(statVec_[i]);
269  statv.resize(statVec_[i]);
270  for (int j = 0; j < statVec_[i]; ++j) {
271  statx[j] = xstat[offset+j];
272  statv[j] = vstat[offset+j];
273  }
274  risk_[i]->updateHessVec(obj,v,statv,x,statx,tol);
275  offset += statVec_[i];
276  }
277  }
278 
280  std::vector<Real> &hvstat,
281  const Vector<Real> &v,
282  const std::vector<Real> &vstat,
283  const Vector<Real> &x,
284  const std::vector<Real> &xstat,
285  SampleGenerator<Real> &sampler) {
286  std::vector<Real> stath, statx, statv;
287  int offset(0);
288  for (uint i = 0; i < size_; ++i) {
289  stath.resize(statVec_[i]);
290  statx.resize(statVec_[i]);
291  statv.resize(statVec_[i]);
292  for (int j = 0; j < statVec_[i]; ++j) {
293  stath[j] = static_cast<Real>(0);
294  statx[j] = xstat[offset+j];
295  statv[j] = vstat[offset+j];
296  }
297  hv_->zero();
298  risk_[i]->getHessVec(*hv_,stath,v,statv,x,statx,sampler);
299  hv.axpy(lambda_[i],*hv_);
300  for (int j = 0; j < statVec_[i]; ++j) {
301  hvstat[offset+j] = lambda_[i]*stath[j];
302  }
303  offset += statVec_[i];
304  }
305  }
306 };
307 
308 }
309 
310 #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: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.
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: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.
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.