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  Teuchos::RCP<PositiveFunction<Real> > positiveFunction_;
56 
57  std::vector<Real> target_;
58  std::vector<Real> order_;
59  std::vector<Real> coeff_;
60  std::vector<Real> pval_;
61  std::vector<Real> pgv_;
62 
63  std::vector<Teuchos::RCP<Vector<Real> > > pg0_;
64  std::vector<Teuchos::RCP<Vector<Real> > > pg_;
65  std::vector<Teuchos::RCP<Vector<Real> > > phv_;
66 
68 
69 public:
70 
71  MeanDeviationFromTarget( Real target, Real order, Real coeff,
72  Teuchos::RCP<PositiveFunction<Real> > &pf )
73  : RiskMeasure<Real>(), positiveFunction_(pf), firstReset_(true) {
74  // Initialize storage for problem data
75  target_.clear(); order_.clear(); coeff_.clear();
76  target_.push_back(target);
77  order_.push_back((order < 2.0) ? 2.0 : order);
78  coeff_.push_back((coeff < 0.0) ? 1.0 : coeff);
79  // Initialize additional storage
80  pg_.clear(); pg0_.clear(); phv_.clear(); pval_.clear(); pgv_.clear();
81  pg_.resize(order_.size());
82  pg0_.resize(order_.size());
83  phv_.resize(order_.size());
84  pval_.resize(order_.size(),0.0);
85  pgv_.resize(order_.size(),0.0);
86  }
87 
88  MeanDeviationFromTarget( std::vector<Real> &target, std::vector<Real> &order, std::vector<Real> &coeff,
89  Teuchos::RCP<PositiveFunction<Real> > &pf )
90  : RiskMeasure<Real>(), positiveFunction_(pf), firstReset_(true) {
91  // Initialize storage for problem data
92  target_.clear(); order_.clear(); coeff_.clear();
93  if ( order.size() != target.size() ) {
94  target.resize(order.size(),0.0);
95  }
96  if ( order.size() != coeff.size() ) {
97  coeff.resize(order.size(),1.0);
98  }
99  for ( unsigned i = 0; i < order.size(); i++ ) {
100  target_.push_back(target[i]);
101  order_.push_back((order[i] < 2.0) ? 2.0 : order[i]);
102  coeff_.push_back((coeff[i] < 0.0) ? 1.0 : coeff[i]);
103  }
104  // Initialize additional storage
105  pg_.clear(); pg0_.clear(); phv_.clear(); pval_.clear(); pgv_.clear();
106  pg_.resize(order_.size());
107  pg0_.resize(order_.size());
108  phv_.resize(order_.size());
109  pval_.resize(order_.size(),0.0);
110  pgv_.resize(order_.size(),0.0);
111  }
112 
113  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x) {
115  if (firstReset_) {
116  for ( unsigned p = 0; p < order_.size(); p++ ) {
117  pg0_[p] = (x.dual()).clone();
118  pg_[p] = (x.dual()).clone();
119  phv_[p] = (x.dual()).clone();
120  }
121  firstReset_ = false;
122  }
123  for ( unsigned p = 0; p < order_.size(); p++ ) {
124  pg0_[p]->zero(); pg_[p]->zero(); phv_[p]->zero();
125  pval_[p] = 0.0; pgv_[p] = 0.0;
126  }
127  }
128 
129  void reset(Teuchos::RCP<Vector<Real> > &x0, const Vector<Real> &x,
130  Teuchos::RCP<Vector<Real> > &v0, const Vector<Real> &v) {
131  RiskMeasure<Real>::reset(x0,x,v0,v);
132  if (firstReset_) {
133  for ( unsigned p = 0; p < order_.size(); p++ ) {
134  pg0_[p] = (x.dual()).clone();
135  pg_[p] = (x.dual()).clone();
136  phv_[p] = (x.dual()).clone();
137  }
138  firstReset_ = false;
139  }
140  for ( unsigned p = 0; p < order_.size(); p++ ) {
141  pg0_[p]->zero(); pg_[p]->zero(); phv_[p]->zero();
142  pval_[p] = 0.0; pgv_[p] = 0.0;
143  }
144  }
145 
146  void update(const Real val, const Real weight) {
147  Real diff = 0.0, pf0 = 0.0;
148  RiskMeasure<Real>::val_ += weight * val;
149  for ( unsigned p = 0; p < order_.size(); p++ ) {
150  diff = val-target_[p];
151  pf0 = positiveFunction_->evaluate(diff,0);
152  pval_[p] += weight * std::pow(pf0,order_[p]);
153  }
154  }
155 
156  void update(const Real val, const Vector<Real> &g, const Real weight) {
157  Real diff = 0.0, pf0 = 0.0, pf1 = 0.0, c = 0.0;
158  for ( unsigned p = 0; p < order_.size(); p++ ) {
159  diff = val-target_[p];
160  pf0 = positiveFunction_->evaluate(diff,0);
161  pf1 = positiveFunction_->evaluate(diff,1);
162  c = std::pow(pf0,order_[p]-1.0) * pf1;
163  (pg_[p])->axpy(weight * c,g);
164  pval_[p] += weight * std::pow(pf0,order_[p]);
165  }
166  RiskMeasure<Real>::g_->axpy(weight,g);
167  }
168 
169  void update(const Real val, const Vector<Real> &g, const Real gv, const Vector<Real> &hv,
170  const Real weight) {
171  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;
172  for ( unsigned p = 0; p < order_.size(); p++ ) {
173  diff = val - target_[p];
174  pf0 = positiveFunction_->evaluate(diff,0);
175  pf1 = positiveFunction_->evaluate(diff,1);
176  pf2 = positiveFunction_->evaluate(diff,2);
177  p0 = std::pow(pf0,order_[p]);
178  p1 = std::pow(pf0,order_[p]-1.0);
179  p2 = std::pow(pf0,order_[p]-2.0);
180  c = -(order_[p]-1.0)*p1*pf1;
181  pg0_[p]->axpy(weight*c,g);
182  c = gv*((order_[p]-1.0)*p2*pf1*pf1 + p1*pf2);
183  pg_[p]->axpy(weight*c,g);
184  c = p1*pf1;
185  phv_[p]->axpy(weight*c,hv);
186  pval_[p] += weight*p0;
187  pgv_[p] += weight*p1*pf1*gv;
188  }
189  RiskMeasure<Real>::hv_->axpy(weight,hv);
190  }
191 
193  Real val = RiskMeasure<Real>::val_;
194  Real dev = 0.0;
195  sampler.sumAll(&val,&dev,1);
196  std::vector<Real> pval_sum(pval_.size());
197  sampler.sumAll(&(pval_)[0],&pval_sum[0],pval_.size());
198  for ( unsigned p = 0; p < order_.size(); p++ ) {
199  dev += coeff_[p] * std::pow(pval_sum[p],1.0/order_[p]);
200  }
201  return dev;
202  }
203 
205  sampler.sumAll(*(RiskMeasure<Real>::g_),g);
206  std::vector<Real> pval_sum(pval_.size());
207  sampler.sumAll(&(pval_)[0],&pval_sum[0],pval_.size());
208  Teuchos::RCP<Vector<Real> > pg;
209  for ( unsigned p = 0; p < order_.size(); p++ ) {
210  if ( pval_sum[p] > 0.0 ) {
211  pg = (pg_[p])->clone();
212  sampler.sumAll(*(pg_[p]),*pg);
213  g.axpy(coeff_[p]/std::pow(pval_sum[p],1.0-1.0/order_[p]),*pg);
214  }
215  }
216  }
218  sampler.sumAll(*(RiskMeasure<Real>::hv_),hv);
219  std::vector<Real> pval_sum(pval_.size());
220  sampler.sumAll(&(pval_)[0],&pval_sum[0],pval_.size());
221  std::vector<Real> pgv_sum(pgv_.size());
222  sampler.sumAll(&(pgv_)[0],&pgv_sum[0],pgv_.size());
223  Real c = 0.0;
224  Teuchos::RCP<Vector<Real> > pg, pg0, phv;
225  for ( unsigned p = 0; p < order_.size(); p++ ) {
226  if ( pval_sum[p] > 0.0 ) {
227  pg = (pg_[p])->clone();
228  sampler.sumAll(*(pg_[p]),*pg);
229  pg0 = (pg0_[p])->clone();
230  sampler.sumAll(*(pg0_[p]),*pg0);
231  phv = (phv_[p])->clone();
232  sampler.sumAll(*(phv_[p]),*phv);
233  c = coeff_[p]*(pgv_sum[p]/std::pow(pval_sum[p],2.0-1.0/order_[p]));
234  hv.axpy(c,*pg0);
235  c = coeff_[p]/std::pow(pval_sum[p],1.0-1.0/order_[p]);
236  hv.axpy(c,*pg);
237  hv.axpy(c,*phv);
238  }
239  }
240  }
241 };
242 
243 }
244 
245 #endif
void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:213
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:143
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:74
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)
virtual void reset(Teuchos::RCP< Vector< Real > > &x0, const Vector< Real > &x)
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)