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_ScaledStdVector.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 typename std::vector<Real>::size_type uint;
70 
71 private:
72  std::vector<Real> u_vec_;
73  uint u_size_;
74 
75 public:
77  u_size_ = 99;
78  for ( uint i = 0; i < u_size_; i++ ) {
79  u_vec_.push_back(25.0 + std::pow((-50.0*std::log(0.01*(Real)(i+1))),2.0/3.0));
80  }
81  }
82 
83  Real value( const Vector<Real> &x, Real &tol ) {
84  Teuchos::RCP<const std::vector<Real> > ex
85  = Teuchos::dyn_cast<const PrimalScaledStdVector<Real> >(x).getVector();
86 
87  Real val = 0.0, f = 0.0, u = 0.0;
88  Real x1 = (*ex)[0], x2 = (*ex)[1], x3 = (*ex)[2];
89  for ( uint i = 0; i < u_size_; i++ ) {
90  u = u_vec_[i];
91  f = -0.01*(Real)(i+1) + std::exp(-std::pow(u-x2,x3)/x1);
92  val += f*f;
93  }
94  return val;
95  }
96 
97  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
98  Teuchos::RCP<std::vector<Real> > eg
99  = Teuchos::dyn_cast<DualScaledStdVector<Real> >(g).getVector();
100  Teuchos::RCP<const std::vector<Real> > ex
101  = Teuchos::dyn_cast<const PrimalScaledStdVector<Real> >(x).getVector();
102  g.zero();
103 
104  Real f = 0.0, df1 = 0.0, df2 = 0.0, df3 = 0.0;
105  Real u = 0.0, tmp = 0.0, tmp0 = 0.0, tmp1 = 0.0;
106  Real x1 = (*ex)[0], x2 = (*ex)[1], x3 = (*ex)[2];
107  Real x1sqr = x1*x1;
108  for ( uint i = 0; i < u_size_; i++ ) {
109  u = u_vec_[i];
110  tmp0 = std::pow(u-x2,x3);
111  tmp1 = std::pow(u-x2,x3-1);
112  tmp = std::exp(-tmp0/x1);
113 
114  f = -0.01*(Real)(i+1) + tmp;
115 
116  df1 = tmp*tmp0/x1sqr;
117  df2 = tmp*x3*tmp1/x1;
118  df3 = tmp*tmp0*std::log(u-x2)/x1;
119 
120  (*eg)[0] += 2.0*f*df1;
121  (*eg)[1] += 2.0*f*df2;
122  (*eg)[2] += 2.0*f*df3;
123  }
124  }
125 #if USE_HESSVEC
126  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
127  Teuchos::RCP<std::vector<Real> > ehv
128  = Teuchos::dyn_cast<DualScaledStdVector<Real> >(hv).getVector();
129  Teuchos::RCP<const std::vector<Real> > ev
130  = Teuchos::dyn_cast<const PrimalScaledStdVector<Real> >(v).getVector();
131  Teuchos::RCP<const std::vector<Real> > ex
132  = Teuchos::dyn_cast<const PrimalScaledStdVector<Real> >(x).getVector();
133  hv.zero();
134 
135  Real f, df1, df2, df3;
136  Real df11, df12, df13, df21, df22, df23, df31, df32, df33;
137  Real u, tmp, tmp0, tmp1, tmp2, tmp3, tmp4;
138  Real x1 = (*ex)[0], x2 = (*ex)[1], x3 = (*ex)[2];
139  Real v1 = (*ev)[0], v2 = (*ev)[1], v3 = (*ev)[2];
140  Real x1sqr = x1*x1, x1cub = x1sqr*x1, x1quar = x1cub*x1;
141  for ( uint i = 0; i < u_size_; i++ ) {
142  u = u_vec_[i];
143  tmp0 = std::pow(u-x2,x3);
144  tmp1 = std::pow(u-x2,x3-1);
145  tmp2 = std::pow(u-x2,2*(x3-1));
146  tmp3 = std::pow(u-x2,x3-2);
147  tmp4 = std::pow(u-x2,2*x3-1);
148  tmp = std::exp(-tmp0/x1);
149 
150  f = -0.01*(Real)(i+1) + tmp;
151 
152  df1 = tmp*tmp0/x1sqr;
153  df2 = tmp*x3*tmp1/x1;
154  df3 = tmp*tmp0*std::log(u-x2)/x1;
155 
156  df11 = tmp0*tmp*(tmp0-2.0*x1)/x1quar;
157  df12 = x3*tmp1*tmp*(tmp0-x1)/x1cub;
158  df13 = tmp0*std::log(u-x2)*tmp*(x1-tmp0)/x1cub;
159 
160  df21 = df12;
161  df22 = x3*x3*tmp2*tmp/(x1*x1) - (x3-1)*x3*tmp3*tmp/x1;
162  df23 = -x3*tmp4*std::log(u-x2)*tmp/x1sqr
163  +tmp1*tmp/x1 + x3*tmp1*std::log(u-x2)*tmp/x1;
164 
165  df31 = df13;
166  df32 = df23;
167  df33 = tmp0*std::pow(std::log(u-x2),2)*tmp*(tmp0-x1)/x1sqr;
168 
169  (*ehv)[0] += 2.0*(f*(df11*v1 + df12*v2 + df13*v3) + df1*(df1*v1 + df2*v2 + df3*v3));
170  (*ehv)[1] += 2.0*(f*(df21*v1 + df22*v2 + df23*v3) + df2*(df1*v1 + df2*v2 + df3*v3));
171  (*ehv)[2] += 2.0*(f*(df31*v1 + df32*v2 + df33*v3) + df3*(df1*v1 + df2*v2 + df3*v3));
172  }
173  }
174 #endif
175 };
176 
177 template<class Real>
178 void getHS25( Teuchos::RCP<Objective<Real> > &obj,
179  Teuchos::RCP<BoundConstraint<Real> > &con,
180  Teuchos::RCP<Vector<Real> > &x0,
181  Teuchos::RCP<Vector<Real> > &x ) {
182  // Problem dimension
183  int n = 3;
184 
185  // Set up vector scaling
186  Teuchos::RCP<std::vector<Real> > scale = Teuchos::rcp(new std::vector<Real>(n,0.0));
187  (*scale)[0] = 1.e-4; (*scale)[1] = 1.e-2; (*scale)[2] = 1.0;
188 
189  // Get Initial Guess
190  Teuchos::RCP<std::vector<Real> > x0p = Teuchos::rcp(new std::vector<Real>(n,0.0));
191  (*x0p)[0] = 100.0; (*x0p)[1] = 12.5; (*x0p)[2] = 3.0;
192  x0 = Teuchos::rcp(new PrimalScaledStdVector<Real>(x0p,scale));
193 
194  // Get Solution
195  Teuchos::RCP<std::vector<Real> > xp = Teuchos::rcp(new std::vector<Real>(n,0.0));
196  (*xp)[0] = 50.0; (*xp)[1] = 25.0; (*xp)[2] = 1.5;
197  x = Teuchos::rcp(new PrimalScaledStdVector<Real>(xp,scale));
198 
199  // Instantiate Objective Function
200  obj = Teuchos::rcp(new Objective_HS25<Real>);
201 
202  // Instantiate BoundConstraint
203  Teuchos::RCP<std::vector<Real> > lp = Teuchos::rcp(new std::vector<Real>(n,0.0));
204  (*lp)[0] = 0.1; (*lp)[1] = 0.0; (*lp)[2] = 0.0;
205  Teuchos::RCP<Vector<Real> > l = Teuchos::rcp(new StdVector<Real>(lp));
206  Teuchos::RCP<std::vector<Real> > up = Teuchos::rcp(new std::vector<Real>(n,0.0));
207  (*up)[0] = 100.0; (*up)[1] = 25.6; (*up)[2] = 5.0;
208  Teuchos::RCP<Vector<Real> > u = Teuchos::rcp(new StdVector<Real>(up));
209  con = Teuchos::rcp(new BoundConstraint<Real>(l,u));
210 }
211 
212 } // End ZOO Namespace
213 } // End ROL Namespace
214 
215 #endif
Provides the interface to evaluate objective functions.
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
std::vector< Real > u_vec_
Definition: ROL_HS25.hpp:72
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: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:83
std::vector< Real >::size_type uint
Definition: ROL_HS25.hpp:69
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Definition: ROL_HS25.hpp:97
W. Hock and K. Schittkowski 25th test function.
Definition: ROL_HS25.hpp:67
Provides the interface to apply upper and lower bound constraints.
Provides the std::vector implementation of the ROL::Vector interface that handles scalings in the inn...
void getHS25(Teuchos::RCP< Objective< Real > > &obj, Teuchos::RCP< BoundConstraint< Real > > &con, Teuchos::RCP< Vector< Real > > &x0, Teuchos::RCP< Vector< Real > > &x)
Definition: ROL_HS25.hpp:178