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_StdVector.hpp"
87 #include "ROL_Objective.hpp"
88 
89 namespace ROL {
90 namespace ZOO {
91 
94  template<class Real>
95  class Objective_Zakharov : public Objective<Real> {
96  private:
97  Teuchos::RCP<Vector<Real> > k_;
98 
99  public:
100 
101  // Create using a ROL::Vector containing 1,2,3,...,n
102  Objective_Zakharov(const Teuchos::RCP<Vector<Real> > k) : k_(k) {}
103 
104  Real value( const Vector<Real> &x, Real &tol ) {
105 
106  Real xdotx = x.dot(x);
107  Real kdotx = x.dot(*k_);
108 
109  Real val = xdotx + pow(kdotx,2)/4.0 + pow(kdotx,4)/16.0;
110 
111  return val;
112  }
113 
114  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
115 
116  Real kdotx = x.dot(*k_);
117  Real coeff = 0.25*(2.0*kdotx+pow(kdotx,3.0));
118 
119  g.set(x);
120  g.scale(2.0);
121  g.axpy(coeff,*k_);
122  }
123 
124 #if USE_HESSVEC
125  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
126 
127  Real kdotx = x.dot(*k_);
128  Real kdotv = v.dot(*k_);
129  Real coeff = 0.25*(2.0+3.0*pow(kdotx,2.0))*kdotv;
130 
131  hv.set(v);
132  hv.scale(2.0);
133  hv.axpy(coeff,*k_);
134  }
135 #endif
136  void invHessVec( Vector<Real> &ihv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
137 
138  Real kdotv = v.dot(*k_);
139  Real kdotx = x.dot(*k_);
140  Real kdotk = (*k_).dot(*k_);
141  Real coeff = -kdotv/(2.0*kdotk+16.0/(2.0+3.0*pow(kdotx,2.0)));
142 
143  ihv.set(v);
144  ihv.scale(0.5);
145  ihv.axpy(coeff,*k_);
146  }
147 };
148 
149 
150 
151 template<class Real>
152 void getZakharov( Teuchos::RCP<Objective<Real> > &obj, Vector<Real> &x0, Vector<Real> &x ) {
153  // Cast Initial Guess and Solution Vectors
154  Teuchos::RCP<std::vector<Real> > x0p =
155  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(x0)).getVector());
156  Teuchos::RCP<std::vector<Real> > xp =
157  Teuchos::rcp_const_cast<std::vector<Real> >((Teuchos::dyn_cast<StdVector<Real> >(x)).getVector());
158  int n = xp->size();
159  // Resize Vectors
160  n = 10;
161 
162  Teuchos::RCP<std::vector<Real> > k_rcp = Teuchos::rcp(new std::vector<Real>(n,0));
163  for(int i=0;i<n;++i) {
164  (*k_rcp)[i] = i+1.0;
165  }
166  Teuchos::RCP<Vector<Real> > k = Teuchos::rcp(new StdVector<Real>(k_rcp));
167  x0p->resize(n);
168  xp->resize(n);
169  // Instantiate Objective Function
170  obj = Teuchos::rcp( new Objective_Zakharov<Real>(k) );
171  // Get Initial Guess
172  (*x0p)[0] = 3.0;
173  (*x0p)[1] = 3.0;
174  // Get Solution
175  (*xp)[0] = 0.0;
176  (*xp)[1] = 0.0;
177  }
178 
179 
180 }// End ZOO Namespace
181 }// End ROL Namespace
182 
183 #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:141
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:72
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.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:194