I am trying to define a constraint that is going to limit the number of unique values generated by the solver. It can generate as many duplicates as possible to solve the problem but there is a limit on unique values.
I am trying to define a constraint that is going to limit the number of unique values generated by the solver. It can generate as many duplicates as possible to solve the problem but there is a limit on unique values.
For each value, create a boolean variable selected_value that is true iff at least one value is assigned to it.
For this you will need 2 sets of constraints:
from left to right: selected_value implies the value is selected
model.AddBoolOr([var_i_equals_value, ...]).OnlyEnforcedIf(selected_value)
and from right to left, the value is selected implies selected_value is true
for var_i_equals_value in ...:
model.AddImplication(var_i_equals_value, selected_value)
And then you just need to constraint the sum of the selected values
model.Add(sum(selected_value_array) <= 10)
Now you need to ask if you really need integer variables, or just a matrix of Boolean variables. I suggest the latter.