/* Global constraint soft_all_equal_ctr in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Csoft_all_equal_ctr.html """ Constraint soft_all_equal_ctr​(VARIABLES,​N) Purpose Consider the equality constraints involving two distinct variables of the collection VARIABLES. Among the previous set of constraints, N is less than or equal to the number of equality constraints that hold. Example ​(6, <5, 1, 5, 5>) Within the collection <5, 1, 5, 5> six equality constraints holds. Consequently, the soft_all_equal_ctr constraint holds since the argument N=6 is less than or equal to the number of equality constraints that hold. """ This program 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, X = new_list(N), X :: 1..5, NN :: 0..N*N, X = [5,1,5,5], % NN #= 6, soft_all_equal_ctr(X, NN), Vars = X ++ [NN], solve(Vars), println(x=X), println(nn=NN), nl, fail, nl. soft_all_equal_ctr(X, N) => Len = X.len, N #>= 0, N #<= Len * Len, N #<= sum([X[I] #= X[J] : I in 1..Len, J in 1..Len, I != J]).