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:
70  typedef typename std::vector<Real>::size_type uint;
71 
72  std::vector<Real> lambda_;
73  std::vector<Teuchos::ParameterList> parlist_;
74  std::vector<Teuchos::RCP<RiskMeasure<Real> > > risk_;
76 
77  Teuchos::RCP<Vector<Real> > dualVector0_;
79 
80  void checkInputs(void) const {
81  uint lSize = lambda_.size(), rSize = risk_.size();
82  TEUCHOS_TEST_FOR_EXCEPTION((lSize!=rSize),std::invalid_argument,
83  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Convex combination parameter and risk measure arrays have different sizes!");
84  Real sum(0), zero(0), one(1);
85  for (uint i = 0; i < lSize; ++i) {
86  TEUCHOS_TEST_FOR_EXCEPTION((lambda_[i]>one || lambda_[i]<zero), std::invalid_argument,
87  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Element of convex combination parameter array out of range!");
88  TEUCHOS_TEST_FOR_EXCEPTION(risk_[i] == Teuchos::null, std::invalid_argument,
89  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Risk measure pointer is null!");
90  sum += lambda_[i];
91  }
92  TEUCHOS_TEST_FOR_EXCEPTION((std::abs(sum-one) > std::sqrt(ROL_EPSILON<Real>())),std::invalid_argument,
93  ">>> ERROR (ROL::ConvexCombinationRiskMeasure): Coefficients do not sum to one!");
94  }
95 
96 public:
106  ConvexCombinationRiskMeasure(Teuchos::ParameterList &parlist)
107  : RiskMeasure<Real>(), size_(0), firstReset_(true) {
108  Teuchos::ParameterList &list
109  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Convex Combination Risk Measure");
110  // Get convex combination parameters
111  Teuchos::Array<Real> lambda
112  = Teuchos::getArrayFromStringParameter<Real>(list,"Convex Combination Parameters");
113  lambda_ = lambda.toVector();
114  size_ = lambda_.size();
115  // Build risk measures
116  risk_.clear(); risk_.resize(size_,Teuchos::null);
117  parlist_.clear(); parlist_.resize(size_);
118  for (uint i = 0; i < size_; ++i) {
119  std::ostringstream convert;
120  convert << i;
121  std::string si = convert.str();
122  Teuchos::ParameterList &ilist = list.sublist(si);
123  std::string name = ilist.get<std::string>("Name");
124  parlist_[i].sublist("SOL").sublist("Risk Measure").set("Name",name);
125  parlist_[i].sublist("SOL").sublist("Risk Measure").sublist(name) = ilist;
126  risk_[i] = RiskMeasureFactory<Real>(parlist_[i]);
127  }
128  // Check inputs
129  checkInputs();
130  }
131 
132  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
133  std::vector<Real> stat, stati;
134  int N = 0, Ni = 0;
135  // Must make x a risk vector with appropriate statistic
136  const RiskVector<Real> &xr = Teuchos::dyn_cast<const RiskVector<Real> >(x);
137  Teuchos::RCP<const Vector<Real> > xptr = xr.getVector();
138  xr.getStatistic(stat);
139  x0 = Teuchos::rcp_const_cast<Vector<Real> >(xptr);
140  for (uint i = 0; i < size_; ++i) {
141  // Build temporary risk vector
142  RiskVector<Real> xri(parlist_[i],x0);
143  // Set statistic from original risk vector
144  xri.getStatistic(stati);
145  Ni = stati.size();
146  for (int j = 0; j < Ni; ++j) {
147  stati[j] = stat[N+j];
148  }
149  xri.setStatistic(stati);
150  N += Ni;
151  // Reset current risk measure
152  risk_[i]->reset(x0,xri);
153  }
154  if (firstReset_) {
155  dualVector0_ = x0->dual().clone();
156  firstReset_ = false;
157  }
158  dualVector0_->zero();
159  }
160 
161  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
162  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
164  std::vector<Real> xstat, xstati, vstat, vstati;
165  int N = 0, Ni = 0;
166  // Must make x and v risk vectors with appropriate statistics
167  const RiskVector<Real> &xr = Teuchos::dyn_cast<const RiskVector<Real> >(x);
168  const RiskVector<Real> &vr = Teuchos::dyn_cast<const RiskVector<Real> >(v);
169  Teuchos::RCP<const Vector<Real> > xptr = xr.getVector();
170  Teuchos::RCP<const Vector<Real> > vptr = vr.getVector();
171  x0 = Teuchos::rcp_const_cast<Vector<Real> >(xptr);
172  v0 = Teuchos::rcp_const_cast<Vector<Real> >(vptr);
173  xr.getStatistic(xstat);
174  vr.getStatistic(vstat);
175  for (uint i = 0; i < size_; ++i) {
176  // Build temporary risk vector
177  RiskVector<Real> xri(parlist_[i],x0), vri(parlist_[i],v0);
178  // Set statistic from original risk vector
179  xri.getStatistic(xstati);
180  vri.getStatistic(vstati);
181  Ni = xstati.size();
182  for (int j = 0; j < Ni; ++j) {
183  xstati[j] = xstat[N+j];
184  vstati[j] = vstat[N+j];
185  }
186  xri.setStatistic(xstati);
187  vri.setStatistic(vstati);
188  N += Ni;
189  // Reset current risk measure
190  risk_[i]->reset(x0,xri,v0,vri);
191  }
192  if (firstReset_) {
193  dualVector0_ = x0->dual().clone();
194  firstReset_ = false;
195  }
196  dualVector0_->zero();
197  }
198 
199  void update(const Real val, const Real weight) {
200  for (uint i = 0; i < size_; ++i) {
201  risk_[i]->update(val,weight);
202  }
203  }
204 
206  Real val(0);
207  for (uint i = 0; i < size_; ++i) {
208  val += lambda_[i]*risk_[i]->getValue(sampler);
209  }
210  return val;
211  }
212 
213  void update(const Real val, const Vector<Real> &g, const Real weight) {
214  for (uint i = 0; i < size_; ++i) {
215  risk_[i]->update(val,g,weight);
216  }
217  }
218 
220  g.zero();
221  // g does not have the correct dimension if it is a risk vector
222  RiskVector<Real> &gr = Teuchos::dyn_cast<RiskVector<Real> >(g);
223  std::vector<Real> stat, stati;
224  for (uint i = 0; i < size_; ++i) {
226  risk_[i]->getGradient(gri,sampler);
227  (gr.getVector())->axpy(lambda_[i],*dualVector0_);
228  gri.getStatistic(stati);
229  for (uint j = 0; j < stati.size(); ++j) {
230  stat.push_back(lambda_[i]*stati[j]);
231  }
232  }
233  gr.setStatistic(stat);
234  }
235 
236  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
237  const Real weight) {
238  for (uint i = 0; i < size_; ++i) {
239  risk_[i]->update(val,g,gv,hv,weight);
240  }
241  }
242 
244  hv.zero();
245  // hv does not have the correct dimension if it is a risk vector
246  RiskVector<Real> &hvr = Teuchos::dyn_cast<RiskVector<Real> >(hv);
247  std::vector<Real> stat, stati;
248  for (uint i = 0; i < size_; ++i) {
250  risk_[i]->getHessVec(hvri,sampler);
251  (hvr.getVector())->axpy(lambda_[i],*dualVector0_);
252  hvri.getStatistic(stati);
253  for (uint j = 0; j < stati.size(); ++j) {
254  stat.push_back(lambda_[i]*stati[j]);
255  }
256  }
257  hvr.setStatistic(stat);
258  }
259 };
260 
261 }
262 
263 #endif
std::vector< Teuchos::ParameterList > parlist_
void update(const Real val, const Vector< Real > &g, const Real weight)
Update internal risk measure storage for gradient computation.
void update(const Real val, const Real weight)
Update internal risk measure storage for value computation.
std::vector< Teuchos::RCP< RiskMeasure< Real > > > risk_
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
ConvexCombinationRiskMeasure(Teuchos::ParameterList &parlist)
Constructor.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< const Vector< Real > > getVector(void) const
void setStatistic(const Real stat)
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
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.
Real getValue(SampleGenerator< Real > &sampler)
Return risk measure value.
Teuchos::RCP< const StdVector< Real > > getStatistic(void) const
Provides the interface to implement risk measures.
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
Provides an interface for a convex combination of risk measures.
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
Reset internal risk measure storage. Called for Hessian-times-a-vector computation.