/* Global constraint sum_of_weights_of_distinct_values in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Csum_of_weights_of_distinct_values.html """ Constraint sum_of_weights_of_distinct_values​(VARIABLES,​VALUES,​COST)​ Purpose All variables of the VARIABLES collection take a value in the VALUES collection. In addition COST is the sum of the weight attributes associated with the distinct values taken by the variables of VARIABLES. Example ( <1,6,1>, , 12 ) The sum_of_weights_of_distinct_values constraint holds since its last argument COST=12 is equal to the sum 5+7 of the weights of the values 1 and 6 that occur within the <1,6,1> collection. """ This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import util,cp. main => go. go => N = 3, M = 3, Variables = new_list(N), Variables :: 1..6, Values = new_array(M,2), Values :: 1..7, Cost :: 1..1000, Variables = [1,6,1], Values = {{1,5}, {2,3}, {6,7}}, % Cost #= 12, sum_of_weights_of_distinct_values(Variables, Values, Cost), Vars = Variables ++ Values.array_matrix_to_list_matrix.flatten ++ [Cost], solve(Vars), println(variables=Variables), println(values=Values), println(cost=Cost), nl, fail, nl. sum_of_weights_of_distinct_values(Variables, Values, Cost) => VarLen = Variables.len, ValLen = Values.len, Cost #= sum([ Values[I,2]*(sum([ Variables[J] #= Values[I,1] : J in 1..VarLen]) #>0) : I in 1..ValLen ] ).