% % Global constraint all_different except 0 in MiniZinc. % % From Global constraint catalogue: % http://www.emn.fr/x-info/sdemasse/gccat/sec4.6.html % """ % Enforce all variables of the collection VARIABLES to take distinct values, except those % variables that are assigned to 0. % % Example % (<5, 0, 1, 9, 0, 3>) % % The alldifferent_except_0 constraint holds since all the values (that are different from 0) % 5, 1, 9 and 3 are distinct. % % % Model created by Hakan Kjellerstrand, hakank@bonetmail.com % See also my MiniZinc page: http://www.hakank.org/minizinc include "globals.mzn"; int: n; array[1..n] of var 0..9: x; var int: z; % number of zeros solve satisfy; % solve minimize z; predicate all_different_except_0(array[int] of var int: x) = let { int: n = length(x) } in forall(i,j in 1..n where i != j) ( (x[i] > 0 /\ x[j] > 0) -> x[i] != x[j] ) ; constraint x = [5,0,1,9,0,3] /\ all_different_except_0(x) /\ z = sum(i in 1..n) (bool2int(x[i] = 0)) ; % % data % n = 6;