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_Objective.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  Teuchos::RCP<Vector<Real> > k_;
99 
100  public:
101 
102  // Create using a ROL::Vector containing 1,2,3,...,n
103  Objective_Zakharov(const Teuchos::RCP<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 void getZakharov( Teuchos::RCP<Objective<Real> > &obj, Vector<Real> &x0, Vector<Real> &x ) {
168 
169  using Teuchos::RCP;
170  using Teuchos::rcp;
171  using Teuchos::dyn_cast;
172 
173  typedef std::vector<Real> vector;
174  typedef Vector<Real> V;
175  typedef StdVector<Real> SV;
176 
177  typedef typename vector::size_type uint;
178 
179  // Cast Initial Guess and Solution Vectors
180  RCP<vector> x0p = dyn_cast<SV>(x0).getVector();
181  RCP<vector> xp = dyn_cast<SV>(x).getVector();
182 
183  uint n = xp->size();
184  // Resize Vectors
185  n = 10;
186 
187  RCP<vector> k_rcp = rcp(new vector(n,0));
188  for(uint i=0;i<n;++i) {
189  (*k_rcp)[i] = i+1.0;
190  }
191  RCP<V> k = rcp(new SV(k_rcp));
192  x0p->resize(n);
193  xp->resize(n);
194  // Instantiate Objective Function
195  obj = rcp( new Objective_Zakharov<Real>(k) );
196  // Get Initial Guess
197  (*x0p)[0] = 3.0;
198  (*x0p)[1] = 3.0;
199  // Get Solution
200  (*xp)[0] = 0.0;
201  (*xp)[1] = 0.0;
202  }
203 
204 
205 }// End ZOO Namespace
206 }// End ROL Namespace
207 
208 #endif
Provides the interface to evaluate objective functions.
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:143
void invHessVec(Vector< Real > &ihv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply inverse Hessian approximation to vector.
Objective_Zakharov(const Teuchos::RCP< Vector< Real > > k)
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
void getZakharov(Teuchos::RCP< Objective< Real > > &obj, Vector< Real > &x0, Vector< Real > &x)
Teuchos::RCP< Vector< Real > > k_
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
virtual Real dot(const Vector &x) const =0
Compute where .
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Provides the std::vector implementation of the ROL::Vector interface.
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:196