与えられた n と mに対して、エントリが 0 または 1の n × m の部分巡回行列すべてを反復処理します。同じ合計を持つ列のサブセットが 2 つ存在しないような行列があるかどうかを調べたいと思います。ここで列を追加するときは、要素ごとに行います。私の現在のコードは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]
では、解に任意の数のゼロを使用できます。代わりに、ソリューションで許可されるゼロの正確な数を指定するにはどうすればよいですか?