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  private:
68  Real alpha_;
69 
70  Real const1_;
71  Real const2_;
72 
73  public:
74  Objective_Rosenbrock(Real alpha = 100.0) : alpha_(alpha), const1_(100.0), const2_(20.0) {}
75 
76  Real value( const Vector<Real> &x, Real &tol ) {
77  XPrim & ex =
78  Teuchos::dyn_cast<XPrim>(const_cast <Vector<Real> &>(x));
79  Teuchos::RCP<const std::vector<Real> > xp = ex.getVector();
80 
81  int n = xp->size();
82  Real val = 0;
83  for( int i=0; i<n/2; i++ ) {
84  val += alpha_ * pow(pow((*xp)[2*i],2) - (*xp)[2*i+1], 2);
85  val += pow((*xp)[2*i] - 1.0, 2);
86  }
87 
89  //Real error = tol*(2.0*((Real)rand())/((Real)RAND_MAX)-1.0);
90  //val += this->const1_*error;
91 
92  return val;
93  }
94 
95  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
96  Teuchos::RCP<const std::vector<Real> > xp =
97  (Teuchos::dyn_cast<XPrim>(const_cast<Vector<Real> &>(x))).getVector();
98  Teuchos::RCP<std::vector<Real> > gp =
99  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<XDual>(g)).getVector());
100 
101  int n = xp->size();
102  for( int i=0; i<n/2; i++ ) {
103  (*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);
104  (*gp)[2*i+1] = -2.0*alpha_*(pow((*xp)[2*i],2) - (*xp)[2*i+1]);
105 
107  //Real error0 = tol*(2.0*((Real)rand())/((Real)RAND_MAX)-1.0);
108  //Real error1 = tol*(2.0*((Real)rand())/((Real)RAND_MAX)-1.0);
109  //(*gp)[2*i] += this->const2_*error0/std::sqrt(n);
110  //(*gp)[2*i+1] += this->const2_*error1/std::sqrt(n);
111  }
112  }
113 #if USE_HESSVEC
114  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
115  Teuchos::RCP<const std::vector<Real> > xp =
116  (Teuchos::dyn_cast<XPrim>(const_cast<Vector<Real> &>(x))).getVector();
117  Teuchos::RCP<const std::vector<Real> > vp =
118  (Teuchos::dyn_cast<XPrim>(const_cast<Vector<Real> &>(v))).getVector();
119  Teuchos::RCP<std::vector<Real> > hvp =
120  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<XDual>(hv)).getVector());
121 
122  int n = xp->size();
123  for( int 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] = h11*(*vp)[2*i] + h12*(*vp)[2*i+1];
129  (*hvp)[2*i+1] = h12*(*vp)[2*i] + h22*(*vp)[2*i+1];
130  }
131  }
132 #endif
133  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
134  Teuchos::RCP<const std::vector<Real> > xp =
135  (Teuchos::dyn_cast<XPrim>(const_cast<Vector<Real> &>(x))).getVector();
136  Teuchos::RCP<const std::vector<Real> > vp =
137  (Teuchos::dyn_cast<XDual>(const_cast<Vector<Real> &>(v))).getVector();
138  Teuchos::RCP<std::vector<Real> > hvp =
139  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<XPrim>(hv)).getVector());
140 
141  int n = xp->size();
142  for( int i=0; i<n/2; i++ ) {
143  Real h11 = 4.0*alpha_*(3.0*pow((*xp)[2*i],2)-(*xp)[2*i+1]) + 2.0;
144  Real h12 = -4.0*alpha_*(*xp)[2*i];
145  Real h22 = 2.0*alpha_;
146 
147  (*hvp)[2*i] = (1.0/(h11*h22-h12*h12))*( h22*(*vp)[2*i] - h12*(*vp)[2*i+1]);
148  (*hvp)[2*i+1] = (1.0/(h11*h22-h12*h12))*(-h12*(*vp)[2*i] + h11*(*vp)[2*i+1]);
149  }
150  }
151  };
152 
153  template<class Real, class XPrim, class XDual>
154  void getRosenbrock( Teuchos::RCP<Objective<Real> > &obj, Vector<Real> &x0, Vector<Real> &x ) {
155  // Cast Initial Guess and Solution Vectors
156  Teuchos::RCP<std::vector<Real> > x0p =
157  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<XPrim>(x0)).getVector());
158  Teuchos::RCP<std::vector<Real> > xp =
159  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<XPrim>(x)).getVector());
160  int n = xp->size();
161  // Resize Vectors
162  n = 100;
163  x0p->resize(n);
164  xp->resize(n);
165  // Instantiate Objective Function
166  obj = Teuchos::rcp( new Objective_Rosenbrock<Real, XPrim, XDual> );
167  // Get Initial Guess
168  for (int i=0; i<n/2; i++) {
169  (*x0p)[2*i] = -1.2;
170  (*x0p)[2*i+1] = 1.0;
171  }
172  // Get Solution
173  for( int i=0; i<n; i++ ) {
174  (*xp)[i] = 1.0;
175  }
176  }
177 
178 }// End ZOO Namespace
179 }// End ROL Namespace
180 
181 #endif
Provides the interface to evaluate objective functions.
Rosenbrock'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:72
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Objective_Rosenbrock(Real alpha=100.0)
void getRosenbrock(Teuchos::RCP< Objective< Real > > &obj, Vector< Real > &x0, Vector< Real > &x)