ROL
ROL_MeanDeviationFromTarget.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_MEANDEVIATIONFROMTARGET_HPP
45 #define ROL_MEANDEVIATIONFROMTARGET_HPP
46 
47 #include "ROL_RiskMeasure.hpp"
48 #include "ROL_PositiveFunction.hpp"
49 
50 namespace ROL {
51 
52 template<class Real>
53 class MeanDeviationFromTarget : public RiskMeasure<Real> {
54 private:
55  std::vector<Real> target_;
56  std::vector<Real> order_;
57  std::vector<Real> coeff_;
58  std::vector<Real> pval_;
59  std::vector<Real> pgv_;
60  std::vector<Teuchos::RCP<Vector<Real> > > pg0_;
61  std::vector<Teuchos::RCP<Vector<Real> > > pg_;
62  std::vector<Teuchos::RCP<Vector<Real> > > phv_;
63  Teuchos::RCP<PositiveFunction<Real> > positiveFunction_;
64 
65 public:
66  MeanDeviationFromTarget( Real target, Real order, Real coeff,
67  Teuchos::RCP<PositiveFunction<Real> > &pf ) : positiveFunction_(pf) {
68  target_.clear();
69  target_.push_back(target);
70  order_.clear();
71  order_.push_back((order < 2.0) ? 2.0 : order);
72  coeff_.clear();
73  coeff_.push_back((coeff < 0.0) ? 1.0 : coeff);
74  }
75  MeanDeviationFromTarget( std::vector<Real> &target, std::vector<Real> &order, std::vector<Real> &coeff,
76  Teuchos::RCP<PositiveFunction<Real> > &pf ) : positiveFunction_(pf) {
77  target_.clear();
78  order_.clear();
79  coeff_.clear();
80  if ( order.size() != target.size() ) {
81  target.resize(order.size(),0.0);
82  }
83  if ( order.size() != coeff.size() ) {
84  coeff.resize(order.size(),1.0);
85  }
86  for ( unsigned i = 0; i < order.size(); i++ ) {
87  target_.push_back(target[i]);
88  order_.push_back((order[i] < 2.0) ? 2.0 : order[i]);
89  coeff_.push_back((coeff[i] < 0.0) ? 1.0 : coeff[i]);
90  }
91  }
92 
93  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
95  this->pval_.clear();
96  this->pval_.resize(this->order_.size(),0.0);
97  this->pgv_.clear();
98  this->pgv_.resize(this->order_.size(),0.0);
99  this->pg_.clear();
100  this->pg0_.clear();
101  this->phv_.clear();
102  for ( unsigned p = 0; p < this->order_.size(); p++ ) {
103  this->pg0_.push_back(x.clone());
104  this->pg_.push_back(x.clone());
105  this->phv_.push_back(x.clone());
106  }
109  x0 = Teuchos::rcp(&const_cast<Vector<Real> &>(x),false);
110  }
111 
112  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
113  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
115  this->pval_.clear();
116  this->pval_.resize(this->order_.size(),0.0);
117  this->pgv_.clear();
118  this->pgv_.resize(this->order_.size(),0.0);
119  this->pg_.clear();
120  this->pg0_.clear();
121  this->phv_.clear();
122  for ( unsigned p = 0; p < this->order_.size(); p++ ) {
123  this->pg_.push_back(x.clone());
124  this->pg0_.push_back(x.clone());
125  this->phv_.push_back(x.clone());
126  }
129  x0 = Teuchos::rcp(&const_cast<Vector<Real> &>(x),false);
130  v0 = Teuchos::rcp(&const_cast<Vector<Real> &>(v),false);
131  }
132 
133  void update(const Real val, const Real weight) {
134  Real diff = 0.0, pf0 = 0.0;
135  RiskMeasure<Real>::val_ += weight * val;
136  for ( unsigned p = 0; p < this->order_.size(); p++ ) {
137  diff = val-this->target_[p];
138  pf0 = this->positiveFunction_->evaluate(diff,0);
139  this->pval_[p] += weight * std::pow(pf0,this->order_[p]);
140  }
141  }
142 
143  void update(const Real val, const Vector<Real> &g, const Real weight) {
144  Real diff = 0.0, pf0 = 0.0, pf1 = 0.0, c = 0.0;
145  for ( unsigned p = 0; p < this->order_.size(); p++ ) {
146  diff = val-this->target_[p];
147  pf0 = this->positiveFunction_->evaluate(diff,0);
148  pf1 = this->positiveFunction_->evaluate(diff,1);
149  c = std::pow(pf0,this->order_[p]-1.0) * pf1;
150  (this->pg_[p])->axpy(weight * c,g);
151  this->pval_[p] += weight * std::pow(pf0,this->order_[p]);
152  }
153  RiskMeasure<Real>::g_->axpy(weight,g);
154  }
155 
156  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
157  const Real weight) {
158  Real diff = 0.0, pf0 = 0.0, pf1 = 0.0, pf2 = 0.0, p0 = 0.0, p1 = 0.0, p2 = 0.0, c = 0.0;
159  for ( unsigned p = 0; p < this->order_.size(); p++ ) {
160  diff = val - this->target_[p];
161  pf0 = this->positiveFunction_->evaluate(diff,0);
162  pf1 = this->positiveFunction_->evaluate(diff,1);
163  pf2 = this->positiveFunction_->evaluate(diff,2);
164  p0 = std::pow(pf0,this->order_[p]);
165  p1 = std::pow(pf0,this->order_[p]-1.0);
166  p2 = std::pow(pf0,this->order_[p]-2.0);
167  c = -(this->order_[p]-1.0)*p1*pf1;
168  this->pg0_[p]->axpy(weight*c,g);
169  c = gv*((this->order_[p]-1.0)*p2*pf1*pf1 + p1*pf2);
170  this->pg_[p]->axpy(weight*c,g);
171  c = p1*pf1;
172  this->phv_[p]->axpy(weight*c,hv);
173  this->pval_[p] += weight*p0;
174  this->pgv_[p] += weight*p1*pf1*gv;
175  }
176  RiskMeasure<Real>::hv_->axpy(weight,hv);
177  }
178 
180  Real val = RiskMeasure<Real>::val_;
181  Real dev = 0.0;
182  sampler.sumAll(&val,&dev,1);
183  std::vector<Real> pval_sum(this->pval_.size());
184  sampler.sumAll(&(this->pval_)[0],&pval_sum[0],this->pval_.size());
185  for ( unsigned p = 0; p < this->order_.size(); p++ ) {
186  dev += this->coeff_[p] * std::pow(pval_sum[p],1.0/this->order_[p]);
187  }
188  return dev;
189  }
190 
192  sampler.sumAll(*(RiskMeasure<Real>::g_),g);
193  std::vector<Real> pval_sum(this->pval_.size());
194  sampler.sumAll(&(this->pval_)[0],&pval_sum[0],this->pval_.size());
195  Teuchos::RCP<Vector<Real> > pg;
196  for ( unsigned p = 0; p < this->order_.size(); p++ ) {
197  if ( pval_sum[p] > 0.0 ) {
198  pg = (this->pg_[p])->clone();
199  sampler.sumAll(*(this->pg_[p]),*pg);
200  g.axpy(this->coeff_[p]/std::pow(pval_sum[p],1.0-1.0/this->order_[p]),*pg);
201  }
202  }
203  }
205  sampler.sumAll(*(RiskMeasure<Real>::hv_),hv);
206  std::vector<Real> pval_sum(this->pval_.size());
207  sampler.sumAll(&(this->pval_)[0],&pval_sum[0],this->pval_.size());
208  std::vector<Real> pgv_sum(this->pgv_.size());
209  sampler.sumAll(&(this->pgv_)[0],&pgv_sum[0],this->pgv_.size());
210  Real c = 0.0;
211  Teuchos::RCP<Vector<Real> > pg, pg0, phv;
212  for ( unsigned p = 0; p < this->order_.size(); p++ ) {
213  if ( pval_sum[p] > 0.0 ) {
214  pg = (this->pg_[p])->clone();
215  sampler.sumAll(*(this->pg_[p]),*pg);
216  pg0 = (this->pg0_[p])->clone();
217  sampler.sumAll(*(this->pg0_[p]),*pg0);
218  phv = (this->phv_[p])->clone();
219  sampler.sumAll(*(this->phv_[p]),*phv);
220  c = this->coeff_[p]*(pgv_sum[p]/std::pow(pval_sum[p],2.0-1.0/this->order_[p]));
221  hv.axpy(c,*pg0);
222  c = this->coeff_[p]/std::pow(pval_sum[p],1.0-1.0/this->order_[p]);
223  hv.axpy(c,*pg);
224  hv.axpy(c,*phv);
225  }
226  }
227  }
228 };
229 
230 }
231 
232 #endif
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
MeanDeviationFromTarget(std::vector< Real > &target, std::vector< Real > &order, std::vector< Real > &coeff, Teuchos::RCP< PositiveFunction< Real > > &pf)
void getGradient(Vector< Real > &g, SampleGenerator< Real > &sampler)
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:141
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void update(const Real val, const Vector< Real > &g, const Real gv, const Vector< Real > &hv, const Real weight)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
void sumAll(Real *input, Real *output, int dim) const
MeanDeviationFromTarget(Real target, Real order, Real coeff, Teuchos::RCP< PositiveFunction< Real > > &pf)
Real getValue(SampleGenerator< Real > &sampler)
std::vector< Teuchos::RCP< Vector< Real > > > phv_
void update(const Real val, const Real weight)
Teuchos::RCP< PositiveFunction< Real > > positiveFunction_
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x, Teuchos::RCP< Vector< Real > > &v0, const Vector< Real > &v)
std::vector< Teuchos::RCP< Vector< Real > > > pg_
std::vector< Teuchos::RCP< Vector< Real > > > pg0_
void getHessVec(Vector< Real > &hv, SampleGenerator< Real > &sampler)
void update(const Real val, const Vector< Real > &g, const Real weight)