argmin-rs / argmin

Numerical optimization in pure Rust
http://argmin-rs.org
Apache License 2.0
1k stars 79 forks source link

How best to share computation between cost and gradient computations in a solver-agnostic way? #366

Closed micahscopes closed 9 months ago

micahscopes commented 1 year ago

I'm working on a problem where the gradient and cost functions share some common computation, including some weights that take quadratic complexity.

Short of implementing a custom solver, what's the right way in argmin to share some computations between the gradient and cost function implementations?

TheIronBorn commented 1 year ago

Probably some form of a custom wrapper type in between your problem and the Gradient/CostFunction traits which computes both and then provides the traits with the cached data

stefan-k commented 1 year ago

Sorry about the late reply! @TheIronBorn is right, some form of "precomputing and caching" is necessary. One thing to look out for may be (depending on what you are trying to do exactly) the order in which cost and gradient are computed in the solver you are using. Furthermore, if a solver uses a line search, this could require additional measures in your solution.