Skip to main content

How can I add constraints on the rank of the matrix

Answered

Comments

3 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

    Hi Shawn, 

    Please see the code snippet below using the Gurobi Matrix API in version 10.0 as an example implementation. It is assumed that \(X\) is a binary matrix and a nonzero row refers to a row with at least one of its elements being equal to 1. 

    m = gp.Model("")

    X = m.addMVar((20, 20), vtype=GRB.BINARY, name="x")
    y = m.addMVar(20, vtype=GRB.BINARY, name="y")

    # Ensure at most 10 non-zero rows are selected
    m.addConstr(y.sum() <= 10)

    # If row i is not selected, all its elements equals 0
    m.addConstr(X.sum(axis=1) <= 20 * y, name="")
    # If row i is selected, at least one of its elements equals 1
    m.addConstr(X.sum(axis=1) >= y, name="")
     
    Best regards,
    Maliheh
    0
  • Shawn Xu
    • Gurobi-versary
    • First Comment
    • First Question

    Thank you Maliheh! Solved!

    0

Post is closed for comments.