/* Global constraint minimum_weight_alldifferent in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Cminimum_weight_alldifferent.html """ Constraint minimum_weight_alldifferent (VARIABLES, MATRIX, COST) Purpose All variables of the VARIABLES collection should take a distinct value located within interval [1,|VARIABLES|]. In addition COST is equal to the sum of the costs associated with the fact that we assign value i to variable j. These costs are given by the matrix MATRIX. Example ( <2, 3, 1, 4>, < i-1 j-1 c-4, i-1 j-2 c-1, i-1 j-3 c-7, i-1 j-4 c-0, i-2 j-1 c-1, i-2 j-2 c-0, i-2 j-3 c-8, i-2 j-4 c-2, i-3 j-1 c-3, i-3 j-2 c-2, i-3 j-3 c-1, i-3 j-4 c-6, i-4 j-1 c-0, i-4 j-2 c-0, i-4 j-3 c-6, i-4 j-4 c-5 >, 17 ) The minimum_weight_alldifferent constraint holds since the cost 17 corresponds to the sum MATRIX[(1-1)*4+2].c+ MATRIX[(2-1)*4+3].c+ MATRIX[(3-1)*4+1].c+ MATRIX[(4-1)*4+4].c= MATRIX[2].c+ MATRIX[7].c+ MATRIX[9].c+ MATRIX[16].c=1+8+3+5. """ 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 = 4, Variables = new_list(N), Variables :: 1..N, Matrix = new_array(N,N), Matrix :: 0..9, Cost :: 0..1000, Variables = [2,3,1,4], Matrix = {{4,1,7,0}, {1,0,8,2}, {3,2,1,6}, {0,0,6,5}}, % Cost #= 17, minimum_weight_alldifferent(Variables,Matrix,Cost), Vars = Variables ++ Matrix.vars ++ [Cost], solve($[min(Cost)],Vars), % solve($[],Vars), println(variables=Variables), println(cost=Cost), foreach(Row in Matrix) println(Row) end, nl, fail, nl. go => true. minimum_weight_alldifferent(Variables,Matrix,Cost) => all_different(Variables), Cost #= sum([ sum([(Variables[I] #= J)*Matrix[I,J] : J in 1..Matrix[1].len]) : I in 1..Variables.len]).