Running Gurobi model in python function
AnsweredCurrently we are using Gurobi in python. So, I instantiate the model, define the model and get results all in one python function. This function is called multiple times- one function call to evaluate one problem instance.
At the end of the function I return the objective value and do model.reset(1). I do this so that the model is reset and each problem instance is evaluated separately. Two problem instances should have completely different runs of Gurobi (no parameter values overlap).
Is this correct and are there any other things to note while wrapping a Gurobi model in a python function?
(For example, should the model be deleted at the end of the function instead of reset etc.).
Also should I do model.reset(0) or model.reset(1)? Currently doing model.reset(1).
So a toy example of what I mean:
def run_gurobi_model(items, knapsack, item_weights, item_profits, knapsack_capacities):
# initlializing model
model = gp.Model('some name')
# initializing variable - decides placement of an item to knapsack
x = model.addVars(items, knapsack, name='item placement')
# Contraints
1. Some mathematical expression that restricts item placements that violates knapsack capacity
# Setting objective and optimizing
obj = total profit based on profit values of items that are placed
model.setObjective(obj, GRB.MAXIMIZE)
model.optimize()
# Resetting model and returning profit that model run gives
obj_value = model.getObjective().getValue()
model.reset(1)
return obj_value
Thanks.
-
Hi Aryaman,
In every function call, you are not changing Gurobi parameters, you are changing the problem input that results in a new model object. You can remove the model.reset(1) from the end and just have model.dispose() to delete the model object. In the next function call, when you initialize the model, you create a new model object for the new problem input.
In case you are calling the function \(\texttt{run_gurobi_model}\) in parallel, it is important to create a separate environment object for each parallel job too. Please check the article How do I use multiprocessing in Python with Gurobi?
Best regards,
Maliheh
0 -
Hi Maliheh,
Thank you so much for your response. I have two follow up questions.
1. Would keeping model.reset(1) instead of model.dispose() lead to an incorrect solution?
2. If I run the function run_gurobi_model in multiple Jupyter notebook files at the same time do I need to explicitly change the environment? I am asking since I think each Jupyter notebook has its own ipykernel, a new gurobi environment should be created in each notebook. Is this thinking correct?
Again, thanks a lot for your help.
Shaan
0 -
Hi Shaan,
1. Would keeping model.reset(1) instead of model.dispose() lead to an incorrect solution?
Not necessarily. The model.reset(1) essentially does not have any impact because you have already queried the results you need and a new model will be created in the next function call. The best practice is to free all resources as soon as you do not need the object. Therefore, it is highly recommended to call model.dispose() at the end and delete model.reset(1) as it does not do anything in your setup.
2. If I run the function run_gurobi_model in multiple Jupyter notebook files at the same time do I need to explicitly change the environment? I am asking since I think each Jupyter notebook has its own ipykernel, a new gurobi environment should be created in each notebook. Is this thinking correct?
I think you are right that each notebook will use a different environment. Following the best practice advice above, when you rely on the default environment, you can call disposeDefaultEnv() to release all the resources as soon as you do not need the environment. And this is even more important in a Jupyter notebook which is a long-running Python session and the Gurobi environment would exist for the entire duration unless it is explicitly freed.
Best regards,
Maliheh
0
Please sign in to leave a comment.
Comments
3 comments