/* Global constraint discrepancy in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Cdiscrepancy.html """ discrepancy(VARIABLES,K) Purpose K is the number of variables of the collection VARIABLES that take their value in their respective sets of bad values. Example ( < var-4 bad-{1, 4, 6}, var-5 bad-{0, 1}, var-5 bad-{1, 6, 9}, var-4 bad-{1,4}, var-1 bad-{} >, 2 ) The discrepancy constraint holds since exactly K=2 variables (i.e., the first and fourth variables) of the VARIABLES collection take their value within their respective sets of bad values. """ 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 ?=> Bad = [ [1,4,6], [0,1], [1,6,9], [1,4], [] ], N = Bad.len, BadFlatten = Bad.flatten, V = new_list(N), V :: BadFlatten.min..BadFlatten.max, % V = [4,5,5,4,1], K #= 2, discrepancy(V, Bad, K), Vars = V ++ Bad ++ [K], solve(Vars), println(bad=Bad), println(v=V), println(k=K), nl, fail, nl. go => true. discrepancy(V, Bad, K) => K #= sum([ sum([V[I] #= Bad[I,J] : J in 1..Bad[I].len,Bad[I].len>0])#=1 : I in 1..V.len]).