coin-or / python-mip

Python-MIP: collection of Python tools for the modeling and solution of Mixed-Integer Linear programs
Eclipse Public License 2.0
514 stars 89 forks source link

`model.remove` broken for variables in mip 1.14.0 #294

Closed cdiener closed 1 year ago

cdiener commented 1 year ago

Describe the bug Removing variables from a model is broken due to trying to write a protected property. This was caught while running the tests for the optlang package:

src/optlang/interface.py:1249: in variables
    self.update()
src/optlang/interface.py:1491: in update
    self._remove_variables(rm_var)
src/optlang/coinor_cbc_interface.py:687: in _remove_variables
    self.problem.remove(mip_vars)
../../.local/lib/python3.10/site-packages/mip/model.py:1424: in remove
    self.vars.remove(vlist)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

self = <mip.lists.VarList object at 0x7f3610954fd0>, vars = [<mip.entities.Var object at 0x7f36109783a0>]

    def remove(self: "VarList", vars: List["mip.Var"]):
        iv = [1 for i in range(len(self.__vars))]
        vlist = [v.idx for v in vars]
        vlist.sort()
        for i in vlist:
            iv[i] = 0
        self.__model.solver.remove_vars(vlist)
        i = 0
        for v in self.__vars:
            if iv[v.idx] == 0:
>               v.idx = -1
E               AttributeError: can't set attribute 'idx'

../../.local/lib/python3.10/site-packages/mip/lists.py:72: AttributeError

To Reproduce

Try to remove variables from a model.

Expected behavior

It should remove the variable and not crash.

Desktop (please complete the following information):

Additional context Add any other context about the problem here.

cdiener commented 1 year ago

Thanks!