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_Types.hpp"
49 #include <iostream>
50 
74 namespace ROL {
75 
76 template <class Real>
77 class Objective {
78 public:
79 
80  virtual ~Objective() {}
81 
89  virtual void update( const Vector<Real> &x, bool flag = true, int iter = -1 ) {}
90 
97  virtual Real value( const Vector<Real> &x, Real &tol ) = 0;
98 
112  virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) ;
113 
121  virtual Real dirDeriv( const Vector<Real> &x, const Vector<Real> &d, Real &tol ) ;
122 
131  virtual void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol );
132 
141  virtual void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
142  TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument,
143  ">>> ERROR (ROL::Objective): invHessVec not implemented!");
144  //hv.set(v.dual());
145  }
146 
155  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
156  Pv.set(v.dual());
157  }
158 
179  virtual std::vector<std::vector<Real> > checkGradient( const Vector<Real> &x,
180  const Vector<Real> &d,
181  const bool printToStream = true,
182  std::ostream & outStream = std::cout,
183  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
184  const int order = 1 ) {
185  return checkGradient(x, x.dual(), d, printToStream, outStream, numSteps, order);
186  }
187 
210  virtual std::vector<std::vector<Real> > checkGradient( const Vector<Real> &x,
211  const Vector<Real> &g,
212  const Vector<Real> &d,
213  const bool printToStream = true,
214  std::ostream & outStream = std::cout,
215  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
216  const int order = 1 );
217 
218 
239  virtual std::vector<std::vector<Real> > checkGradient( const Vector<Real> &x,
240  const Vector<Real> &d,
241  const std::vector<Real> &steps,
242  const bool printToStream = true,
243  std::ostream & outStream = std::cout,
244  const int order = 1 ) {
245 
246  return checkGradient(x, x.dual(), d, steps, printToStream, outStream, order);
247 
248  }
249 
250 
273  virtual std::vector<std::vector<Real> > checkGradient( const Vector<Real> &x,
274  const Vector<Real> &g,
275  const Vector<Real> &d,
276  const std::vector<Real> &steps,
277  const bool printToStream = true,
278  std::ostream & outStream = std::cout,
279  const int order = 1 );
280 
301  virtual std::vector<std::vector<Real> > checkHessVec( const Vector<Real> &x,
302  const Vector<Real> &v,
303  const bool printToStream = true,
304  std::ostream & outStream = std::cout,
305  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
306  const int order = 1 ) {
307 
308  return checkHessVec(x, x.dual(), v, printToStream, outStream, numSteps, order);
309 
310  }
311 
333  virtual std::vector<std::vector<Real> > checkHessVec( const Vector<Real> &x,
334  const Vector<Real> &hv,
335  const Vector<Real> &v,
336  const bool printToStream = true,
337  std::ostream & outStream = std::cout,
338  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
339  const int order = 1) ;
340 
341 
362  virtual std::vector<std::vector<Real> > checkHessVec( const Vector<Real> &x,
363  const Vector<Real> &v,
364  const std::vector<Real> &steps,
365  const bool printToStream = true,
366  std::ostream & outStream = std::cout,
367  const int order = 1 ) {
368 
369  return checkHessVec(x, x, v, steps, printToStream, outStream, order);
370 
371  }
372 
394  virtual std::vector<std::vector<Real> > checkHessVec( const Vector<Real> &x,
395  const Vector<Real> &hv,
396  const Vector<Real> &v,
397  const std::vector<Real> &steps,
398  const bool printToStream = true,
399  std::ostream & outStream = std::cout,
400  const int order = 1) ;
401 
402 
417  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
418  const Vector<Real> &v,
419  const Vector<Real> &w,
420  const bool printToStream = true,
421  std::ostream & outStream = std::cout ) {
422 
423  return checkHessSym(x, x, v, w, printToStream, outStream);
424 
425  }
426 
442  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
443  const Vector<Real> &hv,
444  const Vector<Real> &v,
445  const Vector<Real> &w,
446  const bool printToStream = true,
447  std::ostream & outStream = std::cout );
448 
449 // Definitions for parametrized (stochastic) objective functions
450 private:
451  std::vector<Real> param_;
452 
453 protected:
454  const std::vector<Real> getParameter(void) const {
455  return param_;
456  }
457 
458 public:
459  virtual void setParameter(const std::vector<Real> &param) {
460  param_.assign(param.begin(),param.end());
461  }
462 
463 }; // class Objective
464 
465 } // namespace ROL
466 
467 // include templated definitions
468 #include <ROL_ObjectiveDef.hpp>
469 
470 #endif
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:74
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:213
virtual void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
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:73
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:196
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.