From e76f1e0115dff4d38d41411774fe47291394b565 Mon Sep 17 00:00:00 2001 From: ItsNiklas Date: Mon, 11 Nov 2024 19:18:42 +0200 Subject: [PATCH] test: add indentical variable constraints --- test/mip_test.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/mip_test.py b/test/mip_test.py index 10ab6cb0..b205df14 100644 --- a/test/mip_test.py +++ b/test/mip_test.py @@ -4,6 +4,7 @@ from os import environ import networkx as nx +from mip.entities import LinExpr import mip.gurobi import mip.highs from mip import Model, xsum, OptimizationStatus, MAXIMIZE, BINARY, INTEGER @@ -578,6 +579,28 @@ def test_obj_const2(self, solver: str): assert model.objective_const == 1 +@skip_on(NotImplementedError) +@pytest.mark.parametrize("solver", SOLVERS) +@pytest.mark.parametrize("constraint, lb, ub", [ + (lambda x: x + x >= 3, 1, 2), + (lambda x: x - x >= 0, 1, 2), + (lambda x: x == x, 1, 2), + (lambda x: x >= x, 1, 2), + (lambda x: x <= x, -2, -1), + # (lambda x: LinExpr([x, x, x], [2, -1, -1], sense="="), 1, 2), +]) +def test_identical_vars(solver: str, constraint, lb, ub): + """Try if constraints are correctly added when variables are identical""" + m = Model(solver_name=solver) + x = m.add_var(name="x", lb=lb, ub=ub, obj=1) + + m.add_constr(constraint(x)) + + m.optimize() + assert m.status == OptimizationStatus.OPTIMAL + assert lb - TOL <= x.x <= ub + TOL + + @skip_on(NotImplementedError) @pytest.mark.parametrize("val", range(1, 4)) @pytest.mark.parametrize("solver", SOLVERS)