/* Global constraint derangement in Picat. From Global Constraint Catalogue http://www.emn.fr/x-info/sdemasse/gccat/Cderangement.html """ Enforce to have a permutation with no cycle of length one. The permutation is depicted by the succ attribute of the NODES collection. Example ( < index-1 succ-2, index-2 succ-1, index-3 succ-5, index-4 succ-3, index-5 succ-4 > ) In the permutation of the example we have the following 2 cycles: 1->2->1 and 3->5->4->3. Since these cycles have both a length strictly greater than one the corresponding derangement constraint holds. """ This Picat model 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, % X = [2,1,5,3,4], derangement(X), solve(X), println(X), fail, nl. go => true. derangement(X) => subcircuit(X), foreach(I in 1..X.len) X[I] #!= I end.