ROL
ROL_SerialObjective.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 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 //
36 // Questions? Contact lead developers:
37 // Drew Kouri (dpkouri@sandia.gov) and
38 // Denis Ridzal (dridzal@sandia.gov)
39 //
40 // ************************************************************************
41 // @HEADER
42 
43 #pragma once
44 #ifndef ROL_SERIALOBJECTIVE_HPP
45 #define ROL_SERIALOBJECTIVE_HPP
46 
47 #include <type_traits>
48 
49 #include "ROL_Objective_SimOpt.hpp"
50 #include "ROL_DynamicObjective.hpp"
51 #include "ROL_SerialFunction.hpp"
52 
67 namespace ROL {
68 
69 template<typename Real>
70 class SerialObjective : public Objective_SimOpt<Real>,
71  public SerialFunction<Real> {
72 private:
76 
77  Ptr<DynamicObjective<Real>> obj_; // Objective over a single time step
78 
79 public:
80 
86 
88  const Vector<Real>& u_initial,
89  const TimeStampsPtr<Real> timeStampsPtr ) :
90  SerialFunction<Real>::SerialFunction( u_initial, timeStampsPtr ),
91  obj_(obj) {}
92 
94  virtual Real value( const Vector<Real>& u,
95  const Vector<Real>& z,
96  Real& tol ) override {
97 
98  auto& up = partition(u);
99  auto& zp = partition(z);
100  Real result = 0;
101 
102  if( !getSkipInitialCondition() )
103  result += obj_->value( getInitialCondition(), up[0], zp[0], ts(0) );
104 
105  for( size_type k=1; k<numTimeSteps(); ++k )
106  result += obj_->value( up[k-1], up[k], zp[k], ts(k) );
107 
108  return result;
109  } // value
110 
111  virtual void gradient_1( Vector<Real>& g,
112  const Vector<Real>& u,
113  const Vector<Real>& z,
114  Real& tol ) override {
115 
116  auto& gp = partition(g);
117  auto& up = partition(u);
118  auto& zp = partition(z);
119 
120  auto tmp = clone(gp[0]);
121  auto& x = *tmp;
122 
123  // TODO: Implement skip initial condition
124 
125  obj_->gradient_un( gp[0], getInitialCondition(), up[0], zp[0], ts(0) );
126  obj_->gradient_uo( x, up[0], up[1], zp[1], ts(1) );
127  gp[0].plus(x);
128 
129  for( size_type k=1; k<numTimeSteps()-1; ++k ) {
130  obj_->gradient_un( gp[k], up[k-1], up[k], zp[k], ts(k) );
131  obj_->gradient_uo( x, up[k], up[k+1], zp[k+1], ts(k+1) );
132  gp[k].plus(x);
133  }
134 
135  size_t N = numTimeSteps()-1;
136 
137  obj_->gradient_un( gp[N], up[N-1], up[N], zp[N], ts(N) );
138 
139  } // gradient_1
140 
141  virtual void gradient_2( Vector<Real>& g,
142  const Vector<Real>& u,
143  const Vector<Real>& z,
144  Real& tol ) override {
145 
146  auto& gp = partition(g);
147  auto& up = partition(u);
148  auto& zp = partition(z);
149 
150  if( !getSkipInitialCondition() )
151  obj_->gradient_z( gp[0], getInitialCondition(), up[0], zp[0], ts(0) );
152 
153  for( size_type k=1; k<numTimeSteps(); ++k )
154  obj_->gradient_z( gp[k], up[k-1], up[k], zp[k], ts(k) ); // df[k]/dz[k]
155 
156  } // gradient_2
157 
158  virtual void hessVec_11( Vector<Real>& hv,
159  const Vector<Real>& v,
160  const Vector<Real>& u,
161  const Vector<Real>& z,
162  Real& tol ) override {
163 
164  auto& hvp = partition(hv); auto& vp = partition(v);
165  auto& up = partition(u); auto& zp = partition(z);
166 
167  auto tmp = clone(hvp[0]);
168  auto& x = *tmp;
169 
170  // TODO: Implement skip initial condition
171 
172  obj_->hessVec_un_un( hvp[0], vp[0], getInitialCondition(), up[0], zp[0], ts(0) );
173  obj_->hessVec_uo_uo( x, vp[0], up[0], up[1], zp[1], ts(1) );
174  hvp[0].plus(x);
175 
176  for( size_type k=1; k<numTimeSteps()-1; ++k ) {
177  obj_->hessVec_un_un( hvp[k], vp[k], up[k-1], up[k], zp[k], ts(k) );
178  obj_->hessVec_uo_uo( x, vp[k], up[k], up[k+1], zp[k+1], ts(k+1) );
179  hvp[k].plus(x);
180  }
181 
182  size_t N = numTimeSteps()-1;
183 
184  obj_->hessVec_un_un( hvp[N], vp[N], up[N-1], up[N], zp[N], ts(N) );
185 
186  } // hessVec_11
187 
188  virtual void hessVec_12( Vector<Real>& hv,
189  const Vector<Real>& v,
190  const Vector<Real>& u,
191  const Vector<Real>& z,
192  Real& tol ) override {
193 
194  auto& hvp = partition(hv); auto& vp = partition(v);
195  auto& up = partition(u); auto& zp = partition(z);
196 
197  auto tmp = clone(hvp[0]);
198  auto& x = *tmp;
199 
200  // TODO: Implement skip initial condition
201 
202  obj_->hessVec_un_z( hvp[0], vp[0], getInitialCondition(), up[0], zp[0], ts(0) );
203  obj_->hessVec_uo_z( x, vp[0], up[0], up[1], zp[1], ts(1) );
204  hvp[0].plus(x);
205 
206  for( size_type k=1; k<numTimeSteps()-1; ++k ) {
207  obj_->hessVec_un_z( hvp[k], vp[k], up[k-1], up[k], zp[k], ts(k) );
208  obj_->hessVec_uo_z( x, vp[k], up[k], up[k+1], zp[k+1], ts(k+1) );
209  hvp[k].plus(x);
210  }
211 
212  size_t N = numTimeSteps()-1;
213 
214  obj_->hessVec_un_z( hvp[N], vp[N], up[N-1], up[N], zp[N], ts(N) );
215 
216 
217  } // hessVec_22
218 
219  virtual void hessVec_21( Vector<Real>& hv,
220  const Vector<Real>& v,
221  const Vector<Real>& u,
222  const Vector<Real>& z,
223  Real& tol ) override {
224 
225  auto& hvp = partition(hv); auto& vp = partition(v);
226  auto& up = partition(u); auto& zp = partition(z);
227 
228  auto tmp = clone(hvp[0]);
229  auto& x = *tmp;
230 
231  // TODO: Implement skip initial condition
232 
233  obj_->hessVec_z_un( hvp[0], vp[0], getInitialCondition(), up[0], zp[0], ts(0) );
234 
235  for( size_type k=1; k<numTimeSteps(); ++k ) {
236  obj_->hessVec_z_un( hvp[k], vp[k], up[k-1], up[k], zp[k], ts(k) );
237  obj_->hessVec_z_uo( x, vp[k-1], up[k-1], up[k], zp[k], ts(k) );
238  hvp[k].plus(x);
239  }
240 
241  } // hessVec_21
242 
243  virtual void hessVec_22( Vector<Real>& hv,
244  const Vector<Real>& v,
245  const Vector<Real>& u,
246  const Vector<Real>& z,
247  Real& tol ) override {
248 
249  auto& hvp = partition(hv); auto& vp = partition(v);
250  auto& up = partition(u); auto& zp = partition(z);
251 
252  if( !getSkipInitialCondition() )
253  obj_->hessVec_z_z( hvp[0], vp[0], getInitialCondition(), up[0], zp[0], ts(0) );
254 
255  for( size_type k=1; k<numTimeSteps(); ++k )
256  obj_->hessVec_z_z( hvp[k], vp[k], up[k-1], up[k], zp[k], ts(k) );
257 
258 
259  } // hessVec_22
260 
261 }; // SerialObjective
262 
263 
264 // Helper function to create a new SerialObjective
265 
266 template<typename DynObj, typename Real, typename P = Ptr<SerialObjective<Real>> >
267 inline typename std::enable_if<std::is_base_of<DynamicObjective<Real>,DynObj>::value,P>::type
268 make_SerialObjective( const Ptr<DynObj>& obj,
269  const Vector<Real>& u_initial,
270  const TimeStampsPtr<Real> timeStampsPtr ) {
271  return makePtr<SerialObjective<Real>>(obj,u_initial,timeStampsPtr);
272 }
273 
274 } // namespace ROL
275 
276 
277 #endif // ROL_SERIALOBJECTIVE_HPP
PartitionedVector< Real > & partition(Vector< Real > &x)
const Vector< Real > & getInitialCondition() const
Provides the interface to evaluate simulation-based objective functions.
typename PV< Real >::size_type size_type
virtual void gradient_2(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol) override
Compute gradient with respect to second component.
virtual void hessVec_11(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol) override
Apply Hessian approximation to vector.
Defines the linear algebra of vector space on a generic partitioned vector.
ROL::Objective_SimOpt value
size_type numTimeSteps() const
virtual Real value(const Vector< Real > &u, const Vector< Real > &z, Real &tol) override
Compute value.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
Defines the time-dependent objective function interface for simulation-based optimization. Computes time-local contributions of value, gradient, Hessian-vector product etc to a larger composite objective defined over the simulation time. In contrast to other objective classes Objective_TimeSimOpt has a default implementation of value which returns zero, as time-dependent simulation based optimization problems may have an objective value which depends only on the final state of the system.
virtual void hessVec_22(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol) override
SerialObjective(const Ptr< DynamicObjective< Real >> &obj, const Vector< Real > &u_initial, const TimeStampsPtr< Real > timeStampsPtr)
typename std::vector< Real >::size_type size_type
Ptr< Vector< Real > > clone(const Vector< Real > &x)
Ptr< DynamicObjective< Real > > obj_
Evaluates ROL::DynamicObjective over a sequential set of time intervals.
std::enable_if< std::is_base_of< DynamicObjective< Real >, DynObj >::value, P >::type make_SerialObjective(const Ptr< DynObj > &obj, const Vector< Real > &u_initial, const TimeStampsPtr< Real > timeStampsPtr)
bool getSkipInitialCondition() const
Provides behavior common to SerialObjective as SerialConstaint.
Ptr< std::vector< TimeStamp< Real >>> TimeStampsPtr
virtual void hessVec_12(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol) override
const TimeStamp< Real > & ts(size_type i) const
virtual void gradient_1(Vector< Real > &g, const Vector< Real > &u, const Vector< Real > &z, Real &tol) override
Compute gradient with respect to first component.
virtual void hessVec_21(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &u, const Vector< Real > &z, Real &tol) override