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
-
Official comment
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. -
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
Post is closed for comments.
Comments
3 comments