ROL
ROL_MeanVariance.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_MEANVARIANCE_HPP
45 #define ROL_MEANVARIANCE_HPP
46 
47 #include "ROL_RiskMeasure.hpp"
48 #include "ROL_PositiveFunction.hpp"
49 
50 namespace ROL {
51 
52 template<class Real>
53 class MeanVariance : public RiskMeasure<Real> {
54 private:
55 
56  Teuchos::RCP<PositiveFunction<Real> > positiveFunction_;
57 
58  std::vector<Real> order_;
59  std::vector<Real> coeff_;
60 
61  std::vector<Real> weights_;
62  std::vector<Real> value_storage_;
63  std::vector<Teuchos::RCP<Vector<Real> > > gradient_storage_;
64  std::vector<Teuchos::RCP<Vector<Real> > > hessvec_storage_;
65  std::vector<Real> gradvec_storage_;
66 
67 public:
68 
69  MeanVariance( Real order, Real coeff,
70  Teuchos::RCP<PositiveFunction<Real> > &pf )
71  : RiskMeasure<Real>(), positiveFunction_(pf) {
72  order_.clear(); coeff_.clear();
73  order_.push_back((order < 2.0) ? 2.0 : order);
74  coeff_.push_back((coeff < 0.0) ? 1.0 : coeff);
75  }
76  MeanVariance( std::vector<Real> &order, std::vector<Real> &coeff,
77  Teuchos::RCP<PositiveFunction<Real> > &pf )
78  : RiskMeasure<Real>(), positiveFunction_(pf) {
79  order_.clear(); coeff_.clear();
80  if ( order.size() != coeff.size() ) {
81  coeff.resize(order.size(),1.0);
82  }
83  for ( unsigned i = 0; i < order.size(); i++ ) {
84  order_.push_back((order[i] < 2.0) ? 2.0 : order[i]);
85  coeff_.push_back((coeff[i] < 0.0) ? 1.0 : coeff[i]);
86  }
87  }
88 
89  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
91  value_storage_.clear();
92  gradient_storage_.clear();
93  gradvec_storage_.clear();
94  hessvec_storage_.clear();
95  weights_.clear();
96  }
97 
98  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
99  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
100  RiskMeasure<Real>::reset(x0,x,v0,v);
101  value_storage_.clear();
102  gradient_storage_.clear();
103  gradvec_storage_.clear();
104  hessvec_storage_.clear();
105  weights_.clear();
106  }
107 
108  void update(const Real val, const Real weight) {
109  RiskMeasure<Real>::val_ += weight * val;
110  value_storage_.push_back(val);
111  weights_.push_back(weight);
112  }
113 
114  void update(const Real val, const Vector<Real> &g, const Real weight) {
115  RiskMeasure<Real>::val_ += weight * val;
116  RiskMeasure<Real>::g_->axpy(weight,g);
117  value_storage_.push_back(val);
118  gradient_storage_.push_back(g.clone());
119  typename std::vector<Teuchos::RCP<Vector<Real> > >::iterator it = gradient_storage_.end();
120  it--;
121  (*it)->set(g);
122  weights_.push_back(weight);
123  }
124 
125  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
126  const Real weight) {
127  RiskMeasure<Real>::val_ += weight * val;
128  RiskMeasure<Real>::gv_ += weight * gv;
129  RiskMeasure<Real>::g_->axpy(weight,g);
130  RiskMeasure<Real>::hv_->axpy(weight,hv);
131  value_storage_.push_back(val);
132  gradient_storage_.push_back(g.clone());
133  typename std::vector<Teuchos::RCP<Vector<Real> > >::iterator it = gradient_storage_.end();
134  it--;
135  (*it)->set(g);
136  gradvec_storage_.push_back(gv);
137  hessvec_storage_.push_back(hv.clone());
138  it = hessvec_storage_.end();
139  it--;
140  (*it)->set(hv);
141  weights_.push_back(weight);
142  }
143 
145  // Compute expected value
146  Real val = RiskMeasure<Real>::val_;
147  Real ev = 0.0;
148  sampler.sumAll(&val,&ev,1);
149  // Compute deviation
150  val = 0.0;
151  Real diff = 0.0, pf0 = 0.0, var = 0.0;
152  for ( unsigned i = 0; i < weights_.size(); i++ ) {
153  diff = value_storage_[i]-ev;
154  pf0 = positiveFunction_->evaluate(diff,0);
155  for ( unsigned p = 0; p < order_.size(); p++ ) {
156  val += coeff_[p] * std::pow(pf0,order_[p]) * weights_[i];
157  }
158  }
159  sampler.sumAll(&val,&var,1);
160  // Return mean plus deviation
161  return ev + var;
162  }
163 
165  g.zero();
166  // Compute expected value
167  Real val = RiskMeasure<Real>::val_;
168  Real ev = 0.0;
169  sampler.sumAll(&val,&ev,1);
170  sampler.sumAll(*(RiskMeasure<Real>::g_),g);
171  // Compute deviation
172  Teuchos::RCP<Vector<Real> > gs = g.clone(); gs->zero();
173  Teuchos::RCP<Vector<Real> > gtmp = g.clone(); gtmp->zero();
174  Real diff = 0.0, pf0 = 0.0, pf1 = 0.0, c = 0.0, ec = 0.0, ecs = 0.0;
175  for ( unsigned i = 0; i < weights_.size(); i++ ) {
176  c = 0.0;
177  diff = value_storage_[i]-ev;
178  pf0 = positiveFunction_->evaluate(diff,0);
179  pf1 = positiveFunction_->evaluate(diff,1);
180  for ( unsigned p = 0; p < order_.size(); p++ ) {
181  c += coeff_[p]*order_[p]*std::pow(pf0,order_[p]-1.0)*pf1;
182  }
183  ec += weights_[i]*c;
184  gtmp->axpy(weights_[i]*c,*(gradient_storage_[i]));
185  }
186  sampler.sumAll(&ec,&ecs,1);
187  g.scale(1.0-ecs);
188  sampler.sumAll(*gtmp,*gs);
189  g.plus(*gs);
190  }
191 
193  hv.zero();
194  // Compute expected value
195  Real val = RiskMeasure<Real>::val_;
196  Real ev = 0.0;
197  sampler.sumAll(&val,&ev,1);
198  Real gv = RiskMeasure<Real>::gv_;
199  Real egv = 0.0;
200  sampler.sumAll(&gv,&egv,1);
201  Teuchos::RCP<Vector<Real> > g = hv.clone();
202  sampler.sumAll(*(RiskMeasure<Real>::g_),*g);
203  sampler.sumAll(*(RiskMeasure<Real>::hv_),hv);
204  // Compute deviation
205  Real diff = 0.0, pf0 = 0.0, pf1 = 0.0, pf2 = 0.0;
206  Real cg = 0.0, ecg = 0.0, ecgs = 0.0, ch = 0.0, ech = 0.0, echs = 0.0;
207  Teuchos::RCP<Vector<Real> > htmp = hv.clone(); htmp->zero();
208  Teuchos::RCP<Vector<Real> > hs = hv.clone(); hs->zero();
209  for ( unsigned i = 0; i < weights_.size(); i++ ) {
210  cg = 0.0;
211  ch = 0.0;
212  diff = value_storage_[i]-ev;
213  pf0 = positiveFunction_->evaluate(diff,0);
214  pf1 = positiveFunction_->evaluate(diff,1);
215  pf2 = positiveFunction_->evaluate(diff,2);
216  for ( unsigned p = 0; p < order_.size(); p++ ) {
217  cg += coeff_[p]*order_[p]*(gradvec_storage_[i]-egv)*
218  ((order_[p]-1.0)*std::pow(pf0,order_[p]-2.0)*pf1*pf1+
219  std::pow(pf0,order_[p]-1.0)*pf2);
220  ch += coeff_[p]*order_[p]*std::pow(pf0,order_[p]-1.0)*pf1;
221  }
222  ecg += weights_[i]*cg;
223  ech += weights_[i]*ch;
224  htmp->axpy(weights_[i]*cg,*(gradient_storage_[i]));
225  htmp->axpy(weights_[i]*ch,*(hessvec_storage_[i]));
226  }
227  sampler.sumAll(&ech,&echs,1);
228  hv.scale(1.0-echs);
229  sampler.sumAll(&ecg,&ecgs,1);
230  hv.axpy(-ecgs,*g);
231  sampler.sumAll(*htmp,*hs);
232  hv.plus(*hs);
233  }
234 };
235 
236 }
237 
238 #endif
Real getValue(SampleGenerator< Real > &sampler)
std::vector< Teuchos::RCP< Vector< Real > > > gradient_storage_
virtual void scale(const Real alpha)=0
Compute where .
MeanVariance(Real order, Real coeff, Teuchos::RCP< PositiveFunction< Real > > &pf)
std::vector< Real > gradvec_storage_
virtual void plus(const Vector &x)=0
Compute , where .
Teuchos::RCP< PositiveFunction< Real > > positiveFunction_
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:143
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
void update(const Real val, const Real weight)
std::vector< Real > order_
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
std::vector< Real > weights_
MeanVariance(std::vector< Real > &order, std::vector< Real > &coeff, Teuchos::RCP< PositiveFunction< Real > > &pf)
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
std::vector< Real > coeff_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
void sumAll(Real *input, Real *output, int dim) const
void update(const Real val, const Vector< Real > &g, const Real weight)
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
std::vector< Teuchos::RCP< Vector< Real > > > hessvec_storage_
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
std::vector< Real > value_storage_