ROL
ROL_HS42.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
16#ifndef ROL_HS42_HPP
17#define ROL_HS42_HPP
18
19#include "ROL_StdVector.hpp"
20#include "ROL_StdObjective.hpp"
21#include "ROL_StdConstraint.hpp"
22#include "ROL_TestProblem.hpp"
23#include "ROL_Bounds.hpp"
25#include "ROL_Types.hpp"
26
27namespace ROL {
28namespace ZOO {
29
36template<class Real>
37class Objective_HS42 : public StdObjective<Real> {
38public:
39
40 Real value( const std::vector<Real> &x, Real &tol ) {
41 const Real c1(1), c2(2), c3(3), c4(4);
42 return std::pow(x[0]-c1,c2) + std::pow(x[1]-c2,c2)
43 + std::pow(x[2]-c3,c2) + std::pow(x[3]-c4,c2);
44 }
45
46 void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
47 const Real c1(1), c2(2), c3(3), c4(4);
48 g[0] = c2*(x[0]-c1);
49 g[1] = c2*(x[1]-c2);
50 g[2] = c2*(x[2]-c3);
51 g[3] = c2*(x[3]-c4);
52 }
53
54 void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
55 const Real c2(2);
56 hv[0] = c2*v[0];
57 hv[1] = c2*v[1];
58 hv[2] = c2*v[2];
59 hv[3] = c2*v[3];
60 }
61};
62
63template<class Real>
64class Constraint_HS42a : public StdConstraint<Real> {
65public:
67
68 void value( std::vector<Real> &c, const std::vector<Real> &x, Real &tol ) {
69 const Real c2(2);
70 c[0] = x[0] - c2;
71 }
72
73 void applyJacobian(std::vector<Real> &jv, const std::vector<Real> &v,
74 const std::vector<Real> &x, Real &tol) {
75 jv[0] = v[0];
76 }
77
78 void applyAdjointJacobian( std::vector<Real> &ajv, const std::vector<Real> &v,
79 const std::vector<Real> &x, Real &tol ) {
80 ajv[0] = v[0];
81 ajv[1] = static_cast<Real>(0);
82 ajv[2] = static_cast<Real>(0);
83 ajv[3] = static_cast<Real>(0);
84 }
85
86 void applyAdjointHessian(std::vector<Real> &ahuv, const std::vector<Real> &u,
87 const std::vector<Real> &v, const std::vector<Real> &x,
88 Real &tol) {
89 ahuv.assign(ahuv.size(),static_cast<Real>(0));
90 }
91};
92
93template<class Real>
94class Constraint_HS42b : public StdConstraint<Real> {
95public:
97
98 void value( std::vector<Real> &c, const std::vector<Real> &x, Real &tol ) {
99 const Real c2(2);
100 c[0] = std::pow(x[2],c2) + std::pow(x[3],c2) - c2;
101 }
102
103 void applyJacobian(std::vector<Real> &jv, const std::vector<Real> &v,
104 const std::vector<Real> &x, Real &tol) {
105 const Real c2(2);
106 jv[0] = c2*x[2]*v[2] + c2*x[3]*v[3];
107 }
108
109 void applyAdjointJacobian( std::vector<Real> &ajv, const std::vector<Real> &v,
110 const std::vector<Real> &x, Real &tol ) {
111 const Real c2(2);
112 ajv[0] = static_cast<Real>(0);
113 ajv[1] = static_cast<Real>(0);
114 ajv[2] = c2*x[2]*v[0];
115 ajv[3] = c2*x[3]*v[0];
116 }
117
118 void applyAdjointHessian(std::vector<Real> &ahuv, const std::vector<Real> &u,
119 const std::vector<Real> &v, const std::vector<Real> &x,
120 Real &tol) {
121 const Real c2(2);
122 ahuv[0] = static_cast<Real>(0);
123 ahuv[1] = static_cast<Real>(0);
124 ahuv[2] = c2*v[2]*u[0];
125 ahuv[3] = c2*v[3]*u[0];
126 }
127};
128
129template<class Real>
130class getHS42 : public TestProblem<Real> {
131public:
132 getHS42(void) {}
133
134 Ptr<Objective<Real>> getObjective(void) const {
135 return ROL::makePtr<Objective_HS42<Real>>();
136 }
137
138 Ptr<Vector<Real>> getInitialGuess(void) const {
139 int n = 4;
140 return ROL::makePtr<StdVector<Real>>(n,1.0);
141 }
142
143 Ptr<Vector<Real>> getSolution(const int i = 0) const {
144 int n = 4;
145 ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
146 (*xp)[0] = static_cast<Real>(2);
147 (*xp)[1] = static_cast<Real>(2);
148 (*xp)[2] = static_cast<Real>(0.6*std::sqrt(2));
149 (*xp)[3] = static_cast<Real>(0.8*std::sqrt(2));
150 return ROL::makePtr<StdVector<Real>>(xp);
151 }
152
153 Ptr<Constraint<Real>> getEqualityConstraint(void) const {
154 std::vector<Ptr<Constraint<Real>>> cvec(2);
155 cvec[0] = makePtr<Constraint_HS42a<Real>>();
156 cvec[1] = makePtr<Constraint_HS42b<Real>>();
157 return ROL::makePtr<Constraint_Partitioned<Real>>(cvec);
158 }
159
160 Ptr<Vector<Real>> getEqualityMultiplier(void) const {
161 std::vector<Ptr<Vector<Real>>> lvec(2);
162 lvec[0] = makePtr<StdVector<Real>>(makePtr<std::vector<Real>>(1,0.0));
163 lvec[1] = makePtr<StdVector<Real>>(makePtr<std::vector<Real>>(1,0.0));
164 return ROL::makePtr<PartitionedVector<Real>>(lvec);
165 }
166};
167
168} // End ZOO Namespace
169} // End ROL Namespace
170
171#endif
Contains definitions of test objective functions.
Contains definitions of custom data types in ROL.
Defines the equality constraint operator interface for StdVectors.
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector's.
void value(std::vector< Real > &c, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:68
void applyAdjointHessian(std::vector< Real > &ahuv, const std::vector< Real > &u, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:86
void applyJacobian(std::vector< Real > &jv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:73
void applyAdjointJacobian(std::vector< Real > &ajv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:78
void value(std::vector< Real > &c, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:98
void applyJacobian(std::vector< Real > &jv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:103
void applyAdjointJacobian(std::vector< Real > &ajv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:109
void applyAdjointHessian(std::vector< Real > &ahuv, const std::vector< Real > &u, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:118
W. Hock and K. Schittkowski 42th test function.
Definition ROL_HS42.hpp:37
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:46
Real value(const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:40
void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_HS42.hpp:54
Ptr< Constraint< Real > > getEqualityConstraint(void) const
Definition ROL_HS42.hpp:153
Ptr< Vector< Real > > getEqualityMultiplier(void) const
Definition ROL_HS42.hpp:160
Ptr< Vector< Real > > getInitialGuess(void) const
Definition ROL_HS42.hpp:138
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition ROL_HS42.hpp:143
Ptr< Objective< Real > > getObjective(void) const
Definition ROL_HS42.hpp:134