ROL
ROL_HS3.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
14
15#ifndef USE_HESSVEC
16#define USE_HESSVEC 1
17#endif
18
19#ifndef ROL_HS3_HPP
20#define ROL_HS3_HPP
21
22#include "ROL_StdVector.hpp"
23#include "ROL_TestProblem.hpp"
24#include "ROL_Bounds.hpp"
25#include "ROL_Types.hpp"
26
27namespace ROL {
28namespace ZOO {
29
32 template<class Real>
33 class Objective_HS3 : public Objective<Real> {
34
35 typedef std::vector<Real> vector;
36 typedef Vector<Real> V;
38
39 private:
40
41 ROL::Ptr<const vector> getVector( const V& x ) {
42
43 return dynamic_cast<const SV&>(x).getVector();
44 }
45
46 ROL::Ptr<vector> getVector( V& x ) {
47
48 return dynamic_cast<SV&>(x).getVector();
49 }
50
51 public:
53
54 Real value( const Vector<Real> &x, Real &tol ) {
55
56
57 ROL::Ptr<const vector> ex = getVector(x);
58 return (*ex)[1] + 1.e-5 * std::pow((*ex)[1] - (*ex)[0],2.0);
59 }
60
61 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
62
63
64 ROL::Ptr<const vector> ex = getVector(x);
65 ROL::Ptr<vector> eg = getVector(g);
66 (*eg)[0] = -1.e-5 * 2.0 * ((*ex)[1] - (*ex)[0]);
67 (*eg)[1] = 1.0 + 1.e-5 * 2.0 * ((*ex)[1] - (*ex)[0]);
68 }
69#if USE_HESSVEC
70 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
71
72
73 ROL::Ptr<const vector> ex = getVector(x);
74 ROL::Ptr<const vector> ev = getVector(v);
75 ROL::Ptr<vector> ehv = getVector(hv);
76 Real h11 = 1.e-5 * 2.0;
77 Real h22 = 1.e-5 * 2.0;
78 Real h12 = -1.e-5 * 2.0;
79 Real h21 = -1.e-5 * 2.0;
80
81 (*ehv)[0] = h11 * (*ev)[0] + h12 * (*ev)[1];
82 (*ehv)[1] = h21 * (*ev)[0] + h22 * (*ev)[1];
83 }
84#endif
85 void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
86
87
88 ROL::Ptr<const vector> ex = getVector(x);
89 ROL::Ptr<const vector> ev = getVector(v);
90 ROL::Ptr<vector> ehv = getVector(hv);
91
92 Real h11 = 1.e-5 * 2.0;
93 Real h22 = 1.e-5 * 2.0;
94 Real h12 = -1.e-5 * 2.0;
95 Real h21 = -1.e-5 * 2.0;
96
97 (*ehv)[0] = 1.0/(h11*h22 - h12*h21) * (h22 * (*ev)[0] - h12 * (*ev)[1]);
98 (*ehv)[1] = 1.0/(h11*h22 - h12*h21) * (-h21 * (*ev)[0] + h11 * (*ev)[1]);
99 }
100 };
101
102template<class Real>
103class getHS3 : public TestProblem<Real> {
104public:
105 getHS3(void) {}
106
107 Ptr<Objective<Real>> getObjective(void) const {
108 // Instantiate Objective Function
109 return ROL::makePtr<Objective_HS3<Real>>();
110 }
111
112 Ptr<Vector<Real>> getInitialGuess(void) const {
113 // Problem dimension
114 int n = 2;
115 // Get Initial Guess
116 ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,0.0);
117 (*x0p)[0] = 10.0; (*x0p)[1] = 1.0;
118 return ROL::makePtr<StdVector<Real>>(x0p);
119 }
120
121 Ptr<Vector<Real>> getSolution(const int i = 0) const {
122 // Problem dimension
123 int n = 2;
124 // Get Solution
125 ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
126 (*xp)[0] = 0.0; (*xp)[1] = 0.0;
127 return ROL::makePtr<StdVector<Real>>(xp);
128 }
129
130 Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
131 // Problem dimension
132 int n = 2;
133 // Instantiate BoundConstraint
134 ROL::Ptr<std::vector<Real> > lp = ROL::makePtr<std::vector<Real>>(n,0.0);
135 (*lp)[0] = ROL_NINF<Real>(); (*lp)[1] = 0.0;
136 ROL::Ptr<Vector<Real> > l = ROL::makePtr<StdVector<Real>>(lp);
137 ROL::Ptr<std::vector<Real> > up = ROL::makePtr<std::vector<Real>>(n,0.0);
138 (*up)[0] = ROL_INF<Real>(); (*up)[1] = ROL_INF<Real>();
139 ROL::Ptr<Vector<Real> > u = ROL::makePtr<StdVector<Real>>(up);
140 return ROL::makePtr<Bounds<Real>>(l,u);
141 }
142};
143
144} // End ZOO Namespace
145} // End ROL Namespace
146
147#endif
Contains definitions of test objective functions.
Contains definitions of custom data types in ROL.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Defines the linear algebra or vector space interface.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Definition ROL_HS3.hpp:61
Vector< Real > V
Definition ROL_HS3.hpp:36
Real value(const Vector< Real > &x, Real &tol)
Definition ROL_HS3.hpp:54
ROL::Ptr< vector > getVector(V &x)
Definition ROL_HS3.hpp:46
StdVector< Real > SV
Definition ROL_HS3.hpp:37
ROL::Ptr< const vector > getVector(const V &x)
Definition ROL_HS3.hpp:41
std::vector< Real > vector
Definition ROL_HS3.hpp:35
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Definition ROL_HS3.hpp:85
Ptr< Vector< Real > > getInitialGuess(void) const
Definition ROL_HS3.hpp:112
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition ROL_HS3.hpp:121
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition ROL_HS3.hpp:130
Ptr< Objective< Real > > getObjective(void) const
Definition ROL_HS3.hpp:107
Real ROL_NINF(void)
Definition ROL_Types.hpp:74
Real ROL_INF(void)
Definition ROL_Types.hpp:71