The problem can be easily modeled as a CSP problem with 2 sets of N/2 variables (called A_{i} and B_{i}, i in 1..N/2) whose initial domain are the set of integers {1..N}. The variables are constrained as follows:

To break symmetries (avoiding permutations of a group, and swapping the two groups) it is better to add these redundant constraints:

A good heuristic consists in iteratively placing the biggest missing value (thus in descending order). NB: doing so the “all different” constraint is not needed (only different values are assigned one by one).

If only the first solution is wanted then it is further preferable to first try to put this value in the set which has the smallest sum (sum of already placed values). If all solutions are wanted this value will also be tried in the other group so the test on the sums can involve a little overhead.

File Type Notes
set_partition.lp4 ASP Gringo 4 B-Prolog Comet
set_partition.ecl ECLiPSe
set_partition_simple.essence Essence
set_partition_full.essence Essence
set_partition.eprime EssencePrime
set_partition.cpp Gecode JSR-331
set_partition.mzn MiniZinc
partition.mzn MiniZinc Numberjack
set_partition.cs or-tools/C# or-tools/Python
SetPartition_oscar.scala OscaR SICStus Prolog