/* Global constraint cardinality_atmost in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Ccardinality_atmost.html """ ATMOST is the maximum number of occurrences of each value of VALUES within the variables of the collection VARIABLES. Example ( 2, <2, 1, 7, 1, 2>, <5, 7, 2, 9> ) In this example, values 5, 7, 2 and 9 occur respectively 0, 1, 2 and 0 times within the collection <2, 1, 7, 1, 2>. As a consequence, the cardinality_atmost constraint holds since its first argument ATMOST is assigned to the maximum number of occurrences 2. """ 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 = 5, M = 4, Variables = new_list(N), Variables :: 1..9, Values = new_list(M), Values :: 1..9, NVar :: 0..9, Variables = [2,1,7,1,2], Values = [5,7,2,9], % NVar #= 2, cardinality_atmost(NVar, Variables, Values), Vars = Variables ++ Values ++ [NVar], solve(Vars), println(variables=Variables), println(values=Values), println(nvar=NVar), nl, fail, nl. go => true. cardinality_atmost(NVar, Variables, Values) => foreach(I in 1..Values.len) sum([Values[I] #= Variables[J] : J in 1..Variables.len]) #<= NVar end.