/* Global constraint soft all_different in Picat. Implements the two global constraints: * soft_all_different_ctr * soft_all_different_var soft_all_different_ctr: http://www.emn.fr/x-info/sdemasse/gccat/Csoft_all_different_ctr.html """ Consider the disequality constraints involving two distinct variables of the collection VARIABLES. Among the previous set of constraints, C is the number of disequality constraints that do not hold. Example (4, <5, 1, 9, 1, 5, 5>) Within the collection <5, 1, 9, 1, 5, 5> the first and fifth values, the first and sixth values, the second and fourth values, and the fifth and sixth values are identical. Consequently, the argument C = 4 is fixed to the number of disequality constraints that do not hold (i.e, 4) and the soft_alldifferent_ctr constraint holds. """ soft_alldifferent_var: http://www.emn.fr/x-info/sdemasse/gccat/Csoft_alldifferent_var.html """ C is the minimum number of variables of the collection VARIABLES for which the value needs to be changed in order that all variables of VARIABLES take a distinct value. Example (3, <5, 1, 9, 1, 5, 5>) Within the collection <5, 1, 9, 1, 5, 5>, 3 and 2 items are respectively fixed to values 5 and 1. Therefore one must change the values of at least (3-1) + (2-1) = 3 items to get back to 6 distinct values. Consequently, the soft_alldifferent_var constraint holds since its first argument C is fixed to 3. """ 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 ?=> N = 6, X = new_list(N), X :: 1..9, A :: 0..1000, B :: 0..1000, D #= B - A, X = [5,1,9,1,5,5], % _var gives 3, _ctr gives 4 % X = [5,5,5,5,5,5], % _var gives 5, _ctr gives 15 % X = [1,1,1,1,3,3], % increasing(X), all_different_soft_var(A, X), all_different_soft_ctr(B, X), % A #< b, % A #= 3, Vars = X ++ [A,B,D], solve(Vars), println(x=X), println([a=A,b=B,d=D]), nl, fail, nl. go => true. % % all_different_soft_ctr % all_different_soft_ctr(Diffs, X) => Diffs #= sum([X[I] #= X[J] : I in 1..X.len, J in 1..I-1]). % % all_different_soft_var % all_different_soft_var(Diffs, X) => Diffs #= sum([ sum([X[I] #= X[J] : J in 1..I-1]) #> 0 : I in 1..X.len]).