如何在 OR 工具中使用 AddBoolXOr
How to use AddBoolXOr in OR-tools
我尝试了解 OR 工具。我想以模 2 求解这个方程组:
x1 + x2 + x3 + 0 = 0
x1 + x2 + 0 + x4 = 1
x1 + 0 + x3 + x4 = 1
0 + x2 + x3 + x4 = 1
这等于因为模 2 为:
x1 ^ x2 ^ x3 = 0
x1 ^ x2 ^ x4 = 1
x1 ^ x3 ^ x4 = 1
x2 ^ x3 ^ x4 = 1
使用按位异或(参见 )。
所以我尝试了以下代码:
from ortools.sat.python import cp_model
# Creates the model.
model = cp_model.CpModel()
# Creates the variables.
x1 = model.NewBoolVar('x1')
x2 = model.NewBoolVar('x2')
x3 = model.NewBoolVar('x3')
x4 = model.NewBoolVar('x4')
# Creates the constraints.
model.AddBoolXOr(x1 ^ x2 ^ x3 == 0)
model.AddBoolXOr(x1 ^ x2 ^ x4 == 1)
model.AddBoolXOr(x1 ^ x3 ^ x4 == 1)
model.AddBoolXOr(x2 ^ x3 ^ x4 == 1)
# Creates a solver and solves the model.
solver = cp_model.CpSolver()
status = solver.Solve(model)
if status == cp_model.OPTIMAL:
print('x1 = %i' % solver.Value(x1))
print('x2 = %i' % solver.Value(x2))
print('x3 = %i' % solver.Value(x3))
print('x4 = %i' % solver.Value(x4))
但我得到:
'calling xor on a linear expression is not supported, '
NotImplementedError: calling xor on a linear expression is not supported, please use CpModel.AddBoolXor
如果我使用 AddBoolXor
而不是 AddBoolXOr
我得到:
AttributeError: 'CpModel' object has no attribute 'AddBoolXor'
AddBoolXor
采用布尔文字数组。
model.AddBoolXOr([x1, x2.Not(), x3])
AddBoolXor(xi)
的语义是sum(xi) % 2 == 1
所以,在你的情况下
# Creates the constraints.
model.AddBoolXOr([x1, x2, x3, True])
model.AddBoolXOr([x1, x2, x4])
model.AddBoolXOr([x1, x3, x4])
model.AddBoolXOr([x2, x3, x4])
我尝试了解 OR 工具。我想以模 2 求解这个方程组:
x1 + x2 + x3 + 0 = 0
x1 + x2 + 0 + x4 = 1
x1 + 0 + x3 + x4 = 1
0 + x2 + x3 + x4 = 1
这等于因为模 2 为:
x1 ^ x2 ^ x3 = 0
x1 ^ x2 ^ x4 = 1
x1 ^ x3 ^ x4 = 1
x2 ^ x3 ^ x4 = 1
使用按位异或(参见
所以我尝试了以下代码:
from ortools.sat.python import cp_model
# Creates the model.
model = cp_model.CpModel()
# Creates the variables.
x1 = model.NewBoolVar('x1')
x2 = model.NewBoolVar('x2')
x3 = model.NewBoolVar('x3')
x4 = model.NewBoolVar('x4')
# Creates the constraints.
model.AddBoolXOr(x1 ^ x2 ^ x3 == 0)
model.AddBoolXOr(x1 ^ x2 ^ x4 == 1)
model.AddBoolXOr(x1 ^ x3 ^ x4 == 1)
model.AddBoolXOr(x2 ^ x3 ^ x4 == 1)
# Creates a solver and solves the model.
solver = cp_model.CpSolver()
status = solver.Solve(model)
if status == cp_model.OPTIMAL:
print('x1 = %i' % solver.Value(x1))
print('x2 = %i' % solver.Value(x2))
print('x3 = %i' % solver.Value(x3))
print('x4 = %i' % solver.Value(x4))
但我得到:
'calling xor on a linear expression is not supported, ' NotImplementedError: calling xor on a linear expression is not supported, please use CpModel.AddBoolXor
如果我使用 AddBoolXor
而不是 AddBoolXOr
我得到:
AttributeError: 'CpModel' object has no attribute 'AddBoolXor'
AddBoolXor
采用布尔文字数组。
model.AddBoolXOr([x1, x2.Not(), x3])
AddBoolXor(xi)
的语义是sum(xi) % 2 == 1
所以,在你的情况下
# Creates the constraints.
model.AddBoolXOr([x1, x2, x3, True])
model.AddBoolXOr([x1, x2, x4])
model.AddBoolXOr([x1, x3, x4])
model.AddBoolXOr([x2, x3, x4])