ROL
ROL_ParametrizedCompositeObjective.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_PARAMETRIZED_COMPOSITE_OBJECTIVE_H
45 #define ROL_PARAMETRIZED_COMPOSITE_OBJECTIVE_H
46 
48 
55 namespace ROL {
56 
57 template <class Real>
59 private:
60  const std::vector<Teuchos::RCP<ParametrizedObjective<Real> > > obj_vec_;
61  const Teuchos::RCP<ParametrizedStdObjective<Real> > std_obj_;
62 
63  Teuchos::RCP<std::vector<Real> > obj_value_, obj_grad_, obj_gv_, obj_hess_;
64  Teuchos::RCP<StdVector<Real> > obj_value_vec_, obj_grad_vec_, obj_gv_vec_, obj_hess_vec_;
65  std::vector<Teuchos::RCP<Vector<Real> > > vec_grad_, vec_hess_;
66 
68 
69  void initialize(const Vector<Real> &x) {
70  int size = obj_vec_.size();
71  if (!isInitialized_){
72  vec_grad_.clear(); vec_grad_.resize(size,Teuchos::null);
73  vec_hess_.clear(); vec_hess_.resize(size,Teuchos::null);
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  ParametrizedCompositeObjective(const std::vector<Teuchos::RCP<ParametrizedObjective<Real> > > &obj_vec,
117  const Teuchos::RCP<ParametrizedStdObjective<Real> > &std_obj)
118  : obj_vec_(obj_vec), std_obj_(std_obj), isInitialized_(false),
119  isValueComputed_(false), isGradientComputed_(false) {
120  obj_value_ = Teuchos::rcp(new std::vector<Real>(obj_vec_.size(),0));
121  obj_value_vec_ = Teuchos::rcp(new StdVector<Real>(obj_value_));
122  obj_grad_ = Teuchos::rcp(new std::vector<Real>(obj_vec_.size(),0));
123  obj_grad_vec_ = Teuchos::rcp(new StdVector<Real>(obj_grad_));
124  obj_gv_ = Teuchos::rcp(new std::vector<Real>(obj_vec_.size(),0));
125  obj_gv_vec_ = Teuchos::rcp(new StdVector<Real>(obj_gv_));
126  obj_hess_ = Teuchos::rcp(new std::vector<Real>(obj_vec_.size(),0));
127  obj_hess_vec_ = Teuchos::rcp(new StdVector<Real>(obj_hess_));
128  }
129 
130  void setParameter(const std::vector<Real> & param) {
132  const int size = obj_vec_.size();
133  for (int i = 0; i < size; ++i) {
134  obj_vec_[i]->setParameter(param);
135  }
136  std_obj_->setParameter(param);
137  isValueComputed_ = false; // Recompute value every time
138  isGradientComputed_ = false; // Recompute gradient every time
139  }
140 
141  void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
142  int size = obj_vec_.size();
143  for (int i = 0; i < size; ++i) {
144  obj_vec_[i]->update(x,flag,iter);
145  }
146  isValueComputed_ = false;
147  isGradientComputed_ = (flag ? false : isGradientComputed_);
148  }
149 
150  Real value( const Vector<Real> &x, Real &tol ) {
151  computeValue(x,tol);
152  return std_obj_->value(*obj_value_vec_,tol);
153  }
154 
155  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
156  g.zero();
157  computeGradient(x,tol);
158  int size = obj_vec_.size();
159  for (int i = 0; i < size; ++i) {
160  g.axpy((*obj_grad_)[i],*(vec_grad_[i]));
161  }
162  }
163 
164  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
165  hv.zero();
166  computeHessVec(v,x,tol);
167  int size = obj_vec_.size();
168  for (int i = 0; i < size; ++i) {
169  hv.axpy((*obj_grad_)[i],*(vec_hess_[i]));
170  hv.axpy((*obj_hess_)[i],*(vec_grad_[i]));
171  }
172  }
173 
174 };
175 
176 } // namespace ROL
177 
178 #endif
void computeValue(const Vector< Real > &x, Real &tol)
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector's.
const Teuchos::RCP< ParametrizedStdObjective< Real > > std_obj_
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:215
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:145
std::vector< Teuchos::RCP< Vector< Real > > > vec_grad_
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:159
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:76
void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
void computeGradient(const Vector< Real > &x, Real &tol)
const std::vector< Teuchos::RCP< ParametrizedObjective< Real > > > obj_vec_
void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
void computeHessVec(const Vector< Real > &v, const Vector< Real > &x, Real &tol)
ParametrizedCompositeObjective(const std::vector< Teuchos::RCP< ParametrizedObjective< Real > > > &obj_vec, const Teuchos::RCP< ParametrizedStdObjective< Real > > &std_obj)
void setParameter(const std::vector< Real > &param)
virtual void setParameter(const std::vector< Real > &param)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Provides the interface to evaluate parametrized composite objective functions.
std::vector< Teuchos::RCP< Vector< Real > > > vec_hess_