Can model.copy() also copy my own data structures of variables in the original model (in Python)?
AnsweredGreetings,
I made a model and added binary decision variables into a data structure like the following in Python:
def make_model(some_arguments):
model = grb.Model()
......
x[i,j] = model.addVar(......)
y[i,j] = model.addVar(......)
......
model._data = x, y
return model
Then in another function I can extract those groups of variables, i.e. x, y, like
def solve_model(model):
x, y = model._data
......
Now I want to make a copy of the original model and manipulate the copy and solve it again. the code is something like
copy = model.copy()
This is a good copy of the original model but it does not copy my data structure of x, y. When I call the following function
def solve_copy(copy):
x, y = copy._data
for i in y:
y[i].ub = 0
......
The line of 'x, y = copy._data' will throw an error of 'AttributeError: 'gurobipy.Model' object has no attribute '_data''. then I cannot manipulate y with their upper bounds.
Can anyone give me some suggestions about how to copy a model and its data structures of decision variables simultaneously?
Thanks,
Larry

Hello there,
I don't know the policy of this forum regarding duplicate posts.
To avoid posting the same topic, I thus post here to see if anyone has an answer to this, as I also would like to be able to copy a model together with its custom attributes (attributes starting with _)
Thanks a lot,
Regards,
Baptiste0 
Hi Larry and Baptiste,
I don't suppose there is a way currently to copy user data when calling Model.copy(). I recommend to only use one data object _data to store all your user data and after copying the model call model2._data = model._data.
Cheers,
Matthias0 
Hi Matthias,
Thanks for your answer.
However for the case described here, using model2._data = model._data will not work, as model._data contains variables from model, model2._data would also contain the variables from model. Here is a minimal working example in Python :
m = Model()
x = m.addVar(vtype=GRB.BINARY, name="x")
y = m.addVar(vtype=GRB.BINARY, name="y")
m.update()
m._data = [x,y]
m2 = m.copy()
m2._data = m._data
x2,y2 = m2._data
m2.addConstr(x2 + y2 == 1)The addConstr call then throw an error "GurobiError: Variable not in model".
I am not surprised by this though, but it would be nice to be able to make it work "easily".
Of course, you can always create the correct m2._data using
m2._data = [m2.getVarByName("x"), m2.getVarByName("y")]
but it is a bit cumbersome.The usecase I was thinking about is e.g. callbacks, where using custom attributes makes it really easy to access the variables inside the callback. Having the correct variables directly in m2._data make it easier to use e.g. two different callbacks.
But I'll use getVarByName to get it done, the current behavior makes sense in a way, I was just wondering if there was some obscure way to easily do this.
Thanks again,
Regards,
Baptiste0 
Thank you, Baptiste, for sharing your practice. It's helpful.
Larry
0
Please sign in to leave a comment.
Comments
4 comments