PatWie / CppNumericalSolvers

a lightweight header-only C++17 library of numerical optimization methods for nonlinear functions based on Eigen
MIT License
873 stars 201 forks source link

Conjugate gradient descent tries to allocate memory for hessian #129

Closed kyearb closed 3 years ago

kyearb commented 3 years ago

Using methods that don't require a hessian (such as conjugate gradient descent) still tries to allocate memory for a hessian. This makes it impossible to use for solving problems with a large number of variables.

Since some of these methods are used specifically to require less memory, it would be nice if the hessians are not allocated by default.

PatWie commented 3 years ago

Is a private member of

Eigen::Matrix<TScalar, Eigen::Dynamic, Eigen::Dynamic>;

really an issue? Could you please post more details. The solver itself does not allocate memory for the Hessian.

kyearb commented 3 years ago

The issue is when the State struct is instantiated in Function.Eval(). Regardless of the solver used (or the order specified when calling Function.Eval()), a hessian of size (dim,dim) is initialized.

Line 24-29 in function.h:

  State(const int dim, const int order)
      : dim(dim),
        order(order),
        x(vector_t::Zero(dim)),
        gradient(vector_t::Zero(dim)),
        hessian(matrix_t::Zero(dim, dim)) {}