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

How to save model construction in a file to be able to update later

回答済み

コメント

3件のコメント

  • Maliheh Aramon
    • Gurobi Staff

    Hi, 

    The Gurobi model object can be written into a file in one of the following four formats:

    For the scenario you described, it makes sense to save the model in an MPS or an LP format by calling the model.write("model.mps") or model.write("model.lp") method. You can then read the model file in by calling model.read("model.mps") or model.read("model.lp") method. Make sure all the variables and constraints have explicit names before writing the model into a file. 

    Please check the links provided to learn more about how different file formats differ. 

    Best regards,

    Maliheh

    0
  • Farzane Ezzati
    • Gurobi-versary
    • First Comment
    • First Question

    Hey Maliheh,

    I have a question regarding your answer.

    Assume I have saved the model with variables named X , Y and various constraints in .mps format in a file named a.mps. I want to load/open the model for modification in another file (could be anything such as python file). How can I access the model X and Y variables and constraints after I opened it?

    Thanks,

    Farzane 

     

     

    0
  • Maliheh Aramon
    • Gurobi Staff

    Hi Farzane, 

    How can I access the model X and Y variables and constraints after I opened it?

    You can use the global function gurobipy.read() function to read the model file in as a Model object. You can then rely on methods such as Model.getConstrs() and Model.getVars() to retrieve the list of all linear constraints and variables, respectively, in the model. You can then access each constraint or variable information using the available linear constraint and variable attributes.

    To access a linear constraint or a variable by name, you can use the methods Model.getConstrByName() and Model.getVarByName()

    To retrieve the list of quadratic constraints or general constraints, you can use the methods Model.getQConstrs() and Model.getGenConstrs().

    Best regards,

    Maliheh

    0

サインインしてコメントを残してください。