ROL
ROL_MeanValueObjective_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_MEANVALUEOBJECTIVE_DEF_HPP
11 #define ROL_MEANVALUEOBJECTIVE_DEF_HPP
12 
13 namespace ROL {
14 
15 template<typename Real>
17  const Ptr<SampleGenerator<Real>> &sampler) : obj_(obj) {
18  std::vector<Real> param = computeSampleMean(sampler);
19  obj_->setParameter(param);
20 }
21 
22 template<typename Real>
23 void MeanValueObjective<Real>::update( const Vector<Real> &x, UpdateType type, int iter ) {
24  obj_->update(x,type,iter);
25 }
26 
27 template<typename Real>
28 void MeanValueObjective<Real>::update( const Vector<Real> &x, bool flag, int iter ) {
29  obj_->update(x,flag,iter);
30 }
31 
32 template<typename Real>
33 Real MeanValueObjective<Real>::value( const Vector<Real> &x, Real &tol ) {
34  return obj_->value(x,tol);
35 }
36 
37 template<typename Real>
39  obj_->gradient(g,x,tol);
40 }
41 
42 template<typename Real>
44  const Vector<Real> &x, Real &tol ) {
45  obj_->hessVec(hv,v,x,tol);
46 }
47 
48 template<typename Real>
50  const Vector<Real> &x, Real &tol ) {
51  obj_->precond(Pv,v,x,tol);
52 }
53 
54 template<typename Real>
55 std::vector<Real> MeanValueObjective<Real>::computeSampleMean(const Ptr<SampleGenerator<Real>> &sampler) const {
56  // Compute mean value of inputs and set parameter in objective
57  int dim = sampler->getMyPoint(0).size(), nsamp = sampler->numMySamples();
58  std::vector<Real> loc(dim), mean(dim), pt(dim);
59  Real wt(0);
60  for (int i = 0; i < nsamp; i++) {
61  pt = sampler->getMyPoint(i);
62  wt = sampler->getMyWeight(i);
63  for (int j = 0; j < dim; j++) {
64  loc[j] += wt*pt[j];
65  }
66  }
67  sampler->sumAll(&loc[0],&mean[0],dim);
68  return mean;
69 }
70 
71 }
72 
73 #endif
Provides the interface to evaluate objective functions.
MeanValueObjective(const Ptr< Objective< Real >> &obj, const Ptr< SampleGenerator< Real >> &sampler)
void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Apply preconditioner to vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
void update(const Vector< Real > &x, UpdateType type, int iter=-1) override
Update objective function.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol) override
Compute gradient.
std::vector< Real > computeSampleMean(const Ptr< SampleGenerator< Real >> &sampler) const
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Apply Hessian approximation to vector.
Real value(const Vector< Real > &x, Real &tol) override
Compute value.
const Ptr< Objective< Real > > obj_
const Ptr< Obj > obj_
constexpr auto dim