Over a million developers have joined DZone.

Efficient Use Of Space On Multiple CDs

·
Algorithm::BinPack
Algorithm::Bucketizer
Algorithm::Knapsack
http://search.cpan.org/~andale/Algorithm-Knapsack-0.02/bin/filesack
Burning ISOs to maximize DVD space
How to maximise the content of my data CD
http://bttb.sourceforge.net/ (on Windows, which I'm not)
The Match Fit algorithm
Bin Packing


#!/usr/bin/perl
#
#
#
use warnings;
use strict;
use Cwd;

use Algorithm::BinPack;
my $cd_size = 734003200;

my $bp = Algorithm::BinPack->new(binsize => $cd_size);

my $dir = cwd;
my %size;

opendir(DIR, $dir) || die "Can't open dir '$dir': $!\n";
for my $file ( readdir DIR ) {

    if ( -f "$dir/$file" ) {
        my $size = (stat $file)[7];
        $size{$file} = $size;
        $bp->add_item(label => "$file",   size => $size);
    }

}
closedir DIR;

for my $cd ($bp->pack_bins) {

    print "CD size: ", $cd->{size}, "\n";

    for my $file (@{ $cd->{items} }) {
        printf "  %-9s\t%-32s\n", $file->{size}, $file->{label};
    }

    print $/;

}



#!/usr/bin/perl
#
#
#
use strict;
use warnings;
use Cwd;

use Algorithm::Bucketizer;

my $cd_size = 734003200;
my $algorithm = "brute_force";
# my $algorithm = "random";
my $seconds = 1800;
my $number_of_rounds = 16777216;

my $b = Algorithm::Bucketizer->new( bucketsize => $cd_size );

my $dir = cwd;
my %size;

opendir(DIR, $dir) || die "Can't open dir '$dir': $!\n";
for my $file ( readdir DIR ) {

    if ( -f "$dir/$file" ) {
        my $size = (stat $file)[7];
        $size{$file} = $size;
        $b->add_item($file, $size);
    }

}
closedir DIR;

$b->optimize(
    algorithm  => $algorithm,
    maxtime    => $seconds,
    maxrounds  => $number_of_rounds
);

for my $bucket ($b->buckets()) {

    print "CD ", $bucket->serial(), $/;
    my $size = 0;

    for my $item ($bucket->items()) {

    print "$size{$item}\t$item\n";
    $size += $size{$item};

    }

    print "Size: $size\n\n";

}



#!/usr/bin/perl
#
#
#
use warnings;
use strict;
use Cwd;

use Algorithm::Knapsack;

my $cd_size = 734003200;

my $dir = cwd;
my %size;
my @weights;

opendir(DIR, $dir) || die "Can't open dir '$dir': $!\n";
for my $file ( readdir DIR ) {

    if ( -f "$dir/$file" ) {
        my $size = (stat $file)[7];
        $size{$size} = $file;
        push @weights, $size;
    }

}
closedir DIR;

my $knapsack = Algorithm::Knapsack->new(
    capacity => $cd_size,
    weights  => \@weights,
);

$knapsack->compute();

foreach my $solution ($knapsack->solutions()) {

    print $/;

    for my $sz ( @{$solution} ) {

        print "$weights[$sz]\t$size{$weights[$sz]}\n";

    }

}

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}