Skip to content

Commit

Permalink
Merge pull request #236 from moorepants/issue-235
Browse files Browse the repository at this point in the history
Covert tol to float before passing to ipopt.
  • Loading branch information
moorepants authored Nov 26, 2023
2 parents 82d5dd4 + 095f95d commit 2048826
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions cyipopt/scipy_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,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):
Expand Down
16 changes: 16 additions & 0 deletions cyipopt/tests/unit/test_scipy_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 an 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():
Expand Down

0 comments on commit 2048826

Please sign in to comment.