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

Incorrect imports in Model #388

Open
chopin57otu opened this issue Jul 26, 2024 · 1 comment
Open

Incorrect imports in Model #388

chopin57otu opened this issue Jul 26, 2024 · 1 comment

Comments

@chopin57otu
Copy link

Describe the bug
Cannot pass own solver to class Model constructor because of incorrect imports.

To Reproduce
To reproduce:

import mip
from mip.cbc import SolverCbc


class MySolver(SolverCbc):
    pass


class MyModel(mip.Model):
    def __init__(self, name: str = "", sense: str = mip.MINIMIZE, *args, **kwargs):
        if kwargs["solver_name"].upper() in ("MYSOLVER"):
            kwargs["solver"] = MySolver(self, name, sense)
        super().__init__(name, sense, *args, **kwargs)


if __name__ == "__main__":
    solver = MyModel(solver_name="MYSOLVER")
  File "C:\nano_sources\shop\shop\models\t_solver.py", line 17, in <module>
    solver = MyModel(solver_name="MYSOLVER")
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "...\t_solver.py", line 13, in __init__
    super().__init__(name, sense, *args, **kwargs)
  File "...\Lib\site-packages\mip\model.py", line 107, in __init__
    self.constrs = mip.ConstrList(self)
                   ^^^
UnboundLocalError: cannot access local variable 'mip' where it is not associated with a value

Expected behavior
Basically it is not possible to pass any argument in solver and not get the error above. Please see my bugreport for cpython for explanation why this happens and what is wrong

Desktop (please complete the following information):

  • Operating System, version: Windows
  • Python version: Probably all of them, tested on 3.11 and 3.12
  • Python-MIP version (we recommend you to test with the latest version): 1.15

Additional context
The reason I want to do this are some inconsistencies in handling nans in LinExpr between CBC and Gurobi. We have created model exploiting the CBC behaviour, which is throwing away anything containing nan. This is useful when defining models with LinExprTensor where some of the elements does not contain any variable, leading e.g. to constraints like 1==1. I wanted to create my own solver to make it consistent.

@chopin57otu chopin57otu changed the title Incorrect imports in Module Incorrect imports in Model Jul 30, 2024
@chopin57otu
Copy link
Author

chopin57otu commented Jul 30, 2024

#389 @sebheger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant