10 #ifndef ROL_MOMENTOBJECTIVE_H
11 #define ROL_MOMENTOBJECTIVE_H
25 std::vector<std::vector<std::pair<int, Real> > >
moments_;
26 ROL::Ptr<BatchManager<Real> >
bman_;
36 Real val(0), xpt(0), xwt(0), sum(0), half(0.5), one(1), two(2);
37 for (
int k = 0; k < numSamples; k++) {
39 val += xwt * ((power==one) ? xpt : std::pow(xpt,power));
41 bman_->sumAll(&val,&sum,1);
42 Real denom = ((std::abs(moment) < ROL_EPSILON<Real>()) ? one : moment);
43 return half*std::pow((sum-moment)/denom,two);
46 void momentGradient(std::vector<Real> &gradx, std::vector<Real> &gradp, Real &scale,
47 const int dim,
const Real power,
const Real moment,
51 gradx.resize(numSamples,0); gradp.resize(numSamples,0);
53 Real xpt(0), xwt(0), xpow(0), psum(0), one(1), two(2);
54 for (
int k = 0; k < numSamples; k++) {
56 xpow = ((power==one) ? one : ((power==two) ? xpt : std::pow(xpt,power-one)));
57 psum += xwt * xpow * xpt;
58 gradx[k] = xwt * xpow * power;
59 gradp[k] = xpow * xpt;
61 bman_->sumAll(&psum,&scale,1);
63 Real denom = ((std::abs(moment) < ROL_EPSILON<Real>()) ? one : moment);
64 scale /= std::pow(denom,two);
67 void momentHessVec(std::vector<Real> &hvx1, std::vector<Real> &hvx2, std::vector<Real> &hvx3,
68 std::vector<Real> &hvp1, std::vector<Real> &hvp2,
69 Real &scale1, Real &scale2, Real &scale3,
70 const int dim,
const Real power,
const Real moment,
76 hvx1.resize(numSamples,0); hvx2.resize(numSamples,0); hvx3.resize(numSamples,0);
77 hvp1.resize(numSamples,0); hvp2.resize(numSamples,0);
78 scale1 = 0; scale2 = 0; scale3 = 0;
79 std::vector<Real> psum(3,0), scale(3,0);
80 Real xpt(0), xwt(0), vpt(0), vwt(0);
81 Real xpow0(0), xpow1(0), xpow2(0),
zero(0), one(1), two(2), three(3);
82 for (
int k = 0; k < numSamples; k++) {
85 xpow2 = ((power==one) ?
zero : ((power==two) ? one : ((power==three) ? xpt :
86 std::pow(xpt,power-two))));
87 xpow1 = ((power==one) ? one : xpow2 * xpt);
89 psum[0] += xwt * xpow1 * vpt;
90 psum[1] += xwt * xpow0;
91 psum[2] += vwt * xpow0;
92 hvx1[k] = power * xwt * xpow1;
93 hvx2[k] = power * (power-one) * xwt * xpow2 * vpt;
94 hvx3[k] = power * vwt * xpow1;
96 hvp2[k] = power * xpow1 * vpt;
98 bman_->sumAll(&psum[0],&scale[0],3);
99 Real denom = ((std::abs(moment) < ROL_EPSILON<Real>()) ? one : moment);
100 Real denom2 = denom*denom;
102 scale1 = scale[0] * power/denom2;
103 scale2 = (scale[1] - moment)/denom2 ;
104 scale3 = scale[2]/denom2;
110 const bool optProb =
true,
const bool optAtom =
true)
118 const std::vector<int> &order,
120 const bool optProb =
true,
const bool optAtom =
true)
124 std::vector<std::pair<int,Real> > data(
numMoments_);
128 data[i] = std::make_pair(order[i],dist[d]->moment(order[i]));
130 moments_[d].assign(data.begin(),data.end());
139 std::vector<std::pair<int, Real> > data;
143 val +=
momentValue(d,(Real)data[m].first,data[m].second,prob,atom);
154 int numSamples = prob.getNumMyAtoms();
155 std::vector<Real> gradx(numSamples,0), gradp(numSamples,0);
157 std::vector<std::pair<int, Real> > data;
158 std::vector<Real> val_wt(numSamples,0), tmp(
dimension_,0);
159 std::vector<std::vector<Real> > val_pt(numSamples,tmp);
163 momentGradient(gradx,gradp,scale,d,(Real)data[m].first,data[m].second,prob,atom);
164 for (
int k = 0; k < numSamples; k++) {
165 (val_pt[k])[d] += scale*gradx[k];
166 val_wt[k] += scale*gradp[k];
173 for (
int k = 0; k < numSamples; k++) {
175 gprob.setProbability(k,val_wt[k]);
191 const int numSamples = prob.getNumMyAtoms();
192 std::vector<Real> hvx1(numSamples,0), hvx2(numSamples,0), hvx3(numSamples,0);
193 std::vector<Real> hvp1(numSamples,0), hvp2(numSamples,0);
194 Real scale1(0), scale2(0), scale3(0);
195 std::vector<std::pair<int, Real> > data;
196 std::vector<Real> val_wt(numSamples,0), tmp(
dimension_,0);
197 std::vector<std::vector<Real> > val_pt(numSamples,tmp);
202 d,(Real)data[m].first,data[m].second,prob,atom,vprob,vatom);
203 for (
int k = 0; k < numSamples; k++) {
204 (val_pt[k])[d] += (scale1+scale3)*hvx1[k] + scale2*(hvx2[k]+hvx3[k]);
205 val_wt[k] += (scale1+scale3)*hvp1[k] + scale2*hvp2[k];
212 for (
int k = 0; k < numSamples; k++) {
214 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