对于给定的 n 和 m,我迭代所有 n × m 部分循环矩阵,其条目为 0 或 1。我想查找是否存在一个矩阵,使得不存在具有相同总和的列的两个子集。在这里,当我们添加列时,我们只是按元素进行。我当前的代码通过ortools使用约束编程。这是我的代码。
from scipy.linalg import circulant
import numpy as np
import itertools
from ortools.constraint_solver import pywrapcp as cs
n = 4
m = 6
def isdetecting(matrix):
solver = cs.Solver("scip")
X = np.array([solver.IntVar(values) for i in range(matrix.shape[1])])
X1 = X.tolist()
for row in matrix:
x = X[row].tolist()
solver.Add(solver.Sum(x) == 0)
db = solver.Phase(X1, solver.CHOOSE_FIRST_UNBOUND, solver.ASSIGN_CENTER_VALUE)
solver.NewSearch(db)
count = 0
#Find just one non-zero solution if there is one
while (solver.NextSolution() and count < 2):
solution = [x.Value() for x in X1]
count += 1
solver.EndSearch()
if (count == 1):
return True
values = [-1,0,1]
nosols = 0
for row in itertools.product([0,1],repeat = m):
M = np.array(circulant(row)[0:n], dtype=bool)
if isdetecting(M):
nosols += 1
print M.astype(int)
该行values = [-1,0,1]
允许解中有任意数量的零。如何指定解决方案中允许的确切数量的零?