ROL
ROL_Objective.hpp
Go to the documentation of this file.
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9 
10 #ifndef ROL_OBJECTIVE_H
11 #define ROL_OBJECTIVE_H
12 
13 #include "ROL_Vector.hpp"
14 #include "ROL_UpdateType.hpp"
15 #include "ROL_Types.hpp"
16 #include <iostream>
17 
41 namespace ROL {
42 
43 template<typename Real>
44 class Objective {
45 private:
46  // Vector storage used for FD approximations (default are null pointers)
47  Ptr<Vector<Real>> prim_, dual_, basis_;
48 
49 public:
50 
51  virtual ~Objective() {}
52 
53  Objective() : prim_(nullPtr), dual_(nullPtr), basis_(nullPtr) {}
54 
62  virtual void update( const Vector<Real> &x, UpdateType type, int iter = -1 ) {
63  ROL_UNUSED(x);
64  ROL_UNUSED(type);
65  ROL_UNUSED(iter);
66  }
67 
75  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {
76  ROL_UNUSED(x);
77  ROL_UNUSED(flag);
78  ROL_UNUSED(iter);
79  }
80 
87  virtual Real value( const Vector<Real> &x, Real &tol ) = 0;
88 
102  virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) ;
103 
111  virtual Real dirDeriv( const Vector<Real> &x, const Vector<Real> &d, Real &tol ) ;
112 
121  virtual void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol );
122 
131  virtual void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
132  ROL_UNUSED(hv);
133  ROL_UNUSED(v);
134  ROL_UNUSED(x);
135  ROL_UNUSED(tol);
136  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
137  ">>> ERROR (ROL::Objective): invHessVec not implemented!");
138  //hv.set(v.dual());
139  }
140 
149  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
150  ROL_UNUSED(x);
151  ROL_UNUSED(tol);
152  Pv.set(v.dual());
153  }
154 
155  virtual void prox( Vector<Real> &Pv, const Vector<Real> &v, Real t, Real &tol){
156  ROL_UNUSED(Pv);
157  ROL_UNUSED(v);
158  ROL_UNUSED(t);
159  ROL_UNUSED(tol);
160  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
161  ">>> ERROR (ROL::Objective): prox not implemented!");
162  }
163 
184  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
185  const Vector<Real> &d,
186  const bool printToStream = true,
187  std::ostream & outStream = std::cout,
188  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
189  const int order = 1 ) {
190  return checkGradient(x, x.dual(), d, printToStream, outStream, numSteps, order);
191  }
192 
215  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
216  const Vector<Real> &g,
217  const Vector<Real> &d,
218  const bool printToStream = true,
219  std::ostream & outStream = std::cout,
220  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
221  const int order = 1 );
222 
223 
244  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
245  const Vector<Real> &d,
246  const std::vector<Real> &steps,
247  const bool printToStream = true,
248  std::ostream & outStream = std::cout,
249  const int order = 1 ) {
250 
251  return checkGradient(x, x.dual(), d, steps, printToStream, outStream, order);
252 
253  }
254 
255 
278  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
279  const Vector<Real> &g,
280  const Vector<Real> &d,
281  const std::vector<Real> &steps,
282  const bool printToStream = true,
283  std::ostream & outStream = std::cout,
284  const int order = 1 );
285 
306  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
307  const Vector<Real> &v,
308  const bool printToStream = true,
309  std::ostream & outStream = std::cout,
310  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
311  const int order = 1 ) {
312 
313  return checkHessVec(x, x.dual(), v, printToStream, outStream, numSteps, order);
314 
315  }
316 
338  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
339  const Vector<Real> &hv,
340  const Vector<Real> &v,
341  const bool printToStream = true,
342  std::ostream & outStream = std::cout,
343  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
344  const int order = 1) ;
345 
346 
367  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
368  const Vector<Real> &v,
369  const std::vector<Real> &steps,
370  const bool printToStream = true,
371  std::ostream & outStream = std::cout,
372  const int order = 1 ) {
373 
374  return checkHessVec(x, x.dual(), v, steps, printToStream, outStream, order);
375 
376  }
377 
399  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
400  const Vector<Real> &hv,
401  const Vector<Real> &v,
402  const std::vector<Real> &steps,
403  const bool printToStream = true,
404  std::ostream & outStream = std::cout,
405  const int order = 1) ;
406 
407 
422  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
423  const Vector<Real> &v,
424  const Vector<Real> &w,
425  const bool printToStream = true,
426  std::ostream & outStream = std::cout ) {
427 
428  return checkHessSym(x, x.dual(), v, w, printToStream, outStream);
429 
430  }
431 
447  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
448  const Vector<Real> &hv,
449  const Vector<Real> &v,
450  const Vector<Real> &w,
451  const bool printToStream = true,
452  std::ostream & outStream = std::cout );
453 
454 // Definitions for parametrized (stochastic) objective functions
455 private:
456  std::vector<Real> param_;
457 
458 protected:
459  const std::vector<Real> getParameter(void) const {
460  return param_;
461  }
462 
463 public:
464  virtual void setParameter(const std::vector<Real> &param) {
465  param_.assign(param.begin(),param.end());
466  }
467 
468 }; // class Objective
469 
470 } // namespace ROL
471 
472 // include templated definitions
473 #include <ROL_ObjectiveDef.hpp>
474 
475 #endif
Ptr< Vector< Real > > dual_
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:192
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.
virtual void prox(Vector< Real > &Pv, const Vector< Real > &v, Real t, Real &tol)
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual void update(const Vector< Real > &x, UpdateType type, int iter=-1)
Update objective function.
#define ROL_UNUSED(x)
Definition: ROL_Vector.hpp:14
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 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.
#define ROL_NUM_CHECKDERIV_STEPS
Number of steps for derivative checks.
Definition: ROL_Types.hpp:40
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:175
virtual void update(const Vector< Real > &x, bool flag=true, int iter=-1)
Update objective function.
virtual ~Objective()
const std::vector< Real > getParameter(void) const
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.