I had a similar problem (with a multi-dimensional set of distances between each pair, but I don't think that makes a difference). My solution was to use a "simulated-annealing" approach.
A pseudo-code description would be something like
my @items
my @best_layout
my $least_error
for (0..100)
{
foreach @items
add to layout at random x,y
best_error_measure = sum of badness of all inter-item distances
my $jiggle_distance = largish
while($jiggle_distance > smallish)
{
randomly pick item try it in four alternate locations
(up, down, left, right)
if(new_error_measure < best_error_measure)
keep the item in its best new location
else
jiggle_distance *= 0.95
}
if(best_error_measure < least_error)
{
save layout into best_layout
least_error = best_error_measure
}
}
This has the advantage that you can take any consideration into account when measuring the "badness" of a layout, for example are there items you want near the centre. For my data it very rapidly converged on a reasonable solution (the optimal solution would have taken much longer but I just needed something that was "good enough").
For my particular data the standard "cluster analysis" approaches gave poor results, and I wanted a two dimensional display of the results. |