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 
54 namespace ROL {
55 
56 template<class Real>
57 class MixedQuantileQuadrangle : public RiskMeasure<Real> {
58 private:
59  Teuchos::RCP<PlusFunction<Real> > plusFunction_;
60 
61  Teuchos::Array<Real> prob_;
62  Teuchos::Array<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 
70  int size_;
71 
73 
74 public:
75 
76  MixedQuantileQuadrangle( Teuchos::ParameterList &parlist )
77  : RiskMeasure<Real>(), firstReset_(true) {
78  Teuchos::ParameterList &list
79  = parlist.sublist("SOL").sublist("Risk Measure").sublist("Mixed-Quantile Quadrangle");
80  // Grab probability and coefficient arrays
81  prob_ = Teuchos::getArrayFromStringParameter<Real>(list,"Probability Array");
82  coeff_ = Teuchos::getArrayFromStringParameter<Real>(list,"Coefficient Array");
83  // Check array sizes
84  int pSize = prob_.size(), cSize = coeff_.size();
85  TEUCHOS_TEST_FOR_EXCEPTION((pSize!=cSize),std::invalid_argument,
86  ">>> ERROR (ROL::MixedQuantileQuadrangle): Probability and coefficient arrays have different sizes!");
87  size_ = pSize;
88  // Check array elements
89  Real sum = 0.0;
90  for (int i = 0; i < size_; i++) {
91  TEUCHOS_TEST_FOR_EXCEPTION((prob_[i]>1. || prob_[i]<0.), std::invalid_argument,
92  ">>> ERROR (ROL::MixedQuantileQuadrangle): Element of probability array out of range!");
93  TEUCHOS_TEST_FOR_EXCEPTION((coeff_[i]>1. || coeff_[i]<0.), std::invalid_argument,
94  ">>> ERROR (ROL::MixedQuantileQuadrangle): Element of coefficient array out of range!");
95  sum += coeff_[i];
96  }
97  TEUCHOS_TEST_FOR_EXCEPTION((std::abs(sum-1.) > std::sqrt(ROL_EPSILON)),std::invalid_argument,
98  ">>> ERROR (ROL::MixedQuantileQuadrangle): Coefficients do not sum to one!");
99  // Build (approximate) plus function
100  plusFunction_ = Teuchos::rcp(new PlusFunction<Real>(list));
101  // Initialize temporary storage
102  xvar_.clear(); xvar_.resize(size_,0.0);
103  vvar_.clear(); vvar_.resize(size_,0.0);
104  vec_.clear(); vec_.resize(size_,0.0);
105  }
106 
107  MixedQuantileQuadrangle(const std::vector<Real> &prob,
108  const std::vector<Real> &coeff,
109  const Teuchos::RCP<PlusFunction<Real> > &pf )
110  : RiskMeasure<Real>(), plusFunction_(pf), firstReset_(true) {
111  prob_.clear(); coeff_.clear();
112  // Check array sizes
113  int pSize = prob.size(), cSize = coeff.size();
114  TEUCHOS_TEST_FOR_EXCEPTION((pSize!=cSize),std::invalid_argument,
115  ">>> ERROR (ROL::MixedQuantileQuadrangle): Probability and coefficient arrays have different sizes!");
116  size_ = pSize;
117  // Check array elements
118  Real sum = 0.0;
119  for (int i = 0; i < size_; i++) {
120  TEUCHOS_TEST_FOR_EXCEPTION((prob[i]>1. || prob[i]<0.), std::invalid_argument,
121  ">>> ERROR (ROL::MixedQuantileQuadrangle): Element of probability array out of range!");
122  TEUCHOS_TEST_FOR_EXCEPTION((coeff[i]>1. || coeff[i]<0.), std::invalid_argument,
123  ">>> ERROR (ROL::MixedQuantileQuadrangle): Element of coefficient array out of range!");
124  prob_.push_back(prob_[i]);
125  coeff_.push_back(coeff_[i]);
126  sum += coeff[i];
127  }
128  TEUCHOS_TEST_FOR_EXCEPTION((std::abs(sum-1.) > std::sqrt(ROL_EPSILON)),std::invalid_argument,
129  ">>> ERROR (ROL::MixedQuantileQuadrangle): Coefficients do not sum to one!");
130  // Initialize temporary storage
131  xvar_.clear(); xvar_.resize(size_,0.0);
132  vvar_.clear(); vvar_.resize(size_,0.0);
133  vec_.clear(); vec_.resize(size_,0.0);
134  }
135 
136  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
138  for (int i = 0; i < size_; i++) {
139  xvar_[i] = Teuchos::dyn_cast<const RiskVector<Real> >(
140  Teuchos::dyn_cast<const Vector<Real> >(x)).getStatistic(i);
141  vec_[i] = 0.0;
142  }
143  if ( firstReset_ ) {
144  dualVector_ = (x0->dual()).clone();
145  firstReset_ = false;
146  }
147  dualVector_->zero();
148  }
149 
150  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
151  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
152  reset(x0,x);
153  v0 = Teuchos::rcp_const_cast<Vector<Real> >(Teuchos::dyn_cast<const RiskVector<Real> >(
154  Teuchos::dyn_cast<const Vector<Real> >(v)).getVector());
155  for (int i = 0; i < size_; i++) {
156  vvar_[i] = Teuchos::dyn_cast<const RiskVector<Real> >(
157  Teuchos::dyn_cast<const Vector<Real> >(v)).getStatistic(i);
158  }
159  }
160 
161  void update(const Real val, const Real weight) {
162  Real pf = 0.0;
163  for (int i = 0; i < size_; i++) {
164  pf = plusFunction_->evaluate(val-xvar_[i],0);
165  RiskMeasure<Real>::val_ += weight*coeff_[i]/(1.0-prob_[i])*pf;
166  }
167  }
168 
169  void update(const Real val, const Vector<Real> &g, const Real weight) {
170  Real pf = 0.0, c = 0.0;
171  for (int i = 0; i < size_; i++) {
172  pf = plusFunction_->evaluate(val-xvar_[i],1);
173  c = weight*coeff_[i]/(1.0-prob_[i])*pf;
174  vec_[i] -= c;
175  RiskMeasure<Real>::g_->axpy(c,g);
176  }
177  }
178 
179  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
180  const Real weight) {
181  Real pf1 = 0.0, pf2 = 0.0, c = 0.0;
182  for (int i = 0; i < size_; i++) {
183  pf1 = plusFunction_->evaluate(val-xvar_[i],1);
184  pf2 = plusFunction_->evaluate(val-xvar_[i],2);
185  c = weight*coeff_[i]/(1.0-prob_[i])*pf2*(gv-vvar_[i]);
186  vec_[i] -= c;
187  //c *= (gv-vvar_[i]);
188  RiskMeasure<Real>::hv_->axpy(c,g);
189  c = weight*coeff_[i]/(1.0-prob_[i])*pf1;
190  RiskMeasure<Real>::hv_->axpy(c,hv);
191  }
192  }
193 
195  Real val = RiskMeasure<Real>::val_;
196  Real cvar = 0.0;
197  sampler.sumAll(&val,&cvar,1);
198  for (int i = 0; i < size_; i++) {
199  cvar += coeff_[i]*xvar_[i];
200  }
201  return cvar;
202  }
203 
205  RiskVector<Real> &gs = Teuchos::dyn_cast<RiskVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(g));
206  std::vector<Real> var(size_,0.0);
207  sampler.sumAll(&vec_[0],&var[0],size_);
208 
209  sampler.sumAll(*(RiskMeasure<Real>::g_),*dualVector_);
210  for (int i = 0; i < size_; i++) {
211  var[i] += coeff_[i];
212  }
213  gs.setStatistic(var);
214  gs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
215  }
216 
218  RiskVector<Real> &hs = Teuchos::dyn_cast<RiskVector<Real> >(Teuchos::dyn_cast<Vector<Real> >(hv));
219  std::vector<Real> var(size_,0.0);
220  sampler.sumAll(&vec_[0],&var[0],size_);
221 
222  sampler.sumAll(*(RiskMeasure<Real>::hv_),*dualVector_);
223 // for (int i = 0; i < size_; i++) {
224 // var[i] *= coeff_[i]/(1.0-prob_[i]);
225 // }
226  hs.setStatistic(var);
227  hs.setVector(*(Teuchos::rcp_dynamic_cast<Vector<Real> >(dualVector_)));
228  }
229 };
230 
231 }
232 
233 #endif
MixedQuantileQuadrangle(Teuchos::ParameterList &parlist)
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
const Real getStatistic(const int i=0) const
void getGradient(Vector< Real > &g, 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 reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
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)
void update(const Real val, const Real weight)
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)
void update(const Real val, const Vector< Real > &g, const Real weight)
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
Real getValue(SampleGenerator< Real > &sampler)
Teuchos::RCP< PlusFunction< Real > > plusFunction_
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:118