ROL
ROL_QuantileRadiusQuadrangle.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_QUANTILERADIUSQUADRANGLE_HPP
45 #define ROL_QUANTILERADIUSQUADRANGLE_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 
54 namespace ROL {
55 
56 template<class Real>
57 class QuantileRadiusQuadrangle : public RiskMeasure<Real> {
58 private:
59  Teuchos::RCP<PlusFunction<Real> > plusFunction_;
60 
61  Real prob_;
62  Real coeff_;
63 
64  Teuchos::RCP<Vector<Real> > dualVector_;
65  std::vector<Real> xvar_;
66  std::vector<Real> vvar_;
67 
68  std::vector<Real> vec_;
69 
71 
72  void checkInputs(void) const {
73  Real zero(0), one(1);
74  // Check inputs
75  TEUCHOS_TEST_FOR_EXCEPTION((prob_>one || prob_<zero), std::invalid_argument,
76  ">>> ERROR (ROL::QuantileRadiusQuadrangle): Confidence level out of range!");
77  TEUCHOS_TEST_FOR_EXCEPTION((coeff_<zero), std::invalid_argument,
78  ">>> ERROR (ROL::QuantileRadiusQuadrangle): Coefficient is negative!");
79  }
80 
81  void initialize(void) {
82  Real zero(0);
83  // Initialize temporary storage
84  xvar_.clear(); xvar_.resize(2,zero);
85  vvar_.clear(); vvar_.resize(2,zero);
86  vec_.clear(); vec_.resize(2,zero);
87  }
88 
89 public:
90 
91  QuantileRadiusQuadrangle( Teuchos::ParameterList &parlist )
92  : RiskMeasure<Real>(), firstReset_(true) {
93  Teuchos::ParameterList &list
94  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Quantile-Radius Quadrangle");
95  // Grab probability and coefficient arrays
96  prob_ = list.get<Real>("Confidence Level");
97  coeff_ = list.get<Real>("Coefficient");
98  // Build (approximate) plus function
99  plusFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
100  checkInputs();
101  initialize();
102  }
103 
104  QuantileRadiusQuadrangle(const Real prob, const Real coeff,
105  const Teuchos::RCP<PlusFunction<Real> > &pf)
106  : RiskMeasure<Real>(), plusFunction_(pf), prob_(prob), coeff_(coeff), firstReset_(true) {
107  checkInputs();
108  initialize();
109  }
110 
111  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
113  Teuchos::dyn_cast<const RiskVector<Real> >(x).getStatistic(xvar_);
114  vec_.assign(2,static_cast<Real>(0));
115  if ( firstReset_ ) {
116  dualVector_ = (x0->dual()).clone();
117  firstReset_ = false;
118  }
119  dualVector_->zero();
120  }
121 
122  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
123  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
124  reset(x0,x);
125  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(v).getVector());
126  Teuchos::dyn_cast<const RiskVector<Real> >(v).getStatistic(vvar_);
127  }
128 
129  void update(const Real val, const Real weight) {
130  Real half(0.5), one(1);
131  Real pf1 = plusFunction_->evaluate(val-xvar_[0],0);
132  Real pf2 = plusFunction_->evaluate(-val-xvar_[1],0);
133  RiskMeasure<Real>::val_ += weight*(val + half*coeff_/(one-prob_)*(pf1 + pf2));
134  }
135 
137  Real val = RiskMeasure<Real>::val_, cvar(0), half(0.5);
138  sampler.sumAll(&val,&cvar,1);
139  cvar += half*coeff_*(xvar_[0] + xvar_[1]);
140  return cvar;
141  }
142 
143  void update(const Real val, const Vector<Real> &g, const Real weight) {
144  Real half(0.5), one(1);
145  Real pf1 = plusFunction_->evaluate(val-xvar_[0],1);
146  Real pf2 = plusFunction_->evaluate(-val-xvar_[1],1);
147  Real c = half*weight*coeff_/(one-prob_);
148  vec_[0] -= c*pf1;
149  vec_[1] -= c*pf2;
150  RiskMeasure<Real>::g_->axpy(weight + c * (pf1 - pf2),g);
151  }
152 
154  Real half(0.5);
155  RiskVector<Real> &gs = Teuchos::dyn_cast<RiskVector<Real> >(g);
156  std::vector<Real> var(2);
157  sampler.sumAll(&vec_[0],&var[0],2);
158 
160  var[0] += half*coeff_;
161  var[1] += half*coeff_;
162  gs.setStatistic(var);
163  gs.setVector(*dualVector_);
164  }
165 
166  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
167  const Real weight) {
168  Real half(0.5), one(1);
169  Real pf11 = plusFunction_->evaluate(val-xvar_[0],1);
170  Real pf12 = plusFunction_->evaluate(val-xvar_[0],2);
171  Real pf21 = plusFunction_->evaluate(-val-xvar_[1],1);
172  Real pf22 = plusFunction_->evaluate(-val-xvar_[1],2);
173  Real c = half*weight*coeff_/(one-prob_);
174  vec_[0] -= c*pf12*(gv-vvar_[0]);
175  vec_[1] -= c*pf22*(-gv-vvar_[1]);
176  RiskMeasure<Real>::hv_->axpy(c*(pf12*(gv-vvar_[0]) + pf22*(-gv-vvar_[1])),g);
177  RiskMeasure<Real>::hv_->axpy(weight + c * (pf11 - pf21),hv);
178  }
179 
181  RiskVector<Real> &hs = Teuchos::dyn_cast<RiskVector<Real> >(hv);
182  std::vector<Real> var(2);
183  sampler.sumAll(&vec_[0],&var[0],2);
184 
186  hs.setStatistic(var);
187  hs.setVector(*dualVector_);
188  }
189 };
190 
191 }
192 
193 #endif
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.
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 sumAll(Real *input, Real *output, int dim) const
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Return risk measure Hessian-times-a-vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
QuantileRadiusQuadrangle(const Real prob, const Real coeff, const Teuchos::RCP< PlusFunction< Real > > &pf)
void setVector(const Vector< Real > &vec)
void update(const Real val, const Vector< Real > &g, const Real weight)
Update internal risk measure storage for gradient computation.
Teuchos::RCP< Vector< Real > > dualVector_
void setStatistic(const Real stat)
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
Return risk measure (sub)gradient.
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
void update(const Real val, const Real weight)
Update internal risk measure storage for value computation.
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Reset internal risk measure storage. Called for value and gradient computation.
Teuchos::RCP< PlusFunction< Real > > plusFunction_
Provides the interface to implement risk measures.
QuantileRadiusQuadrangle(Teuchos::ParameterList &parlist)