How can I add constraints on the rank of the matrix
AnsweredX is a 20*20 square matrix, and I want to let the number of nonzero rows in X <= 10, how can I write the constraint? Appreciate your help
0
-
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,Maliheh0 -
Thank you Maliheh! Solved!
0
Please sign in to leave a comment.
Comments
2 comments