Confusing Behaviour with disposing of Gurobi Environment
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by reading mip/model.py around lines 135-136, where model cleanup is referenced, then reproduce the reported snippets with Python-MIP 1.15.0 and Gurobi on the stated environment. Compare the lifecycle of del model with del model.solver and verify whether both release the Gurobi environment so a subsequent gp.Model() succeeds.
Written by the indexing model from the issue text.
Description
Describe the bug
I have a single use Gurobi licence so I can't have more than one Gurobi environment running at the same time. However, I'm getting some confusing behaviour when trying to clean up the Gurobi environment after a run.
To Reproduce
So if I firstly run this:
import gurobipy as gp
from itertools import product
from mip import Model
model = Model(solver_name='GUROBI')
x = model.add_var(name="X", lb=10, ub=80)
model.objective = x
status = model.optimize()
print(status)
# Create a new model
m = gp.Model()
# Create variables
x = m.addVar(vtype='B', name="x")
# Set objective function
m.setObjective(x, gp.GRB.MAXIMIZE)
# Solve it!
m.optimize()
print(f"Optimal objective value: {m.objVal}")
print(f"Solution values: x={x.X}, y={y.X}, z={z.X}")
gp.disposeDefaultEnv()
Then I get an error GurobiError: Single-use license. Another Gurobi process with pid 3981 running. when it tries to create a new model using gp.Model().
So then I tried to dispose of the model using del model first like so:
import gurobipy as gp
from itertools import product
from mip import Model
model = Model(solver_name='GUROBI')
x = model.add_var(name="X", lb=10, ub=80)
model.objective = x
status = model.optimize()
print(status)
del model
# Create a new model
m = gp.Model()
# Create variables
x = m.addVar(vtype='B', name="x")
# Set objective function
m.setObjective(x, gp.GRB.MAXIMIZE)
# Solve it!
m.optimize()
print(f"Optimal objective value: {m.objVal}")
print(f"Solution values: x={x.X}, y={y.X}, z={z.X}")
gp.disposeDefaultEnv()
However this produces the same error.
The thing that confuses me is that it works when I delete the solver directly like so:
import gurobipy as gp
from itertools import product
from mip import Model
model = Model(solver_name='GUROBI')
x = model.add_var(name="X", lb=10, ub=80)
model.objective = x
status = model.optimize()
print(status)
del model.solver
# Create a new model
m = gp.Model()
# Create variables
x = m.addVar(vtype='B', name="x")
# Set objective function
m.setObjective(x, gp.GRB.MAXIMIZE)
# Solve it!
m.optimize()
print(f"Optimal objective value: {m.objVal}")
print(f"Solution values: x={x.X}, y={y.X}, z={z.X}")
gp.disposeDefaultEnv()
The thing I don't understand is that this line: https://github.com/coin-or/python-mip/blob/master/mip/model.py#L135-L136 implies that when we do del model then it should be doing del model.solver under the hood. So I don't understand why del model.solver works but del model doesn't.
Steps to reproduce the behavior please include attached any files needed to reproduce the error. We can only help you if we can reproduce the error ourselves and debug.
Expected behavior
I would expect both del model and del model.solver to clean up the Gurobi environment.
Desktop (please complete the following information):
- Operating System, version: Debian GNU/Linux 10 (buster)
- Python version: 3.8.12
- Python-MIP version (we recommend you to test with the latest version): 1.15.0
Let me know if you need any more information 😊
- Dominant language
- Linear Programming
- Stars
- 601
- Forks
- 108
- PR merge metrics
- No merged PRs in 30d
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from coin-or/python-mip
-
Difficulty 3/5 1-2 days Newbie friendliness 45/100
coin-or/python-mip#426 ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
coin-or/python-mip#425 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 35/100
coin-or/python-mip#423 · 4 comments · 3 reactions ·
-
Difficulty 3/5 1-2 days Newbie friendliness 55/100
coin-or/python-mip#422 · 1 comment ·
-
Difficulty 4/5 3-5 days Newbie friendliness 45/100
coin-or/python-mip#420 · 4 comments ·
All issues in coin-or/python-mip
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
-
external-issue to-triage
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
googleapis/google-cloud-swift#1151 ·
-
external
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
langchain-ai/langgraph#9074 · 1 comment ·
-
documentation specification
Difficulty 1/5 Under an hour Newbie friendliness 90/100
openai/openai-openapi#584 ·