optlang icon indicating copy to clipboard operation
optlang copied to clipboard

coinor_cbc interface gives incorrect solutions for MIP problems

Open cdiener opened this issue 3 years ago • 0 comments

Problem description

The coinor_cbc interface will give incorrect solutions. Sometimes this happens on the first optimizations and sometimes on later ones, but it is definitely inconsistent.

Code Sample

Create a minimal, complete, verifiable example.

In [1]: from cobra.test import create_test_model

In [2]: from cobra.medium import minimal_medium

In [3]: mod = create_test_model("textbook")

In [4]: mod.solver
Out[4]: <optlang.cplex_interface.Model at 0x7f3c1bba5cd0>

In [5]: minimal_medium(mod, 0.1, minimize_components=True)
Out[5]: 
EX_glc__D_e    10.000000
EX_nh4_e        2.866361
EX_pi_e         0.367870
dtype: float64

In [6]: minimal_medium(mod, 0.1, minimize_components=True)
Out[6]: 
EX_glc__D_e    10.000000
EX_nh4_e        2.866361
EX_pi_e         0.367870
dtype: float64

In [7]: mod.solver = "coinor_cbc"
OSQP and CBC are specialized solvers for quadratic programming (QP) and mixed-integer programming (MIP) problems and may not perform well on general LP problems. So unless you intend to solve a QP or MIP problem, we recommend to change the solver back to a general purpose solver like `model.solver = 'glpk'` for instance.

In [8]: mod.optimize()
Out[8]: <Solution 0.874 at 0x7f3c1baa6cd0>

In [9]: minimal_medium(mod, 0.1, minimize_components=True)
Out[9]: 
EX_glc__D_e    8.259189
EX_nh4_e       0.545280
EX_pi_e        0.367870
dtype: float64

In [10]: minimal_medium(mod, 0.1, minimize_components=True)
Out[10]: 
EX_glc__D_e    2.389862
EX_nh4_e       0.545280
EX_o2_e        3.484045
EX_pi_e        0.367870
dtype: float64

This should always return a solution with 3 components.

Context

optlang has no show_versions.... need to fix the template here.

It is optlang 1.5.1 with cobra 0.22 though.

The cbc interface also fails a lot of other cobrapy tests due to some bugs in the interface, For instance gapfilling will expose a bug where variables are added twice.

cdiener avatar Apr 29 '21 21:04 cdiener