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 
93  virtual Real value( const Vector<Real>& u,
94  const Vector<Real>& z,
95  Real& tol ) override {
96 
97  auto& up = partition(u);
98  auto& zp = partition(z);
99  Real result = 0;
100 
101  if( !getSkipInitialCondition() )
102  result += obj_->value( getInitialCondition(), up[0], zp[0], ts(0) );
103 
104  for( size_type k=1; k<numTimeSteps(); ++k )
105  result += obj_->value( up[k-1], up[k], zp[k], ts(k) );
106 
107  return result;
108  } // value
109 
110  virtual void gradient_1( Vector<Real>& g,
111  const Vector<Real>& u,
112  const Vector<Real>& z,
113  Real& tol ) override {
114 
115  auto& gp = partition(g);
116  auto& up = partition(u);
117  auto& zp = partition(z);
118 
119  auto tmp = clone(gp[0]);
120  auto& x = *tmp;
121 
122  // TODO: Implement skip initial condition
123 
124  obj_->gradient_un( gp[0], getInitialCondition(), up[0], zp[0], ts(0) );
125  obj_->gradient_uo( x, up[0], up[1], zp[1], ts(1) );
126  gp[0].plus(x);
127 
128  for( size_type k=1; k<numTimeSteps()-1; ++k ) {
129  obj_->gradient_un( gp[k], up[k-1], up[k], zp[k], ts(k) );
130  obj_->gradient_uo( x, up[k], up[k+1], zp[k+1], ts(k+1) );
131  gp[k].plus(x);
132  }
133 
134  size_t N = numTimeSteps()-1;
135 
136  obj_->gradient_un( gp[N], up[N-1], up[N], zp[N], ts(N) );
137 
138  } // gradient_1
139 
140  virtual void gradient_2( Vector<Real>& g,
141  const Vector<Real>& u,
142  const Vector<Real>& z,
143  Real& tol ) override {
144 
145  auto& gp = partition(g);
146  auto& up = partition(u);
147  auto& zp = partition(z);
148 
149  if( !getSkipInitialCondition() )
150  obj_->gradient_z( gp[0], getInitialCondition(), up[0], zp[0], ts(0) );
151 
152  for( size_type k=1; k<numTimeSteps(); ++k )
153  obj_->gradient_z( gp[k], up[k-1], up[k], zp[k], ts(k) ); // df[k]/dz[k]
154 
155  } // gradient_2
156 
157  virtual void hessVec_11( Vector<Real>& hv,
158  const Vector<Real>& v,
159  const Vector<Real>& u,
160  const Vector<Real>& z,
161  Real& tol ) override {
162 
163  auto& hvp = partition(hv); auto& vp = partition(v);
164  auto& up = partition(u); auto& zp = partition(z);
165 
166  auto tmp = clone(hvp[0]);
167  auto& x = *tmp;
168 
169  // TODO: Implement skip initial condition
170 
171  obj_->hessVec_un_un( hvp[0], vp[0], getInitialCondition(), up[0], zp[0], ts(0) );
172  obj_->hessVec_uo_uo( x, vp[0], up[0], up[1], zp[1], ts(1) );
173  hvp[0].plus(x);
174 
175  for( size_type k=1; k<numTimeSteps()-1; ++k ) {
176  obj_->hessVec_un_un( hvp[k], vp[k], up[k-1], up[k], zp[k], ts(k) );
177  obj_->hessVec_uo_uo( x, vp[k], up[k], up[k+1], zp[k+1], ts(k+1) );
178  hvp[k].plus(x);
179  }
180 
181  size_t N = numTimeSteps()-1;
182 
183  obj_->hessVec_un_un( hvp[N], vp[N], up[N-1], up[N], zp[N], ts(N) );
184 
185  } // hessVec_11
186 
187  virtual void hessVec_12( Vector<Real>& hv,
188  const Vector<Real>& v,
189  const Vector<Real>& u,
190  const Vector<Real>& z,
191  Real& tol ) override {
192 
193  auto& hvp = partition(hv); auto& vp = partition(v);
194  auto& up = partition(u); auto& zp = partition(z);
195 
196  auto tmp = clone(hvp[0]);
197  auto& x = *tmp;
198 
199  // TODO: Implement skip initial condition
200 
201  obj_->hessVec_un_z( hvp[0], vp[0], getInitialCondition(), up[0], zp[0], ts(0) );
202  obj_->hessVec_uo_z( x, vp[0], up[0], up[1], zp[1], ts(1) );
203  hvp[0].plus(x);
204 
205  for( size_type k=1; k<numTimeSteps()-1; ++k ) {
206  obj_->hessVec_un_z( hvp[k], vp[k], up[k-1], up[k], zp[k], ts(k) );
207  obj_->hessVec_uo_z( x, vp[k], up[k], up[k+1], zp[k+1], ts(k+1) );
208  hvp[k].plus(x);
209  }
210 
211  size_t N = numTimeSteps()-1;
212 
213  obj_->hessVec_un_z( hvp[N], vp[N], up[N-1], up[N], zp[N], ts(N) );
214 
215 
216  } // hessVec_22
217 
218  // TODO: hessVec_21
219 
220 
221  virtual void hessVec_22( Vector<Real>& hv,
222  const Vector<Real>& v,
223  const Vector<Real>& u,
224  const Vector<Real>& z,
225  Real& tol ) override {
226 
227  auto& hvp = partition(hv); auto& vp = partition(v);
228  auto& up = partition(u); auto& zp = partition(z);
229 
230  if( !getSkipInitialCondition() )
231  obj_->hessVec_z_z( hvp[0], vp[0], getInitialCondition(), up[0], zp[0], ts(0) );
232 
233  for( size_type k=1; k<numTimeSteps(); ++k )
234  obj_->hessVec_z_z( hvp[k], vp[k], up[k-1], up[k], zp[k], ts(k) );
235 
236 
237  } // hessVec_22
238 
239 }; // SerialObjective
240 
241 
242 // Helper function to create a new SerialObjective
243 
244 template<typename DynObj, typename Real, typename P = Ptr<SerialObjective<Real>> >
245 inline typename std::enable_if<std::is_base_of<DynamicObjective<Real>,DynObj>::value,P>::type
246 make_SerialObjective( const Ptr<DynObj>& obj,
247  const Vector<Real>& u_initial,
248  const TimeStampsPtr<Real> timeStampsPtr ) {
249  return makePtr<SerialObjective<Real>>(obj,u_initial,timeStampsPtr);
250 }
251 
252 } // namespace ROL
253 
254 
255 #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.