/* (Decomposition of) global constraint nvalue Picat. From MiniZinc: """ Requires that the number of distinct values in 'x' is 'n'. """ Note: Picat has a built-in nvalue/2. Here we define nvalue_me/2 instead. 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, X = new_list(Len), X :: 1..Len, N :: 1..Len, % nvalue(N,X), % built-in nvalue_me(N,X), % decomposition Vars = X ++ [N], solve(Vars), writeln([n=N, x=X]), fail. go => true. % % nvalue(?N,?X) % % Requires that the number of distinct values in X is N. % nvalue_me(N, X) => Len = length(X), LB = min([fd_min(X[I]) : I in 1..Len]), UB = max([fd_max(X[I]) : I in 1..Len]), N #= sum([ (sum([ (X[J] #= I) : J in 1..Len]) #> 0) : I in LB..UB]).