ROL
ROL_MixedQuantileQuadrangle.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_MIXEDQUANTILEQUADRANGLE_HPP
45 #define ROL_MIXEDQUANTILEQUADRANGLE_HPP
46 
47 #include "ROL_RiskMeasure.hpp"
48 #include "ROL_PlusFunction.hpp"
49 #include "ROL_RiskVector.hpp"
50 
51 #include "Teuchos_Array.hpp"
52 #include "Teuchos_ParameterList.hpp"
53 
87 namespace ROL {
88 
89 template<class Real>
90 class MixedQuantileQuadrangle : public RiskMeasure<Real> {
91 private:
92  Teuchos::RCP<PlusFunction<Real> > plusFunction_;
93 
94  Teuchos::Array<Real> prob_;
95  Teuchos::Array<Real> coeff_;
96 
97  Teuchos::RCP<Vector<Real> > dualVector_;
98  std::vector<Real> xvar_;
99  std::vector<Real> vvar_;
100 
101  std::vector<Real> vec_;
102 
103  int size_;
104 
106 
107  void checkInputs(void) const {
108  int pSize = prob_.size(), cSize = coeff_.size();
109  TEUCHOS_TEST_FOR_EXCEPTION((pSize!=cSize),std::invalid_argument,
110  ">>> ERROR (ROL::MixedQuantileQuadrangle): Probability and coefficient arrays have different sizes!");
111  Real sum(0), zero(0), one(1);
112  for (int i = 0; i < pSize; i++) {
113  TEUCHOS_TEST_FOR_EXCEPTION((prob_[i]>one || prob_[i]<zero), std::invalid_argument,
114  ">>> ERROR (ROL::MixedQuantileQuadrangle): Element of probability array out of range!");
115  TEUCHOS_TEST_FOR_EXCEPTION((coeff_[i]>one || coeff_[i]<zero), std::invalid_argument,
116  ">>> ERROR (ROL::MixedQuantileQuadrangle): Element of coefficient array out of range!");
117  sum += coeff_[i];
118  }
119  TEUCHOS_TEST_FOR_EXCEPTION((std::abs(sum-one) > std::sqrt(ROL_EPSILON<Real>())),std::invalid_argument,
120  ">>> ERROR (ROL::MixedQuantileQuadrangle): Coefficients do not sum to one!");
121  TEUCHOS_TEST_FOR_EXCEPTION(plusFunction_ == Teuchos::null, std::invalid_argument,
122  ">>> ERROR (ROL::MixedQuantileQuadrangle): PlusFunction pointer is null!");
123  }
124 
125  void initialize(void) {
126  size_ = prob_.size();
127  // Initialize temporary storage
128  Real zero(0);
129  xvar_.clear(); xvar_.resize(size_,zero);
130  vvar_.clear(); vvar_.resize(size_,zero);
131  vec_.clear(); vec_.resize(size_,zero);
132  }
133 
134 public:
135 
136  MixedQuantileQuadrangle( Teuchos::ParameterList &parlist )
137  : RiskMeasure<Real>(), firstReset_(true) {
138  Teuchos::ParameterList &list
139  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mixed-Quantile Quadrangle");
140  // Grab probability and coefficient arrays
141  prob_ = Teuchos::getArrayFromStringParameter<Real>(list,"Probability Array");
142  coeff_ = Teuchos::getArrayFromStringParameter<Real>(list,"Coefficient Array");
143  plusFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
144  // Check inputs
145  checkInputs();
146  initialize();
147  }
148 
149  MixedQuantileQuadrangle(const std::vector<Real> &prob,
150  const std::vector<Real> &coeff,
151  const Teuchos::RCP<PlusFunction<Real> > &pf )
152  : RiskMeasure<Real>(), plusFunction_(pf), prob_(prob), coeff_(coeff), firstReset_(true) {
153  checkInputs();
154  initialize();
155  }
156 
157  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
159  Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic(xvar_);
160  vec_.assign(size_,static_cast<Real>(0));
161  if ( firstReset_ ) {
162  dualVector_ = (x0->dual()).clone();
163  firstReset_ = false;
164  }
165  dualVector_->zero();
166  }
167 
168  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
169  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
170  reset(x0,x);
171  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(v).getVector());
172  Teuchos::dyn_cast<const RiskVector<Real> >(v).getStatistic(vvar_);
173  }
174 
175  void update(const Real val, const Real weight) {
176  Real pf(0), one(1);
177  for (int i = 0; i < size_; i++) {
178  pf = plusFunction_->evaluate(val-xvar_[i],0);
179  RiskMeasure<Real>::val_ += weight*coeff_[i]/(one-prob_[i])*pf;
180  }
181  }
182 
184  Real val = RiskMeasure<Real>::val_, cvar(0);
185  sampler.sumAll(&val,&cvar,1);
186  for (int i = 0; i < size_; i++) {
187  cvar += coeff_[i]*xvar_[i];
188  }
189  return cvar;
190  }
191 
192  void update(const Real val, const Vector<Real> &g, const Real weight) {
193  Real pf(0), c(0), one(1);
194  for (int i = 0; i < size_; i++) {
195  pf = plusFunction_->evaluate(val-xvar_[i],1);
196  c = weight*coeff_[i]/(one-prob_[i])*pf;
197  vec_[i] -= c;
198  RiskMeasure<Real>::g_->axpy(c,g);
199  }
200  }
201 
203  RiskVector<Real> &gs = Teuchos::dyn_cast<RiskVector<Real> >(g);
204  std::vector<Real> var(size_);
205  sampler.sumAll(&vec_[0],&var[0],size_);
206 
208  for (int i = 0; i < size_; i++) {
209  var[i] += coeff_[i];
210  }
211  gs.setStatistic(var);
212  gs.setVector(*dualVector_);
213  }
214 
215  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
216  const Real weight) {
217  Real pf1(0), pf2(0), c(0), one(1);
218  for (int i = 0; i < size_; i++) {
219  pf1 = plusFunction_->evaluate(val-xvar_[i],1);
220  pf2 = plusFunction_->evaluate(val-xvar_[i],2);
221  c = weight*coeff_[i]/(one-prob_[i])*pf2*(gv-vvar_[i]);
222  vec_[i] -= c;
223  //c *= (gv-vvar_[i]);
224  RiskMeasure<Real>::hv_->axpy(c,g);
225  c = weight*coeff_[i]/(one-prob_[i])*pf1;
226  RiskMeasure<Real>::hv_->axpy(c,hv);
227  }
228  }
229 
231  RiskVector<Real> &hs = Teuchos::dyn_cast<RiskVector<Real> >(hv);
232  std::vector<Real> var(size_);
233  sampler.sumAll(&vec_[0],&var[0],size_);
234 
236 // for (int i = 0; i < size_; i++) {
237 // var[i] *= coeff_[i]/(1.0-prob_[i]);
238 // }
239  hs.setStatistic(var);
240  hs.setVector(*dualVector_);
241  }
242 };
243 
244 }
245 
246 #endif
MixedQuantileQuadrangle(Teuchos::ParameterList &parlist)
Provides an interface for a convex combination of conditional value-at-risks.
void sumAll(Real *input, Real *output, int dim) const
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
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:74
void setVector(const Vector< Real > &vec)
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
void setStatistic(const Real stat)
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.
void update(const Real val, const Real weight)
Update internal risk measure storage for value computation.
MixedQuantileQuadrangle(const std::vector< Real > &prob, const std::vector< Real > &coeff, const Teuchos::RCP< PlusFunction< Real > > &pf)
Teuchos::RCP< Vector< Real > > dualVector_
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.
void update(const Real val, const Vector< Real > &g, const Real weight)
Update internal risk measure storage for gradient computation.
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
Real getValue(SampleGenerator< Real > &sampler)
Return risk measure value.
Provides the interface to implement risk measures.
Teuchos::RCP< PlusFunction< Real > > plusFunction_