ROL
ROL_Cubic.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_CUBIC_HPP
20#define ROL_CUBIC_HPP
21
23#include "ROL_StdObjective.hpp"
24#include "ROL_StdConstraint.hpp"
25#include "ROL_TestProblem.hpp"
26
27namespace ROL {
28namespace ZOO {
29
30 template<class Real>
31 class Objective_Cubic : public StdObjective<Real> {
32 public:
34
35 Real value( const std::vector<Real> &x, Real &tol ) {
36 return std::pow(x[0],3)+std::pow(x[1],3);
37 }
38
39 void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
40 const Real three(3);
41 g[0] = three*std::pow(x[0],2);
42 g[1] = three*std::pow(x[1],2);
43 }
44#if USE_HESSVEC
45 void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
46 const Real six(6);
47 hv[0] = six*x[0]*v[0];
48 hv[1] = six*x[1]*v[1];
49 }
50#endif
51 void invHessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
52 const Real six(6);
53 hv[0] = v[0]/(six*x[0]);
54 hv[1] = v[1]/(six*x[1]);
55 }
56 };
57
58 template<class Real>
59 class Constraint_Cubic : public StdConstraint<Real> {
60 public:
62
63 void value( std::vector<Real> &c, const std::vector<Real> &x, Real &tol ) {
64 c[0] = std::pow(x[0],3) + x[1];
65 }
66
67 void applyJacobian( std::vector<Real> &jv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
68 const Real three(3);
69 jv[0] = three*std::pow(x[0],2)*v[0] + v[1];
70 }
71
72 void applyAdjointJacobian( std::vector<Real> &ajv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
73 const Real three(3);
74 ajv[0] = three*std::pow(x[0],2)*v[0];
75 ajv[1] = v[0];
76 }
77#if USE_HESSVEC
78 void applyAdjointHessian( std::vector<Real> &ahuv, const std::vector<Real> &u, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
79 const Real zero(0), six(6);
80 ahuv[0] = six*x[0]*u[0]*v[0];
81 ahuv[1] = zero;
82 }
83#endif
84 };
85
86 template<class Real>
87 class getCubic : public TestProblem<Real> {
88 private:
89 const int type_;
90
91 public:
92 getCubic(int type = 0) : type_(type) {}
93
94 Ptr<Objective<Real>> getObjective(void) const {
95 return makePtr<Objective_Cubic<Real>>();
96 }
97
98 Ptr<Vector<Real>> getInitialGuess(void) const {
99 int n = 2;
100 Ptr<std::vector<Real>> scale = makePtr<std::vector<Real>>(n,static_cast<Real>( 1.0));
101 Ptr<std::vector<Real>> xp = makePtr<std::vector<Real>>(n,static_cast<Real>(-0.9));
102 return makePtr<PrimalScaledStdVector<Real>>(xp,scale);
103 }
104
105 Ptr<Vector<Real>> getSolution(const int i = 0) const {
106 int n = 2;
107 Ptr<std::vector<Real>> scale = makePtr<std::vector<Real>>(n,static_cast<Real>( 1.0));
108 Ptr<std::vector<Real>> xp = makePtr<std::vector<Real>>(n,static_cast<Real>(-1.0));
109 if (type_ == 1) {
110 const Real one(1), /*two(2),*/ three(3), six(6);
111 Real x = -one/std::pow(three,one/six);
112 Real y = -std::pow(x,3);
113 (*xp)[0] = x;
114 (*xp)[1] = y;
115 }
116 if (type_ == 2) {
117 // This solution is only approximate
118 (*xp)[0] = static_cast<Real>(-0.8374930678347255);
119 (*xp)[1] = static_cast<Real>( 0.5774131462277658);
120 }
121 return makePtr<PrimalScaledStdVector<Real>>(xp,scale);
122 }
123
124 Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
125 int n = 2;
126 Ptr<Vector<Real>> l = makePtr<StdVector<Real>>(n,-1.0);
127 Ptr<Vector<Real>> u = makePtr<StdVector<Real>>(n, 1.0);
128 return makePtr<Bounds<Real>>(l,u);
129 }
130
131 Ptr<Constraint<Real>> getEqualityConstraint(void) const {
132 if (type_ == 1) {
133 return makePtr<Constraint_Cubic<Real>>();
134 }
135 return nullPtr;
136 }
137
138 Ptr<Vector<Real>> getEqualityMultiplier(void) const {
139 if (type_ == 1) {
140 return makePtr<StdVector<Real>>(1,0.0);
141 }
142 return nullPtr;
143 }
144
145 Ptr<Constraint<Real>> getInequalityConstraint(void) const {
146 if (type_ == 2) {
147 return makePtr<Constraint_Cubic<Real>>();
148 }
149 return nullPtr;
150 }
151
152 Ptr<Vector<Real>> getInequalityMultiplier(void) const {
153 if (type_ == 2) {
154 return makePtr<StdVector<Real>>(1,0.0);
155 }
156 return nullPtr;
157 }
158
159 Ptr<BoundConstraint<Real>> getSlackBoundConstraint(void) const {
160 if (type_ == 2) {
161 Ptr<Vector<Real>> l = makePtr<StdVector<Real>>(1,-0.01);
162 Ptr<Vector<Real>> u = makePtr<StdVector<Real>>(1, 0.01);
163 return makePtr<Bounds<Real>>(l,u);
164 }
165 return nullPtr;
166 }
167 };
168
169}// End ZOO Namespace
170}// End ROL Namespace
171
172#endif
Objective_SerialSimOpt(const Ptr< Obj > &obj, const V &ui) z0_ zero()
Contains definitions of test objective functions.
Defines the equality constraint operator interface for StdVectors.
void applyAdjointHessian(Vector< Real > &ahuv, const Vector< Real > &u, const Vector< Real > &v, const Vector< Real > &x, Real &tol) override
Apply the derivative of the adjoint of the constraint Jacobian at to vector in direction ,...
Specializes the ROL::Objective interface for objective functions that operate on ROL::StdVector's.
virtual void hessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
void value(std::vector< Real > &c, const std::vector< Real > &x, Real &tol)
Definition ROL_Cubic.hpp:63
void applyJacobian(std::vector< Real > &jv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_Cubic.hpp:67
void applyAdjointJacobian(std::vector< Real > &ajv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_Cubic.hpp:72
Real value(const std::vector< Real > &x, Real &tol)
Definition ROL_Cubic.hpp:35
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Definition ROL_Cubic.hpp:39
void invHessVec(std::vector< Real > &hv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Definition ROL_Cubic.hpp:51
getCubic(int type=0)
Definition ROL_Cubic.hpp:92
Ptr< Vector< Real > > getInitialGuess(void) const
Definition ROL_Cubic.hpp:98
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Ptr< Constraint< Real > > getEqualityConstraint(void) const
Ptr< Objective< Real > > getObjective(void) const
Definition ROL_Cubic.hpp:94
Ptr< Vector< Real > > getSolution(const int i=0) const
Ptr< Vector< Real > > getInequalityMultiplier(void) const
Ptr< Vector< Real > > getEqualityMultiplier(void) const
Ptr< Constraint< Real > > getInequalityConstraint(void) const
Ptr< BoundConstraint< Real > > getSlackBoundConstraint(void) const