How rotations are generated by the Square Rotation Program.
For each tip, the program proceeds as follows:
If the number of 'ready' couples is not a multiple of 4, the
program determines who will sit out by selecting couples who have sat
out the fewest number of times, using a ratio of number of tips
danced vs. number of tips sat out. If several couples have the same ratio,
couples to sit out will be selected at random from those couples.
A random rotation is generated.
The following procedure is repeated until no more changes are
possible or until the user clicks the 'abort process' button:
For each pair of couples, a value is calculated indicating
how often those two couples have danced together. This value is
then subtracted from the optimal number of times that the couples
should have danced together. The resulting value is then squared.
All values are added together to produce a 'chi-square-like' statistic.
Two couples (in different squares) are swapped within the
rotation and the chi-square statistic is recalculated. If the new
statistic is less than the old statistic, the new rotation is saved,
otherwise, the previous rotation is restored.
The process continues until the program has tried swapping
every couple with every other couple and no decrease has occurred
within the statistic.
Square numbers are then assigned and 'optimized' in a similar
manner (also using a 'chi-square' statistic) so that dancers move
around between different squares on the floor.