ROL
ROL_CompositeObjective_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_COMPOSITE_OBJECTIVE_DEF_H
11 #define ROL_COMPOSITE_OBJECTIVE_DEF_H
12 
13 namespace ROL {
14 
15 template<typename Real>
17  const Ptr<StdObjective<Real>> &std_obj)
18  : obj_vec_(obj_vec), std_obj_(std_obj), isInitialized_(false),
19  isValueComputed_(false), isGradientComputed_(false) {
20  obj_value_ = makePtr<std::vector<Real>>(obj_vec_.size(),0);
21  obj_value_vec_ = makePtr<StdVector<Real>>(obj_value_);
22  obj_grad_ = makePtr<std::vector<Real>>(obj_vec_.size(),0);
23  obj_grad_vec_ = makePtr<StdVector<Real>>(obj_grad_);
24  obj_gv_ = makePtr<std::vector<Real>>(obj_vec_.size(),0);
25  obj_gv_vec_ = makePtr<StdVector<Real>>(obj_gv_);
26  obj_hess_ = makePtr<std::vector<Real>>(obj_vec_.size(),0);
27  obj_hess_vec_ = makePtr<StdVector<Real>>(obj_hess_);
28 }
29 
30 template<typename Real>
31 void CompositeObjective<Real>::update( const Vector<Real> &x, UpdateType type, int iter ) {
32  int size = obj_vec_.size();
33  for (int i = 0; i < size; ++i) {
34  obj_vec_[i]->update(x,type,iter);
35  }
36  isValueComputed_ = false;
37  isGradientComputed_ = (type==UpdateType::Trial || type==UpdateType::Revert ? isGradientComputed_ : false);
38 }
39 
40 template<typename Real>
41 void CompositeObjective<Real>::update( const Vector<Real> &x, bool flag, int iter ) {
42  int size = obj_vec_.size();
43  for (int i = 0; i < size; ++i) {
44  obj_vec_[i]->update(x,flag,iter);
45  }
46  isValueComputed_ = false;
47  isGradientComputed_ = (flag ? false : isGradientComputed_);
48 }
49 
50 template<typename Real>
51 Real CompositeObjective<Real>::value( const Vector<Real> &x, Real &tol ) {
52  computeValue(x,tol);
53  return std_obj_->value(*obj_value_vec_,tol);
54 }
55 
56 template<typename Real>
58  g.zero();
59  computeGradient(x,tol);
60  int size = obj_vec_.size();
61  for (int i = 0; i < size; ++i) {
62  g.axpy((*obj_grad_)[i],*(vec_grad_[i]));
63  }
64 }
65 
66 template<typename Real>
67 void CompositeObjective<Real>::hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
68  hv.zero();
69  computeHessVec(v,x,tol);
70  int size = obj_vec_.size();
71  for (int i = 0; i < size; ++i) {
72  hv.axpy((*obj_grad_)[i],*(vec_hess_[i]));
73  hv.axpy((*obj_hess_)[i],*(vec_grad_[i]));
74  }
75 }
76 
77 template<typename Real>
78 void CompositeObjective<Real>::setParameter(const std::vector<Real> &param) {
80  const int size = obj_vec_.size();
81  for (int i = 0; i < size; ++i) {
82  obj_vec_[i]->setParameter(param);
83  }
84  std_obj_->setParameter(param);
85  isValueComputed_ = false; // Recompute value every time
86  isGradientComputed_ = false; // Recompute gradient every time
87 }
88 
89 template<typename Real>
91  if (!isInitialized_){
92  int size = obj_vec_.size();
93  vec_grad_.clear(); vec_grad_.resize(size,nullPtr);
94  vec_hess_.clear(); vec_hess_.resize(size,nullPtr);
95  for (int i = 0; i < size; ++i) {
96  vec_grad_[i] = x.dual().clone();
97  vec_hess_[i] = x.dual().clone();
98  }
99  isInitialized_ = true;
100  }
101 }
102 
103 template<typename Real>
105  initialize(x);
106  if (!isValueComputed_) {
107  int size = obj_vec_.size();
108  for (int i = 0; i < size; ++i) {
109  (*obj_value_)[i] = obj_vec_[i]->value(x,tol);
110  }
111  isValueComputed_ = true;
112  }
113 }
114 
115 template<typename Real>
117  computeValue(x,tol);
118  if (!isGradientComputed_) {
119  std_obj_->gradient(*(obj_grad_vec_),*(obj_value_vec_),tol);
120  int size = obj_vec_.size();
121  for (int i = 0; i < size; ++i) {
122  obj_vec_[i]->gradient(*(vec_grad_[i]),x,tol);
123  }
124  isGradientComputed_ = true;
125  }
126 }
127 
128 template<typename Real>
130  computeGradient(x,tol);
131  int size = obj_vec_.size();
132  for (int i = 0; i < size; ++i) {
133  //(*obj_gv_)[i] = vec_grad_[i]->dot(v.dual());
134  (*obj_gv_)[i] = vec_grad_[i]->apply(v);
135  obj_vec_[i]->hessVec(*(vec_hess_[i]),v,x,tol);
136  }
137  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
138 }
139 
140 } // namespace ROL
141 
142 #endif
Provides the interface to evaluate objective functions.
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:192
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Apply Hessian approximation to vector.
Ptr< StdVector< Real > > obj_hess_vec_
void computeValue(const Vector< Real > &x, Real &tol)
const std::vector< Ptr< Objective< Real > > > obj_vec_
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.
Ptr< std::vector< Real > > obj_gv_
void computeGradient(const Vector< Real > &x, Real &tol)
CompositeObjective(const std::vector< Ptr< Objective< Real >>> &obj_vec, const Ptr< StdObjective< Real >> &std_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 initialize(const Vector< Real > &x)
Ptr< std::vector< Real > > obj_value_
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
Ptr< std::vector< Real > > obj_hess_
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol) override
Compute gradient.
Ptr< StdVector< Real > > obj_value_vec_
void update(const Vector< Real > &x, UpdateType type, int iter=-1) override
Update objective function.
Ptr< StdVector< Real > > obj_gv_vec_
virtual void setParameter(const std::vector< Real > &param)
Ptr< std::vector< Real > > obj_grad_
void computeHessVec(const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Ptr< StdVector< Real > > obj_grad_vec_
void setParameter(const std::vector< Real > &param) override