ROL
ROL_HS5.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_HS5_HPP
20#define ROL_HS5_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_HS5 : 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 ROL::Ptr<const vector> ex = getVector(x);
57
58 return std::sin((*ex)[0] + (*ex)[1]) + std::pow((*ex)[0]-(*ex)[1],2.0) - 1.5*(*ex)[0] + 2.5*(*ex)[1] + 1.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
67 (*eg)[0] = std::cos((*ex)[0] + (*ex)[1]) + 2.0*((*ex)[0]-(*ex)[1]) - 1.5;
68 (*eg)[1] = std::cos((*ex)[0] + (*ex)[1]) - 2.0*((*ex)[0]-(*ex)[1]) + 2.5;;
69 }
70#if USE_HESSVEC
71 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
72
73
74 ROL::Ptr<const vector> ex = getVector(x);
75 ROL::Ptr<const vector> ev = getVector(v);
76 ROL::Ptr<vector> ehv = getVector(hv);
77
78 Real h11 = -std::sin((*ex)[0] + (*ex)[1]) + 2.0;
79 Real h22 = -std::sin((*ex)[0] + (*ex)[1]) + 2.0;
80 Real h12 = -std::sin((*ex)[0] + (*ex)[1]) - 2.0;
81 Real h21 = -std::sin((*ex)[0] + (*ex)[1]) - 2.0;
82
83 (*ehv)[0] = h11 * (*ev)[0] + h12 * (*ev)[1];
84 (*ehv)[1] = h21 * (*ev)[0] + h22 * (*ev)[1];
85 }
86#endif
87 void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
88
89
90 ROL::Ptr<const vector> ex = getVector(x);
91 ROL::Ptr<const vector> ev = getVector(v);
92 ROL::Ptr<vector> ehv = getVector(hv);
93
94 Real h11 = -std::sin((*ex)[0] + (*ex)[1]) + 2.0;
95 Real h22 = -std::sin((*ex)[0] + (*ex)[1]) + 2.0;
96 Real h12 = -std::sin((*ex)[0] + (*ex)[1]) - 2.0;
97 Real h21 = -std::sin((*ex)[0] + (*ex)[1]) - 2.0;
98
99 (*ehv)[0] = 1.0/(h11*h22 - h12*h21) * (h22 * (*ev)[0] - h12 * (*ev)[1]);
100 (*ehv)[1] = 1.0/(h11*h22 - h12*h21) * (-h21 * (*ev)[0] + h11 * (*ev)[1]);
101 }
102 };
103
104template<class Real>
105class getHS5 : public TestProblem<Real> {
106public:
107 getHS5(void) {}
108
109 Ptr<Objective<Real>> getObjective(void) const {
110 // Instantiate Objective Function
111 return ROL::makePtr<Objective_HS5<Real>>();
112 }
113
114 Ptr<Vector<Real>> getInitialGuess(void) const {
115 // Problem dimension
116 int n = 2;
117 // Get Initial Guess
118 ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,0.0);
119 (*x0p)[0] = 0.0; (*x0p)[1] = 0.0;
120 return ROL::makePtr<StdVector<Real>>(x0p);
121 }
122
123 Ptr<Vector<Real>> getSolution(const int i = 0) const {
124 // Problem dimension
125 int n = 2;
126 // Get Solution
127 ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
128 (*xp)[0] = -ROL::ScalarTraits<Real>::pi()/3.0 + 1.0/2.0; (*xp)[1] = -ROL::ScalarTraits<Real>::pi()/3.0 - 1.0/2.0;
129 return ROL::makePtr<StdVector<Real>>(xp);
130 }
131
132 Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
133 // Problem dimension
134 int n = 2;
135 // Instantiate BoundConstraint
136 ROL::Ptr<std::vector<Real> > lp = ROL::makePtr<std::vector<Real>>(n,0.0);
137 (*lp)[0] = -1.5; (*lp)[1] = -3.0;
138 ROL::Ptr<Vector<Real> > l = ROL::makePtr<StdVector<Real>>(lp);
139 ROL::Ptr<std::vector<Real> > up = ROL::makePtr<std::vector<Real>>(n,0.0);
140 (*up)[0] = 4.0; (*up)[1] = 3.0;
141 ROL::Ptr<Vector<Real> > u = ROL::makePtr<StdVector<Real>>(up);
142 return ROL::makePtr<Bounds<Real>>(l,u);
143 }
144};
145
146} // End ZOO Namespace
147} // End ROL Namespace
148
149#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.
ROL::Ptr< const vector > getVector(const V &x)
Definition ROL_HS5.hpp:41
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Definition ROL_HS5.hpp:87
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Definition ROL_HS5.hpp:61
std::vector< Real > vector
Definition ROL_HS5.hpp:35
Vector< Real > V
Definition ROL_HS5.hpp:36
Real value(const Vector< Real > &x, Real &tol)
Definition ROL_HS5.hpp:54
StdVector< Real > SV
Definition ROL_HS5.hpp:37
ROL::Ptr< vector > getVector(V &x)
Definition ROL_HS5.hpp:46
Ptr< Objective< Real > > getObjective(void) const
Definition ROL_HS5.hpp:109
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition ROL_HS5.hpp:132
Ptr< Vector< Real > > getInitialGuess(void) const
Definition ROL_HS5.hpp:114
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition ROL_HS5.hpp:123
static constexpr Real pi() noexcept