/* All different pairs in Picat. This model implements: - generate_pairs(N,X) - all_different_pairs(N,X) - increasing_pairs(N,X) - decreasing_pairs(N,X) - num_pairs(N) = M 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. % % generate all pairs for 1..N % go => N = 5, M = num_pairs(N), X = new_array(M,2), X :: 1..N, generate_pairs(N,X), solve(X.vars()), println(X), nl. % % another example N element and N pairs % go2 ?=> N = 5, X = new_array(N,2), X :: 1..N, all_different_pairs(N,X), increasing_pairs(N,X), solve($[ff,split],X.vars()), println(X), fail, nl. go2 => true. num_pairs(N) = N*(N-1) div 2. % % Create all possible pairs of 1..N % For symmetry breaking: X[I,1] #< X[I,2] % % Note: X must be a Mx2 list matrix of decision variables (1..N) % generate_pairs(N,X) => M = (N*(N-1)) div 2, % number of pairs all_distinct($[X[K,1]*(N-1) + X[K,2] : K in 1..M]), % global_cardinality(X.array_matrix_to_list_matrix().flatten(),$[I-N1 : I in 1..N]), % all_different($[X[K,1]*(N-1) + X[K,2] : K in 1..M]), foreach(K in 1..M) X[K,1] #< X[K,2] end. % % ensure all pairs are different % all_different_pairs(N,X) => all_different($[X[K,1]*(N-1) + X[K,2] : K in 1..X.len]). % % % increasing_pairs(N,X) => Len = X.len, Y = new_list(Len), foreach(K in 1..Len) Y[K] #= X[K,1]*(N-1) + X[K,2] end, increasing(Y). % % % decreasing_pairs(N,X) => Len = X.len, Y = new_list(Len), foreach(K in 1..Len) Y[K] #= X[K,1]*(N-1) + X[K,2] end, decreasing(Y).