funkelab / ilpy

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

abort trap in gurobi backend with non PSD matrix #27

Closed tlambert03 closed 1 year ago

tlambert03 commented 1 year ago

the following script results in a ~segfault~ crash in the gurobi backend:

libc++abi: terminating with uncaught exception of type std::runtime_error: Gurobi error in ./ilpy/impl/solvers/GurobiBackend.cpp:236: Q matrix is not positive semi-definite (PSD). Set NonConvex parameter to 2 to solve model.
[1]    56530 abort      python x.py

I have no doubt I've set something up that is impossible, non-convex, or something bad :) ... but it shouldn't segfault either way:

import ilpy

solver = ilpy.Solver(1, ilpy.VariableType.Continuous)

# -(x ** 2)
obj = ilpy.Objective()
obj.set_quadratic_coefficient(0, 0, -1)
solver.set_objective(obj)

# x >= 2
const2 = ilpy.Constraint()
const2.set_coefficient(0, 1)
const2.set_relation(ilpy.Relation.GreaterEqual)
const2.set_value(2)
solver.add_constraint(const2)

print(solver.solve())
funkey commented 1 year ago

That doesn't look like a segfault, I think this is just an unhandled exception of type std::runtime_error. We could catch this kind of exception and turn it into a proper python exception to fail more gracefully.

funkey commented 1 year ago

That part is quite interesting: Set NonConvex parameter to 2 to solve model.

Can it be gurobi allows non-PSD matrices now? If so, then we should just set this magic parameter to 2 ;)

tlambert03 commented 1 year ago

I agree, sorry, wrong term! I think I had seen a segfault when running a test that had included this ... but maybe I'm wrong there too :joy:

anyway, do you think it's as simple as always setting it? Or do we have to set it conditionally?

tlambert03 commented 1 year ago

note to self, can avoid the crash by adding this to model.solve():

    GRBenv* modelenv = GRBgetenv(_model);
    GRB_CHECK(GRBsetintparam(modelenv, GRB_INT_PAR_NONCONVEX, 2));

(this requires a new header file, will PR when finished exploring)

tlambert03 commented 1 year ago

037283ec7b0997ee2a452e174e15fd35f165e55d closes this issue by re-raising the exception in python...

adding support for GRB_INT_PAR_NONCONVEX can be discussed in #28