ROL
ROL_HS45.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 #ifndef USE_HESSVEC
50 #define USE_HESSVEC 1
51 #endif
52 
53 #ifndef ROL_HS45_HPP
54 #define ROL_HS45_HPP
55 
56 #include "ROL_StdVector.hpp"
57 #include "ROL_Objective.hpp"
59 #include "ROL_Types.hpp"
60 
61 namespace ROL {
62 namespace ZOO {
63 
66  template<class Real>
67  class Objective_HS45 : public Objective<Real> {
68  private:
69  int dim_;
70  Real fact_;
71 
72  public:
73  Objective_HS45(int dim = 5) : dim_(dim) {
74  fact_ = 1.0;
75  for ( int i = 0; i < this->dim_; i++ ) {
76  fact_ *= (Real)(i+1);
77  }
78  }
79 
80  Real value( const Vector<Real> &x, Real &tol ) {
81  Teuchos::RCP<const std::vector<Real> > ex =
82  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
83  Real prod = 1.0;
84  for ( int i = 0; i < this->dim_; i++ ) {
85  prod *= (*ex)[i];
86  }
87  return 2.0 - prod/this->fact_;
88  }
89 
90  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
91  Teuchos::RCP<const std::vector<Real> > ex =
92  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
93  Teuchos::RCP<std::vector<Real> > eg =
94  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(g)).getVector());
95  Real prod = 1.0;
96  for ( int j = 0; j < this->dim_; j++ ) {
97  for ( int i = 0; i < this->dim_; i++ ) {
98  if ( j != i ) {
99  prod *= (*ex)[i];
100  }
101  }
102  (*eg)[j] = -prod/this->fact_;
103  prod = 1.0;
104  }
105  }
106 #if USE_HESSVEC
107  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
108  Teuchos::RCP<const std::vector<Real> > ex =
109  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
110  Teuchos::RCP<const std::vector<Real> > ev =
111  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
112  Teuchos::RCP<std::vector<Real> > ehv =
113  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(hv)).getVector());
114  hv.zero();
115  Real prod = 1.0;
116  for ( int l = 0; l < this->dim_; l++ ) {
117  for ( int j = 0; j < this->dim_; j++ ) {
118  if ( l != j ) {
119  for ( int i = 0; i < this->dim_; i++ ) {
120  if ( j != i && l != i ) {
121  prod *= (*ex)[i];
122  }
123  }
124  (*ehv)[l] += -prod/this->fact_*(*ev)[j];
125  }
126  prod = 1.0;
127  }
128  }
129  }
130 #endif
131  };
132 
133  template<class Real>
134  void getHS45( Teuchos::RCP<Objective<Real> > &obj, Teuchos::RCP<BoundConstraint<Real> > &con,
135  Vector<Real> &x0, Vector<Real> &x ) {
136  // Cast Initial Guess and Solution Vectors
137  Teuchos::RCP<std::vector<Real> > x0p =
138  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(x0)).getVector());
139  Teuchos::RCP<std::vector<Real> > xp =
140  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(x)).getVector());
141  int n = xp->size();
142  // Resize Vectors
143  n = 5;
144  x0p->resize(n);
145  xp->resize(n);
146  // Instantiate Objective Function
147  obj = Teuchos::rcp( new Objective_HS45<Real>(n) );
148  // Instantiate BoundConstraint
149  std::vector<Real> l(n,0.0);
150  std::vector<Real> u(n,0.0);
151  for ( int i = 0; i < n; i++ ) {
152  l[i] = 0.0;
153  u[i] = (Real)(i+1);
154  }
155  con = Teuchos::rcp( new StdBoundConstraint<Real>(l,u) );
156  // Get Initial Guess
157  for ( int i = 0; i < n; i++ ) {
158  (*x0p)[i] = 2.0;
159  }
160  con->project(x0);
161  // Get Solution
162  for ( int i = 0; i < n; i++ ) {
163  (*xp)[i] = (Real)(i+1);
164  }
165  }
166 
167 
168 } // End ZOO Namespace
169 } // End ROL Namespace
170 
171 #endif
Contains definitions for std::vector bound constraints.
Provides the interface to evaluate objective functions.
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS45.hpp:80
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Contains definitions of custom data types in ROL.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
Provides the std::vector implementation of the ROL::Vector interface.
void getHS45(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< BoundConstraint< Real > > &con, Vector< Real > &x0, Vector< Real > &x)
Definition: ROL_HS45.hpp:134
Provides the interface to apply upper and lower bound constraints.
W. Hock and K. Schittkowski 45th test function.
Definition: ROL_HS45.hpp:67
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS45.hpp:90
Objective_HS45(int dim=5)
Definition: ROL_HS45.hpp:73