I have two arrays of model variables – arr1
and arr2
. I would like to declare a constraint that would forbid a situation when there is at least 1 true
in arr1
AND at least 1 true
in arr2
. (There can be multiple true
‘s in arr1
, as long as arr2
has only false
‘s – and vice versa. It is also acceptable if there is no true
in neither array).
So, something like that:
arr1 = [model.new_bool_var("a"), model.new_bool_var("b"), ...]
arr2 = [model.new_bool_var("q"), model.new_bool_var("w"), ...]
model.add_at_most_one(bool_or(arr1), bool_or(arr2))
Hower, such syntax doesn’t exist – there is only add_bool_or()
method, not generic bool_or()
that could serve to build inner clauses for more complex constraints.
Therefore, I thought about taking the following approach:
sum_arr1 = model.new_int_var(0, len(arr1), "sum_arr1")
sum_arr2 = model.new_int_var(0, len(arr2), "sum_arr2")
model.add(sum_arr1 == sum(arr1)) # error here
model.add(sum_arr2 == sum(arr2))
arr1_at_least_one = model.new_bool_var("arr1_at_least_one")
arr2_at_least_one = model.new_bool_var("arr2_at_least_one")
model.add(sum_arr1 > 0).only_enforce_if(arr1_at_least_one) # channeling constraints
model.add(sum_arr1 == 0).only_enforce_if(~arr1_at_least_one)
model.add(sum_arr2 > 0).only_enforce_if(arr2_at_least_one)
model.add(sum_arr2 == 0).only_enforce_if(~arr2_at_least_one)
model.add_at_most_one(arr1_at_least_one, arr2_at_least_one)
This looks like a lot of code for such a simple thing. The syntax is correct, however I receive the following error on line #3:
TypeError: not an linear expression: Ellipsis
Could you help me declare such constraint?
In other words, it is NAND(OR(arr1), OR(arr2))
you just need to create the bool var arrays correctly.
What is the semantics of … ? What is the length of the array. The model needs to be grounded when passed to the solver.
The rest of the code is OK.