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
25namespace ROL {
26namespace ZOO {
27
30template<class Real>
31class Objective_SumOfSquares : public Objective<Real> {
32public:
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
55template<class Real>
56class getSumOfSquares : public TestProblem<Real> {
57public:
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
Contains definitions of test objective functions.
Provides the interface to evaluate objective functions.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Defines the linear algebra or vector space interface.
virtual void set(const Vector &x)
Set where .
virtual void scale(const Real alpha)=0
Compute where .
virtual Real dot(const Vector &x) const =0
Compute where .
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Ptr< Objective< Real > > getObjective(void) const
Ptr< Vector< Real > > getInitialGuess(void) const
Ptr< Vector< Real > > getSolution(const int i=0) const