44 #ifndef ROL_MOMENTOBJECTIVE_H 45 #define ROL_MOMENTOBJECTIVE_H 59 std::vector<std::vector<std::pair<int, Real> > >
moments_;
60 Teuchos::RCP<BatchManager<Real> >
bman_;
66 Real
momentValue(
const int dim,
const Real power,
const Real moment,
70 Real val = 0., xpt = 0., xwt = 0., sum = 0.;
71 for (
int k = 0; k < numSamples; k++) {
73 val += xwt * ((power==1) ? xpt : std::pow(xpt,power));
75 bman_->sumAll(&val,&sum,1);
76 return 0.5*std::pow((sum-moment)/moment,2);
79 void momentGradient(std::vector<Real> &gradx, std::vector<Real> &gradp, Real &scale,
80 const int dim,
const Real power,
const Real moment,
84 gradx.resize(numSamples,0.); gradp.resize(numSamples,0.);
86 Real xpt = 0., xwt = 0., xpow = 0., psum = 0.;
87 for (
int k = 0; k < numSamples; k++) {
89 xpow = ((power==1) ? 1. : ((power==2) ? xpt : std::pow(xpt,power-1)));
90 psum += xwt * xpow * xpt;
91 gradx[k] = xwt * xpow * power;
92 gradp[k] = xpow * xpt;
94 bman_->sumAll(&psum,&scale,1);
96 scale /= std::pow(moment,2);
99 void momentHessVec(std::vector<Real> &hvx1, std::vector<Real> &hvx2, std::vector<Real> &hvx3,
100 std::vector<Real> &hvp1, std::vector<Real> &hvp2,
101 Real &scale1, Real &scale2, Real &scale3,
102 const int dim,
const Real power,
const Real moment,
108 hvx1.resize(numSamples,0.); hvx2.resize(numSamples,0.); hvx3.resize(numSamples,0.);
109 hvp1.resize(numSamples,0.); hvp2.resize(numSamples,0.);
110 scale1 = 0.; scale2 = 0.; scale3 = 0.;
111 std::vector<Real> psum(3,0.0), scale(3,0.0);
112 Real xpt = 0., xwt = 0., vpt = 0., vwt = 0.;
113 Real xpow0 = 0., xpow1 = 0., xpow2 = 0.;
114 const Real moment2 = std::pow(moment,2);
115 for (
int k = 0; k < numSamples; k++) {
118 xpow2 = ((power==1) ? 0. : ((power==2) ? 1. : ((power==3) ? xpt :
119 std::pow(xpt,power-2))));
120 xpow1 = ((power==1) ? 1. : xpow2 * xpt);
122 psum[0] += xwt * xpow1 * vpt;
123 psum[1] += xwt * xpow0;
124 psum[2] += vwt * xpow0;
125 hvx1[k] = power * xwt * xpow1;
126 hvx2[k] = power * (power-1.) * xwt * xpow2 * vpt;
127 hvx3[k] = power * vwt * xpow1;
129 hvp2[k] = power * xpow1 * vpt;
131 bman_->sumAll(&psum[0],&scale[0],3);
132 scale1 = scale[0] * power/moment2;
133 scale2 = (scale[1] - moment)/moment2 ;
134 scale3 = scale[2]/moment2;
140 const bool optProb =
true,
const bool optAtom =
true)
141 :
Objective<Real>(), moments_(moments), bman_(bman),
142 optProb_(optProb), optAtom_(optAtom) {
143 dimension_ = moments_.size();
144 numMoments_ = moments_[0].size();
148 const std::vector<int> &order,
150 const bool optProb =
true,
const bool optAtom =
true)
151 :
Objective<Real>(), bman_(bman), optProb_(optProb), optAtom_(optAtom) {
152 numMoments_ = order.size();
153 dimension_ = dist.size();
154 std::vector<std::pair<int,Real> > data(numMoments_);
155 moments_.clear(); moments_.resize(dimension_);
158 data[i] = std::make_pair(order[i],dist[d]->moment(order[i]));
160 moments_[d].assign(data.begin(),data.end());
169 std::vector<std::pair<int, Real> > data;
173 val +=
momentValue(d,(Real)data[m].first,data[m].second,prob,atom);
185 std::vector<Real> gradx(numSamples,0.), gradp(numSamples,0.);
187 std::vector<std::pair<int, Real> > data;
188 std::vector<Real> val_wt(numSamples,0.), tmp(dimension_,0.);
189 std::vector<std::vector<Real> > val_pt(numSamples,tmp);
193 momentGradient(gradx,gradp,scale,d,(Real)data[m].first,data[m].second,prob,atom);
194 for (
int k = 0; k < numSamples; k++) {
195 (val_pt[k])[d] += scale*gradx[k];
196 val_wt[k] += scale*gradp[k];
203 for (
int k = 0; k < numSamples; k++) {
205 gprob.setProbability(k,val_wt[k]);
222 std::vector<Real> hvx1(numSamples,0.), hvx2(numSamples,0.), hvx3(numSamples,0.);
223 std::vector<Real> hvp1(numSamples,0.), hvp2(numSamples,0.);
224 Real scale1 = 0., scale2 = 0., scale3 = 0.;
225 std::vector<std::pair<int, Real> > data;
226 std::vector<Real> val_wt(numSamples,0.), tmp(dimension_,0.);
227 std::vector<std::vector<Real> > val_pt(numSamples,tmp);
232 d,(Real)data[m].first,data[m].second,prob,atom,vprob,vatom);
233 for (
int k = 0; k < numSamples; k++) {
234 (val_pt[k])[d] += (scale1+scale3)*hvx1[k] + scale2*(hvx2[k]+hvx3[k]);
235 val_wt[k] += (scale1+scale3)*hvp1[k] + scale2*hvp2[k];
242 for (
int k = 0; k < numSamples; k++) {
244 hprob.setProbability(k,val_wt[k]);
Provides the interface to evaluate objective functions.
void momentGradient(std::vector< Real > &gradx, std::vector< Real > &gradp, Real &scale, const int dim, const Real power, const Real moment, const ProbabilityVector< Real > &prob, const AtomVector< Real > &atom) const
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
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 int dim, const Real power, const Real moment, const ProbabilityVector< Real > &prob, const AtomVector< Real > &atom, const ProbabilityVector< Real > &vprob, const AtomVector< Real > &vatom) const
const Real getProbability(const int i) const
void setAtom(const int i, const std::vector< Real > &pt)
Contains definitions of custom data types in ROL.
const Teuchos::RCP< const ProbabilityVector< Real > > getProbabilityVector(void) const
virtual void zero()
Set to zero vector.
Defines the linear algebra or vector space interface.
std::vector< std::vector< std::pair< int, Real > > > moments_
Teuchos::RCP< const std::vector< Real > > getAtom(const int i) const
Teuchos::RCP< BatchManager< Real > > bman_
const Teuchos::RCP< const AtomVector< Real > > getAtomVector(void) const
Provides the std::vector implementation of the ROL::Vector interface.
Provides the std::vector implementation of the ROL::Vector interface.
Real momentValue(const int dim, const Real power, const Real moment, const ProbabilityVector< Real > &prob, const AtomVector< Real > &atom) const
int getNumMyAtoms(void) const
MomentObjective(const std::vector< Teuchos::RCP< Distribution< Real > > > &dist, const std::vector< int > &order, const Teuchos::RCP< BatchManager< Real > > &bman, const bool optProb=true, const bool optAtom=true)
Real value(const Vector< Real > &x, Real &tol)
Compute value.
MomentObjective(const std::vector< std::vector< std::pair< int, Real > > > &moments, const Teuchos::RCP< BatchManager< Real > > &bman, const bool optProb=true, const bool optAtom=true)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
int getNumMyAtoms(void) const