/* Global constraint same in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Csame.html """ The variables of the VARIABLES2 collection correspond to the variables of the VARIABLES1 collection according to a permutation. Example ( <1,9,1,5,2,1>, <9,1,1,1,2,5> ) The same constraint holds since values 1, 2, 5 and 9 have the same number of occurrences within both collections <1,9,1,5,2,1> and <9,1,1,1,2,5>. Figure 4.240.1 illustrates this correspondence. """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go => N = 6, Variables1 = new_list(N), Variables1 :: 0..9, Variables2 = new_list(N), Variables2 :: 0..9, Variables1 = [1,9,0,5,2,1], % Variables2 = [9,1,1,2,5,0], same(Variables1,Variables2), Vars = Variables1 ++ Variables2, solve(Vars), println(variables1=Variables1), println(variables2=Variables2), nl, fail, nl. % % same(VARIABLES1, VARIABLES2) % same(Variables1, Variables2) => Len = Variables1.len, Ub = max([fd_max(V) : V in Variables1]), GCC = new_list(Ub+1), GCC :: 0..Len, global_cardinality2(Variables1, GCC), global_cardinality2(Variables2, GCC). global_cardinality2(A, Gcc) => Len = length(A), Max = length(Gcc), Gcc :: 0..Len, foreach(I in 0..Max-1) count(I,A,#=,Gcc[I+1]) end.