ROL
ROL_LinearCombinationObjective_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_LINEARCOMBINATIONOBJECTIVE_DEF_H
11 #define ROL_LINEARCOMBINATIONOBJECTIVE_DEF_H
12 
13 namespace ROL {
14 
15 template<typename Real>
17  : Objective<Real>(), obj_(obj),
18  xdual_(nullPtr), initialized_(false) {
19  size_ = obj_.size();
20  weights_.clear(); weights_.assign(size_,static_cast<Real>(1));
21 }
22 
23 template<typename Real>
25  const std::vector<Ptr<Objective<Real>>> &obj)
26  : Objective<Real>(), obj_(obj), weights_(weights), size_(weights.size()),
27  xdual_(nullPtr), initialized_(false) {}
28 
29 template<typename Real>
31  for (size_t i=0; i<size_; ++i) {
32  obj_[i]->update(x,type,iter);
33  }
34 }
35 
36 template<typename Real>
37 void LinearCombinationObjective<Real>::update(const Vector<Real> &x, bool flag, int iter) {
38  for (size_t i=0; i<size_; ++i) {
39  obj_[i]->update(x,flag,iter);
40  }
41 }
42 
43 template<typename Real>
45  Real val(0);
46  for (size_t i = 0; i < size_; i++) {
47  val += weights_[i]*obj_[i]->value(x,tol);
48  }
49  return val;
50 }
51 
52 template<typename Real>
54  if (!initialized_) {
55  xdual_ = g.clone();
56  initialized_ = true;
57  }
58  g.zero();
59  for (size_t i = 0; i < size_; i++) {
60  obj_[i]->gradient(*xdual_,x,tol);
61  g.axpy(weights_[i],*xdual_);
62  }
63 }
64 
65 template<typename Real>
67  if (!initialized_) {
68  xdual_ = hv.clone();
69  initialized_ = true;
70  }
71  hv.zero();
72  for (size_t i = 0; i < size_; i++) {
73  obj_[i]->hessVec(*xdual_,v,x,tol);
74  hv.axpy(weights_[i],*xdual_);
75  }
76 }
77 
78 template<typename Real>
79 void LinearCombinationObjective<Real>::setParameter(const std::vector<Real> &param) {
81  for (size_t i = 0; i < size_; ++i) {
82  obj_[i]->setParameter(param);
83  }
84 }
85 
86 } // namespace ROL
87 
88 #endif
Provides the interface to evaluate objective functions.
const std::vector< Ptr< Objective< Real > > > obj_
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
const double weights[4][5]
Definition: ROL_Types.hpp:838
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:119
Real value(const Vector< Real > &x, Real &tol) override
Compute value.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol) override
Compute gradient.
LinearCombinationObjective(const std::vector< Ptr< Objective< Real >>> &obj)
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:133
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
void setParameter(const std::vector< Real > &param) override
virtual void setParameter(const std::vector< Real > &param)
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Apply Hessian approximation to vector.
void update(const Vector< Real > &x, UpdateType type, int iter=-1) override
Update objective function.
const Ptr< Obj > obj_