ROL
ROL_MomentObjective.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_MOMENTOBJECTIVE_H
45 #define ROL_MOMENTOBJECTIVE_H
46 
47 #include "ROL_Objective.hpp"
48 #include "ROL_SROMVector.hpp"
49 #include "ROL_Types.hpp"
50 #include <iostream>
51 
52 namespace ROL {
53 
54 template <class Real>
55 class MomentObjective : public Objective<Real> {
56 private:
57  const std::vector<std::vector<std::pair<size_t, Real> > > moments_;
58 
59  Real momentValue(const size_t dim, const Real power, const Real moment, const SROMVector<Real> &x) const {
60  const size_t numSamples = x.getNumSamples();
61  Real val = 0., xpt = 0., xwt = 0.;
62  for (size_t k = 0; k < numSamples; k++) {
63  xpt = (*x.getPoint(k))[dim]; xwt = x.getWeight(k);
64  val += xwt * ((power==1) ? xpt : std::pow(xpt,power));
65  }
66  return 0.5*std::pow((val-moment)/moment,2);
67  }
68 
69  void momentGradient(std::vector<Real> &gradx, std::vector<Real> &gradp, Real &scale,
70  const size_t dim, const Real power, const Real moment, const SROMVector<Real> &x) const {
71  const size_t numSamples = x.getNumSamples();
72  gradx.resize(numSamples,0.); gradp.resize(numSamples,0.);
73  scale = 0.;
74  Real xpt = 0., xwt = 0., xpow = 0.;
75  for (size_t k = 0; k < numSamples; k++) {
76  xpt = (*x.getPoint(k))[dim]; xwt = x.getWeight(k);
77  xpow = ((power==1) ? 1. : ((power==2) ? xpt : std::pow(xpt,power-1)));
78  scale += xwt * xpow * xpt;
79  gradx[k] = xwt * xpow * power;
80  gradp[k] = xpow * xpt;
81  }
82  scale -= moment;
83  scale /= std::pow(moment,2);
84  }
85 
86  void momentHessVec(std::vector<Real> &hvx1, std::vector<Real> &hvx2, std::vector<Real> &hvx3,
87  std::vector<Real> &hvp1, std::vector<Real> &hvp2,
88  Real &scale1, Real &scale2, Real &scale3,
89  const size_t dim, const Real power, const Real moment,
90  const SROMVector<Real> &x, const SROMVector<Real> &v) const {
91  const size_t numSamples = x.getNumSamples();
92  hvx1.resize(numSamples,0.); hvx2.resize(numSamples,0.); hvx3.resize(numSamples,0.);
93  hvp1.resize(numSamples,0.); hvp2.resize(numSamples,0.);
94  scale1 = 0.; scale2 = 0.; scale3 = 0.;
95  Real xpt = 0., xwt = 0., vpt = 0., vwt = 0.;
96  Real xpow0 = 0., xpow1 = 0., xpow2 = 0.;
97  const Real moment2 = std::pow(moment,2);
98  for (size_t k = 0; k < numSamples; k++) {
99  xpt = (*x.getPoint(k))[dim]; xwt = x.getWeight(k);
100  vpt = (*v.getPoint(k))[dim]; vwt = v.getWeight(k);
101  xpow2 = ((power==1) ? 0. : ((power==2) ? 1. : ((power==3) ? xpt :
102  std::pow(xpt,power-2))));
103  xpow1 = ((power==1) ? 1. : xpow2 * xpt);
104  xpow0 = xpow1 * xpt;
105  scale1 += xwt * xpow1 * vpt;
106  scale2 += xwt * xpow0;
107  scale3 += vwt * xpow0;
108  hvx1[k] = power * xwt * xpow1;
109  hvx2[k] = power * (power-1.) * xwt * xpow2 * vpt;
110  hvx3[k] = power * vwt * xpow1;
111  hvp1[k] = xpow0;
112  hvp2[k] = power * xpow1 * vpt;
113  }
114  scale1 *= power/moment2;
115  scale2 -= moment;
116  scale2 /= moment2;
117  scale3 /= moment2;
118  }
119 
120 public:
121  MomentObjective(const std::vector<std::vector<std::pair<size_t, Real> > > &moments)
122  : Objective<Real>(), moments_(moments) {}
123 
124  Real value( const Vector<Real> &x, Real &tol ) {
125  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
126  size_t dimension = ex.getDimension();
127  Real val = 0.;
128  std::vector<std::pair<size_t, Real> > data;
129  for (size_t d = 0; d < dimension; d++) {
130  data = moments_[d];
131  for (size_t m = 0; m < data.size(); m++) {
132  val += momentValue(d,(Real)data[m].first,data[m].second,ex);
133  }
134  }
135  return val;
136  }
137 
138  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
139  SROMVector<Real> &eg = Teuchos::dyn_cast<SROMVector<Real> >(g);
140  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
141  size_t dimension = ex.getDimension();
142  size_t numSamples = ex.getNumSamples();
143  std::vector<Real> gradx(numSamples,0.), gradp(numSamples,0.);
144  Real scale = 0.;
145  std::vector<std::pair<size_t, Real> > data;
146  std::vector<Real> val_wt(numSamples,0.), tmp(dimension,0.);
147  std::vector<std::vector<Real> > val_pt(numSamples,tmp);
148  for (size_t d = 0; d < dimension; d++) {
149  data = moments_[d];
150  for (size_t m = 0; m < data.size(); m++) {
151  momentGradient(gradx,gradp,scale,d,(Real)data[m].first,data[m].second,ex);
152  for (size_t k = 0; k < numSamples; k++) {
153  (val_pt[k])[d] += scale*gradx[k];
154  val_wt[k] += scale*gradp[k];
155  }
156  }
157  }
158  for (size_t k = 0; k < numSamples; k++) {
159  eg.setPoint(k,val_pt[k]);
160  eg.setWeight(k,val_wt[k]);
161  }
162  }
163 
164  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
165  SROMVector<Real> &ehv = Teuchos::dyn_cast<SROMVector<Real> >(hv);
166  const SROMVector<Real> &ev = Teuchos::dyn_cast<const SROMVector<Real> >(v);
167  const SROMVector<Real> &ex = Teuchos::dyn_cast<const SROMVector<Real> >(x);
168  const size_t dimension = ex.getDimension();
169  const size_t numSamples = ex.getNumSamples();
170  std::vector<Real> hvx1(numSamples,0.), hvx2(numSamples,0.), hvx3(numSamples,0.);
171  std::vector<Real> hvp1(numSamples,0.), hvp2(numSamples,0.);
172  Real scale1 = 0., scale2 = 0., scale3 = 0.;
173  std::vector<std::pair<size_t, Real> > data;
174  std::vector<Real> val_wt(numSamples,0.), tmp(dimension,0.);
175  std::vector<std::vector<Real> > val_pt(numSamples,tmp);
176  for (size_t d = 0; d < dimension; d++) {
177  data = moments_[d];
178  for (size_t m = 0; m < data.size(); m++) {
179  momentHessVec(hvx1,hvx2,hvx3,hvp1,hvp2,scale1,scale2,scale3,
180  d,(Real)data[m].first,data[m].second,ex,ev);
181  for (size_t k = 0; k < numSamples; k++) {
182  (val_pt[k])[d] += (scale1+scale3)*hvx1[k] + scale2*(hvx2[k]+hvx3[k]);
183  val_wt[k] += (scale1+scale3)*hvp1[k] + scale2*hvp2[k];
184  }
185  }
186  }
187  for (size_t k = 0; k < numSamples; k++) {
188  ehv.setPoint(k,val_pt[k]);
189  ehv.setWeight(k,val_wt[k]);
190  }
191  }
192 }; // class SROMObjective
193 
194 } // namespace ROL
195 
196 #endif
Provides the interface to evaluate objective functions.
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Teuchos::RCP< const std::vector< Element > > getPoint(const size_t i) const
const size_t getDimension(void) const
const Element getWeight(const size_t i) const
void setWeight(const size_t i, const Element wt)
Contains definitions of custom data types in ROL.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real momentValue(const size_t dim, const Real power, const Real moment, const SROMVector< Real > &x) const
const size_t getNumSamples(void) const
const std::vector< std::vector< std::pair< size_t, Real > > > moments_
Provides the std::vector implementation of the ROL::Vector interface.
void momentHessVec(std::vector< Real > &hvx1, std::vector< Real > &hvx2, std::vector< Real > &hvx3, std::vector< Real > &hvp1, std::vector< Real > &hvp2, Real &scale1, Real &scale2, Real &scale3, const size_t dim, const Real power, const Real moment, const SROMVector< Real > &x, const SROMVector< Real > &v) const
Real value(const Vector< Real > &x, Real &tol)
Compute value.
void momentGradient(std::vector< Real > &gradx, std::vector< Real > &gradp, Real &scale, const size_t dim, const Real power, const Real moment, const SROMVector< Real > &x) const
MomentObjective(const std::vector< std::vector< std::pair< size_t, Real > > > &moments)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
void setPoint(const size_t i, const std::vector< Element > &pt)