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

Pass args and kwargs to Hessian of objective function #176

Closed
wants to merge 4 commits into from
Closed
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
25 changes: 16 additions & 9 deletions cyipopt/scipy_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def __init__(self,
jac_nnz_col=()):
if not SCIPY_INSTALLED:
msg = 'Install SciPy to use the `IpoptProblemWrapper` class.'
raise ImportError()
raise ImportError(msg)
self.obj_hess = None
self.last_x = None
if hessp is not None:
Expand All @@ -118,7 +118,9 @@ def __init__(self,
fun = MemoizeJac(fun)
jac = fun.derivative
elif not callable(jac):
raise NotImplementedError('jac has to be bool or a function')
raise NotImplementedError(
f'jac has to be bool or a function (got {type(jac)!r})'
)
self.fun = fun
self.jac = jac
self.args = args
Expand Down Expand Up @@ -146,7 +148,8 @@ def __init__(self,
con_fun = MemoizeJac(con_fun)
con_jac = con_fun.derivative
elif not callable(con_jac):
raise NotImplementedError('jac has to be bool or a function')
msg = f'jac of constraints has to be bool or a function (got {type(con_jac)!r})'
raise NotImplementedError(msg)
if (self.obj_hess is not None
and con_hessian is None) or (self.obj_hess is None
and con_hessian is not None):
Expand Down Expand Up @@ -198,7 +201,7 @@ def jacobian(self, x):
return np.hstack(jac_values)

def hessian(self, x, lagrange, obj_factor):
H = obj_factor * self.obj_hess(x) # type: ignore
H = obj_factor * self.obj_hess(x, *self.args, **self.kwargs) # type: ignore
# split the lagrangian multipliers for each constraint hessian
lagrs = np.split(lagrange, np.cumsum(self._constraint_dims[:-1]))
for hessian, args, lagr in zip(self._constraint_hessians,
Expand Down Expand Up @@ -270,6 +273,8 @@ def get_constraint_dimensions(constraints, x0):


def get_constraint_bounds(constraints, x0, INF=1e19):
TYPE_EQ = 'eq'
TYPE_INEQ = 'ineq'
cl = []
cu = []
if isinstance(constraints, dict):
Expand All @@ -280,12 +285,14 @@ def get_constraint_bounds(constraints, x0, INF=1e19):
else:
m = len(np.atleast_1d(con['fun'](x0, *con.get('args', []))))
cl.extend(np.zeros(m))
if con['type'] == 'eq':
if con['type'] == TYPE_EQ:
cu.extend(np.zeros(m))
elif con['type'] == 'ineq':
elif con['type'] == TYPE_INEQ:
cu.extend(INF * np.ones(m))
else:
raise ValueError(con['type'])
valid_types = TYPE_EQ, TYPE_INEQ
msg = f"Invalid constraint type: {con['type']!r}. Valid types are {valid_types}."
raise ValueError(msg)
cl = np.array(cl)
cu = np.array(cu)

Expand Down Expand Up @@ -384,8 +391,8 @@ def minimize_ipopt(fun,
try:
nlp.add_option(option, value)
except TypeError as e:
msg = 'Invalid option for IPOPT: {0}: {1} (Original message: "{2}")'
raise TypeError(msg.format(option, value, e))
msg = f'Invalid option for IPOPT: {option}: {value} (Original message: {e!r})'
raise TypeError(msg)

x, info = nlp.solve(_x0)

Expand Down
118 changes: 118 additions & 0 deletions cyipopt/tests/integration/test_args_kwargs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
"""Test passing extra arguments (*args and **kwargs) to objective function and its jac and hess.

Here, these extra arguments are NumPy arrays `dataX` and `dataY`
used to fit a linear regression `Y ~ a + b * X` using least squares.

NOTE: The mandatory `kwarg` keyword argument in functions below exists
solely to trigger an exception if not passed from `cyipopt.minimize_ipopt`.
"""
import pytest

import numpy as np
import cyipopt


def objective_with_args_kwargs(
x: np.ndarray, dataX: np.ndarray, dataY: np.ndarray, *, kwarg
):
"Least squares loss for a linear regression `Y ~ a + b * X`"
a, b = x
return ((dataY - a * dataX - b) ** 2).mean()


def jac_with_args_kwargs(x: np.ndarray, dataX: np.ndarray, dataY: np.ndarray, *, kwarg):
# BOTH `dataX` and `dataY` are used!
a, b = x
return np.array(
[
(2 * (dataY - a * dataX - b) * -dataX).mean(),
(2 * (dataY - a * dataX - b) * -1).mean(),
]
)


def hess_with_args_kwargs(
x: np.ndarray, dataX: np.ndarray, dataY: np.ndarray, *, kwarg
):
# Only `dataX` is used, but `dataY` must be provided as well
# to be consistent with `objective_with_args_kwargs` and `jac_with_args_kwargs`.
dataX_mean = dataX.mean()
return np.array([[2 * (dataX**2).mean(), 2 * dataX_mean], [2 * dataX_mean, 2.0]])


def test_args_kwargs():
"""Practical example of when args and kwargs are useful."""
# 1. Define data points.
dataX = np.asfarray([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
dataY = np.asfarray([1, 2, 5, 3, 5, 4, 7, 5, 3, 8])

# 2. Minimize loss function.
x0 = np.asfarray([2.0, 4.0])
res = cyipopt.minimize_ipopt(
objective_with_args_kwargs,
x0,
args=(dataX, dataY),
kwargs={"kwarg": 0},
jac=jac_with_args_kwargs,
hess=hess_with_args_kwargs,
)
print(res)

# 3. Check solution against known values.
# Expected values calculated using Mathematica's `NMinimize`.
assert res.success is True
expected_fun = 2.22181818181818181818181818182
expected_x = np.asfarray([0.490909090909090909090909090909, 1.6])
np.testing.assert_allclose(res.fun, expected_fun)
np.testing.assert_allclose(res.x, expected_x)


class TestMissingArgsKwargs:
x0 = np.asfarray([2.0, 4.0])

def test_missing_everything(self):
with pytest.raises(TypeError):
cyipopt.minimize_ipopt(
objective_with_args_kwargs,
self.x0,
# Missing args AND mandatory kwargs.
# args=(dataX, dataY), kwargs={'kwarg': 0},
jac=jac_with_args_kwargs,
hess=hess_with_args_kwargs,
)

def test_missing_args(self):
with pytest.raises(TypeError):
cyipopt.minimize_ipopt(
objective_with_args_kwargs,
self.x0,
# Missing args.
args=(),
kwargs={"kwarg": 0},
jac=jac_with_args_kwargs,
hess=hess_with_args_kwargs,
)

def test_missing_kwargs(self):
with pytest.raises(TypeError):
cyipopt.minimize_ipopt(
objective_with_args_kwargs,
self.x0,
# Missing kwargs.
args=(2, 3),
kwargs={},
jac=jac_with_args_kwargs,
hess=hess_with_args_kwargs,
)

def test_incorrect_number_of_args(self):
with pytest.raises(TypeError):
cyipopt.minimize_ipopt(
objective_with_args_kwargs,
self.x0,
# Need two args, but passed only one.
args=(2,),
kwargs={"kwarg": 0},
jac=jac_with_args_kwargs,
hess=hess_with_args_kwargs,
)