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
13namespace ROL {
14
15template<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
23template<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
29template<typename Real>
31 for (size_t i=0; i<size_; ++i) {
32 obj_[i]->update(x,type,iter);
33 }
34}
35
36template<typename Real>
37void 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
43template<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
52template<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
65template<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
78template<typename Real>
79void 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
const Ptr< Obj > obj_
void setParameter(const std::vector< Real > &param) override
const std::vector< Ptr< Objective< Real > > > obj_
Real value(const Vector< Real > &x, Real &tol) override
Compute value.
LinearCombinationObjective(const std::vector< Ptr< Objective< Real > > > &obj)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol) override
Compute gradient.
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.
Provides the interface to evaluate objective functions.
virtual void setParameter(const std::vector< Real > &param)
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
Defines the linear algebra or vector space interface.
virtual void zero()
Set to zero vector.
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .