ROL
ROL_CompositeObjective_Def.hpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
44 #ifndef ROL_COMPOSITE_OBJECTIVE_DEF_H
45 #define ROL_COMPOSITE_OBJECTIVE_DEF_H
46 
47 namespace ROL {
48 
49 template<typename Real>
51  const Ptr<StdObjective<Real>> &std_obj)
52  : obj_vec_(obj_vec), std_obj_(std_obj), isInitialized_(false),
53  isValueComputed_(false), isGradientComputed_(false) {
54  obj_value_ = makePtr<std::vector<Real>>(obj_vec_.size(),0);
55  obj_value_vec_ = makePtr<StdVector<Real>>(obj_value_);
56  obj_grad_ = makePtr<std::vector<Real>>(obj_vec_.size(),0);
57  obj_grad_vec_ = makePtr<StdVector<Real>>(obj_grad_);
58  obj_gv_ = makePtr<std::vector<Real>>(obj_vec_.size(),0);
59  obj_gv_vec_ = makePtr<StdVector<Real>>(obj_gv_);
60  obj_hess_ = makePtr<std::vector<Real>>(obj_vec_.size(),0);
61  obj_hess_vec_ = makePtr<StdVector<Real>>(obj_hess_);
62 }
63 
64 template<typename Real>
65 void CompositeObjective<Real>::update( const Vector<Real> &x, UpdateType type, int iter ) {
66  int size = obj_vec_.size();
67  for (int i = 0; i < size; ++i) {
68  obj_vec_[i]->update(x,type,iter);
69  }
70  isValueComputed_ = false;
71  isGradientComputed_ = (type==UpdateType::Trial || type==UpdateType::Revert ? isGradientComputed_ : false);
72 }
73 
74 template<typename Real>
75 void CompositeObjective<Real>::update( const Vector<Real> &x, bool flag, int iter ) {
76  int size = obj_vec_.size();
77  for (int i = 0; i < size; ++i) {
78  obj_vec_[i]->update(x,flag,iter);
79  }
80  isValueComputed_ = false;
81  isGradientComputed_ = (flag ? false : isGradientComputed_);
82 }
83 
84 template<typename Real>
85 Real CompositeObjective<Real>::value( const Vector<Real> &x, Real &tol ) {
86  computeValue(x,tol);
87  return std_obj_->value(*obj_value_vec_,tol);
88 }
89 
90 template<typename Real>
92  g.zero();
93  computeGradient(x,tol);
94  int size = obj_vec_.size();
95  for (int i = 0; i < size; ++i) {
96  g.axpy((*obj_grad_)[i],*(vec_grad_[i]));
97  }
98 }
99 
100 template<typename Real>
101 void CompositeObjective<Real>::hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
102  hv.zero();
103  computeHessVec(v,x,tol);
104  int size = obj_vec_.size();
105  for (int i = 0; i < size; ++i) {
106  hv.axpy((*obj_grad_)[i],*(vec_hess_[i]));
107  hv.axpy((*obj_hess_)[i],*(vec_grad_[i]));
108  }
109 }
110 
111 template<typename Real>
112 void CompositeObjective<Real>::setParameter(const std::vector<Real> &param) {
114  const int size = obj_vec_.size();
115  for (int i = 0; i < size; ++i) {
116  obj_vec_[i]->setParameter(param);
117  }
118  std_obj_->setParameter(param);
119  isValueComputed_ = false; // Recompute value every time
120  isGradientComputed_ = false; // Recompute gradient every time
121 }
122 
123 template<typename Real>
125  if (!isInitialized_){
126  int size = obj_vec_.size();
127  vec_grad_.clear(); vec_grad_.resize(size,nullPtr);
128  vec_hess_.clear(); vec_hess_.resize(size,nullPtr);
129  for (int i = 0; i < size; ++i) {
130  vec_grad_[i] = x.dual().clone();
131  vec_hess_[i] = x.dual().clone();
132  }
133  isInitialized_ = true;
134  }
135 }
136 
137 template<typename Real>
139  initialize(x);
140  if (!isValueComputed_) {
141  int size = obj_vec_.size();
142  for (int i = 0; i < size; ++i) {
143  (*obj_value_)[i] = obj_vec_[i]->value(x,tol);
144  }
145  isValueComputed_ = true;
146  }
147 }
148 
149 template<typename Real>
151  computeValue(x,tol);
152  if (!isGradientComputed_) {
153  std_obj_->gradient(*(obj_grad_vec_),*(obj_value_vec_),tol);
154  int size = obj_vec_.size();
155  for (int i = 0; i < size; ++i) {
156  obj_vec_[i]->gradient(*(vec_grad_[i]),x,tol);
157  }
158  isGradientComputed_ = true;
159  }
160 }
161 
162 template<typename Real>
164  computeGradient(x,tol);
165  int size = obj_vec_.size();
166  for (int i = 0; i < size; ++i) {
167  //(*obj_gv_)[i] = vec_grad_[i]->dot(v.dual());
168  (*obj_gv_)[i] = vec_grad_[i]->apply(v);
169  obj_vec_[i]->hessVec(*(vec_hess_[i]),v,x,tol);
170  }
171  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
172 }
173 
174 } // namespace ROL
175 
176 #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:226
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:153
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:167
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
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