optimagic-dev / optimagic

optimagic is a Python package for numerical optimization. It is a unified interface to optimizers from SciPy, NlOpt and other packages. optimagic's minimize function works just like SciPy's, so you don't have to adjust your code. You simply get more optimizers for free. On top you get diagnostic tools, parallel numerical derivatives and more.
https://optimagic.readthedocs.io/
MIT License
270 stars 30 forks source link

Result processing crashes if DFOLS starts from optimum #538

Closed Mv77 closed 1 month ago

Mv77 commented 1 month ago

Bug description

Hi! I noticed that if you start an OLS minimization problem from an initial point that is the optimum (so that no iterations happen), minimize crashes with (at least) DFOLS. The issue arises in the processing of results, when trying to figure out the number of iterations the problem ran for.

To Reproduce

Here is a minimal example:

import optimagic as om
import numpy as np

# A simple loss
@om.mark.least_squares
def loss(x):
    return x

x0 = np.array([0.0, 0.0])
res =  om.minimize(
    fun=loss,
    params=x0,
    algorithm="nag_dfols",
    bounds=om.Bounds(np.ones_like(x0) * -1.0, np.ones_like(x0)),
)

Expected behavior

Would have expected to still get a result object, even if no iterations occurred.

Screenshots/Error messages

The error is

  File "[...]\optimagic\optimizers\nag_optimizers.py", line 633, in nag_dfols_internal
    n_iterations=int(res["n_iterations"]),
                 ^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

System

Thanks as always!

janosg commented 1 month ago

Thanks a lot for the thorough bug description! I'll have a look as soon as possible!

timmens commented 1 month ago

Hey!

I've opened a PR (#544) to fix the problem. If it's urgent, you can work with that branch. We cannot merge that PR yet because we must wait for an upstream fix from DFO-LS, as there are problems with their new version. As long as you stay below DFO-LS<=1.5.0, there should be no problem. I believe that they will push a fix relatively quickly though.