ROL
ROL_Quartic.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_QUARTIC_HPP
20#define ROL_QUARTIC_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_Quartic : public StdObjective<Real> {
32 public:
34
35 Real value( const std::vector<Real> &x, Real &tol ) {
36 const Real one(1);
37 return std::pow(x[0]-one, 4) + std::pow(x[1]-one, 4);
38 }
39
40 void gradient( std::vector<Real> &g, const std::vector<Real> &x, Real &tol ) {
41 const Real one(1), four(4);
42 g[0] = four * std::pow(x[0]-one, 3);
43 g[1] = four * std::pow(x[1]-one, 3);
44 }
45#if USE_HESSVEC
46 void hessVec( std::vector<Real> &hv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
47 const Real one(1), twelve(12);
48 hv[0] = twelve * std::pow(x[0]-one, 2) * v[0];
49 hv[1] = twelve * std::pow(x[1]-one, 2) * v[1];
50 }
51#endif
52 };
53
54 template<class Real>
55 class Constraint_Quartic : public StdConstraint<Real> {
56 public:
58
59 void value( std::vector<Real> &c, const std::vector<Real> &x, Real &tol ) {
60 const Real half(0.5);
61 c[0] = std::pow(x[0], 2) - half*x[1];
62 c[1] = std::pow(x[1], 2) - half*x[0];
63 }
64
65 void applyJacobian( std::vector<Real> &jv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
66 const Real half(0.5), two(2);
67 jv[0] = two*x[0]*v[0] - half*v[1];
68 jv[1] = two*x[1]*v[1] - half*v[0];
69 }
70
71 void applyAdjointJacobian( std::vector<Real> &ajv, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
72 const Real half(0.5), two(2);
73 ajv[0] = two*x[0]*v[0] - half*v[1];
74 ajv[1] = two*x[1]*v[1] - half*v[0];
75 }
76#if USE_HESSVEC
77 void applyAdjointHessian( std::vector<Real> &ahuv, const std::vector<Real> &u, const std::vector<Real> &v, const std::vector<Real> &x, Real &tol ) {
78 const Real two(2);
79 ahuv[0] = two*u[0]*v[0];
80 ahuv[1] = two*u[1]*v[1];
81 }
82#endif
83 };
84
85 template<class Real>
86 class getQuartic : public TestProblem<Real> {
87 public:
89
90 Ptr<Objective<Real>> getObjective(void) const {
91 return makePtr<Objective_Quartic<Real>>();
92 }
93
94 Ptr<Vector<Real>> getInitialGuess(void) const {
95 int n = 2;
96 Ptr<std::vector<Real>> scale = makePtr<std::vector<Real>>(n,static_cast<Real>(1.0));
97 Ptr<std::vector<Real>> xp = makePtr<std::vector<Real>>(n,static_cast<Real>(1.0));
98 return makePtr<PrimalScaledStdVector<Real>>(xp,scale);
99 }
100
101 Ptr<Vector<Real>> getSolution(const int i = 0) const {
102 int n = 2;
103 Ptr<std::vector<Real>> scale = makePtr<std::vector<Real>>(n,static_cast<Real>(1.0));
104 Ptr<std::vector<Real>> xp = makePtr<std::vector<Real>>(n,static_cast<Real>(0.5));
105 return makePtr<PrimalScaledStdVector<Real>>(xp,scale);
106 }
107
108 Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
109 int n = 2;
110 Ptr<std::vector<Real>> scale = makePtr<std::vector<Real>>(n,static_cast<Real>(1.0));
111 Ptr<std::vector<Real>> lp = makePtr<std::vector<Real>>(n,static_cast<Real>(0.0));
112 Ptr<std::vector<Real>> up = makePtr<std::vector<Real>>(n,static_cast<Real>(0.0));
113 (*lp)[0] = static_cast<Real>( 0.5);
114 (*lp)[1] = static_cast<Real>(-2.9);
115 (*up)[0] = static_cast<Real>( 5.8);
116 (*up)[1] = static_cast<Real>( 2.9);
117 Ptr<Vector<Real>> l = makePtr<PrimalScaledStdVector<Real>>(lp,scale);
118 Ptr<Vector<Real>> u = makePtr<PrimalScaledStdVector<Real>>(up,scale);
119 return makePtr<Bounds<Real>>(l,u);
120 }
121
122 Ptr<Constraint<Real>> getInequalityConstraint(void) const {
123 return makePtr<Constraint_Quartic<Real>>();
124 }
125
126 Ptr<Vector<Real>> getInequalityMultiplier(void) const {
127 Ptr<std::vector<Real>> scale = makePtr<std::vector<Real>>(2,static_cast<Real>(1.0));
128 Ptr<std::vector<Real>> lp = makePtr<std::vector<Real>>(2,static_cast<Real>(0.0));
129 return makePtr<DualScaledStdVector<Real>>(lp,scale);
130 }
131
132 Ptr<BoundConstraint<Real>> getSlackBoundConstraint(void) const {
133 Ptr<std::vector<Real>> scale = makePtr<std::vector<Real>>(2,static_cast<Real>(1.0));
134 Ptr<std::vector<Real>> up = makePtr<std::vector<Real>>(2,static_cast<Real>(0.0));
135 Ptr<Vector<Real>> u = makePtr<DualScaledStdVector<Real>>(up,scale);
136 return makePtr<Bounds<Real>>(*u,false);
137 }
138 };
139
140}// End ZOO Namespace
141}// End ROL Namespace
142
143#endif
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 applyAdjointJacobian(std::vector< Real > &ajv, 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)
void applyJacobian(std::vector< Real > &jv, const std::vector< Real > &v, const std::vector< Real > &x, Real &tol)
Real value(const std::vector< Real > &x, Real &tol)
void gradient(std::vector< Real > &g, const std::vector< Real > &x, Real &tol)
Ptr< Objective< Real > > getObjective(void) const
Ptr< BoundConstraint< Real > > getSlackBoundConstraint(void) const
Ptr< Constraint< Real > > getInequalityConstraint(void) const
Ptr< Vector< Real > > getSolution(const int i=0) const
Ptr< Vector< Real > > getInequalityMultiplier(void) const
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Ptr< Vector< Real > > getInitialGuess(void) const