ROL
ROL_Secant.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 
44 #ifndef ROL_SECANT_H
45 #define ROL_SECANT_H
46 
51 #include "ROL_Types.hpp"
52 
53 namespace ROL {
54 
55 template<class Real>
56 struct SecantState {
57  std::vector<Teuchos::RCP<Vector<Real> > > iterDiff; // Step Storage
58  std::vector<Teuchos::RCP<Vector<Real> > > gradDiff; // Gradient Storage
59  std::vector<Real> product; // Step-Gradient Inner Product Storage
60  std::vector<Real> product2; // Step-Gradient Inner Product Storage
61  int storage; // Storage Size
62  int current; // Current Storage Size
63  int iter; // Current Optimization Iteration
64 };
65 
66 template<class Real>
67 class Secant {
68 private:
69 
70  Teuchos::RCP<SecantState<Real> > state_; // Secant State
71 
72 public:
73 
74  virtual ~Secant() {}
75 
76  // Constructor
77  Secant( int M = 10 ) {
78  state_ = Teuchos::rcp( new SecantState<Real> );
79  state_->storage = M;
80  state_->current = -1;
81  state_->iter = 0;
82  }
83 
84  Teuchos::RCP<SecantState<Real> >& get_state() { return state_; }
85 
86  // Update Secant Approximation
87  virtual void update( const Vector<Real> &grad, const Vector<Real> &gp, const Vector<Real> &s,
88  const Real snorm, const int iter ) {
89  state_->iter = iter;
90  Teuchos::RCP<Vector<Real> > gradDiff = grad.clone();
91  gradDiff->set(grad);
92  gradDiff->axpy(-1.0,gp);
93 
94  Real sy = s.dot(gradDiff->dual());
95  if (sy > ROL_EPSILON*snorm*snorm) {
96  if (state_->current < state_->storage-1) {
97  state_->current++; // Increment Storage
98  }
99  else {
100  state_->iterDiff.erase(state_->iterDiff.begin()); // Remove first element of s list
101  state_->gradDiff.erase(state_->gradDiff.begin()); // Remove first element of y list
102  state_->product.erase(state_->product.begin()); // Remove first element of rho list
103  }
104  state_->iterDiff.push_back(s.clone());
105  state_->iterDiff[state_->current]->set(s); // s=x_{k+1}-x_k
106  state_->gradDiff.push_back(grad.clone());
107  state_->gradDiff[state_->current]->set(*gradDiff); // y=g_{k+1}-g_k
108  state_->product.push_back(sy); // ys=1/rho
109  }
110  }
111 
112  // Apply Secant Approximate Inverse Hessian
113  virtual void applyH( Vector<Real> &Hv, const Vector<Real> &v, const Vector<Real> &x ) = 0;
114 
115  // Apply Initial Secant Approximate Inverse Hessian
116  virtual void applyH0( Vector<Real> &Hv, const Vector<Real> &v, const Vector<Real> &x ) {
117  Hv.set(v.dual());
118  if (state_->iter != 0 && state_->current != -1) {
119  Real yy = state_->gradDiff[state_->current]->dot(*(state_->gradDiff[state_->current]));
120  Hv.scale(state_->product[state_->current]/yy);
121  }
122  }
123 
124  // Apply Secant Approximate Hessian
125  virtual void applyB( Vector<Real> &Bv, const Vector<Real> &v, const Vector<Real> &x ) = 0;
126 
127  // Apply Initial Secant Approximate Hessian
128  virtual void applyB0( Vector<Real> &Bv, const Vector<Real> &v, const Vector<Real> &x ) {
129  Bv.set(v.dual());
130  if (state_->iter != 0 && state_->current != -1) {
131  Real yy = state_->gradDiff[state_->current]->dot(*(state_->gradDiff[state_->current]));
132  Bv.scale(yy/state_->product[state_->current]);
133  }
134  }
135 
136  // Test Secant Approximations
137  void test( const Vector<Real> &x, const Vector<Real> &s ) {
138  Teuchos::RCP<Vector<Real> > vec = x.clone();
139  Teuchos::RCP<Vector<Real> > Hvec = x.clone();
140  Teuchos::RCP<Vector<Real> > Bvec = x.clone();
141 
142  // Print BHv -> Should be v
143  vec->set(s);
144  applyH(*Hvec,*vec,x);
145  applyB(*Bvec,*Hvec,x);
146  vec->axpy(-1.0,*Bvec);
147  std::cout << " ||BHv-v|| = " << vec->norm() << "\n";
148 
149  // Print HBv -> Should be v
150  vec->set(s);
151  applyB(*Bvec,*vec,x);
152  applyH(*Hvec,*Bvec,x);
153  vec->axpy(-1.0,*Hvec);
154  std::cout << " ||HBv-v|| = " << vec->norm() << "\n";
155  }
156 
157 };
158 
159 }
160 
161 #include "ROL_lBFGS.hpp"
162 #include "ROL_lDFP.hpp"
163 #include "ROL_lSR1.hpp"
164 #include "ROL_BarzilaiBorwein.hpp"
165 
166 namespace ROL {
167  template<class Real>
168  inline Teuchos::RCP<Secant<Real> > getSecant( ESecant esec = SECANT_LBFGS, int L = 10, int BBtype = 1 ) {
169  switch (esec) {
170  case SECANT_LBFGS: return Teuchos::rcp( new lBFGS<Real>(L) );
171  case SECANT_LDFP: return Teuchos::rcp( new lDFP<Real>(L) );
172  case SECANT_LSR1: return Teuchos::rcp( new lSR1<Real>(L) );
173  case SECANT_BARZILAIBORWEIN: return Teuchos::rcp( new BarzilaiBorwein<Real>(BBtype) );
174  default: return Teuchos::null;
175  }
176  }
177 }
178 
179 #endif
virtual void update(const Vector< Real > &grad, const Vector< Real > &gp, const Vector< Real > &s, const Real snorm, const int iter)
Definition: ROL_Secant.hpp:87
virtual const Vector & dual() const
Return dual representation of , for example, the result of applying a Riesz map, or change of basis...
Definition: ROL_Vector.hpp:211
std::vector< Teuchos::RCP< Vector< Real > > > iterDiff
Definition: ROL_Secant.hpp:57
virtual void scale(const Real alpha)=0
Compute where .
virtual void applyB0(Vector< Real > &Bv, const Vector< Real > &v, const Vector< Real > &x)
Definition: ROL_Secant.hpp:128
virtual void applyH0(Vector< Real > &Hv, const Vector< Real > &v, const Vector< Real > &x)
Definition: ROL_Secant.hpp:116
std::vector< Teuchos::RCP< Vector< Real > > > gradDiff
Definition: ROL_Secant.hpp:58
void test(const Vector< Real > &x, const Vector< Real > &s)
Definition: ROL_Secant.hpp:137
virtual void applyB(Vector< Real > &Bv, const Vector< Real > &v, const Vector< Real > &x)=0
Provides definitions for limited-memory DFP operators.
Definition: ROL_lDFP.hpp:54
Contains definitions of custom data types in ROL.
Provides definitions for limited-memory SR1 operators.
Definition: ROL_lSR1.hpp:56
virtual Teuchos::RCP< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
Provides definitions for limited-memory BFGS operators.
Definition: ROL_lBFGS.hpp:54
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:72
virtual Real dot(const Vector &x) const =0
Compute where .
ESecant
Enumeration of secant update algorithms.
Definition: ROL_Types.hpp:295
Provides interface for and implements limited-memory secant operators.
Definition: ROL_Secant.hpp:67
Teuchos::RCP< Secant< Real > > getSecant(ESecant esec=SECANT_LBFGS, int L=10, int BBtype=1)
Definition: ROL_Secant.hpp:168
std::vector< Real > product2
Definition: ROL_Secant.hpp:60
Secant(int M=10)
Definition: ROL_Secant.hpp:77
Teuchos::RCP< SecantState< Real > > & get_state()
Definition: ROL_Secant.hpp:84
virtual ~Secant()
Definition: ROL_Secant.hpp:74
Teuchos::RCP< SecantState< Real > > state_
Definition: ROL_Secant.hpp:70
Provides definitions for Barzilai-Borwein operators.
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:194
std::vector< Real > product
Definition: ROL_Secant.hpp:59
virtual void applyH(Vector< Real > &Hv, const Vector< Real > &v, const Vector< Real > &x)=0
static const double ROL_EPSILON
Platform-dependent machine epsilon.
Definition: ROL_Types.hpp:115