我想根据我使用 dpcplex 模型的一些约束生成一个 20x38 二进制矩阵。一些矩阵单元预定义如下(行、列、参数):
[(8,3,0),(14,0,0),(14,2,0),(16,0,0),(16,1,0),(12,0,0),( 10,0,0),(10,8,0),(10,9,0),(17,7,0),(17,8,0),(8,0,0),(13, 8,0),(13,9,0),(1,0,1),(15,19,0)]
我需要用一些约束填充其他矩阵单元:
- 列的总和必须等于 10
- 行的总和必须等于 19
- 每行的最后 4 个单元格必须是可选的:只允许 1010 或 0101
- 不超过 2 个连续的 0 或 1
- 每行中每 5 个单元格的总和必须在 [2,3] 范围内:没有 11011 或 00100
- 连续 0 对的总和必须 <=3 :在每一行中,我们不允许有超过 3 对 00 和 3 对 11
问题是我的模型没有返回任何解决方案。我不确定我的模型是否正确。
这是我的代码:
from docplex.mp.model import Model
cond=[[8,3,0],[1,37,0],[6,9,0]]
model = Model("MatrixComple")
R = [i for i in range(20)]
R1=[i for i in range(38)]
R2=[34,35,36,37]
R3=[i for i in range(36)]
R4=[i for i in range(34)]
R5=[i for i in range(37)]
idx = [(i, j) for i in R for j in R1 ]
x = model.binary_var_dict(idx,name= "x")
"""pre-defined cells"""
for i in R:
for j in R1:
for item in cond:
i1,i2,i3=item
model.add_constraint(x[i1, i2] == i3)
"""sum of columns must be equal to 10 """
model.add_constraints(model.sum(x[i, j] for i in R) == 10 for j in R2)
"""sum of rows must be equal to 19 """
model.add_constraints(model.sum(x[i, j] for j in R1) == 19 for i in R)
"""(apply to all rows)last 4 cells of each row must be alternative: just 1010 or 0101 is allowed"""
model.add_constraints(model.sum(x[(i, j)] for j in R2 ) == 2 for i in R )
model.add_constraints(x[(i, 34)] ==x[(i, 36)] for i in R )
"""no more that 2 consecutive 0s or 1s : 110 or 001 or 101 or 010
this rule can not be applied to pre-defined cells. For example if we have 000 or 111 in pre-defined conditions,
we need to apply this rule for the rest of matrix not the pre-defined cells
"""
model.add_constraints(model.sum(x[i, j]+x[i,j+1]+x[i,j+2] for j in R3) <=2 for i in R)
model.add_constraints(model.sum(x[i, j]+x[i,j+1]+x[i,j+2] for j in R3) >=1 for i in R)
""" (apply to all rows) sum of every 5 cells in each row must be in range [2,3] : no 11011 or 00100 is allowed """
model.add_constraints(model.sum(x[i, j]+x[i,j+1]+x[i,j+2]+x[i,j+3]+x[i,j+4]for j in R4) <=3 for i in R)
model.add_constraints(model.sum(x[i, j]+x[i,j+1]+x[i,j+2]+x[i,j+3]+x[i,j+4]for j in R4) >=2 for i in R)
""" (apply to all rows) sum of pair of consecutive 0s must be <=3 : in each row we are not allowed to have
more than 3 pair of 00 """
for i in R:
s=0
for j in R5:
if x[i, j]==x[i,j+1]==0:
s+=1
model.add_constraint(s<= 3)
""" (apply to all rows) sum of pair of consecutive 1s must be <=3 : in each row we are not allowed to have
more than 3 pair of 11 """
for i in R:
s=0
for j in R5:
if x[i, j]==x[i,j+1]==1:
s+=1
model.add_constraint(s<= 3)
solution = model.solve()
print(solution)