ROL
ROL_Zakharov.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 
79 #ifndef USE_HESSVEC
80 #define USE_HESSVEC 1
81 #endif
82 
83 #ifndef ROL_ZAKHAROV_HPP
84 #define ROL_ZAKHAROV_HPP
85 
86 #include "ROL_TestProblem.hpp"
87 #include "ROL_StdVector.hpp"
88 
89 
90 namespace ROL {
91 namespace ZOO {
92 
95 template<class Real>
96 class Objective_Zakharov : public Objective<Real> {
97 private:
98  ROL::Ptr<Vector<Real> > k_;
99 
100 public:
101 
102  // Create using a ROL::Vector containing 1,2,3,...,n
103  Objective_Zakharov(const ROL::Ptr<Vector<Real> > k) : k_(k) {}
104 
105  Real value( const Vector<Real> &x, Real &tol ) {
106 
107  Real xdotx = x.dot(x);
108  Real kdotx = x.dot(*k_);
109 
110  Real val = xdotx + pow(kdotx,2)/4.0 + pow(kdotx,4)/16.0;
111 
112  return val;
113  }
114 
115  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
116 
117  Real kdotx = x.dot(*k_);
118  Real coeff = 0.25*(2.0*kdotx+pow(kdotx,3.0));
119 
120  g.set(x);
121  g.scale(2.0);
122  g.axpy(coeff,*k_);
123  }
124 
125  Real dirDeriv( const Vector<Real> &x, const Vector<Real> &d, Real &tol ) {
126 
127  Real kdotd = d.dot(*k_);
128  Real kdotx = x.dot(*k_);
129  Real xdotd = x.dot(d);
130 
131  Real coeff = 0.25*(2.0*kdotx+pow(kdotx,3.0));
132 
133  Real deriv = 2*xdotd + coeff*kdotd;
134 
135  return deriv;
136 
137  }
138 
139 #if USE_HESSVEC
140  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
141 
142  Real kdotx = x.dot(*k_);
143  Real kdotv = v.dot(*k_);
144  Real coeff = 0.25*(2.0+3.0*pow(kdotx,2.0))*kdotv;
145 
146  hv.set(v);
147  hv.scale(2.0);
148  hv.axpy(coeff,*k_);
149  }
150 #endif
151  void invHessVec( Vector<Real> &ihv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
152 
153  Real kdotv = v.dot(*k_);
154  Real kdotx = x.dot(*k_);
155  Real kdotk = (*k_).dot(*k_);
156  Real coeff = -kdotv/(2.0*kdotk+16.0/(2.0+3.0*pow(kdotx,2.0)));
157 
158  ihv.set(v);
159  ihv.scale(0.5);
160  ihv.axpy(coeff,*k_);
161  }
162 };
163 
164 
165 
166 template<class Real>
167 class getZakharov : public TestProblem<Real> {
168 public:
169  getZakharov(void) {}
170 
171  Ptr<Objective<Real>> getObjective(void) const {
172  // Problem dimension
173  int n = 10;
174  // Instantiate Objective Function
175  ROL::Ptr<std::vector<Real> > k_ptr = ROL::makePtr<std::vector<Real>>(n,0.0);
176  for ( int i = 0; i < n; i++ ) {
177  (*k_ptr)[i] = i+1.0;
178  }
179  ROL::Ptr<Vector<Real> > k = ROL::makePtr<StdVector<Real>>(k_ptr);
180  return ROL::makePtr<Objective_Zakharov<Real>>(k);
181  }
182 
183  Ptr<Vector<Real>> getInitialGuess(void) const {
184  // Problem dimension
185  int n = 10;
186  // Get Initial Guess
187  ROL::Ptr<std::vector<Real> > x0p = ROL::makePtr<std::vector<Real>>(n,3.0);
188  return ROL::makePtr<StdVector<Real>>(x0p);
189  }
190 
191  Ptr<Vector<Real>> getSolution(const int i = 0) const {
192  // Problem dimension
193  int n = 10;
194  // Get Solution
195  ROL::Ptr<std::vector<Real> > xp = ROL::makePtr<std::vector<Real>>(n,0.0);
196  return ROL::makePtr<StdVector<Real>>(xp);
197  }
198 };
199 
200 
201 }// End ZOO Namespace
202 }// End ROL Namespace
203 
204 #endif
Provides the interface to evaluate objective functions.
Objective_Zakharov(const ROL::Ptr< Vector< Real > > k)
virtual void scale(const Real alpha)=0
Compute where .
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
virtual void axpy(const Real alpha, const Vector &x)
Compute where .
Definition: ROL_Vector.hpp:153
void invHessVec(Vector< Real > &ihv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Ptr< Vector< Real > > getSolution(const int i=0) const
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
ROL::Ptr< Vector< Real > > k_
virtual Real dot(const Vector &x) const =0
Compute where .
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Ptr< Objective< Real > > getObjective(void) const
Ptr< Vector< Real > > getInitialGuess(void) const
Contains definitions of test objective functions.
Real dirDeriv(const Vector< Real > &x, const Vector< Real > &d, Real &tol)
Compute directional derivative.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209