33
44#include < stdexcept>
55#include " LBFGSHessian.hpp"
6+ #include " optimization/Iterate.hpp"
7+ #include " symbolic/Range.hpp"
68
79#ifdef WITH_LAPACK
810#include " fortran_interface.h"
@@ -16,18 +18,50 @@ namespace uno {
1618 LBFGSHessian::LBFGSHessian (size_t dimension, size_t memory_size):
1719 HessianModel (),
1820 dimension (dimension),
19- memory_size (memory_size),
21+ memory_capacity (memory_size),
2022 S_matrix (dimension, memory_size),
2123 Y_matrix (dimension, memory_size),
24+ L_matrix (memory_size, memory_size),
25+ D_matrix (memory_size),
2226 M_matrix (memory_size, memory_size) {
2327 }
2428
2529 void LBFGSHessian::initialize_statistics (Statistics& /* statistics*/ , const Options& /* options*/ ) const {
2630 // do nothing
2731 }
2832
29- void LBFGSHessian::notify_accepted_iterate (const Iterate& /* iterate*/ ) {
33+ void LBFGSHessian::notify_accepted_iterate (const Iterate& current_iterate, const Iterate& trial_iterate) {
34+ std::cout << " Adding vector to L-BFGS memory at slot " << this ->current_available_slot << ' \n ' ;
35+ // this->current_available_slot lives in [0, this->memory_capacity)
3036
37+ // TODO figure out if we're extending or replacing in memory
38+
39+ // fill the S matrix
40+ for (size_t variable_index: Range (this ->dimension )) {
41+ this ->S_matrix .entry (variable_index, this ->current_available_slot ) = trial_iterate.primals [variable_index] -
42+ current_iterate.primals [variable_index];
43+ }
44+ std::cout << " S:\n " << this ->S_matrix ;
45+
46+ // fill the Y matrix
47+ // TODO
48+
49+ // fill the D matrix (diagonal)
50+ this ->D_matrix [this ->current_available_slot ] = 1 .; // TODO dot(s_new, y_new)
51+
52+ // fill the L matrix (lower triangular with a zero diagonal)
53+ for (size_t column_index: Range (this ->current_memory_size )) {
54+ for (size_t row_index: Range (column_index+1 , this ->current_memory_size )) {
55+ this ->L_matrix .entry (row_index, column_index) = 1 .; // TODO dot(s_i, y_j)
56+ }
57+ }
58+ std::cout << " L:\n " << this ->L_matrix ;
59+
60+ // if we exceed the size of the memory, we start over and replace the older point in memory
61+ this ->current_available_slot = (this ->current_available_slot + 1 ) % this ->memory_capacity ;
62+ this ->current_memory_size = std::min (current_memory_size + 1 , this ->memory_capacity );
63+ std::cout << " There are now " << this ->current_memory_size << " iterates in memory (capacity " <<
64+ this ->memory_capacity << " )\n " ;
3165 }
3266
3367 void LBFGSHessian::evaluate_hessian (Statistics& /* statistics*/ , const OptimizationProblem& /* problem*/ ,
@@ -36,8 +70,11 @@ namespace uno {
3670 throw std::runtime_error (" LBFGSHessian::evaluate_hessian not implemented" );
3771 }
3872
39- void LBFGSHessian::compute_hessian_vector_product (const OptimizationProblem& /* problem*/ , const Vector<double >& /* vector*/ ,
40- const Vector<double >& /* constraint_multipliers*/ , Vector<double >& /* result*/ ) {
41- throw std::runtime_error (" LBFGSHessian::compute_hessian_vector_product not implemented" );
73+ void LBFGSHessian::compute_hessian_vector_product (const OptimizationProblem& /* problem*/ , const Vector<double >& vector,
74+ const Vector<double >& /* constraint_multipliers*/ , Vector<double >& result) {
75+ // throw std::runtime_error("LBFGSHessian::compute_hessian_vector_product not implemented");
76+ for (size_t variable_index: Range (vector.size ())) {
77+ result[variable_index] = vector[variable_index];
78+ }
4279 }
4380} // namespace
0 commit comments