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_TestProblem.hpp"
58 #include "ROL_Bounds.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 
69  typedef std::vector<Real> vector;
70  typedef Vector<Real> V;
72 
73  typedef typename vector::size_type uint;
74 
75  private:
77  Real fact_;
78 
79  ROL::Ptr<const vector> getVector( const V& x ) {
80 
81  return dynamic_cast<const SV&>(x).getVector();
82  }
83 
84  ROL::Ptr<vector> getVector( V& x ) {
85 
86  return dynamic_cast<SV&>(x).getVector();
87  }
88 
89  public:
90  Objective_HS45(int dim = 5) : dim_((uint)dim) {
91  fact_ = 1.0;
92  for ( uint i = 0; i < dim_; i++ ) {
93  fact_ *= (Real)(i+1);
94  }
95  }
96 
97  Real value( const Vector<Real> &x, Real &tol ) {
98 
99  ROL::Ptr<const vector> ex = getVector(x);
100  Real prod = 1.0;
101  for ( uint i = 0; i < dim_; i++ ) {
102  prod *= (*ex)[i];
103  }
104  return 2.0 - prod/fact_;
105  }
106 
107  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
108 
109  ROL::Ptr<const vector> ex = getVector(x);
110  ROL::Ptr<vector> eg = getVector(g);
111 
112  Real prod = 1.0;
113  for ( uint j = 0; j < dim_; j++ ) {
114  for ( uint i = 0; i < dim_; i++ ) {
115  if ( j != i ) {
116  prod *= (*ex)[i];
117  }
118  }
119  (*eg)[j] = -prod/fact_;
120  prod = 1.0;
121  }
122  }
123 #if USE_HESSVEC
124  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
125 
126 
127  ROL::Ptr<const vector> ex = getVector(x);
128  ROL::Ptr<const vector> ev = getVector(v);
129  ROL::Ptr<vector> ehv = getVector(hv);
130 
131  hv.zero();
132  Real prod = 1.0;
133  for ( uint l = 0; l < dim_; l++ ) {
134  for ( uint j = 0; j < dim_; j++ ) {
135  if ( l != j ) {
136  for ( uint i = 0; i < dim_; i++ ) {
137  if ( j != i && l != i ) {
138  prod *= (*ex)[i];
139  }
140  }
141  (*ehv)[l] += -prod/fact_*(*ev)[j];
142  }
143  prod = 1.0;
144  }
145  }
146  }
147 #endif
148  };
149 
150 template<class Real>
151 class getHS45 : public TestProblem<Real> {
152 public:
153  getHS45(void) {}
154 
155  Ptr<Objective<Real>> getObjective(void) const {
156  // Problem dimension
157  int n = 10;
158  // Instantiate Objective Function
159  return ROL::makePtr<Objective_HS45<Real>>(n);
160  }
161 
162  Ptr<Vector<Real>> getInitialGuess(void) const {
163  // Problem dimension
164  int n = 10;
165  // Get Initial Guess
166  ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,0.0);
167  for ( int i = 0; i < n; i++ ) {
168  (*x0p)[i] = 2.0;
169  }
170  return ROL::makePtr<StdVector<Real>>(x0p);
171  }
172 
173  Ptr<Vector<Real>> getSolution(const int i = 0) const {
174  // Problem dimension
175  int n = 10;
176  // Get Solution
177  ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
178  for ( int i = 0; i < n; i++ ) {
179  (*xp)[i] = (Real)(i+1);
180  }
181  return ROL::makePtr<StdVector<Real>>(xp);
182  }
183 
184  Ptr<BoundConstraint<Real>> getBoundConstraint(void) const {
185  // Problem dimension
186  int n = 10;
187  // Instantiate BoundConstraint
188  ROL::Ptr<std::vector<Real> > lp = ROL::makePtr<std::vector<Real>>(n,0.0);
189  ROL::Ptr<std::vector<Real> > up = ROL::makePtr<std::vector<Real>>(n,0.0);
190  for ( int i = 0; i < n; i++ ) {
191  (*lp)[i] = 0.0;
192  (*up)[i] = static_cast<Real>(i+1);
193  }
194  ROL::Ptr<Vector<Real> > l = ROL::makePtr<StdVector<Real>>(lp);
195  ROL::Ptr<Vector<Real> > u = ROL::makePtr<StdVector<Real>>(up);
196  return ROL::makePtr<Bounds<Real>>(l,u);
197  }
198 };
199 
200 
201 } // End ZOO Namespace
202 } // End ROL Namespace
203 
204 #endif
Provides the interface to evaluate objective functions.
typename PV< Real >::size_type size_type
Ptr< Objective< Real > > getObjective(void) const
Definition: ROL_HS45.hpp:155
Ptr< BoundConstraint< Real > > getBoundConstraint(void) const
Definition: ROL_HS45.hpp:184
std::vector< Real > vector
Definition: ROL_HS45.hpp:69
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS45.hpp:97
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.
vector::size_type uint
Definition: ROL_HS45.hpp:73
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:167
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
ROL::Ptr< vector > getVector(V &x)
Definition: ROL_HS45.hpp:84
Contains definitions of test objective functions.
W. Hock and K. Schittkowski 45th test function.
Definition: ROL_HS45.hpp:67
StdVector< Real > SV
Definition: ROL_HS45.hpp:71
Vector< Real > V
Definition: ROL_HS45.hpp:70
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS45.hpp:107
Ptr< Vector< Real > > getInitialGuess(void) const
Definition: ROL_HS45.hpp:162
ROL::Ptr< const vector > getVector(const V &x)
Definition: ROL_HS45.hpp:79
constexpr auto dim
Ptr< Vector< Real > > getSolution(const int i=0) const
Definition: ROL_HS45.hpp:173
Objective_HS45(int dim=5)
Definition: ROL_HS45.hpp:90