|
Algorithm::BinPack - efficiently pack items into bins |
Algorithm::BinPack - efficiently pack items into bins
Algorithm::BinPack efficiently packs items into bins. The bins are
given a maximum size, and the items are packed in with as little empty
space as possible. An example use would be packing files onto CDs, and
you want to use as few CDs as possible.
my $bp = Algorithm::BinPack->new(binsize => 4);
$bp->add_item(label => "one", size => 1);
$bp->add_item(label => "two", size => 2);
$bp->add_item(label => "three", size => 3);
$bp->add_item(label => "four", size => 4);
for ($bp->pack_bins) {
print "Bin size: ", $_->{size}, "\n";
print " Item: ", $_->{label}, "\n" for @{ $_->{items} };
}
Algorithm::BinPack object. The maximum bin size is
specified as a named argument 'binsize', and is required. A fudge
factor may be specified as a named argument 'fudge'. If a fudge factor
is specified, any items' sizes will be rounded up to a number divisible
by the fudge factor. This can help keep items with similar sizes in
order by their labels.
my $bp = Algorithm::BinPack->new(binsize => 4);
my $bp = Algorithm::BinPack->new(binsize => 100, fudge => 10);
$bp->add_item(label => 'one', size => 1);
$bp->add_item(label => 'two', size => 2, desc => 'The second numeral');
$bp->add_item(qw(label three size 3));
$bp->add_item(qw(label four size 4 random key));
for my $bin ($bp->pack_bins) {
print "Bin size: ", $bin->{size}, "\n";
for my $item (@{ $bin->{items} }) {
printf " %-6s %-20s\n", $_, $item->{$_} for keys %{ $item };
print " ---\n";
}
}
This module implements the bin packing algorithm described in 'The Algorithm Design Manual' by Steven S. Skiena.
This module is similar to the Algorithm::Bucketizer manpage, but has a few key differences. The algorithms in Algorithm::Bucketizer are based on optimization by multiple iterations, so the module is set up differently. The algorithm used in Algorithm::BinPack is predictable, and does not require multiple iterations. I also figured it could use a name that's more well-known (searching for variations on ``bin packing'' finds more relevant results than variations on ``bucketizer'').
Carey Tilden <revdiablo@wd39.com>
Copyright (C) 2004 by Carey Tilden
This code is dual licensed. You may choose from one of the following: - http://creativecommons.org/licenses/by/1.0/ - http://d.revinc.org/pages/license
|
Algorithm::BinPack - efficiently pack items into bins |