/* Global constraint open_atmost in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Copen_atmost.html """ Constraint open_atmost(S,N,VARIABLES,VALUE) Purpose Let V be the variables of the collection VARIABLES for which the corresponding position belongs to the set S. At most N variables of V are assigned to value VALUE. Example ({2,3,4},1,<2,2,4,5>,2) The open_atmost constraint holds since, within the last three (i.e., S={2,3,4}) values of the collection <2,2,4,5>, at most N=1 value is equal to value VALUE=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 ?=> Num = 4, Variables = new_list(Num), Variables :: 1..5, S = new_list(Num), S :: 0..1, N :: 1..Num, Value :: 1..Num, Variables = [2,2,4,5], S = [0,1,1,1], % the set {2,3,4} Value #= 2, % N #= 1, open_atmost(S, N, Variables, Value), Vars = Variables ++ S ++ [N,Value], solve(Vars), println(variables=Variables), println(s=[I : I in 1..S.len, S[I] == 1]), println(variables_selection=[Variables[I] : I in 1..S.len, S[I] == 1]), println(value=Value), println(n=N), nl, fail, nl. go => true. open_atmost(S,N,Variables,Value) => sum([S[I] #= 1 #/\ Variables[I] #= Value : I in 1..Variables.len]) #=< N.