/* Global constraint counts in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Ccounts.html """ counts (VALUES, VARIABLES, RELOP, LIMIT) Let N be the number of variables of the VARIABLES collection assigned to a value of the VALUES collection. Enforce condition N RELOP LIMIT to hold. Example ( <1, 3, 4, 9>, <4, 5, 5, 4, 1, 5>, =, 3 ) Values 1, 3, 4 and 9 of the VALUES collection are assigned to 3 items of the VARIABLES=<4, 5, 5, 4, 1, 5> collection. The counts constraint holds since this number is in fact equal (RELOP is set to =) to the last argument of the counts constraint. """ 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 ?=> M = 4, N = 6, Variables = new_list(N), Variables :: 1..5, Values = new_list(M), Values :: 1..9, Limit :: 0..9, Values = [1,3,4,9], Variables = [4,5,5,4,1,5], % Limit #= 3, counts(Values, Variables, #=, Limit), Vars = Variables ++ Values ++ [Limit], solve(Vars), println(values=Values), println(variables=Variables), println(limit=Limit), nl, fail, nl. go => true. % % counts(VARIABLES, VALUES, RELOP, LIMIT) % counts(Variables, Values, Relop, Limit) => S #= sum([ sum([Variables[I] #= Values[J] : J in 1..Values.len]) : I in 1..Variables.len]), call(Relop,S,Limit).