# How to make a copy of a Gurobi model in Python?

Suppose I have made a Gurobi model in Python model = Model(), defined variables, constraints, objective function, and solved it. However, I want to make a deepcopy of model denoted as new_model. How should I make a deepcopy of this model? I have tried new_model = copy.deepcopy(model), but it causes an error KeyError: '__deepcopy__'.

This question and answer are directly related to your question. But you can also look at Gurobi's discussion about copying a model in its own website, or this google group. In summary, you need to name your variable, and after copying the model remap your variable with those names(which need to be saved before).

from gurobipy import Model
# A model with 10 variables...
variable_names = {}
model = Model("test")
variables = {}
for i in range(10):
variable_names[i] = "var{0}".format(i)