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"
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  private:
69  std::vector<Real> u_vec_;
70  int u_size_;
71 
72  public:
74  u_size_ = 99;
75  for ( int i = 0; i < u_size_; i++ ) {
76  u_vec_.push_back(25.0 + std::pow((-50.0*std::log(0.01*(Real)(i+1))),2.0/3.0));
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 
84  Real val = 0.0, f = 0.0;
85  for ( int i = 0; i < this->u_size_; i++ ) {
86  f = -0.01*(Real)(i+1) + std::exp(-1.0/(*ex)[0] * std::pow(this->u_vec_[i]-(*ex)[1],(*ex)[2]));
87  val += f*f;
88  }
89  return val;
90  }
91 
92  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
93  Teuchos::RCP<const std::vector<Real> > ex =
94  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
95  Teuchos::RCP<std::vector<Real> > eg =
96  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(g)).getVector());
97  g.zero();
98 
99  Real f = 0.0, df1 = 0.0, df2 = 0.0, df3 = 0.0, tmp = 0.0;
100  for ( int i = 0; i < this->u_size_; i++ ) {
101  tmp = std::pow(this->u_vec_[i]-(*ex)[1],(*ex)[2])/(*ex)[0];
102  f = -0.01*(Real)(i+1) + std::exp(-tmp);
103  df1 = std::exp(-tmp)*tmp/(*ex)[0];
104  df2 = std::exp(-tmp)*(*ex)[2]*std::pow(this->u_vec_[i]-(*ex)[1],(*ex)[2]-1.0)/(*ex)[0];
105  df3 = std::exp(-tmp)*tmp*std::log(this->u_vec_[i]-(*ex)[1]);
106  (*eg)[0] += 2.0*f*df1;
107  (*eg)[1] += 2.0*f*df2;
108  (*eg)[2] += 2.0*f*df3;
109  }
110  }
111 #if USE_HESSVEC
112  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
113  Teuchos::RCP<const std::vector<Real> > ex =
114  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
115  Teuchos::RCP<const std::vector<Real> > ev =
116  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
117  Teuchos::RCP<std::vector<Real> > ehv =
118  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(hv)).getVector());
119  hv.zero();
120  }
121 #endif
122  void invHessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
123  Teuchos::RCP<const std::vector<Real> > ex =
124  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(x))).getVector();
125  Teuchos::RCP<const std::vector<Real> > ev =
126  (Teuchos::dyn_cast<StdVector<Real> >(const_cast<Vector<Real> &>(v))).getVector();
127  Teuchos::RCP<std::vector<Real> > ehv =
128  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(hv)).getVector());
129  hv.zero();
130  }
131  };
132 
133  template<class Real>
134  void getHS25( 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 = 3;
144  x0p->resize(n);
145  xp->resize(n);
146  // Instantiate Objective Function
147  obj = Teuchos::rcp( new Objective_HS25<Real> );
148  // Instantiate BoundConstraint
149  std::vector<Real> l, u;
150  l.push_back(0.1);
151  l.push_back(0.0);
152  l.push_back(0.0);
153  u.push_back(100.0);
154  u.push_back(25.6);
155  u.push_back(5.0);
156  con = Teuchos::rcp( new StdBoundConstraint<Real>(l,u) );
157  // Get Initial Guess
158  (*x0p)[0] = 100.0;
159  (*x0p)[1] = 12.5;
160  (*x0p)[2] = 3.0;
161  // Get Solution
162  (*xp)[0] = 50.0;
163  (*xp)[1] = 25.0;
164  (*xp)[2] = 1.5;
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.
void getHS25(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< BoundConstraint< Real > > &con, Vector< Real > &x0, Vector< Real > &x)
Definition: ROL_HS25.hpp:134
std::vector< Real > u_vec_
Definition: ROL_HS25.hpp:69
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.
virtual void zero()
Set to zero vector.
Definition: ROL_Vector.hpp:155
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Definition: ROL_HS25.hpp:80
Provides the std::vector implementation of the ROL::Vector interface.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS25.hpp:92
W. Hock and K. Schittkowski 25th test function.
Definition: ROL_HS25.hpp:67
Provides the interface to apply upper and lower bound constraints.
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:122