/* Global constraint k_same in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Ck_same.html """ k_same(SETS) Purpose Given |SETS| sets, each containing the same number of domain variables, the k_same constraint enforces that the multisets of values assigned to each set are all i dentical. Example ( < set-<1, 9, 1, 5, 2, 1>, set-<9, 1, 1, 1, 2, 5>, set-<5, 2, 1, 1, 9, 1> > ) The k_same constraint holds since: * The first and second collections of variables are assigned to the same multiset. * The second and third collections of variables are also assigned to the same multiset. """ 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 = 3, M = 6, X = new_array(N,M), X :: 1..9, X = {{1,9,1,5,2,1}, {_,9,_,_,_,1}, % test % {9,1,1,1,2,5}, {5,2,1,1,9,1}}, k_same(X), solve(X), println(X), fail, nl. go => true. k_same(X) => N = X.len, M = X[1].len, foreach(I in 2..N) Max = fd_max_array(X[I]), IGcc = $[J-C : J in 0..Max], JGcc = $[J-C : J in 0..Max], global_cardinality([X[I,K] : K in 1..M], IGcc), global_cardinality([X[I-1,K] : K in 1..M], JGcc), foreach(K in 1..Max) IGcc[K] #= JGcc[K] end end. fd_max_array(A) = max(Maxes) => Maxes = [fd_max(A[I]) : I in 1..A.len].