ROL
ROL_HS25.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_HS25_HPP
54 #define ROL_HS25_HPP
55 
56 #include "ROL_StdVector.hpp"
57 #include "ROL_Objective.hpp"
58 #include "ROL_BoundConstraint.hpp"
59 #include "ROL_Types.hpp"
60 
61 namespace ROL {
62 namespace ZOO {
63 
66  template<class Real>
67  class Objective_HS25 : public Objective<Real> {
68 
69  typedef std::vector<Real> vector;
70  typedef Vector<Real> V;
71  typedef StdVector<Real> SV;
72 
73  typedef typename vector::size_type uint;
74 
75  private:
76  std::vector<Real> u_vec_;
77  uint u_size_;
78 
79  Teuchos::RCP<const vector> getVector( const V& x ) {
80  using Teuchos::dyn_cast;
81  return dyn_cast<const SV>(x).getVector();
82  }
83 
84  Teuchos::RCP<vector> getVector( V& x ) {
85  using Teuchos::dyn_cast;
86  return dyn_cast<SV>(x).getVector();
87  }
88 
89  public:
91  u_size_ = 99;
92  for ( uint i = 0; i < u_size_; i++ ) {
93  u_vec_.push_back(25.0 + std::pow((-50.0*std::log(0.01*(Real)(i+1))),2.0/3.0));
94  }
95  }
96 
97  Real value( const Vector<Real> &x, Real &tol ) {
98 
99  using Teuchos::RCP;
100  RCP<const vector> ex = getVector(x);
101 
102  Real val = 0.0, f = 0.0;
103  for ( uint i = 0; i < u_size_; i++ ) {
104  f = -0.01*(Real)(i+1) + std::exp(-1.0/(*ex)[0] * std::pow(u_vec_[i]-(*ex)[1],(*ex)[2]));
105  val += f*f;
106  }
107  return val;
108  }
109 
110  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
111 
112  using Teuchos::RCP;
113  RCP<const vector> ex = getVector(x);
114  RCP<vector> eg = getVector(g);
115  g.zero();
116 
117  Real f = 0.0, df1 = 0.0, df2 = 0.0, df3 = 0.0, tmp = 0.0;
118  for ( uint i = 0; i < u_size_; i++ ) {
119  tmp = std::pow(u_vec_[i]-(*ex)[1],(*ex)[2])/(*ex)[0];
120  f = -0.01*(Real)(i+1) + std::exp(-tmp);
121  df1 = std::exp(-tmp)*tmp/(*ex)[0];
122  df2 = std::exp(-tmp)*(*ex)[2]*std::pow(u_vec_[i]-(*ex)[1],(*ex)[2]-1.0)/(*ex)[0];
123  df3 = std::exp(-tmp)*tmp*std::log(u_vec_[i]-(*ex)[1]);
124  (*eg)[0] += 2.0*f*df1;
125  (*eg)[1] += 2.0*f*df2;
126  (*eg)[2] += 2.0*f*df3;
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> ex = getVector(x);
134  RCP<const vector> ev = getVector(v);
135  RCP<vector> ehv = getVector(hv);
136  }
137 #endif
138  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
139 
140  using Teuchos::RCP;
141  RCP<const vector> ex = getVector(x);
142  RCP<const vector> ev = getVector(v);
143  RCP<vector> ehv = getVector(hv);
144  hv.zero();
145  }
146  };
147 
148  template<class Real>
149  void getHS25( Teuchos::RCP<Objective<Real> > &obj, Teuchos::RCP<BoundConstraint<Real> > &con,
150  Vector<Real> &x0, Vector<Real> &x ) {
151 
152  typedef std::vector<Real> vector;
153  typedef ROL::Vector<Real> V;
154  typedef ROL::StdVector<Real> SV;
155  using Teuchos::RCP;
156  using Teuchos::rcp;
157  using Teuchos::dyn_cast;
158 
159  // Cast Initial Guess and Solution Vectors
160  RCP<vector> x0p = dyn_cast<SV>(x0).getVector();
161  RCP<vector> xp = dyn_cast<SV>(x).getVector();
162 
163  uint n = xp->size();
164  // Resize Vectors
165  n = 3;
166  x0p->resize(n);
167  xp->resize(n);
168  // Instantiate Objective Function
169  obj = rcp( new Objective_HS25<Real> );
170  // Instantiate BoundConstraint
171 
172  RCP<vector> l_rcp = rcp( new vector(n,0.0) );
173  RCP<vector> u_rcp = rcp( new vector(n,0.0) );
174  l_rcp->push_back(0.1);
175  l_rcp->push_back(0.0);
176  l_rcp->push_back(0.0);
177  u_rcp->push_back(100.0);
178  u_rcp->push_back(25.6);
179  u_rcp->push_back(5.0);
180 
181  RCP<V> l = rcp( new SV(l_rcp) );
182  RCP<V> u = rcp( new SV(u_rcp) );
183 
184  con = rcp( new BoundConstraint<Real>(l,u) );
185 
186  // Get Initial Guess
187  (*x0p)[0] = 100.0;
188  (*x0p)[1] = 12.5;
189  (*x0p)[2] = 3.0;
190 
191  // Get Solution
192  (*xp)[0] = 50.0;
193  (*xp)[1] = 25.0;
194  (*xp)[2] = 1.5;
195  }
196 
197 
198 } // End ZOO Namespace
199 } // End ROL Namespace
200 
201 #endif
Provides the interface to evaluate objective functions.
void getHS25(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< BoundConstraint< Real > > &con, Vector< Real > &x0, Vector< Real > &x)
Definition: ROL_HS25.hpp:149
vector::size_type uint
Definition: ROL_HS25.hpp:73
Vector< Real > V
Definition: ROL_HS25.hpp:70
std::vector< Real > u_vec_
Definition: ROL_HS25.hpp:76
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.
StdVector< Real > SV
Definition: ROL_HS25.hpp:71
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:157
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS25.hpp:97
std::vector< Real > vector
Definition: ROL_HS25.hpp:69
Provides the std::vector implementation of the ROL::Vector interface.
Teuchos::RCP< vector > getVector(V &x)
Definition: ROL_HS25.hpp:84
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS25.hpp:110
W. Hock and K. Schittkowski 25th test function.
Definition: ROL_HS25.hpp:67
Provides the interface to apply upper and lower bound constraints.
Teuchos::RCP< const vector > getVector(const V &x)
Definition: ROL_HS25.hpp:79
void invHessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Definition: ROL_HS25.hpp:138