/* Global constraint assign_and_counts in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Cassign_and_counts.html """ Given several items (each of them having a specific colour that may not be initially fixed), and different bins, assign each item to a bin, so that the total number n of items of colour COLOURS in each bin satisfies the condition n RELOP LIMIT. Example ( <4>, < bin-1 colour-4, bin-3 colour-4, bin-1 colour-4, bin-1 colour-5 >, <=, 2 ) Figure 4.23.1 shows the solution associated with the example. The items and the bins are respectively represented by little squares and by the different columns. Each little square contains the value of the key attribute of the item to which it corresponds. The items for which the colour attribute is equal to 4 are located under the thick line. The assign_and_counts constraint holds since for each used bin (i.e., namely bins 1 and 3) the number of assigned items for which the colour attribute is equal to 4 is less than or equal to the limit 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 = 4, NumColors = 5, Bins = new_list(N), Bins :: 1..N, Colors = new_list(N), Colors :: 1..NumColors, Color :: 1..NumColors, NVal :: 1..N, % Bins = [1,1,1,1], % the constraint do not hold since there are % 3 occurrences of color 4 for bin 1, which is not % <= 2 Bins = [1,3,1,1], % the constraint holds Colors = [4,4,4,5], Color #= 4, NVal #= 2, assign_and_counts(Color, Bins, Colors, #=<, NVal), Vars = Bins ++ Colors ++ [Color,NVal], solve(Vars), println(bins=Bins), println(colors=Colors), println(color=Color), println(nval=NVal), nl, fail, nl. go => true. assign_and_counts(Color, Bins, Colors, Relop, NVal) => foreach(B in 1..Bins.len) T #= sum([Bins[C] #= B #/\ Colors[C] #= Color : C in 1..Colors.len]), call(Relop,T,NVal) end.