Reputation: 1
I use PyChoco.
I have three vectors of 0 and 1 named O1, O2, O3 and I would like to write the constraint:
O3 = (01 + O2) %2
The best I have done:
O1 = model.intvars(2,0,1)
O2 = model.intvars(2,0,1)
O3 = model.intvars(2,0,2)
for k in range(2) :
model.arithm(O1[k], '+', O2[k], '=', O3[k])
but the components of O3 are O,1 or 2 because I don't succeed to add the %2.
Upvotes: 0
Views: 59
Reputation: 7
to add the %2 you need to add in the model the "mod" constraint. And to do that, you will have to flatten your constraint. Flattening is a very common operation that has to be done during modeling, to have your constraints in a way the solver accepts them.
In your case, this means that you will have to add a new auxilliary variable to represent the expression O1[k] + O2[k], i.e., O1[k], '+', O2[k] = AUX1.
Then another auxilliary variable will represent the expression AUX1%2 (i.e., (O1[k] + O2[k]) % 2): AUX1 % 2 = AUX2
Then, you will use the last auxillary variable (AUX2) in the arithm constraint.
However, I would suggest to use a modeling language, where you can post the constraint as you like it, and the burden of flattening for the specific solver is on the modeling language.
An example in python is CPMpy, which supports choco solver, along with more solvers like the ortools CP-SAT.
Here is how you can do that in cpmpy, using pychoco:
import cpmpy as cp
O1 = cp.intvar(0,1,shape=2)
O2 = cp.intvar(0,1,shape=2)
O3 = cp.intvar(0,2,shape=2)
model = cp.Model()
model += (O1 + O2) % 2 == O3
# The above is the same as using the following for loop as cpmpy variables are based in numpy
#for k in range(2) :
# model += (O1[k] + O2[k]) % 2 == O3[k]
model.solve("choco")
Upvotes: 0