Skip to main content

Why Variable is not in model after model.copy()

Answered

Comments

4 comments

  • Jonasz Staszek
    • Community Moderator Community Moderator
    • Gurobi-versary
    • Thought Leader
    • First Question

    Hi Runfeng,

    in Gurobi a variable is an object which is tied to a model instance. Whenever you create a new model instance (e.g. by copying or creating a presolved model) you also generate "new" variables.

    One way to go about this could be, for example, trying to recreate the LinExpr by storing the names of the variables which are included in it in a list and then rebuilding it by, say:

    LinExprN = gp.LinExpr([1 for _ in var_names_to_include], [n.getVarByName(i) for i in var_names_to_include])

    Perhaps someone from Gurobi team can offer a more efficient approach?

    Best regards
    Jonasz

    0
  • Runfeng Yu
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Jonasz,

    Thank you for your plain explanation and timely reply!
    And I will try this method to recreate my codes.

    Best regards,
    Runfeng

    0
  • Elisabeth Rodríguez Heck
    • Gurobi Staff Gurobi Staff

    Hi Runfeng, hi Jonasz,

    Perhaps someone from Gurobi team can offer a more efficient approach?

    Jonasz reply is correct, a Gurobi Var object is always associated with a particular model. It is not possible to create variables (or constraints) only once and use them on multiple models.

    Rebuilding the LinExpr as Jonasz suggests is the way to go.

    Best regards,

    Elisabeth

    0
  • Runfeng Yu
    • Gurobi-versary
    • Conversationalist
    • First Question

    Hi Elisabeth,

    Thank for your reply! And it really helps. 

    Best regards,
    Runfeng

    0

Please sign in to leave a comment.