メインコンテンツへスキップ

Can model.copy() also copy my own data structures of variables in the original model (in Python)?

回答済み

コメント

5件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff Gurobi Staff
    This post is more than three years old. Some information may not be up to date. For current information, please check the Gurobi Documentation or Knowledge Base. If you need more help, please create a new post in the community forum. Or why not try our AI Gurobot?.
  • Baptiste Lambin
    • Gurobi-versary
    • First Comment

    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,
    Baptiste

    0
  • Matthias Miltenberger
    • Gurobi Staff Gurobi Staff

    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,
    Matthias

    0
  • Baptiste Lambin
    • Gurobi-versary
    • First Comment

    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,
    Baptiste

     

    0
  • Bo Lan
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you, Baptiste, for sharing your practice. It's helpful.

    Larry

    0

投稿コメントは受け付けていません。