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

How to add more constraints after loading the model from a ".mps" file

回答済み

コメント

2件のコメント

  • 正式なコメント
    Simranjit Kaur
    • 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?.
  • Jaromił Najman
    • Gurobi Staff

    When you read a model from a file, you have to get access to the variables first before adding a constraint.

    If you know the names of the variables, one way of retrieving the variables and working with them is

    import gurobipy as gp

    # read model with only 2 variables "x" and "y"
    m = gp.read("myLP.lp")
    V = m.getVars()
    myVars = {}
    # create a dictionary {'x': <gurobi.Var x>, 'y': <gurobi.Var y>}
    for v in V:
    myVars[v.VarName] = v
    # you can now access the constraints via their names
    m.addConstr(myVars["x"] + myVars["y"] <= 0)

    If you don't have any information about the variable names, i.e., you don't know the names and/or their meaning, then I don't see a good way to work the variables when reading a model from a file.

    Best regards,
    Jaromił

    0

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