ROL
ROL_Objective.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_OBJECTIVE_H
45 #define ROL_OBJECTIVE_H
46 
47 #include "ROL_Vector.hpp"
48 #include "ROL_UpdateType.hpp"
49 #include "ROL_Types.hpp"
50 #include <iostream>
51 
75 namespace ROL {
76 
77 template<typename Real>
78 class Objective {
79 private:
80  // Vector storage used for FD approximations (default are null pointers)
81  Ptr<Vector<Real>> prim_, dual_, basis_;
82 
83 public:
84 
85  virtual ~Objective() {}
86 
87  Objective() : prim_(nullPtr), dual_(nullPtr), basis_(nullPtr) {}
88 
96  virtual void update( const Vector<Real> &x, UpdateType type, int iter = -1 ) {
97  ROL_UNUSED(x);
98  ROL_UNUSED(type);
99  ROL_UNUSED(iter);
100  }
101 
109  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
110  ROL_UNUSED(x);
111  ROL_UNUSED(flag);
112  ROL_UNUSED(iter);
113  }
114 
121  virtual Real value( const Vector<Real> &x, Real &tol ) = 0;
122 
136  virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) ;
137 
145  virtual Real dirDeriv( const Vector<Real> &x, const Vector<Real> &d, Real &tol ) ;
146 
155  virtual void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol );
156 
165  virtual void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
166  ROL_UNUSED(hv);
167  ROL_UNUSED(v);
168  ROL_UNUSED(x);
169  ROL_UNUSED(tol);
170  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
171  ">>> ERROR (ROL::Objective): invHessVec not implemented!");
172  //hv.set(v.dual());
173  }
174 
183  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
184  ROL_UNUSED(x);
185  ROL_UNUSED(tol);
186  Pv.set(v.dual());
187  }
188 
209  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
210  const Vector<Real> &d,
211  const bool printToStream = true,
212  std::ostream & outStream = std::cout,
213  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
214  const int order = 1 ) {
215  return checkGradient(x, x.dual(), d, printToStream, outStream, numSteps, order);
216  }
217 
240  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
241  const Vector<Real> &g,
242  const Vector<Real> &d,
243  const bool printToStream = true,
244  std::ostream & outStream = std::cout,
245  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
246  const int order = 1 );
247 
248 
269  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
270  const Vector<Real> &d,
271  const std::vector<Real> &steps,
272  const bool printToStream = true,
273  std::ostream & outStream = std::cout,
274  const int order = 1 ) {
275 
276  return checkGradient(x, x.dual(), d, steps, printToStream, outStream, order);
277 
278  }
279 
280 
303  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
304  const Vector<Real> &g,
305  const Vector<Real> &d,
306  const std::vector<Real> &steps,
307  const bool printToStream = true,
308  std::ostream & outStream = std::cout,
309  const int order = 1 );
310 
331  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
332  const Vector<Real> &v,
333  const bool printToStream = true,
334  std::ostream & outStream = std::cout,
335  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
336  const int order = 1 ) {
337 
338  return checkHessVec(x, x.dual(), v, printToStream, outStream, numSteps, order);
339 
340  }
341 
363  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
364  const Vector<Real> &hv,
365  const Vector<Real> &v,
366  const bool printToStream = true,
367  std::ostream & outStream = std::cout,
368  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
369  const int order = 1) ;
370 
371 
392  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
393  const Vector<Real> &v,
394  const std::vector<Real> &steps,
395  const bool printToStream = true,
396  std::ostream & outStream = std::cout,
397  const int order = 1 ) {
398 
399  return checkHessVec(x, x.dual(), v, steps, printToStream, outStream, order);
400 
401  }
402 
424  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
425  const Vector<Real> &hv,
426  const Vector<Real> &v,
427  const std::vector<Real> &steps,
428  const bool printToStream = true,
429  std::ostream & outStream = std::cout,
430  const int order = 1) ;
431 
432 
447  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
448  const Vector<Real> &v,
449  const Vector<Real> &w,
450  const bool printToStream = true,
451  std::ostream & outStream = std::cout ) {
452 
453  return checkHessSym(x, x.dual(), v, w, printToStream, outStream);
454 
455  }
456 
472  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
473  const Vector<Real> &hv,
474  const Vector<Real> &v,
475  const Vector<Real> &w,
476  const bool printToStream = true,
477  std::ostream & outStream = std::cout );
478 
479 // Definitions for parametrized (stochastic) objective functions
480 private:
481  std::vector<Real> param_;
482 
483 protected:
484  const std::vector<Real> getParameter(void) const {
485  return param_;
486  }
487 
488 public:
489  virtual void setParameter(const std::vector<Real> &param) {
490  param_.assign(param.begin(),param.end());
491  }
492 
493 }; // class Objective
494 
495 } // namespace ROL
496 
497 // include templated definitions
498 #include <ROL_ObjectiveDef.hpp>
499 
500 #endif
Ptr< Vector< Real > > dual_
Provides the interface to evaluate objective functions.
virtual Real value(const Vector< Real > &x, Real &tol)=0
Compute value.
virtual Real dirDeriv(const Vector< Real > &x, const Vector< Real > &d, Real &tol)
Compute directional derivative.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Contains definitions of custom data types in ROL.
virtual void precond(Vector< Real > &Pv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply preconditioner to vector.
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const std::vector< Real > &steps, const bool printToStream=true, std::ostream &outStream=std::cout, const int order=1)
Finite-difference gradient check with specified step sizes.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
#define ROL_UNUSED(x)
Definition: ROL_Vector.hpp:48
std::vector< Real > param_
virtual std::vector< std::vector< Real > > checkGradient(const Vector< Real > &x, const Vector< Real > &d, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference gradient check.
virtual void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
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 void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Ptr< Vector< Real > > prim_
Ptr< Vector< Real > > basis_
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const std::vector< Real > &steps, const bool printToStream=true, std::ostream &outStream=std::cout, const int order=1)
Finite-difference Hessian-applied-to-vector check with specified step sizes.
const std::vector< Real > getParameter(void) const
#define ROL_NUM_CHECKDERIV_STEPS
Number of steps for derivative checks.
Definition: ROL_Types.hpp:74
virtual void setParameter(const std::vector< Real > &param)
virtual std::vector< std::vector< Real > > checkHessVec(const Vector< Real > &x, const Vector< Real > &v, const bool printToStream=true, std::ostream &outStream=std::cout, const int numSteps=ROL_NUM_CHECKDERIV_STEPS, const int order=1)
Finite-difference Hessian-applied-to-vector check.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
virtual ~Objective()
virtual std::vector< Real > checkHessSym(const Vector< Real > &x, const Vector< Real > &v, const Vector< Real > &w, const bool printToStream=true, std::ostream &outStream=std::cout)
Hessian symmetry check.