44 #ifndef ROL_MOMENTOBJECTIVE_H
45 #define ROL_MOMENTOBJECTIVE_H
59 std::vector<std::vector<std::pair<int, Real> > >
moments_;
60 ROL::Ptr<BatchManager<Real> >
bman_;
70 Real val(0), xpt(0), xwt(0), sum(0), half(0.5), one(1), two(2);
71 for (
int k = 0; k < numSamples; k++) {
73 val += xwt * ((power==one) ? xpt : std::pow(xpt,power));
75 bman_->sumAll(&val,&sum,1);
76 Real denom = ((std::abs(moment) < ROL_EPSILON<Real>()) ? one : moment);
77 return half*std::pow((sum-moment)/denom,two);
80 void momentGradient(std::vector<Real> &gradx, std::vector<Real> &gradp, Real &scale,
81 const int dim,
const Real power,
const Real moment,
85 gradx.resize(numSamples,0); gradp.resize(numSamples,0);
87 Real xpt(0), xwt(0), xpow(0), psum(0), one(1), two(2);
88 for (
int k = 0; k < numSamples; k++) {
90 xpow = ((power==one) ? one : ((power==two) ? xpt : std::pow(xpt,power-one)));
91 psum += xwt * xpow * xpt;
92 gradx[k] = xwt * xpow * power;
93 gradp[k] = xpow * xpt;
95 bman_->sumAll(&psum,&scale,1);
97 Real denom = ((std::abs(moment) < ROL_EPSILON<Real>()) ? one : moment);
98 scale /= std::pow(denom,two);
101 void momentHessVec(std::vector<Real> &hvx1, std::vector<Real> &hvx2, std::vector<Real> &hvx3,
102 std::vector<Real> &hvp1, std::vector<Real> &hvp2,
103 Real &scale1, Real &scale2, Real &scale3,
104 const int dim,
const Real power,
const Real moment,
110 hvx1.resize(numSamples,0); hvx2.resize(numSamples,0); hvx3.resize(numSamples,0);
111 hvp1.resize(numSamples,0); hvp2.resize(numSamples,0);
112 scale1 = 0; scale2 = 0; scale3 = 0;
113 std::vector<Real> psum(3,0), scale(3,0);
114 Real xpt(0), xwt(0), vpt(0), vwt(0);
115 Real xpow0(0), xpow1(0), xpow2(0),
zero(0), one(1), two(2), three(3);
116 for (
int k = 0; k < numSamples; k++) {
119 xpow2 = ((power==one) ?
zero : ((power==two) ? one : ((power==three) ? xpt :
120 std::pow(xpt,power-two))));
121 xpow1 = ((power==one) ? one : xpow2 * xpt);
123 psum[0] += xwt * xpow1 * vpt;
124 psum[1] += xwt * xpow0;
125 psum[2] += vwt * xpow0;
126 hvx1[k] = power * xwt * xpow1;
127 hvx2[k] = power * (power-one) * xwt * xpow2 * vpt;
128 hvx3[k] = power * vwt * xpow1;
130 hvp2[k] = power * xpow1 * vpt;
132 bman_->sumAll(&psum[0],&scale[0],3);
133 Real denom = ((std::abs(moment) < ROL_EPSILON<Real>()) ? one : moment);
134 Real denom2 = denom*denom;
136 scale1 = scale[0] * power/denom2;
137 scale2 = (scale[1] - moment)/denom2 ;
138 scale3 = scale[2]/denom2;
144 const bool optProb =
true,
const bool optAtom =
true)
152 const std::vector<int> &order,
154 const bool optProb =
true,
const bool optAtom =
true)
158 std::vector<std::pair<int,Real> > data(
numMoments_);
162 data[i] = std::make_pair(order[i],dist[d]->moment(order[i]));
164 moments_[d].assign(data.begin(),data.end());
173 std::vector<std::pair<int, Real> > data;
177 val +=
momentValue(d,(Real)data[m].first,data[m].second,prob,atom);
188 int numSamples = prob.getNumMyAtoms();
189 std::vector<Real> gradx(numSamples,0), gradp(numSamples,0);
191 std::vector<std::pair<int, Real> > data;
192 std::vector<Real> val_wt(numSamples,0), tmp(
dimension_,0);
193 std::vector<std::vector<Real> > val_pt(numSamples,tmp);
197 momentGradient(gradx,gradp,scale,d,(Real)data[m].first,data[m].second,prob,atom);
198 for (
int k = 0; k < numSamples; k++) {
199 (val_pt[k])[d] += scale*gradx[k];
200 val_wt[k] += scale*gradp[k];
207 for (
int k = 0; k < numSamples; k++) {
209 gprob.setProbability(k,val_wt[k]);
225 const int numSamples = prob.getNumMyAtoms();
226 std::vector<Real> hvx1(numSamples,0), hvx2(numSamples,0), hvx3(numSamples,0);
227 std::vector<Real> hvp1(numSamples,0), hvp2(numSamples,0);
228 Real scale1(0), scale2(0), scale3(0);
229 std::vector<std::pair<int, Real> > data;
230 std::vector<Real> val_wt(numSamples,0), tmp(
dimension_,0);
231 std::vector<std::vector<Real> > val_pt(numSamples,tmp);
236 d,(Real)data[m].first,data[m].second,prob,atom,vprob,vatom);
237 for (
int k = 0; k < numSamples; k++) {
238 (val_pt[k])[d] += (scale1+scale3)*hvx1[k] + scale2*(hvx2[k]+hvx3[k]);
239 val_wt[k] += (scale1+scale3)*hvp1[k] + scale2*hvp2[k];
246 for (
int k = 0; k < numSamples; k++) {
248 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.
const ROL::Ptr< const ProbabilityVector< Real > > getProbabilityVector(void) const
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
ROL::Ptr< const std::vector< Real > > getAtom(const int i) const
void setAtom(const int i, const std::vector< Real > &pt)
Contains definitions of custom data types in ROL.
MomentObjective(const std::vector< std::vector< std::pair< int, Real > > > &moments, const ROL::Ptr< BatchManager< Real > > &bman, const bool optProb=true, const bool optAtom=true)
virtual void zero()
Set to zero vector.
Defines the linear algebra or vector space interface.
std::vector< std::vector< std::pair< int, Real > > > moments_
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
MomentObjective(const std::vector< ROL::Ptr< Distribution< Real > > > &dist, const std::vector< int > &order, const ROL::Ptr< BatchManager< Real > > &bman, const bool optProb=true, const bool optAtom=true)
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
const ROL::Ptr< const AtomVector< Real > > getAtomVector(void) const
ROL::Ptr< BatchManager< Real > > bman_
Real value(const Vector< Real > &x, Real &tol)
Compute value.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
int getNumMyAtoms(void) const