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

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

回答済み

コメント

5件のコメント

  • 正式なコメント
    Simranjit Kaur
    • Gurobi Staff
    This post is more than two 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 try Gurobot, our chatbot interface offering instant, expert-level support.
  • Jonasz Staszek
    • 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

    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

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