/* Global constraint among_low_up in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Camong_low_up.html """ Between LOW and UP variables of the VARIABLES collection are assigned to a value of the VALUES collection. Example ( 1, 2, <9, 2, 4, 5>, <0, 2, 4, 6, 8> ) The among_low_up constraint holds since between 1 and 2 values (i.e., in fact 2 values) of the collection of values <9, 2, 4, 5> belong to the set of values {0, 2, 4, 6, 8}. """ 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 ?=> X = new_list(4), X :: 0..9, Y = new_list(5), Y :: 0..9, Low :: 1..X.len, Up :: 1..X.len, X = [9,2,4,5], Y = [0,2,4,6,8], among_low_up(Low, Up, X, Y), Low #= 1, Up #= 2, Vars = X ++ Y ++ [Low,Up], solve(Vars), println(x=X), println(y=Y), println([low=Low,up=Up]), nl, fail, nl. go => true. % % among_low_up % % low and up should probably be fixed before calling. % among_low_up(Low, Up, X, Y) => NVar #= sum([ sum([X[I] #= Y[J] : J in 1..Y.len]) : I in 1..X.len]), NVar #>= Low, NVar #<= Up, Low #<= Up, Low #>= 0, Up #<= X.len.