Skip to content

Commit

Permalink
Added MIP bilinear test. Also, we now allow constraints to have param…
Browse files Browse the repository at this point in the history
…s which are not in model.
  • Loading branch information
tBuLi committed Sep 25, 2023
1 parent 920a1b6 commit 8881b65
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
3 changes: 1 addition & 2 deletions symfit/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,7 @@ def as_constraint(cls, constraint, model, constraint_type=None, **init_kwargs):
if set(instance.params) <= set(model.params):
instance.params = model.params
else:
raise ModelError('The parameters of ``constraint`` have to be a '
'subset of those of ``model``.')
model.params = sorted(set(model.params) | set(instance.params), key=lambda x: x.name)

return instance

Expand Down
10 changes: 6 additions & 4 deletions tests/test_mip.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_parameter_array_bounds():


def test_bilinear():
# Create variables
# Solve the bilinear example with MIP and compare to Fit.
x, y, z = parameters('x, y, z', min=0)

objective = 1.0 * x
Expand All @@ -25,8 +25,10 @@ def test_bilinear():

mip = MIP(- objective, constraints=constraints)
mip_result = mip.execute()
print(mip_result)

fit = Fit(- objective, constraints=constraints)
fit_result = fit.execute()
print(fit_result)

assert mip_result[x] == pytest.approx(fit_result.value(x), abs=1e-6)
assert mip_result[y] == pytest.approx(fit_result.value(y), abs=1e-6)
assert mip_result[z] == pytest.approx(fit_result.value(z), abs=1e-6)
assert mip_result.objective_value == pytest.approx(fit_result.objective_value, abs=1e-6)

0 comments on commit 8881b65

Please sign in to comment.