Python Gurobi 中覆盖约束的覆盖矩阵

Coverage matrix to covering constraints in Python Gurobi

令 C 为集合覆盖问题的二元覆盖矩阵,我想将其转换为 Gurobi 中的适当覆盖约束。我已经设法使用 scipy.csr_matrix 让它工作,但过程似乎很慢。我想知道有没有更有效的方法。

# Convert coverage to sparse matrix
csr_cover = csr_matrix(C.astype(bool))
cover_rows = np.split(csr_cover.indices, csr_cover.indptr)[1:-1]

# add facility coverages to the covering constraints
for i in range(numDemands):
    m.addConstr(quicksum(X[j] for j in range(numSites) if i in cover_rows[j]) >= 1)

与上述方法相比,这似乎可以更快地构建约束:

cover_rows = [np.nonzero(t)[0] for t in C]

for i in range(numDemands):
    m.addConstr(quicksum(X[j] for j in cover_rows[i]) >= 1)