What are you trying to optimize? Are you trying to get an even distribution of data across a fixed number of drives? Efficiently fill drives of fixed size? The latter is (as AppleFritter points out) the knapsack problem.

For case 1), a simple and quite effective solution is to go from largest object to smallest, putting it in the least filled bin.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my %bins = ( A => 0, B => 0, C => 0, D => 0, ); my %dir; my $int; while(<DATA>) { chomp; my ($size, $name) = split /\s+/; $name .= ++$int if $dir{$name}; # Disabiguation; all your names ar +e identical $dir{$name} = $size; } for my $name (sort {$dir{$b} <=> $dir{$a}} keys %dir) { my ($smallest) = sort {$bins{$a} <=> $bins{$b}} keys %bins; $bins{$smallest} += $dir{$name}; } print Dumper(\%bins);
output:
$VAR1 = { 'A' => '9885811019', 'D' => '9885704251', 'C' => '9885960464', 'B' => '9885704533' };

For case 2), you would put it in the smallest available space in which it fits.

#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $max = 10**10; my $new_bin = 'A'; my %bins; my %dir; my $int; while(<DATA>) { chomp; my ($size, $name) = split /\s+/; $name .= ++$int if $dir{$name}; # Disabiguation; all your names ar +e identical $dir{$name} = $size; } DIR_LOOP: for my $name (sort {$dir{$b} <=> $dir{$a}} keys %dir) { for my $bin_name (sort {$bins{$b} <=> $bins{$a}} keys %bins) { if ($bins{$bin_name} + $dir{$name} < $max) { $bins{$bin_name} += $dir{$name}; next DIR_LOOP; } } $bins{$new_bin++} = $dir{$name}; } print Dumper(\%bins);
outputs
$VAR1 = { 'A' => '9999831421', 'D' => '9543689031', 'C' => '9999660101', 'B' => '9999999714' };

Update: Added code


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


In reply to Re: Computer science Problem - single dimension bin packing by kennethk
in thread Computer science Problem - single dimension bin packing by davis

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.