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 public:
73 
74  QuantileRadiusQuadrangle( Teuchos::ParameterList &parlist )
75  : RiskMeasure<Real>(), firstReset_(true) {
76  Teuchos::ParameterList &list
77  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Quantile-Radius Quadrangle");
78  // Grab probability and coefficient arrays
79  prob_ = list.get("Confidence Level",0.5);
80  coeff_ = list.get("Coefficient",1.0);
81  // Check inputs
82  TEUCHOS_TEST_FOR_EXCEPTION((prob_>1. || prob_<0.), std::invalid_argument,
83  ">>> ERROR (ROL::QuantileRadiusQuadrangle): Confidence level out of range!");
84  TEUCHOS_TEST_FOR_EXCEPTION((coeff_<0.), std::invalid_argument,
85  ">>> ERROR (ROL::QuantileRadiusQuadrangle): Coefficient is negative!");
86  // Build (approximate) plus function
87  plusFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
88  // Initialize temporary storage
89  xvar_.clear(); xvar_.resize(2,0.0);
90  vvar_.clear(); vvar_.resize(2,0.0);
91  vec_.clear(); vec_.resize(2,0.0);
92  }
93 
94  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
96  for (int i = 0; i < 2; i++) {
97  xvar_[i] = Teuchos::dyn_cast<const RiskVector<Real> >(
98  Teuchos::dyn_cast<const Vector<Real> >(x)).getStatistic(i);
99  vec_[i] = 0.0;
100  }
101  if ( firstReset_ ) {
102  dualVector_ = (x0->dual()).clone();
103  firstReset_ = false;
104  }
105  dualVector_->zero();
106  }
107 
108  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
109  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
110  reset(x0,x);
111  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(
112  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
113  for (int i = 0; i < 2; i++) {
114  vvar_[i] = Teuchos::dyn_cast<const RiskVector<Real> >(
115  Teuchos::dyn_cast<const Vector<Real> >(v)).getStatistic(i);
116  }
117  }
118 
119  void update(const Real val, const Real weight) {
120  Real pf1 = plusFunction_->evaluate(val-xvar_[0],0);
121  Real pf2 = plusFunction_->evaluate(-val-xvar_[1],0);
122  RiskMeasure<Real>::val_ += weight*(val + 0.5*coeff_/(1.0-prob_)*(pf1 + pf2));
123  }
124 
125  void update(const Real val, const Vector<Real> &g, const Real weight) {
126  Real pf1 = plusFunction_->evaluate(val-xvar_[0],1);
127  Real pf2 = plusFunction_->evaluate(-val-xvar_[1],1);
128  Real c = 0.5*weight*coeff_/(1.0-prob_);
129  vec_[0] -= c*pf1;
130  vec_[1] -= c*pf2;
131  RiskMeasure<Real>::g_->axpy(weight + c * (pf1 - pf2),g);
132  }
133 
134  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
135  const Real weight) {
136  Real pf11 = plusFunction_->evaluate(val-xvar_[0],1);
137  Real pf12 = plusFunction_->evaluate(val-xvar_[0],2);
138  Real pf21 = plusFunction_->evaluate(-val-xvar_[1],1);
139  Real pf22 = plusFunction_->evaluate(-val-xvar_[1],2);
140  Real c = 0.5*weight*coeff_/(1.0-prob_);
141  vec_[0] -= c*pf12*(gv-vvar_[0]);
142  vec_[1] -= c*pf22*(-gv-vvar_[1]);
143  RiskMeasure<Real>::hv_->axpy(c*(pf12*(gv-vvar_[0]) + pf22*(-gv-vvar_[1])),g);
144  RiskMeasure<Real>::hv_->axpy(weight + c * (pf11 - pf21),hv);
145  }
146 
148  Real val = RiskMeasure<Real>::val_;
149  Real cvar = 0.0;
150  sampler.sumAll(&val,&cvar,1);
151  cvar += 0.5*coeff_*(xvar_[0] + xvar_[1]);
152  return cvar;
153  }
154 
156  RiskVector<Real> &gs = Teuchos::dyn_cast<RiskVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(g));
157  std::vector<Real> var(2,0.0);
158  sampler.sumAll(&vec_[0],&var[0],2);
159 
160  sampler.sumAll(*(RiskMeasure<Real>::g_),*dualVector_);
161  var[0] += 0.5*coeff_;
162  var[1] += 0.5*coeff_;
163  gs.setStatistic(var);
164  gs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
165  }
166 
168  RiskVector<Real> &hs = Teuchos::dyn_cast<RiskVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(hv));
169  std::vector<Real> var(2,0.0);
170  sampler.sumAll(&vec_[0],&var[0],2);
171 
172  sampler.sumAll(*(RiskMeasure<Real>::hv_),*dualVector_);
173  hs.setStatistic(var);
174  hs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
175  }
176 };
177 
178 }
179 
180 #endif
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
Real getValue(SampleGenerator< Real > &sampler)
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
const Real getStatistic(const int i=0) const
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void sumAll(Real *input, Real *output, int dim) const
void setVector(const Vector< Real > &vec)
Teuchos::RCP< const Vector< Real > > getVector() const
void update(const Real val, const Vector< Real > &g, const Real weight)
Teuchos::RCP< Vector< Real > > dualVector_
void setStatistic(const Real stat)
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
void update(const Real val, const Real weight)
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Teuchos::RCP< PlusFunction< Real > > plusFunction_
QuantileRadiusQuadrangle(Teuchos::ParameterList &parlist)