/* Global constraint open_atleast in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Copen_atleast.html """ Constraint open_atleast(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 least N variables of V are assigned to value VALUE. Example ({2,3,4},2,<4,2,4,4>,4) The open_atleast constraint holds since, within the last three (i.e., S={2,3,4}) values of the collection <4,2,4,4>, at least N=2 values are equal to value VALUE=4. """ 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..4, % var set of 1..num: s; S = new_list(Num), S :: 0..1, N :: 1..Num, Value :: 1..Num, Variables = [4,2,4,4], S = [0,1,1,1], % the set {2,3,4} Value #= 4, % N #= 2, open_atleast(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(variable_selection=[Variables[I] : I in 1..Variables.len, S[I] == 1]), println(n=N), println(value=Value), nl, fail, nl. go => true. open_atleast(S,N,Variables,Value) => sum([S[I] #= 1 #/\ Variables[I] #= Value : I in 1..Variables.len]) #>= N.