/* Decomposition of global constraint exactly in Picat. From MiniZinc: """ Requires exactly 'n' variables in 'x' to take the value 'v'. """ Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. % % exactly(?N,?X,?N) % % Requires exactly N variables in X to take the value V. % exactly(N, X, V) => Len = X.length, BB = new_list(Len), BB :: 0..1, foreach(I in 1..Len) % B in 0..1, X[I] #= V #<=> BB[I] #=1 end, N #= sum(BB), solve(BB). % using count exactly2(N,X,V) => count(V,X,#=,N). go ?=> N :: 2..3, % N: number of V in X V :: 1..4, % V: the value Len = 4, X = new_list(Len), X :: 1..Len, exactly2(N,X,V), Vars = X ++ [N,V], solve([],Vars), writeln([v=V,n=N]), writeln(x=X), nl, fail. go => true.