AFAICT (after having played around a bit with the module), Algorithm::Knapsack only seems to compute exact solutions (i.e. where capacity is filled entirely), an approach which might not be efficiently applicable to a set of real world file sizes... Also, the computation time gets prohibitively long for file sets as shown below.

So, here's another approach using the related Algorithm::Bucketizer.  Note that this module doesn't claim to provide optimal solutions (in the sense of the knapsack problem).  But most likely good enough for most practical purposes:

#!/usr/bin/perl use strict; use warnings; use Algorithm::Bucketizer; my @filesizes = map { (stat)[7] } glob "8*.pl"; print "input (file sizes):\n@filesizes\n\n"; my $b = Algorithm::Bucketizer->new( bucketsize => 10_000, algorithm => "retry", ); for my $i (0..$#filesizes) { $b->add_item($i, $filesizes[$i]); } $b->optimize( algorithm => "random", maxrounds => 1000, ); print "total = file sizes...\n"; for my $bucket ($b->buckets()) { my $total = $bucket->level(); print "$total = @filesizes[ $bucket->items() ]\n"; } __END__ input (file sizes): 361 1304 1988 200 132 715 236 603 674 200 319 319 508 1027 291 154 206 + 1781 735 2625 424 999 1152 110 43 79 421 181 1240 667 67 1085 179 13 +0 593 2240 2925 182 189 566 414 1833 898 1689 4959 87 513 111 1769 91 +0 262 2129 573 2506 363 325 4044 5298 193 298 195 132 826 229 294 198 + 103 857 898 172 89 117 11211 693 390 663 482 1523 387 1121 188 1197 +283 2530 459 1643 1265 636 146 8735 852 2584 372 781 1257 886 174 176 + 637 601 466 1327 164 134 962 1340 total = file sizes... 9959 = 134 2925 513 1340 466 603 424 910 1327 229 193 43 852 9972 = 1769 319 67 1689 1121 130 110 2584 283 1304 390 206 9947 = 667 826 459 1265 674 262 372 508 1240 154 89 236 189 181 1197 7 +81 715 132 9988 = 8735 387 573 176 117 9956 = 4044 1781 174 2240 898 87 179 421 132 9953 = 1152 1257 1643 195 886 663 693 414 198 735 482 566 319 200 79 1 +11 172 188 9974 = 2129 1988 898 4959 9987 = 5298 637 1085 2530 291 146 9942 = 2625 999 1523 593 857 363 2506 182 294 6149 = 1833 164 601 962 200 325 1027 298 103 636

In reply to Re^3: I want to maximize data storage in a DVD by almut
in thread I want to maximize data storage in a DVD by mgrangeiro

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.