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

Which constraints are active at Optimization Termination

回答済み

コメント

2件のコメント

  • 正式なコメント
    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.
  • Riley Clement
    • Gurobi Staff

    Hi Nitin,

    Any constraint that is active at the solution will theoretically have slack value of 0 ("there is no slack in the constraint").  In practice we compare against a number close to 0, such as 1e-6, due to the consequences of floating point arithmetic.

    There looks to be a typo in the post from the screenshot and the condition should be

    if c.Slack < 1e-6:

    Equality constraints will always have a theoretical slack of 0 so you can filter them out via the "Sense" attribute as you suggest.

    You may wish to play around with the following toy model, where the optimal solution and active constraints at the solution should be apparent.

    m = gp.Model()

    x = m.addVar()
    y = m.addVar()
    c1=m.addConstr(x + y <= 1)
    c2=m.addConstr(2*x + y <= 2)
    c3=m.addConstr(x + 2*y == 2)
    m.setObjective(y, gp.GRB.MAXIMIZE)

    m.optimize()
    print(x.X, y.X) # should be (0,1)
    print(c1.Slack, c2.Slack, c3.Slack)  # should be (0,1,0)

    # note for c2 slack:
    # what is the value of 2 - y - 2*x at the optimal solution?

    - Riley

    1

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