/* Global constraint among in Picat. Among: Requires exactly 'n' variables in 'x' to take one of the values in 'v'. From Global Constraint Catalog: http://www.emn.fr/x-info/sdemasse/gccat/Camong.html """ Constraint among(NVAR,VARIABLES,VALUES) ... Purpose NVAR is the number of variables of the collection VARIABLES that take their value in VALUES. Example: (3, <4, 5, 5, 4, 1>, <1,5,8>) The among constraint holds since exactly 3 values of the collection of values <4, 5, 5, 4, 1> belong to the set of values {1, 5, 8}. """ Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => Len = 5, R = 8, X = new_list(Len), X :: 1..R, % % The set {1,5,8} % Note that this is a list V = [1,5,8], % X = [4,5,5,4,1], % the example above % N: number of elements in X that is in V N :: 1..Len, N #= 3, among(N, X, V), solve(X), println(x=X), println(n=N), nl. go2 => Len = 5, R = 8, X = new_list(Len), X :: 1..R, % % The set {1,5,8} % Note that this is a list V = [1,5,8], % X = [4,5,5,4,1], % the example above % N: number of elements in X that is in V N :: 1..Len, N #= 3, L = findall([X,N], ( among(N, X, V), solve(X) )), Len2 = length(L), foreach([X2,N2] in L) writeln([n=N2, x=X2, v=V]) end, nl, writeln(len=Len2), nl. /* among(n, x, v) From MiniZinc globals.mzn: """ Requires exactly 'n' variables in 'x' to take one of the values in 'v'. """ */ among(N,X,V) => N #= sum([B : El in X, B #<=> El in V]).