ROL
ROL_Rosenbrock.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// Whether or not to use the exact Hessian-times-a-vector
16#ifndef USE_HESSVEC
17#define USE_HESSVEC 1
18#endif
19
20#ifndef ROL_ROSENBROCK_HPP
21#define ROL_ROSENBROCK_HPP
22
23#include "ROL_StdVector.hpp"
24#include "ROL_TestProblem.hpp"
25
26namespace ROL {
27namespace ZOO {
28
31template< class Real, class XPrim=StdVector<Real>, class XDual=StdVector<Real> >
32class Objective_Rosenbrock : public Objective<Real> {
33
34 typedef std::vector<Real> vector;
35 typedef Vector<Real> V;
36
37 typedef typename vector::size_type uint;
38
39private:
40 Real alpha_;
41
42 Real const1_;
43 Real const2_;
44
45 template<class VectorType>
46 ROL::Ptr<const vector> getVector( const V& x ) {
47 return dynamic_cast<const VectorType&>((x)).getVector();
48 }
49
50 template<class VectorType>
51 ROL::Ptr<vector> getVector( V& x ) {
52 return dynamic_cast<VectorType&>(x).getVector();
53 }
54
55public:
56 Objective_Rosenbrock(Real alpha = 100.0) : alpha_(alpha), const1_(100.0), const2_(20.0) {}
57
58 Real value( const Vector<Real> &x, Real &tol ) {
59
60
61 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
62
63 uint n = xp->size();
64 Real val = 0;
65 for( uint i=0; i<n/2; i++ ) {
66 val += alpha_ * pow(pow((*xp)[2*i],2) - (*xp)[2*i+1], 2);
67 val += pow((*xp)[2*i] - 1.0, 2);
68 }
69
71 //Real error = tol*(2.0*((Real)rand())/((Real)RAND_MAX)-1.0);
72 //val += this->const1_*error;
73
74 return val;
75 }
76
77 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
78
79
80 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
81 ROL::Ptr<vector> gp = getVector<XDual>(g);
82
83 uint n = xp->size();
84 for( uint i=0; i<n/2; i++ ) {
85 (*gp)[2*i] = 4.0*alpha_*(pow((*xp)[2*i],2) - (*xp)[2*i+1])*(*xp)[2*i] + 2.0*((*xp)[2*i]-1.0);
86 (*gp)[2*i+1] = -2.0*alpha_*(pow((*xp)[2*i],2) - (*xp)[2*i+1]);
87
89 //Real error0 = tol*(2.0*((Real)rand())/((Real)RAND_MAX)-1.0);
90 //Real error1 = tol*(2.0*((Real)rand())/((Real)RAND_MAX)-1.0);
91 //(*gp)[2*i] += this->const2_*error0/std::sqrt(n);
92 //(*gp)[2*i+1] += this->const2_*error1/std::sqrt(n);
93 }
94 }
95#if USE_HESSVEC
96 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
97
98
99 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
100 ROL::Ptr<const vector> vp = getVector<XPrim>(v);
101 ROL::Ptr<vector> hvp = getVector<XDual>(hv);
102
103 uint n = xp->size();
104 for( uint i=0; i<n/2; i++ ) {
105 Real h11 = 4.0*alpha_*(3.0*pow((*xp)[2*i],2)-(*xp)[2*i+1]) + 2.0;
106 Real h12 = -4.0*alpha_*(*xp)[2*i];
107 Real h22 = 2.0*alpha_;
108
109 (*hvp)[2*i] = h11*(*vp)[2*i] + h12*(*vp)[2*i+1];
110 (*hvp)[2*i+1] = h12*(*vp)[2*i] + h22*(*vp)[2*i+1];
111 }
112 }
113#endif
114 void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
115
116
117
118 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
119 ROL::Ptr<const vector> vp = getVector<XDual>(v);
120 ROL::Ptr<vector> hvp = getVector<XPrim>(hv);
121
122 uint n = xp->size();
123 for( uint i=0; i<n/2; i++ ) {
124 Real h11 = 4.0*alpha_*(3.0*pow((*xp)[2*i],2)-(*xp)[2*i+1]) + 2.0;
125 Real h12 = -4.0*alpha_*(*xp)[2*i];
126 Real h22 = 2.0*alpha_;
127
128 (*hvp)[2*i] = (1.0/(h11*h22-h12*h12))*( h22*(*vp)[2*i] - h12*(*vp)[2*i+1]);
129 (*hvp)[2*i+1] = (1.0/(h11*h22-h12*h12))*(-h12*(*vp)[2*i] + h11*(*vp)[2*i+1]);
130 }
131 }
132};
133
134template<class Real>
135class getRosenbrock : public TestProblem<Real> {
136public:
138
139 Ptr<Objective<Real>> getObjective(void) const {
140 // Instantiate Objective Function
141 return ROL::makePtr<Objective_Rosenbrock<Real>>();
142 }
143
144 Ptr<Vector<Real>> getInitialGuess(void) const {
145 // Problem dimension
146 int n = 100;
147 // Get Initial Guess
148 ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,0.0);
149 for ( int i = 0; i < n/2; i++ ) {
150 (*x0p)[2*i] = -1.2;
151 (*x0p)[2*i+1] = 1.0;
152 }
153 return ROL::makePtr<StdVector<Real>>(x0p);
154 }
155
156 Ptr<Vector<Real>> getSolution(const int i = 0) const {
157 // Problem dimension
158 int n = 100;
159 // Get Solution
160 ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
161 for ( int li = 0; li < n; li++ ) {
162 (*xp)[li] = 1.0;
163 }
164 return ROL::makePtr<StdVector<Real>>(xp);
165 }
166};
167
168}// End ZOO Namespace
169}// End ROL Namespace
170
171#endif
Contains definitions of test objective functions.
Provides the interface to evaluate objective functions.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Defines the linear algebra or vector space interface.
ROL::Ptr< vector > getVector(V &x)
Objective_Rosenbrock(Real alpha=100.0)
ROL::Ptr< const vector > getVector(const V &x)
Real value(const Vector< Real > &x, Real &tol)
Compute value.
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Ptr< Objective< Real > > getObjective(void) const
Ptr< Vector< Real > > getSolution(const int i=0) const
Ptr< Vector< Real > > getInitialGuess(void) const