/* Global constraint sum_ctr in Picat. From Global Constraint Catalogue: http://www.emn.fr/x-info/sdemasse/gccat/Csum_ctr.html """ sum_ctr(VARIABLES, CTR, VAR) Constraint the sum of a set of domain variables. More precisely, let S denote the sum of the variables of the VARIABLES collection. Enforce the following constraint to hold: S CTR VAR. [CTR are: [=, !=, <, >=, > , <=] Example (<1, 1,4>, =, 6) The sum_ctr constraint holds since the condition 1+1+4=6 is satisfied. """ 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 = 3, X = new_list(N), X :: 1..5, S #>= 0, % the sum % X = [1,1,4], sum_ctr(X, 1, S), S #= 6, solve(X ++ [S]), println(x=X), println(s=S), nl, fail, nl. % % sum_ctr % % CTR coding: % -2: < % -1: <= % 0: = % 1: >= % 2: > % else: != % sum_ctr(X, Ctr, S) => SS #= sum(X), if Ctr == -2 then SS #< S elseif Ctr == -1 then SS #<= S elseif Ctr == 0 then SS #= S elseif Ctr == 1 then SS #>= S elseif Ctr == 2 then SS #> S else SS #!= S end.