Is it possible to add a XOR Equation Constraint to a CP-Solver model in OR-Tools?

Viewed 310

I'm trying to solve a problem with Google OR-Tools' CP-Solver. Is it possible to add a constraint like this: x1 XOR x2 XOR x3 == 0 Thanks in advance.

1 Answers

AddBoolXOr of n booleans means that the sum is odd. You could just add another True boolean.

from ortools.sat.python import cp_model

model = cp_model.CpModel()
solver = cp_model.CpSolver()


a = model.NewBoolVar("")
b = model.NewBoolVar("")
c = model.NewBoolVar("")
model.AddBoolXOr([a, b, c, 1])

solver.Solve(model)

print([solver.Value(x) for x in (a, b, c)])
Related