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

How can I add constraints on the rank of the matrix

回答済み

コメント

2件のコメント

  • 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

サインインしてコメントを残してください。