/* Global constraint sequence in Picat. From the MiniZinc definition: """ Requires that in each subsequence 'x[i], ..., x[i + l - 1]' the sum of the variables is between 'mn' and 'mx'. sequence(array[int] of var int: x, int: l, int: mn, int: mx) """ Model created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> N = 11, X = new_list(N), X :: 1..N, % Sum of X (just for show) Sum #= sum(X), % Length may be a decision varible, % but must be fixed Length :: 2..4, Length #= 3, N1 = N-1, LBound :: 1..N1, % lower bound UBound :: 1..N1, % upper bound LBound #= UBound, % LBound #=< UBound, sequence(X,Length,LBound,UBound), Vars = X ++ [LBound, UBound, Length], solve([], Vars), writeln(x=X), writeln(sum=Sum), writeln(lower_bound=LBound), writeln(upper_bound=UBound), writeln(length=Length), nl, fail. go => true. % % sequence(?X,+Length,?LBound,?UBound) % % Ensures that all sums of every subsequence of length Length % in array X is between LBound and UBound % sequence(X,Length, LBound,UBound) => foreach(I in 1..X.length-Length+1) Sum #= sum([X[J] : J in I..I+Length-1]), Sum #>= LBound, Sum #=< UBound end.