Skip to main content

Can we print constraints as they are added? Hows?

Answered

Comments

2 comments

  • Official comment
    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.
  • Maliheh Aramon
    • Gurobi Staff

    Are you using a third-party API or a Gurobi API to build your model? Please have a look at the strategies outlined in the article "How do I improve the time to build my model?" to first identify the bottlenecks in your script and then make modifications accordingly.

    The response to your question in the title of the post is yes. The model modifications are performed in a lazy fashion in the Gurobi Optimizer. Therefore, in order to query constraints' attributes (for sanity checks) before the model is optimized, you would need to call the model.update() method explicitly. See the script below as an example.

    # Add the constraint \sum x_i <= 10
    model.addConstr(x.sum() <= 10, name="c0")
    # Add the constraint c to the model
    model.update()
    # Query the LHS, the RHS, and the sense of the constraint c0
    c0 = model.getConstrByName("c0")
    LHS, RHS, sense = model.getRow(c0), c0.RHS, c0.Sense

    Printing \(\texttt{LHS}\), \(\texttt{RHS}\), and \(\texttt{sense}\) should give:

    In [16]: LHS
    Out[16]: <gurobi.LinExpr: x[0] + x[1] + x[2] + x[3] + x[4] + x[5] + x[6] + x[7] + x[8] + x[9]>

    In [17]: RHS
    Out[17]: 10.0

    In [18]: sense
    Out[18]: '<'

    The article "How do I access the left-hand side of a constraint?" is also a useful read. 

    Best regards,

    Maliheh

    0

Post is closed for comments.