Running multiple (unrelated) LP models
Answered-
Hi Nan,
If the models are completely different from each other and you have to create them from scratch in each iteration, then you should create a new model at the beginning of your loop iteration and dispose of it at the end. This can be automated by using Python's context manager. The method model.reset() only resets the solution progress, the model formulation stays the same.
Note that each model requires a Gurobi environment (e.g., for licensing), but the same environment can be used for all the models.
You might use something like the following:import gurobipy as gp
...
with gp.Env() as env: # The environment is disposed of automatically through the context manager # upon leaving this block.
for i in range(N): with gp.Model(env=env) as model: # 'model' is now an instance tied to the enclosing Env object 'env'. # The model is disposed of automatically through the context manager # upon leaving this block. model.addVars(...)
...
model.optimize()
...In this article, there are a few further hints to clean up your models and environments.
1 -
Hi Mario, thank you for your answer! It has gotten much clearer now what to do. I do wonder: why do we need a Gurobi Env for this use case? It’s the first time I see it, although I have solved other LP models with Gurobi. Thank you for your answer!
0 -
Hi Nan,
Mario is away for a few days so I'll jump in. Every time you solve a model with Gurobi you will have used a Gurobi Env. If you didn't explicitly create one then a default environment would have been used. It is often not necessary to explicitly create an environment but it is good practice.
You can learn more about this here:
How do I manage Gurobi environments in gurobipy?- Riley
1
Please sign in to leave a comment.
Comments
3 comments