LSLOpt  1.0
BFGSStorage.hpp
1 #pragma once
2 
3 #include "../ScalarTraits.hpp"
4 #include "../Types.hpp"
5 
6 
7 namespace LSLOpt {
8 
9 namespace Implementation {
10 
22 template<typename Scalar, typename OutputFunction>
23 struct BFGSStorage {
32  Eigen::Index n,
33  Eigen::Index,
34  Scalar,
35  OutputFunction& output_function);
36 
43  void reset();
44 
54  const Vector<Scalar>& v);
55 
64  Scalar calculate_vHv(
65  const Vector<Scalar>& v);
66 
72  const Matrix<Scalar>& calculate_H();
73 
83  const Vector<Scalar>& v);
84 
93  Scalar calculate_vBv(
94  const Vector<Scalar>& v);
95 
101  const Matrix<Scalar>& calculate_B();
102 
113  bool update(
114  const Vector<Scalar>& s,
115  const Vector<Scalar>& y,
116  const Vector<Scalar>& g);
117 
119  Eigen::Index n;
120 
126  bool initial;
127 
129  OutputFunction& output_function;
130 };
131 
132 template<typename Scalar, typename OutputFunction>
134  Eigen::Index n,
135  Eigen::Index,
136  Scalar,
137  OutputFunction& output_function)
138 : n(n)
139 , initial(true)
140 , output_function(output_function)
141 {
142 
143 }
144 
145 template<typename Scalar, typename OutputFunction>
147 {
150  initial = true;
151 }
152 
153 template<typename Scalar, typename OutputFunction>
155  const Vector<Scalar>& v)
156 {
157  // matrix vector product
158  return H * v;
159 }
160 
161 template<typename Scalar, typename OutputFunction>
163  const Vector<Scalar>& v)
164 {
165  // matrix vector and dot product
166  return v.dot(calculate_Hv(v));
167 }
168 
169 template<typename Scalar, typename OutputFunction>
171 {
172  return H;
173 }
174 
175 template<typename Scalar, typename OutputFunction>
177  const Vector<Scalar>& v)
178 {
179  // matrix vector product
180  return B * v;
181 }
182 
183 template<typename Scalar, typename OutputFunction>
185  const Vector<Scalar>& v)
186 {
187  // matrix vector and dot product
188  return v.dot(calculate_Bv(v));
189 }
190 
191 template<typename Scalar, typename OutputFunction>
193 {
194  return B;
195 }
196 
197 template<typename Scalar, typename OutputFunction>
199  const Vector<Scalar>& s,
200  const Vector<Scalar>& y,
201  const Vector<Scalar>& g)
202 {
203  if (initial) {
204  // scale the initial (inverse) Hessian
205  H *= y.dot(s) / y.dot(y);
206  B *= y.dot(y) / y.dot(s);
207  initial = false;
208  }
209 
210  // update the Hessian approximation
211  B = B - (B * s * s.transpose() * B)/(s.transpose() * B * s)
212  + (y * y.transpose()) / (s.transpose() * y);
213 
214  Scalar rho = Scalar{1} / y.dot(s);
215 
216  // update the inverse Hessian approximation
217  H = (Matrix<Scalar>::Identity(n, n) - s * y.transpose() * rho)
218  * H * (Matrix<Scalar>::Identity(n, n) - y * s.transpose() * rho)
219  + rho * s * s.transpose();
226  return !H.unaryExpr(&scalar_traits<Scalar>::is_nan).any()
227  && !B.unaryExpr(&scalar_traits<Scalar>::is_nan).any();
228 }
229 
230 }
231 
232 }
Scalar calculate_vBv(const Vector< Scalar > &v)
Calculate normalized scalar product of vector with Hessian approximation .
Definition: BFGSStorage.hpp:184
Vector< Scalar > calculate_Hv(const Vector< Scalar > &v)
Calculate product of inverse Hessian approximation with vector .
Definition: BFGSStorage.hpp:154
BFGS storage.
Definition: BFGSStorage.hpp:23
BFGSStorage(Eigen::Index n, Eigen::Index, Scalar, OutputFunction &output_function)
Construct a BFGS storage.
Definition: BFGSStorage.hpp:133
Traits for scalar values.
Definition: ScalarTraits.hpp:16
const Matrix< Scalar > & calculate_B()
Access the Hessian approximation .
Definition: BFGSStorage.hpp:192
bool update(const Vector< Scalar > &s, const Vector< Scalar > &y, const Vector< Scalar > &g)
Update the (inverse) Hessian approximation.
Definition: BFGSStorage.hpp:198
Matrix< Scalar > B
Hessian approximation.
Definition: BFGSStorage.hpp:124
Eigen::Index n
dimensionality of the problem
Definition: BFGSStorage.hpp:119
Eigen::Matrix< Scalar, Eigen::Dynamic, 1 > Vector
Vector type used.
Definition: Types.hpp:15
Eigen::Matrix< Scalar, Eigen::Dynamic, Eigen::Dynamic > Matrix
Matrix type used.
Definition: Types.hpp:24
Matrix< Scalar > H
inverse Hessian approximation
Definition: BFGSStorage.hpp:122
BFGS optimizations.
Definition: BFGS.hpp:24
OutputFunction & output_function
output function for status messages
Definition: BFGSStorage.hpp:129
Vector< Scalar > calculate_Bv(const Vector< Scalar > &v)
Calculate product of Hessian approximation with vector .
Definition: BFGSStorage.hpp:176
void reset()
Reset the (inverse) Hessian approximation to identity matrix.
Definition: BFGSStorage.hpp:146
bool initial
is this the initial approximation?
Definition: BFGSStorage.hpp:126
const Matrix< Scalar > & calculate_H()
Access the inverse Hessian approximation .
Definition: BFGSStorage.hpp:170
Scalar calculate_vHv(const Vector< Scalar > &v)
Calculate normalized scalar product of vector with inverse Hessian approximation ...
Definition: BFGSStorage.hpp:162