10000 minimize_ipopt raises TypeError for `tol` argument · Issue #235 · mechmotum/cyipopt · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

minimize_ipopt raises TypeError for tol argument #235

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
timmens opened this issue Oct 24, 2023 · 4 comments · Fixed by #236
Closed

minimize_ipopt raises TypeError for tol argument #235

timmens opened this issue Oct 24, 2023 · 4 comments · Fixed by #236

Comments

@timmens
Copy link
timmens commented Oct 24, 2023

Problem

Minimization using cyipopt.minimize_ipopt raises a TypeError for the tol argument on the same code in version 1.3.0 but not in version 1.2.0. Please take a look at the minimal reproducible example below.

Minimal reproducible example

import cyipopt
import numpy as np

def fun(x):
    return np.sum(x ** 2)

res = cyipopt.minimize_ipopt(
    fun=fun,
    x0=np.zeros(2),
    tol=1e-9,
)

Error excerpt:

TypeError: Invalid option for IPOPT: b'tol': 1e-09 (Original message: "Invalid option type")

Probably caused by

It seems that cyipopt.minimize_ipopt calls the function cyipopt.scipy_interface._minimize_ipopt_iv internally (l.534), which converts the argument tol to a numpy.float (l.668), which then causes nlp.add_option(option, value) (l.598) to raise the TypeError.


Full error message

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File ~/miniforge3/envs/[...]/lib/python3.10/site-packages/cyipopt/scipy_interface.py:599, in minimize_ipopt(fun, x0, args, kwargs, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    598 try:
--> 599     nlp.add_option(option, value)
    600 except TypeError as e:

File ~/miniforge3/envs/[...]/lib/python3.10/site-packages/cyipopt/cython/ipopt_wrapper.pyx:503, in ipopt_wrapper.Problem.add_option()

TypeError: Invalid option type

During handling of the above exception, another exception occurred:

TypeError                                 Traceback (most recent call last)
Cell In[2], line 12
      7     return np.sum(x ** 2)
      9 x0 = np.zeros(2)
---> 12 res = minimize_ipopt(
     13     fun=fun,
     14     x0=x0,
     15     tol=1e-9,
     16 )

File ~/miniforge3/envs/[...]/lib/python3.10/site-packages/cyipopt/scipy_interface.py:602, in minimize_ipopt(fun, x0, args, kwargs, method, jac, hess, hessp, bounds, constraints, tol, callback, options)
    600     except TypeError as e:
    601         msg = 'Invalid option for IPOPT: {0}: {1} (Original message: "{2}")'
--> 602         raise TypeError(msg.format(option, value, e))
    604 x, info = nlp.solve(x0)
    606 return OptimizeResult(x=x,
    607                       success=info['status'] == 0,
    608                       status=info['status'],
   (...)
    613                       njev=problem.njev,
    614                       nit=problem.nit)

TypeError: Invalid option for IPOPT: b'tol': 1e-09 (Original message: "Invalid option type")
@timmens timmens changed the title minimize_ipopt raises TypeError minimize_ipopt raises TypeError for tol argument Oct 24, 2023
@moorepants
Copy link
Collaborator

@mdhaber this may be from some changes you've added in 1.3.0. Looks like tol is converted to a numpy array.

    if tol is not None:
        tol = np.asarray(tol)[()]
        if tol.ndim != 0 or not np.issubdtype(tol.dtype, np.number) or tol <= 0:
            raise ValueError('`tol` must be a positive scalar.')

@moorepants
Copy link
Collaborator

I've made a simple fix for this and can make a bug fix release soon.

@mdhaber
Copy link
Contributor
mdhaber commented Oct 25, 2023

Hmm. If tol is a Python float, tol = np.asarray(tol)[()] makes it NumPy float, which is a Python float.

import numpy as np
x = 1.0
y = np.asarray(x)[()]
isinstance(y, type(x))  # True

So I didn't think that was backward incompatible. I added a test of tol for SciPy's methods to make sure it was being passed to SciPy correctly (

def test_minimize_ipopt_bounds_tol_options():
), but I guess there were no tests of the tol parameter before that checked it for IPOPT.

In any case, sorry for the trouble.

@moorepants
Copy link
Collaborator

No trouble, its part of the process :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants