ROL
ROL_Rosenbrock.hpp
Go to the documentation of this file.
1// @HEADER
2// ************************************************************************
3//
4// Rapid Optimization Library (ROL) Package
5// Copyright (2014) Sandia Corporation
6//
7// Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8// license for use of this work by or on behalf of the U.S. Government.
9//
10// Redistribution and use in source and binary forms, with or without
11// modification, are permitted provided that the following conditions are
12// met:
13//
14// 1. Redistributions of source code must retain the above copyright
15// notice, this list of conditions and the following disclaimer.
16//
17// 2. Redistributions in binary form must reproduce the above copyright
18// notice, this list of conditions and the following disclaimer in the
19// documentation and/or other materials provided with the distribution.
20//
21// 3. Neither the name of the Corporation nor the names of the
22// contributors may be used to endorse or promote products derived from
23// this software without specific prior written permission.
24//
25// THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36//
37// Questions? Contact lead developers:
38// Drew Kouri (dpkouri@sandia.gov) and
39// Denis Ridzal (dridzal@sandia.gov)
40//
41// ************************************************************************
42// @HEADER
43
49// Whether or not to use the exact Hessian-times-a-vector
50#ifndef USE_HESSVEC
51#define USE_HESSVEC 1
52#endif
53
54#ifndef ROL_ROSENBROCK_HPP
55#define ROL_ROSENBROCK_HPP
56
57#include "ROL_StdVector.hpp"
58#include "ROL_TestProblem.hpp"
59
60namespace ROL {
61namespace ZOO {
62
65template< class Real, class XPrim=StdVector<Real>, class XDual=StdVector<Real> >
66class Objective_Rosenbrock : public Objective<Real> {
67
68 typedef std::vector<Real> vector;
69 typedef Vector<Real> V;
70
71 typedef typename vector::size_type uint;
72
73private:
74 Real alpha_;
75
76 Real const1_;
77 Real const2_;
78
79 template<class VectorType>
80 ROL::Ptr<const vector> getVector( const V& x ) {
81 return dynamic_cast<const VectorType&>((x)).getVector();
82 }
83
84 template<class VectorType>
85 ROL::Ptr<vector> getVector( V& x ) {
86 return dynamic_cast<VectorType&>(x).getVector();
87 }
88
89public:
90 Objective_Rosenbrock(Real alpha = 100.0) : alpha_(alpha), const1_(100.0), const2_(20.0) {}
91
92 Real value( const Vector<Real> &x, Real &tol ) {
93
94
95 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
96
97 uint n = xp->size();
98 Real val = 0;
99 for( uint i=0; i<n/2; i++ ) {
100 val += alpha_ * pow(pow((*xp)[2*i],2) - (*xp)[2*i+1], 2);
101 val += pow((*xp)[2*i] - 1.0, 2);
102 }
103
105 //Real error = tol*(2.0*((Real)rand())/((Real)RAND_MAX)-1.0);
106 //val += this->const1_*error;
107
108 return val;
109 }
110
111 void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
112
113
114 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
115 ROL::Ptr<vector> gp = getVector<XDual>(g);
116
117 uint n = xp->size();
118 for( uint i=0; i<n/2; i++ ) {
119 (*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);
120 (*gp)[2*i+1] = -2.0*alpha_*(pow((*xp)[2*i],2) - (*xp)[2*i+1]);
121
123 //Real error0 = tol*(2.0*((Real)rand())/((Real)RAND_MAX)-1.0);
124 //Real error1 = tol*(2.0*((Real)rand())/((Real)RAND_MAX)-1.0);
125 //(*gp)[2*i] += this->const2_*error0/std::sqrt(n);
126 //(*gp)[2*i+1] += this->const2_*error1/std::sqrt(n);
127 }
128 }
129#if USE_HESSVEC
130 void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
131
132
133 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
134 ROL::Ptr<const vector> vp = getVector<XPrim>(v);
135 ROL::Ptr<vector> hvp = getVector<XDual>(hv);
136
137 uint n = xp->size();
138 for( uint i=0; i<n/2; i++ ) {
139 Real h11 = 4.0*alpha_*(3.0*pow((*xp)[2*i],2)-(*xp)[2*i+1]) + 2.0;
140 Real h12 = -4.0*alpha_*(*xp)[2*i];
141 Real h22 = 2.0*alpha_;
142
143 (*hvp)[2*i] = h11*(*vp)[2*i] + h12*(*vp)[2*i+1];
144 (*hvp)[2*i+1] = h12*(*vp)[2*i] + h22*(*vp)[2*i+1];
145 }
146 }
147#endif
148 void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
149
150
151
152 ROL::Ptr<const vector> xp = getVector<XPrim>(x);
153 ROL::Ptr<const vector> vp = getVector<XDual>(v);
154 ROL::Ptr<vector> hvp = getVector<XPrim>(hv);
155
156 uint n = xp->size();
157 for( uint i=0; i<n/2; i++ ) {
158 Real h11 = 4.0*alpha_*(3.0*pow((*xp)[2*i],2)-(*xp)[2*i+1]) + 2.0;
159 Real h12 = -4.0*alpha_*(*xp)[2*i];
160 Real h22 = 2.0*alpha_;
161
162 (*hvp)[2*i] = (1.0/(h11*h22-h12*h12))*( h22*(*vp)[2*i] - h12*(*vp)[2*i+1]);
163 (*hvp)[2*i+1] = (1.0/(h11*h22-h12*h12))*(-h12*(*vp)[2*i] + h11*(*vp)[2*i+1]);
164 }
165 }
166};
167
168template<class Real>
169class getRosenbrock : public TestProblem<Real> {
170public:
172
173 Ptr<Objective<Real>> getObjective(void) const {
174 // Instantiate Objective Function
175 return ROL::makePtr<Objective_Rosenbrock<Real>>();
176 }
177
178 Ptr<Vector<Real>> getInitialGuess(void) const {
179 // Problem dimension
180 int n = 100;
181 // Get Initial Guess
182 ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,0.0);
183 for ( int i = 0; i < n/2; i++ ) {
184 (*x0p)[2*i] = -1.2;
185 (*x0p)[2*i+1] = 1.0;
186 }
187 return ROL::makePtr<StdVector<Real>>(x0p);
188 }
189
190 Ptr<Vector<Real>> getSolution(const int i = 0) const {
191 // Problem dimension
192 int n = 100;
193 // Get Solution
194 ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
195 for ( int i = 0; i < n; i++ ) {
196 (*xp)[i] = 1.0;
197 }
198 return ROL::makePtr<StdVector<Real>>(xp);
199 }
200};
201
202}// End ZOO Namespace
203}// End ROL Namespace
204
205#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.
Definition: ROL_Vector.hpp:84
Rosenbrock's function.
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