ROL
ROL_SumOfSquares.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 
15 #ifndef USE_HESSVEC
16 #define USE_HESSVEC 1
17 #endif
18 
19 #ifndef ROL_SUMOFSQUARES_HPP
20 #define ROL_SUMOFSQUARES_HPP
21 
22 #include "ROL_StdVector.hpp"
23 #include "ROL_TestProblem.hpp"
24 
25 namespace ROL {
26 namespace ZOO {
27 
30 template<class Real>
31 class Objective_SumOfSquares : public Objective<Real> {
32 public:
33  Real value( const Vector<Real> &x, Real &tol ) {
34  return x.dot(x);
35  }
36 
37  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
38  g.set(x);
39  g.scale(2.0);
40  }
41 
42 #if USE_HESSVEC
43  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
44  hv.set(v);
45  hv.scale(2.0);
46  }
47 #endif
48 
49  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
50  hv.set(v);
51  hv.scale(0.5);
52  }
53 };
54 
55 template<class Real>
56 class getSumOfSquares : public TestProblem<Real> {
57 public:
58  getSumOfSquares(void) {}
59 
60  Ptr<Objective<Real>> getObjective(void) const {
61  // Instantiate Objective Function
62  return ROL::makePtr<Objective_SumOfSquares<Real>>();
63  }
64 
65  Ptr<Vector<Real>> getInitialGuess(void) const {
66  // Problem dimension
67  int n = 100;
68  // Get Initial Guess
69  ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,1.0);
70  return ROL::makePtr<StdVector<Real>>(x0p);
71  }
72 
73  Ptr<Vector<Real>> getSolution(const int i = 0) const {
74  // Problem dimension
75  int n = 100;
76  // Get Solution
77  ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
78  return ROL::makePtr<StdVector<Real>>(xp);
79  }
80 };
81 
82 } // End ZOO Namespace
83 } // End ROL Namespace
84 
85 #endif
Ptr< Vector< Real > > getInitialGuess(void) const
Provides the interface to evaluate objective functions.
virtual void scale(const Real alpha)=0
Compute where .
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Ptr< Objective< Real > > getObjective(void) const
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:46
virtual Real dot(const Vector &x) const =0
Compute where .
Sum of squares function.
Contains definitions of test objective functions.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Ptr< Vector< Real > > getSolution(const int i=0) const
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:175
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.