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_ParameterList.hpp"
52 #include "ROL_LinearOperator.hpp"
53 #include "ROL_Types.hpp"
54 
55 namespace ROL {
56 
57 template<class Real>
58 struct SecantState {
59  ROL::Ptr<Vector<Real> > iterate;
60  std::vector<ROL::Ptr<Vector<Real> > > iterDiff; // Step Storage
61  std::vector<ROL::Ptr<Vector<Real> > > gradDiff; // Gradient Storage
62  std::vector<Real> product; // Step-Gradient Inner Product Storage
63  std::vector<Real> product2; // Step-Gradient Inner Product Storage
64  int storage; // Storage Size
65  int current; // Current Storage Size
66  int iter; // Current Optimization Iteration
67 };
68 
69 template<class Real>
70 class Secant : public LinearOperator<Real> {
71 private:
72 
73  ROL::Ptr<SecantState<Real> > state_; // Secant State
75 
76 public:
77 
78  virtual ~Secant() {}
79 
80  // Constructor
81  Secant( int M = 10 ) : isInitialized_(false) {
82  state_ = ROL::makePtr<SecantState<Real>>();
83  state_->storage = M;
84  state_->current = -1;
85  state_->iter = 0;
86  }
87 
88  ROL::Ptr<SecantState<Real> >& get_state() { return state_; }
89  const ROL::Ptr<SecantState<Real> >& get_state() const { return state_; }
90 
91  // Update Secant Approximation
92  virtual void updateStorage( const Vector<Real> &x, const Vector<Real> &grad,
93  const Vector<Real> &gp, const Vector<Real> &s,
94  const Real snorm, const int iter ) {
95  Real one(1);
96  if ( !isInitialized_ ) {
97  state_->iterate = x.clone();
98  isInitialized_ = true;
99  }
100  state_->iterate->set(x);
101  state_->iter = iter;
102  ROL::Ptr<Vector<Real> > gradDiff = grad.clone();
103  gradDiff->set(grad);
104  gradDiff->axpy(-one,gp);
105 
106  Real sy = s.dot(gradDiff->dual());
107  if (sy > ROL_EPSILON<Real>()*snorm*snorm) {
108  if (state_->current < state_->storage-1) {
109  state_->current++; // Increment Storage
110  }
111  else {
112  state_->iterDiff.erase(state_->iterDiff.begin()); // Remove first element of s list
113  state_->gradDiff.erase(state_->gradDiff.begin()); // Remove first element of y list
114  state_->product.erase(state_->product.begin()); // Remove first element of rho list
115  }
116  state_->iterDiff.push_back(s.clone());
117  state_->iterDiff[state_->current]->set(s); // s=x_{k+1}-x_k
118  state_->gradDiff.push_back(grad.clone());
119  state_->gradDiff[state_->current]->set(*gradDiff); // y=g_{k+1}-g_k
120  state_->product.push_back(sy); // ys=1/rho
121  }
122  }
123 
124  // Apply Secant Approximate Inverse Hessian
125  virtual void applyH( Vector<Real> &Hv, const Vector<Real> &v ) const = 0;
126 
127  // Apply Initial Secant Approximate Inverse Hessian
128  virtual void applyH0( Vector<Real> &Hv, const Vector<Real> &v ) const {
129  Hv.set(v.dual());
130  if (state_->iter != 0 && state_->current != -1) {
131  Real yy = state_->gradDiff[state_->current]->dot(*(state_->gradDiff[state_->current]));
132  Hv.scale(state_->product[state_->current]/yy);
133  }
134  }
135 
136  // Apply Secant Approximate Hessian
137  virtual void applyB( Vector<Real> &Bv, const Vector<Real> &v ) const = 0;
138 
139  // Apply Initial Secant Approximate Hessian
140  virtual void applyB0( Vector<Real> &Bv, const Vector<Real> &v ) const {
141  Bv.set(v.dual());
142  if (state_->iter != 0 && state_->current != -1) {
143  Real yy = state_->gradDiff[state_->current]->dot(*(state_->gradDiff[state_->current]));
144  Bv.scale(yy/state_->product[state_->current]);
145  }
146  }
147 
148  // Test Secant Approximations
149  void test( const Vector<Real> &x, const Vector<Real> &s ) const {
150  ROL::Ptr<Vector<Real> > vec = x.clone();
151  ROL::Ptr<Vector<Real> > Hvec = x.clone();
152  ROL::Ptr<Vector<Real> > Bvec = x.clone();
153  Real one(1);
154 
155  // Print BHv -> Should be v
156  vec->set(s);
157  applyH(*Hvec,*vec);
158  applyB(*Bvec,*Hvec);
159  vec->axpy(-one,*Bvec);
160  std::cout << " ||BHv-v|| = " << vec->norm() << "\n";
161 
162  // Print HBv -> Should be v
163  vec->set(s);
164  applyB(*Bvec,*vec);
165  applyH(*Hvec,*Bvec);
166  vec->axpy(-one,*Hvec);
167  std::cout << " ||HBv-v|| = " << vec->norm() << "\n";
168  }
169 
170  void apply(Vector<Real> &Hv, const Vector<Real> &v, Real &tol) const {
171  applyB(Hv,v);
172  }
173 
174  void applyInverse(Vector<Real> &Hv, const Vector<Real> &v, Real &tol) const {
175  applyH(Hv,v);
176  }
177 
178 };
179 
180 }
181 
182 #include "ROL_SecantFactory.hpp"
183 
184 #endif
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:226
virtual void scale(const Real alpha)=0
Compute where .
virtual ROL::Ptr< Vector > clone() const =0
Clone to make a new (uninitialized) vector.
ROL::Ptr< SecantState< Real > > state_
Definition: ROL_Secant.hpp:73
void apply(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply linear operator.
Definition: ROL_Secant.hpp:170
Contains definitions of custom data types in ROL.
virtual void applyH(Vector< Real > &Hv, const Vector< Real > &v) const =0
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:80
bool isInitialized_
Definition: ROL_Secant.hpp:74
virtual Real dot(const Vector &x) const =0
Compute where .
ROL::Ptr< SecantState< Real > > & get_state()
Definition: ROL_Secant.hpp:88
ROL::Ptr< Vector< Real > > iterate
Definition: ROL_Secant.hpp:59
virtual void applyB0(Vector< Real > &Bv, const Vector< Real > &v) const
Definition: ROL_Secant.hpp:140
std::vector< ROL::Ptr< Vector< Real > > > gradDiff
Definition: ROL_Secant.hpp:61
Provides interface for and implements limited-memory secant operators.
Definition: ROL_Secant.hpp:70
virtual void updateStorage(const Vector< Real > &x, const Vector< Real > &grad, const Vector< Real > &gp, const Vector< Real > &s, const Real snorm, const int iter)
Definition: ROL_Secant.hpp:92
std::vector< Real > product2
Definition: ROL_Secant.hpp:63
Secant(int M=10)
Definition: ROL_Secant.hpp:81
virtual void applyB(Vector< Real > &Bv, const Vector< Real > &v) const =0
void test(const Vector< Real > &x, const Vector< Real > &s) const
Definition: ROL_Secant.hpp:149
Provides the interface to apply a linear operator.
virtual void applyH0(Vector< Real > &Hv, const Vector< Real > &v) const
Definition: ROL_Secant.hpp:128
virtual ~Secant()
Definition: ROL_Secant.hpp:78
void applyInverse(Vector< Real > &Hv, const Vector< Real > &v, Real &tol) const
Apply inverse of linear operator.
Definition: ROL_Secant.hpp:174
std::vector< ROL::Ptr< Vector< Real > > > iterDiff
Definition: ROL_Secant.hpp:60
virtual void set(const Vector &x)
Set where .
Definition: ROL_Vector.hpp:209
std::vector< Real > product
Definition: ROL_Secant.hpp:62
const ROL::Ptr< SecantState< Real > > & get_state() const
Definition: ROL_Secant.hpp:89