Skip to main content

How do I know which constraint is reaching its limit first?

Answered

Comments

5 comments

  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Hi Sandra,

    The Slack attribute can tell you which constraint is active at the current solution. After an optimization run is finished and a feasible point has been found, you can check whether the slack of a linear constraint is almost \(0\)

    for c in model.getConstrs():
    if c.Slack < 1e-6:
    print('Constraint %s is active at solution point' % (c.ConstrName))

    Note that equality constraints are always active. For quadratic constraints, you can get the slack via the QCSlack attribute.

     

    EDIT: Fixed sign from ">" to "<".

    Best regards, 
    Jaromił

    0
  • Sandra Viviana Lopez Chaparro
    • Gurobi-versary
    • First Comment
    • First Question

    Dear Jaromil.

    Thank you for your answer; I would like to point out that I am working in Matlab, using YALMIP, and Gurobi is my solver. Do you have any guide or suggestion to implement this code in Matlab?

    Thank you again.

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Dear Sandra,

    Attributes are not supported in the MATLAB API. To get the information you seek, you have to compute the value of your constraints at the solution point after an optimal optimization run by hand, i.e., multiply each coefficient with the corresponding variable and check vs the respective right hand side.

    Best regards, 
    Jaromił

    0
  • Sudheer Reddy
    • Conversationalist
    • Gurobi-versary
    • Investigator


    Can we get active variables just like active constraint's where slack is 0. For Decision variables if we keep upper and lower bound where I am not writing any constraints 

    Since equality constraints are always active, so is there a way to skip those while doing for loop in model.getConstrs()

    0
  • Jaromił Najman
    • Gurobi Staff Gurobi Staff

    Can we get active variables just like active constraint's where slack is 0. For Decision variables if we keep upper and lower bound where I am not writing any constraints 

    Since equality constraints are always active, so is there a way to skip those while doing for loop in model.getConstrs()

    I am not sure I understand your question. Since you mention \(\texttt{model.getConstrs()}\), I assume that you are using the Python API. In this case, you can access all attributes as described in the documentation.

    0

Please sign in to leave a comment.