Skip to main content

Which constraints are active at Optimization Termination



1 comment

  • Riley Clement
    Gurobi Staff 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)

    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


Please sign in to leave a comment.