PatWie / CppNumericalSolvers

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

Setting the initial approximate Hessian? #145

Closed ghutchis closed 1 year ago

ghutchis commented 1 year ago

Many times in chemistry, we can supply a quick approximate Hessian to start the optimization. It's an improvement over using the identity matrix to start for our applications.

I can certainly patch the ISolver class to capture an H matrix from the user or default to THessian::Identity(DIM, DIM)

Would you be okay with a pull request (to master) for this?

PatWie commented 1 year ago

To understand this issue. In

https://github.com/PatWie/CppNumericalSolvers/blob/9e21e01736237a1e10e57bffca1b00fe6655d9f6/include/cppoptlib/solver/solver.h#L209-L214

you can pass a complete function state including an initial guess of the hessian to the solver. This is automatically done for your function evaluation.

Note, most solver store the inverse of the Hessian. I don't know at the moment of how one would adapt the function_state_t to set that (inverse) value as other solvers like LBFGS do not even use the inverse Hessian in a direct way.

If there is a neat way to initialize the (inverse) Hessian with a good guess I am more than happy to merge it in.

ghutchis commented 1 year ago

Let me think if there's a good way to directly write the inverse guess (i.e., it's generally sparse but inverting a big Hessian is still slow).

ghutchis commented 1 year ago

I can think of a few ML-based methods to predict the inverse Hessian, but it's probably too much trouble. As you said, it's better to use something like LBFGS and then the inverse isn't even used directly.