/* Decomposition of global constraint alldifferent_except_0 in Picat. From Global constraint catalogue: http://www.emn.fr/x-info/sdemasse/gccat/Calldifferent_except_0.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@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. % % Ensure that all values in Xs which are != 0 are different. % alldifferent_except_0(Xs) => foreach(I in 2..Xs.length, J in 1..I-1) (Xs[I] #!= 0 #/\ Xs[J] #!= 0) #=> (Xs[I] #!= Xs[J]) end. go => N = 4, X = new_list(N), X :: 0..N, alldifferent_except_0(X), solve(X), println(x=X), statistics(backtracks, Backtracks), printf("backtracks: %d\n", Backtracks). go2 => N = 4, X = new_list(N), X :: 0..N, alldifferent_except_0(X), increasing(X), Solutions = solve_all(X), println(Solutions), println(sols=length(Solutions)). % From Picat Guide, page 74 my_count(V,L,Rel,N) => sum([V #= E : E in L]) #= Count, call(Rel,Count,N). go3 => N = 6, X = new_list(N), X :: 0..N, alldifferent_except_0(X), count(0,X,#=,2), % my_count(0,X,#=<,2), Solutions = solve_all(X), println(Solutions), println(sols=length(Solutions)), statistics(backtracks, Backtracks), printf("backtracks: %d\n", Backtracks).