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  ROL_UNUSED(x);
91  ROL_UNUSED(flag);
92  ROL_UNUSED(iter);
93  }
94 
101  virtual Real value( const Vector<Real> &x, Real &tol ) = 0;
102 
116  virtual void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) ;
117 
125  virtual Real dirDeriv( const Vector<Real> &x, const Vector<Real> &d, Real &tol ) ;
126 
135  virtual void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol );
136 
145  virtual void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
146  ROL_UNUSED(hv);
147  ROL_UNUSED(v);
148  ROL_UNUSED(x);
149  ROL_UNUSED(tol);
150  ROL_TEST_FOR_EXCEPTION(true, std::invalid_argument,
151  ">>> ERROR (ROL::Objective): invHessVec not implemented!");
152  //hv.set(v.dual());
153  }
154 
163  virtual void precond( Vector<Real> &Pv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
164  ROL_UNUSED(x);
165  ROL_UNUSED(tol);
166  Pv.set(v.dual());
167  }
168 
189  virtual std::vector<std::vector<Real> > checkGradient( const Vector<Real> &x,
190  const Vector<Real> &d,
191  const bool printToStream = true,
192  std::ostream & outStream = std::cout,
193  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
194  const int order = 1 ) {
195  return checkGradient(x, x.dual(), d, printToStream, outStream, numSteps, order);
196  }
197 
220  virtual std::vector<std::vector<Real> > checkGradient( const Vector<Real> &x,
221  const Vector<Real> &g,
222  const Vector<Real> &d,
223  const bool printToStream = true,
224  std::ostream & outStream = std::cout,
225  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
226  const int order = 1 );
227 
228 
249  virtual std::vector<std::vector<Real> > checkGradient( const Vector<Real> &x,
250  const Vector<Real> &d,
251  const std::vector<Real> &steps,
252  const bool printToStream = true,
253  std::ostream & outStream = std::cout,
254  const int order = 1 ) {
255 
256  return checkGradient(x, x.dual(), d, steps, printToStream, outStream, order);
257 
258  }
259 
260 
283  virtual std::vector<std::vector<Real> > checkGradient( const Vector<Real> &x,
284  const Vector<Real> &g,
285  const Vector<Real> &d,
286  const std::vector<Real> &steps,
287  const bool printToStream = true,
288  std::ostream & outStream = std::cout,
289  const int order = 1 );
290 
311  virtual std::vector<std::vector<Real> > checkHessVec( const Vector<Real> &x,
312  const Vector<Real> &v,
313  const bool printToStream = true,
314  std::ostream & outStream = std::cout,
315  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
316  const int order = 1 ) {
317 
318  return checkHessVec(x, x.dual(), v, printToStream, outStream, numSteps, order);
319 
320  }
321 
343  virtual std::vector<std::vector<Real> > checkHessVec( const Vector<Real> &x,
344  const Vector<Real> &hv,
345  const Vector<Real> &v,
346  const bool printToStream = true,
347  std::ostream & outStream = std::cout,
348  const int numSteps = ROL_NUM_CHECKDERIV_STEPS,
349  const int order = 1) ;
350 
351 
372  virtual std::vector<std::vector<Real> > checkHessVec( const Vector<Real> &x,
373  const Vector<Real> &v,
374  const std::vector<Real> &steps,
375  const bool printToStream = true,
376  std::ostream & outStream = std::cout,
377  const int order = 1 ) {
378 
379  return checkHessVec(x, x.dual(), v, steps, printToStream, outStream, order);
380 
381  }
382 
404  virtual std::vector<std::vector<Real> > checkHessVec( const Vector<Real> &x,
405  const Vector<Real> &hv,
406  const Vector<Real> &v,
407  const std::vector<Real> &steps,
408  const bool printToStream = true,
409  std::ostream & outStream = std::cout,
410  const int order = 1) ;
411 
412 
427  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
428  const Vector<Real> &v,
429  const Vector<Real> &w,
430  const bool printToStream = true,
431  std::ostream & outStream = std::cout ) {
432 
433  return checkHessSym(x, x.dual(), v, w, printToStream, outStream);
434 
435  }
436 
452  virtual std::vector<Real> checkHessSym( const Vector<Real> &x,
453  const Vector<Real> &hv,
454  const Vector<Real> &v,
455  const Vector<Real> &w,
456  const bool printToStream = true,
457  std::ostream & outStream = std::cout );
458 
459 // Definitions for parametrized (stochastic) objective functions
460 private:
461  std::vector<Real> param_;
462 
463 protected:
464  const std::vector<Real> getParameter(void) const {
465  return param_;
466  }
467 
468 public:
469  virtual void setParameter(const std::vector<Real> &param) {
470  param_.assign(param.begin(),param.end());
471  }
472 
473 }; // class Objective
474 
475 } // namespace ROL
476 
477 // include templated definitions
478 #include <ROL_ObjectiveDef.hpp>
479 
480 #endif
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.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
#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.
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.