From 682bc391e5a048595cff7b928ccde0e2d7409466 Mon Sep 17 00:00:00 2001 From: "Jason K. Moore" Date: Wed, 25 Oct 2023 06:33:23 +0200 Subject: [PATCH 1/2] Covert tol to float before passing to ipopt. --- cyipopt/scipy_interface.py | 2 ++ cyipopt/tests/unit/test_scipy_optional.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/cyipopt/scipy_interface.py b/cyipopt/scipy_interface.py index 069fa3d7..86372e73 100644 --- a/cyipopt/scipy_interface.py +++ b/cyipopt/scipy_interface.py @@ -668,6 +668,8 @@ def _minimize_ipopt_iv(fun, x0, args, kwargs, method, jac, hess, hessp, 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.') + else: # tol should be a float, not an array + tol = float(tol) options = dict() if options is None else options if not isinstance(options, dict): diff --git a/cyipopt/tests/unit/test_scipy_optional.py b/cyipopt/tests/unit/test_scipy_optional.py index a9b049ce..a9c286cf 100644 --- a/cyipopt/tests/unit/test_scipy_optional.py +++ b/cyipopt/tests/unit/test_scipy_optional.py @@ -30,6 +30,22 @@ def test_minimize_ipopt_import_error_if_no_scipy(): cyipopt.minimize_ipopt(None, None) +@pytest.mark.skipif("scipy" not in sys.modules, + reason="Test only valid if Scipy available.") +def test_tol_type_issue_235(): + # from: https://github.com/mechmotum/cyipopt/issues/235 + + def fun(x): + return np.sum(x ** 2) + + # tol should not raise and error + cyipopt.minimize_ipopt( + fun=fun, + x0=np.zeros(2), + tol=1e-9, + ) + + @pytest.mark.skipif("scipy" not in sys.modules, reason="Test only valid if Scipy available.") def test_minimize_ipopt_input_validation(): From 0b6e7494a1cafc19f0dd3b683ace7bad89e28b3c Mon Sep 17 00:00:00 2001 From: "Jason K. Moore" Date: Wed, 25 Oct 2023 06:45:00 +0200 Subject: [PATCH 2/2] Spelling. --- cyipopt/tests/unit/test_scipy_optional.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cyipopt/tests/unit/test_scipy_optional.py b/cyipopt/tests/unit/test_scipy_optional.py index a9c286cf..5bd91544 100644 --- a/cyipopt/tests/unit/test_scipy_optional.py +++ b/cyipopt/tests/unit/test_scipy_optional.py @@ -38,7 +38,7 @@ def test_tol_type_issue_235(): def fun(x): return np.sum(x ** 2) - # tol should not raise and error + # tol should not raise an error cyipopt.minimize_ipopt( fun=fun, x0=np.zeros(2),