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_Objective.hpp"
59 
60 namespace ROL {
61 namespace ZOO {
62 
65 template< class Real, class XPrim=StdVector<Real>, class XDual=StdVector<Real> >
66 class 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 
73 private:
74  Real alpha_;
75 
76  Real const1_;
77  Real const2_;
78 
79  template<class VectorType>
80  Teuchos::RCP<const vector> getVector( const V& x ) {
81  return Teuchos::dyn_cast<const VectorType>((x)).getVector();
82  }
83 
84  template<class VectorType>
85  Teuchos::RCP<vector> getVector( V& x ) {
86  return Teuchos::dyn_cast<VectorType>(x).getVector();
87  }
88 
89 public:
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  using Teuchos::RCP;
95  RCP<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  using Teuchos::RCP;
114  RCP<const vector> xp = getVector<XPrim>(x);
115  RCP<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  using Teuchos::RCP;
133  RCP<const vector> xp = getVector<XPrim>(x);
134  RCP<const vector> vp = getVector<XPrim>(v);
135  RCP<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  using Teuchos::RCP;
151 
152  RCP<const vector> xp = getVector<XPrim>(x);
153  RCP<const vector> vp = getVector<XDual>(v);
154  RCP<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 
168 template<class Real, class XPrim, class XDual>
169 void getRosenbrock( Teuchos::RCP<Objective<Real> > &obj,
170  Teuchos::RCP<Vector<Real> > &x0,
171  Teuchos::RCP<Vector<Real> > &x ) {
172  // Problem dimension
173  int n = 100;
174 
175  // Get Initial Guess
176  Teuchos::RCP<std::vector<Real> > x0p = Teuchos::rcp(new std::vector<Real>(n,0.0));
177  for ( int i = 0; i < n/2; i++ ) {
178  (*x0p)[2*i] = -1.2;
179  (*x0p)[2*i+1] = 1.0;
180  }
181  x0 = Teuchos::rcp(new XPrim(x0p));
182 
183  // Get Solution
184  Teuchos::RCP<std::vector<Real> > xp = Teuchos::rcp(new std::vector<Real>(n,0.0));
185  for ( int i = 0; i < n; i++ ) {
186  (*xp)[i] = 1.0;
187  }
188  x = Teuchos::rcp(new XPrim(xp));
189 
190  // Instantiate Objective Function
191  obj = Teuchos::rcp(new Objective_Rosenbrock<Real, XPrim, XDual>);
192 }
193 
194 }// End ZOO Namespace
195 }// End ROL Namespace
196 
197 #endif
Provides the interface to evaluate objective functions.
Teuchos::RCP< vector > getVector(V &x)
Rosenbrock&#39;s function.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Teuchos::RCP< const vector > getVector(const V &x)
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
void getRosenbrock(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< Vector< Real > > &x0, Teuchos::RCP< Vector< Real > > &x)
Objective_Rosenbrock(Real alpha=100.0)