% % Global constraint smooth in MiniZinc. % % From Global Constraint Catalogue % http://www.emn.fr/x-info/sdemasse/gccat/Csmooth.html % """ % smooth(NCHANGE, TOLERANCE, VARIABLES) % % Purpose % % NCHANGE is the number of times that |X Y|>TOLERANCE holds; X and Y % correspond to consecutive variables of the collection VARIABLES. % % Example % (1, 2, <1, 3, 4, 5, 2>) % % In the example we have one change between values 5 and 2 since the % difference in absolute value is greater than the tolerance (i.e., |5-2|>2). % Consequently the NCHANGE argument is fixed to 1 and the smooth % constraint holds. % % """ % This MiniZinc model was created by Hakan Kjellerstrand, hakank@bonetmail.com % See also my MiniZinc page: http://www.hakank.org/minizinc % % include "globals.mzn"; int: n = 5; array[1..n] of var 1..5: x :: is_output; var 1..n: nchange :: is_output; var 1..10: tolerance :: is_output; predicate smooth(var int: nchange, var int: tolerance, array[int] of var int: x) = nchange = sum(i in min(index_set(x))+1..max(index_set(x))) ( bool2int(abs(x[i]-x[i-1]) > tolerance) ) ; solve satisfy; constraint x = [1,3,4,5,2] /\ nchange = 1 /\ tolerance = 2 /\ smooth(nchange, tolerance, x) ;