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 
189  virtual void prox( Vector<Real> &Pv, const Vector<Real> &v, Real t, Real &tol){
190  ROL_UNUSED(Pv);
191  ROL_UNUSED(v);
192  ROL_UNUSED(t);
193  ROL_UNUSED(tol);
194  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
195  ">>> ERROR (ROL::Objective): prox not implemented!");
196  }
197 
218  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
219  const Vector<Real> &d,
220  const bool printToStream = true,
221  std::ostream & outStream = std::cout,
222  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
223  const int order = 1 ) {
224  return checkGradient(x, x.dual(), d, printToStream, outStream, numSteps, order);
225  }
226 
249  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
250  const Vector<Real> &g,
251  const Vector<Real> &d,
252  const bool printToStream = true,
253  std::ostream & outStream = std::cout,
254  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
255  const int order = 1 );
256 
257 
278  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
279  const Vector<Real> &d,
280  const std::vector<Real> &steps,
281  const bool printToStream = true,
282  std::ostream & outStream = std::cout,
283  const int order = 1 ) {
284 
285  return checkGradient(x, x.dual(), d, steps, printToStream, outStream, order);
286 
287  }
288 
289 
312  virtual std::vector<std::vector<Real>> checkGradient( const Vector<Real> &x,
313  const Vector<Real> &g,
314  const Vector<Real> &d,
315  const std::vector<Real> &steps,
316  const bool printToStream = true,
317  std::ostream & outStream = std::cout,
318  const int order = 1 );
319 
340  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
341  const Vector<Real> &v,
342  const bool printToStream = true,
343  std::ostream & outStream = std::cout,
344  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
345  const int order = 1 ) {
346 
347  return checkHessVec(x, x.dual(), v, printToStream, outStream, numSteps, order);
348 
349  }
350 
372  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
373  const Vector<Real> &hv,
374  const Vector<Real> &v,
375  const bool printToStream = true,
376  std::ostream & outStream = std::cout,
377  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
378  const int order = 1) ;
379 
380 
401  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
402  const Vector<Real> &v,
403  const std::vector<Real> &steps,
404  const bool printToStream = true,
405  std::ostream & outStream = std::cout,
406  const int order = 1 ) {
407 
408  return checkHessVec(x, x.dual(), v, steps, printToStream, outStream, order);
409 
410  }
411 
433  virtual std::vector<std::vector<Real>> checkHessVec( const Vector<Real> &x,
434  const Vector<Real> &hv,
435  const Vector<Real> &v,
436  const std::vector<Real> &steps,
437  const bool printToStream = true,
438  std::ostream & outStream = std::cout,
439  const int order = 1) ;
440 
441 
456  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
457  const Vector<Real> &v,
458  const Vector<Real> &w,
459  const bool printToStream = true,
460  std::ostream & outStream = std::cout ) {
461 
462  return checkHessSym(x, x.dual(), v, w, printToStream, outStream);
463 
464  }
465 
481  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
482  const Vector<Real> &hv,
483  const Vector<Real> &v,
484  const Vector<Real> &w,
485  const bool printToStream = true,
486  std::ostream & outStream = std::cout );
487 
488 // Definitions for parametrized (stochastic) objective functions
489 private:
490  std::vector<Real> param_;
491 
492 protected:
493  const std::vector<Real> getParameter(void) const {
494  return param_;
495  }
496 
497 public:
498  virtual void setParameter(const std::vector<Real> &param) {
499  param_.assign(param.begin(),param.end());
500  }
501 
502 }; // class Objective
503 
504 } // namespace ROL
505 
506 // include templated definitions
507 #include <ROL_ObjectiveDef.hpp>
508 
509 #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:226
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: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 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: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()
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.