I am new to constraint programming and to Minizinc. I have look for a solution to this not relly hard task but I found nothing.
I want to count the number of different elements that appear in an array: This is the declaration of my array:
array[1..n,1..n] of var 1..n: countLeft;
I have try to do like this:
constraint
forall(j in 1..n) (
length(array2set([countLeft[i,j]|i in 1..stopCountLeft[j]]) )==left_vision[j]
);
But apparently my array is of type: array[int]of var opt int and is not accept by the function array2set.
Any ideas?
There might be different approaches you could take, but an approach that is similar to what you try would be to split the counting of different elements in an array into two steps:
We can use the global
global_cardinalityconstraint to count the occurrences and then use a simply count constraint over its result.Note, however, that this might not be the best code for your model. For some solvers
global_cardinalitymight not be perform well enough. Similarly if yourstopCountLeftcontains variables, then that means that you are creating a array of optional variables, andglobal_cardinalitymight not be defined for optional variables.Instead we can write an implication graph instead. The idea is still the same, but instead of counting the number of a value occurring, we just use a boolean value to signal wether the value is in use.
Note that the problem with this approach is the exponential number of implications posted in the
forallloop. However, I suspect that, with implications being small, it would perform reasonably well.