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

support non-convex quadratic objectives/constraints in gurobi #28

Merged
merged 3 commits into from
May 1, 2023
Merged
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
14 changes: 12 additions & 2 deletions ilpy/impl/solvers/GurobiBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,21 +218,31 @@ GurobiBackend::solve(Solution& x, std::string& msg) {

GRB_CHECK(GRBupdatemodel(_model));

GRBenv* modelenv = GRBgetenv(_model);

if (_timeout > 0) {

GRBenv* modelenv = GRBgetenv(_model);
GRB_CHECK(GRBsetdblparam(modelenv, GRB_DBL_PAR_TIMELIMIT, _timeout));
}

if (_gap >= 0) {

GRBenv* modelenv = GRBgetenv(_model);
if (_absoluteGap)
GRB_CHECK(GRBsetdblparam(modelenv, GRB_DBL_PAR_MIPGAPABS, _gap));
else
GRB_CHECK(GRBsetdblparam(modelenv, GRB_DBL_PAR_MIPGAP, _gap));
}

// Sets the strategy for handling non-convex quadratic objectives
// or non-convex quadratic constraints.
// 0 = an error is reported if the original user model contains non-convex
// quadratic constructs.
// 1 = an error is reported if non-convex quadratic constructs could not be
// discarded or linearized during presolve.
// 2 = non-convex quadratic problems are solved by means of translating them
// into bilinear form and applying spatial branching.
GRB_CHECK(GRBsetintparam(modelenv, GRB_INT_PAR_NONCONVEX, 2));

GRB_CHECK(GRBoptimize(_model));

int status;
Expand Down
26 changes: 10 additions & 16 deletions tests/test_solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
HAVE_GUROBI = False


PREFS = [ilpy.Preference.Scip, pytest.param(ilpy.Preference.Gurobi, marks=gu_marks)]


@pytest.mark.parametrize("as_expression", [True, False], ids=["as_expr", "as_constr"])
@pytest.mark.parametrize(
"preference",
[ilpy.Preference.Scip, pytest.param(ilpy.Preference.Gurobi, marks=gu_marks)],
)
@pytest.mark.parametrize("preference", PREFS)
def test_simple_solver(preference: ilpy.Preference, as_expression: bool) -> None:
num_vars = 10
special_var = 5
Expand Down Expand Up @@ -62,10 +62,7 @@ def test_simple_solver(preference: ilpy.Preference, as_expression: bool) -> None


@pytest.mark.parametrize("as_expression", [True, False], ids=["as_expr", "as_constr"])
@pytest.mark.parametrize(
"preference",
[ilpy.Preference.Scip, pytest.param(ilpy.Preference.Gurobi, marks=gu_marks)],
)
@pytest.mark.parametrize("preference", PREFS)
def test_quadratic_solver(preference: ilpy.Preference, as_expression: bool) -> None:
num_vars = 10
special_var = 5
Expand Down Expand Up @@ -105,22 +102,19 @@ def test_quadratic_solver(preference: ilpy.Preference, as_expression: bool) -> N
assert solution[5] == -2 # jan please check


@pytest.mark.skipif(not HAVE_GUROBI, reason="Gurobi not installed")
def test_non_convex_quadratic_gurobi() -> None:
@pytest.mark.parametrize("preference", PREFS)
def test_non_convex_quadratic(preference: ilpy.Preference) -> None:
# currently, just a smoke test to make sure we don't crash on solve.
obj = ilpy.Objective()
obj.set_quadratic_coefficient(0, 0, -1) # quadratic term (-x^2)

solver = ilpy.Solver(
1, ilpy.VariableType.Continuous, preference=ilpy.Preference.Gurobi
)
solver = ilpy.Solver(1, ilpy.VariableType.Continuous, preference=preference)
solver.set_objective(obj)

constraint = ilpy.Constraint()
constraint.set_coefficient(0, 1)
constraint.set_value(1)
solver.add_constraint(constraint)

# Gurobi will raise an exception at the moment... may be changed later:
with pytest.raises(RuntimeError, match="Q matrix is not positive semi-definite"):
solver.solve()
# Gurobi will give zeros and SCIP will give something like -9999999987
assert solver.solve() is not None