funkelab / ilpy

Unified python wrappers for popular ILP solvers
https://funkelab.github.io/ilpy/
MIT License
3 stars 2 forks source link

test: setConstraints in SCIP backend #41

Closed tlambert03 closed 10 months ago

tlambert03 commented 10 months ago

scip backend after solver, call set_constraints ... it should have an effect ... but it might not (because we're just clearing our internal _constraints vector ... not updating the solver

tlambert03 commented 10 months ago

heres a test that should theoretically pass, but currently fails on scip (as @funkey expected, calling set_constraints doesn't actually do anything to the solver).

    @pytest.mark.parametrize("preference", PREFS)
    def test_solve_twice(preference: ilpy.Preference) -> None:
        solver = ilpy.Solver(2, ilpy.VariableType.Integer, preference=preference)
        x1 = ilpy.Variable("x1", index=0)
        x2 = ilpy.Variable("x2", index=1)

        solver.set_objective((x1 + x2).as_objective(ilpy.Maximize))

        constraints = ilpy.Constraints()
        constraints.add(x1 + x2 <= 10)
        constraints.add(2 * x1 + 3 * x2 >= 12)
        constraints.add(x1 - x2 <= 5)

        solver.set_constraints(constraints)
        solution = solver.solve()
        assert list(solution) == [7, 3] and solution.get_value() == 10

        solver.add_constraint(x1 - x2 <= 3)
        solution = solver.solve()
        assert list(solution) == [6, 4] and solution.get_value() == 10

        solver.set_constraints(constraints)
        solution = solver.solve()
        assert list(solution) == [7, 3] and solution.get_value() == 10
# E       assert ([6.0, 4.0] == [7, 3]
# E         At index 0 diff: 6.0 != 7
# E         Full diff:
# E         - [7, 3]
# E         + [6.0, 4.0])

On gurobi, there's a crash:

libc++abi: terminating due to uncaught exception of type std::runtime_error: Gurobi error in ilpy/impl/solvers/GurobiBackend.cpp:144: Problem deleting constraint
Fatal Python error: Aborted
tlambert03 commented 10 months ago

fixed by #44