Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

or-tools

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.

like image 702
Niels Mittelstädt Avatar asked Oct 29 '25 02:10

Niels Mittelstädt


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)])
like image 184
Stradivari Avatar answered Oct 30 '25 16:10

Stradivari



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!