ROL
ROL_CompositeObjective.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_H
45 #define ROL_COMPOSITE_OBJECTIVE_H
46 
47 #include "ROL_StdObjective.hpp"
48 
55 namespace ROL {
56 
57 template <class Real>
58 class CompositeObjective : public Objective<Real> {
59 private:
60  const std::vector<ROL::Ptr<Objective<Real> > > obj_vec_;
61  const ROL::Ptr<StdObjective<Real> > std_obj_;
62 
63  ROL::Ptr<std::vector<Real> > obj_value_, obj_grad_, obj_gv_, obj_hess_;
64  ROL::Ptr<StdVector<Real> > obj_value_vec_, obj_grad_vec_, obj_gv_vec_, obj_hess_vec_;
65  std::vector<ROL::Ptr<Vector<Real> > > vec_grad_, vec_hess_;
66 
68 
69  void initialize(const Vector<Real> &x) {
70  if (!isInitialized_){
71  int size = obj_vec_.size();
72  vec_grad_.clear(); vec_grad_.resize(size,ROL::nullPtr);
73  vec_hess_.clear(); vec_hess_.resize(size,ROL::nullPtr);
74  for (int i = 0; i < size; ++i) {
75  vec_grad_[i] = x.dual().clone();
76  vec_hess_[i] = x.dual().clone();
77  }
78  isInitialized_ = true;
79  }
80  }
81 
82  void computeValue(const Vector<Real> &x, Real &tol) {
83  initialize(x);
84  if (!isValueComputed_) {
85  int size = obj_vec_.size();
86  for (int i = 0; i < size; ++i) {
87  (*obj_value_)[i] = obj_vec_[i]->value(x,tol);
88  }
89  isValueComputed_ = true;
90  }
91  }
92 
93  void computeGradient(const Vector<Real> &x, Real &tol) {
94  computeValue(x,tol);
95  if (!isGradientComputed_) {
96  std_obj_->gradient(*(obj_grad_vec_),*(obj_value_vec_),tol);
97  int size = obj_vec_.size();
98  for (int i = 0; i < size; ++i) {
99  obj_vec_[i]->gradient(*(vec_grad_[i]),x,tol);
100  }
101  isGradientComputed_ = true;
102  }
103  }
104 
105  void computeHessVec(const Vector<Real> &v, const Vector<Real> &x, Real &tol) {
106  computeGradient(x,tol);
107  int size = obj_vec_.size();
108  for (int i = 0; i < size; ++i) {
109  (*obj_gv_)[i] = vec_grad_[i]->dot(v.dual());
110  obj_vec_[i]->hessVec(*(vec_hess_[i]),v,x,tol);
111  }
112  std_obj_->hessVec(*(obj_hess_vec_),*(obj_gv_vec_),*(obj_value_vec_),tol);
113  }
114 
115 public:
116  CompositeObjective(const std::vector<ROL::Ptr<Objective<Real> > > &obj_vec,
117  const ROL::Ptr<StdObjective<Real> > &std_obj)
118  : obj_vec_(obj_vec), std_obj_(std_obj), isInitialized_(false),
119  isValueComputed_(false), isGradientComputed_(false) {
120  obj_value_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
121  obj_value_vec_ = ROL::makePtr<StdVector<Real>>(obj_value_);
122  obj_grad_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
123  obj_grad_vec_ = ROL::makePtr<StdVector<Real>>(obj_grad_);
124  obj_gv_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
125  obj_gv_vec_ = ROL::makePtr<StdVector<Real>>(obj_gv_);
126  obj_hess_ = ROL::makePtr<std::vector<Real>>(obj_vec_.size(),0);
127  obj_hess_vec_ = ROL::makePtr<StdVector<Real>>(obj_hess_);
128  }
129 
130  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
131  int size = obj_vec_.size();
132  for (int i = 0; i < size; ++i) {
133  obj_vec_[i]->update(x,flag,iter);
134  }
135  isValueComputed_ = false;
136  isGradientComputed_ = (flag ? false : isGradientComputed_);
137  }
138 
139  Real value( const Vector<Real> &x, Real &tol ) {
140  computeValue(x,tol);
141  return std_obj_->value(*obj_value_vec_,tol);
142  }
143 
144  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
145  g.zero();
146  computeGradient(x,tol);
147  int size = obj_vec_.size();
148  for (int i = 0; i < size; ++i) {
149  g.axpy((*obj_grad_)[i],*(vec_grad_[i]));
150  }
151  }
152 
153  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
154  hv.zero();
155  computeHessVec(v,x,tol);
156  int size = obj_vec_.size();
157  for (int i = 0; i < size; ++i) {
158  hv.axpy((*obj_grad_)[i],*(vec_hess_[i]));
159  hv.axpy((*obj_hess_)[i],*(vec_grad_[i]));
160  }
161  }
162 
163 // Definitions for parametrized (stochastic) objective functions
164 public:
165  void setParameter(const std::vector<Real> &param) {
167  const int size = obj_vec_.size();
168  for (int i = 0; i < size; ++i) {
169  obj_vec_[i]->setParameter(param);
170  }
171  std_obj_->setParameter(param);
172  isValueComputed_ = false; // Recompute value every time
173  isGradientComputed_ = false; // Recompute gradient every time
174  }
175 };
176 
177 } // namespace ROL
178 
179 #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
void computeValue(const Vector< Real > &x, Real &tol)
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
const std::vector< ROL::Ptr< Objective< Real > > > obj_vec_
CompositeObjective(const std::vector< ROL::Ptr< Objective< Real > > > &obj_vec, const ROL::Ptr< StdObjective< Real > > &std_obj)
ROL::Ptr< std::vector< Real > > obj_gv_
void computeGradient(const Vector< Real > &x, Real &tol)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
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
ROL::Ptr< StdVector< Real > > obj_value_vec_
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
ROL::Ptr< std::vector< Real > > obj_grad_
void initialize(const Vector< Real > &x)
std::vector< ROL::Ptr< Vector< Real > > > vec_hess_
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector&#39;s.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
const ROL::Ptr< StdObjective< Real > > std_obj_
std::vector< ROL::Ptr< Vector< Real > > > vec_grad_
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
virtual void setParameter(const std::vector< Real > &param)
ROL::Ptr< StdVector< Real > > obj_grad_vec_
void computeHessVec(const Vector< Real > &v, const Vector< Real > &x, Real &tol)
ROL::Ptr< StdVector< Real > > obj_hess_vec_
ROL::Ptr< std::vector< Real > > obj_value_
ROL::Ptr< std::vector< Real > > obj_hess_
Provides the interface to evaluate composite objective functions.
ROL::Ptr< StdVector< Real > > obj_gv_vec_
void setParameter(const std::vector< Real > &param)