/* Global constraint open_alldifferent in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Copen_alldifferent.html """ open_alldifferent(S, VARIABLES) Purpose Let V be the variables of the collection VARIABLES for which the corresponding position belongs to the set S. Enforce all variables of V to take distinct values. Example ({2, 3, 4}, <9,1,9,3>) The open_alldifferent constraint holds since the last three (i.e., S = {2, 3, 4}) values of the collection 9,1,9,3 are distinct. """ Note: This model yield these three solutions to open_alldifferent(X, [9,1,9,3]): x = [9,1,9,3] s = [0,1,0,1] s = [2,4] x = [9,1,9,3] s = [0,1,1,1] s = [2,3,4] x = [9,1,9,3] s = [1,1,0,1] s = [1,2,4] I'm not sure if the last solution is correct... This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> N = 4, X = new_list(N), X :: 1..9, % var set of 1..n: s; S = new_list(N), S :: 0..1, X = [9,1,9,3], % S = [0,1,1,1], % {2,3,4} open_alldifferent(X, S), Vars = X ++ S, % solve($[max(sum(S))],Vars), solve([],Vars), println(x=X), println(s=S), println(s=[I : I in 1..N, S[I] == 1]), nl, fail, nl. go => true. open_alldifferent(X, S) => Len = X.len, all_different_except_0($[X[I]*S[I] : I in 1..Len ]), foreach(I in 1..Len) sum([X[J] #= X[I] : J in 1..Len]) #= 1 #=> S[I] #= 1 end.