ROL
ROL_LeastSquares.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_LEASTSQUARES_HPP
54 #define ROL_LEASTSQUARES_HPP
55 
56 #include "ROL_StdVector.hpp"
57 #include "ROL_Objective.hpp"
58 
59 namespace ROL {
60 namespace ZOO {
61 
64  template<class Real>
65  class Objective_LeastSquares : public Objective<Real> {
66 
67  typedef std::vector<Real> vector;
68  typedef Vector<Real> V;
69  typedef StdVector<Real> SV;
70 
71  typedef typename vector::size_type uint;
72 
73  private:
74 
75  Teuchos::RCP<const vector> getVector( const V& x ) {
76  using Teuchos::dyn_cast;
77  return dyn_cast<const SV>(x).getVector();
78  }
79 
80  Teuchos::RCP<vector> getVector( V& x ) {
81  using Teuchos::dyn_cast;
82  return dyn_cast<SV>(x).getVector();
83  }
84 
85  public:
86 
87  Real value( const Vector<Real> &x, Real &tol ) {
88  using Teuchos::RCP;
89  RCP<const vector> xp = getVector(x);
90 
91  uint n = xp->size();
92  Real h = 1.0/((Real)n+1.0);
93  Real val = 0.0;
94  Real res = 0.0;
95  for (uint i=0; i<n; i++) {
96  if ( i == 0 ) {
97  res = 2.0*h*(5.0/6.0) + 1.0/h*((*xp)[i+1]-2.0*(*xp)[i]);
98  }
99  else if ( i == n-1 ) {
100  res = 2.0*h*(5.0/6.0) + 1.0/h*((*xp)[i-1]-2.0*(*xp)[i]);
101  }
102  else {
103  res = 2.0*h + 1.0/h*((*xp)[i-1]-2.0*(*xp)[i]+(*xp)[i+1]);
104  }
105  val += 0.5*res*res;
106  }
107 
108  return val;
109  }
110 
111  void gradient( Vector<Real> &g, const Vector<Real> &x, Real &tol ) {
112 
113  using Teuchos::RCP;
114  RCP<const vector> xp = getVector(x);
115  RCP<vector> gp = getVector(g);
116 
117  uint n = xp->size();
118  Real h = 1.0/((Real)n+1.0);
119  vector res(n,0.0);
120  for (uint i=0; i<n; i++) {
121  if ( i == 0 ) {
122  res[i] = 2.0*h*(5.0/6.0) + 1.0/h*((*xp)[i+1]-2.0*(*xp)[i]);
123  }
124  else if ( i == n-1 ) {
125  res[i] = 2.0*h*(5.0/6.0) + 1.0/h*((*xp)[i-1]-2.0*(*xp)[i]);
126  }
127  else {
128  res[i] = 2.0*h + 1.0/h*((*xp)[i-1]-2.0*(*xp)[i]+(*xp)[i+1]);
129  }
130  }
131 
132  for (uint i=0; i<n; i++) {
133  if ( i == 0 ) {
134  (*gp)[i] = 1.0/h*(res[i+1]-2.0*res[i]);
135  }
136  else if ( i == n-1 ) {
137  (*gp)[i] = 1.0/h*(res[i-1]-2.0*res[i]);
138  }
139  else {
140  (*gp)[i] = 1.0/h*(res[i-1]-2.0*res[i]+res[i+1]);
141  }
142  }
143  }
144 #if USE_HESSVEC
145  void hessVec( Vector<Real> &hv, const Vector<Real> &v, const Vector<Real> &x, Real &tol ) {
146 
147  using Teuchos::RCP;
148  RCP<const vector> xp = getVector(x);
149  RCP<const vector> vp = getVector(v);
150  RCP<vector> hvp = getVector(hv);
151 
152  uint n = xp->size();
153  Real h = 1.0/((Real)n+1.0);
154  vector res(n,0.0);
155  for (uint i=0; i<n; i++) {
156  if ( i == 0 ) {
157  res[i] = 1.0/h*((*vp)[i+1]-2.0*(*vp)[i]);
158  }
159  else if ( i == n-1 ) {
160  res[i] = 1.0/h*((*vp)[i-1]-2.0*(*vp)[i]);
161  }
162  else {
163  res[i] = 1.0/h*((*vp)[i-1]-2.0*(*vp)[i]+(*vp)[i+1]);
164  }
165  }
166 
167  for (uint i=0; i<n; i++) {
168  if ( i == 0 ) {
169  (*hvp)[i] = 1.0/h*(res[i+1]-2.0*res[i]);
170  }
171  else if ( i == n-1 ) {
172  (*hvp)[i] = 1.0/h*(res[i-1]-2.0*res[i]);
173  }
174  else {
175  (*hvp)[i] = 1.0/h*(res[i-1]-2.0*res[i]+res[i+1]);
176  }
177  }
178  }
179 #endif
180  };
181 
182  template<class Real>
183  void getLeastSquares( Teuchos::RCP<Objective<Real> > &obj, Vector<Real> &x0, Vector<Real> &x ) {
184 
185  typedef std::vector<Real> vector;
186  typedef StdVector<Real> SV;
187 
188  typedef typename vector::size_type uint;
189 
190  using Teuchos::RCP;
191  using Teuchos::rcp;
192  using Teuchos::dyn_cast;
193 
194  // Cast Initial Guess and Solution Vectors
195  RCP<vector> x0p = dyn_cast<SV>(x0).getVector();
196  RCP<vector> xp = dyn_cast<SV>(x).getVector();
197 
198  uint n = xp->size();
199 
200  // Resize Vectors
201  n = 32;
202  x0p->resize(n);
203  xp->resize(n);
204  // Instantiate Objective Function
205  obj = rcp( new Objective_LeastSquares<Real> );
206  // Get Initial Guess
207  for (uint i=0; i<n; i++) {
208  (*x0p)[i] = 0.0;
209  }
210  // Get Solution
211  Real h = 1.0/((Real)n+1.0);
212  Real pt = 0.0;
213  for( uint i=0; i<n; i++ ) {
214  pt = (Real)(i+1)*h;
215  (*xp)[i] = pt*(1.0-pt);
216  }
217  }
218 
219 } // End ZOO Namespace
220 } // End ROL Namespace
221 
222 #endif
Provides the interface to evaluate objective functions.
virtual void hessVec(Vector< Real > &hv, const Vector< Real > &v, const Vector< Real > &x, Real &tol)
Apply Hessian approximation to vector.
void gradient(Vector< Real > &g, const Vector< Real > &x, Real &tol)
Compute gradient.
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:74
Real value(const Vector< Real > &x, Real &tol)
Compute value.
Provides the std::vector implementation of the ROL::Vector interface.
void getLeastSquares(Teuchos::RCP< Objective< Real > > &obj, Vector< Real > &x0, Vector< Real > &x)
Teuchos::RCP< vector > getVector(V &x)
Teuchos::RCP< const vector > getVector(const V &x)