Skip to content
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

Numerical derivative for vector valued constraints #104

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion cyipopt/scipy_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
SCIPY_INSTALLED = True
del scipy
from scipy.optimize import approx_fprime
from scipy.optimize._numdiff import approx_derivative
try:
from scipy.optimize import OptimizeResult
except ImportError:
Expand Down Expand Up @@ -99,7 +100,7 @@ def __init__(self, fun, args=(), kwargs=None, jac=None, hess=None,
con_args = con.get('args', [])
con_kwargs = con.get('kwargs', [])
if con_jac is None:
con_jac = lambda x0, *args, **kwargs: approx_fprime(x0, con_fun, eps, *args, **kwargs)
con_jac = lambda x: approx_derivative(con_fun, x, method='3-point')
self._constraint_funs.append(con_fun)
self._constraint_jacs.append(con_jac)
self._constraint_args.append(con_args)
Expand Down
27 changes: 23 additions & 4 deletions cyipopt/tests/unit/test_scipy_optional.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import cyipopt



@pytest.mark.skipif("scipy" in sys.modules,
reason="Test only valid if no Scipy available.")
def test_minimize_ipopt_import_error_if_no_scipy():
Expand Down Expand Up @@ -59,14 +60,32 @@ def test_minimize_ipopt_nojac_if_scipy():
reason="Test only valid if Scipy available.")
def test_minimize_ipopt_nojac_constraints_if_scipy():
""" `minimize_ipopt` works without Jacobian and with constraints"""
from scipy.optimize import rosen
from scipy.optimize import rosen, rosen_der
x0 = [1.3, 0.7, 0.8, 1.9, 1.2]
constr = {"fun": lambda x: rosen(x) - 1.0, "type": "ineq"}
res = cyipopt.minimize_ipopt(rosen, x0, constraints=constr)
res = cyipopt.minimize_ipopt(rosen, x0, jac=rosen_der, constraints=constr)
assert isinstance(res, dict)
assert np.isclose(res.get("fun"), 1.0)
assert res.get("status") == 0
assert res.get("success") is True
expected_res = np.array([1.001867, 0.99434067, 1.05070075, 1.17906312,
1.38103001])
expected_res = np.array([1.00407015, 0.99655763, 1.05556205, 1.18568342, 1.38386505])
np.testing.assert_allclose(res.get("x"), expected_res)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Two blank lines between functions as per PEP8 convention.


@pytest.mark.skipif("scipy" not in sys.modules,
reason="Test only valid if Scipy available.")
def test_minimize_ipopt_nojac_constraints_vectorvalued_if_scipy():
""" `minimize_ipopt` works without Jacobian and with constraints
without specifying the jacobian of the vector valued constraint
function"""
from scipy.optimize import rosen, rosen_der
x0 = np.array([0.5, 0.75])
bounds = [np.array([0, 1]), np.array([-0.5, 2.0])]
expected_res = 0.25 * np.ones_like(x0)
eq_cons = {"fun" : lambda x: x - expected_res, "type": "eq"}
res = cyipopt.minimize_ipopt(rosen, x0, jac=rosen_der, bounds=bounds, constraints=[eq_cons])
assert isinstance(res, dict)
assert res.get("status") == 0
assert res.get("success") is True
np.testing.assert_allclose(res.get("x"), expected_res)

3 changes: 1 addition & 2 deletions cyipopt/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import warnings
from functools import wraps

from ipopt_wrapper import Problem


def deprecated_warning(new_name):
"""Decorator that issues a FutureWarning for deprecated functionality.
Expand Down Expand Up @@ -91,3 +89,4 @@ def generate_deprecation_warning_msg(what,
f"deprecated in CyIpopt. Please replace all uses and use "
f"'{new_name}' going forward.")
return msg

14 changes: 14 additions & 0 deletions examples/constraints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import numpy as np
from scipy.optimize import rosen, rosen_der
from cyipopt import minimize_ipopt

x0 = np.array([0.5, 0])

bounds = [np.array([0, 1]), np.array([-0.5, 2.0])]

eq_cons = {"type": "eq",
"fun": lambda x: np.array([2*x[0] + x[1] - 1, x[0]**2 - 0.1])}

res = minimize_ipopt(rosen, x0, jac=rosen_der, bounds=bounds, constraints=[eq_cons])

print(res)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be ideal to add a unit test that checks this example, or a similar one.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. See here for how we've written other tests that an scipy-optional. The test can simply be a copy (with the small required changes) of any of the three tests in that module currently marked with @pytest.mark.skipif("scipy" not in sys.module).