/* Global constraint smooth in Picat. 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 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 = 5, X = new_list(N), X :: 1..N, NChange :: 0..N, Tolerance :: 1..10, X =[1,3,4,5,2], % NChange #= 1, Tolerance #= 2, smooth(NChange, Tolerance, X), Vars = X ++ [NChange,Tolerance], solve(Vars), println(x=X), println(nchange=NChange), println(tolerance=Tolerance), nl, fail, nl. smooth(NChange, Tolerance, X) => NChange #= sum([ abs(X[I]-X[I-1]) #> Tolerance : I in 2..X.len]).