Here is the code that I have been working on to solve the knapsack problem that I have been looking for so much help with. I need to get this to work, and I think that I am using the branch and bound method, but I can forsee problem with larger amounts of rectangles. So I need help with two things, please 1) Making the code that I have work and 2) Making that code faster, or where it does not go through each possibility. Thank you for any help you can give me.
use strict; use warnings; use Data::Dumper; open ( OHA, ">c:/printoutKNAP.txt" ) or die ("could not open file. &!" + ); my @lite[][]; $lite[0][0] = 2; $lite[0][1] = 5; $lite[0][2] = 0; $lite[0][3] = 0; $lite[0][4] = 0; $lite[1][0] = 4; $lite[1][1] = 3; $lite[1][2] = 0; $lite[1][3] = 0; $lite[1][4] = 0; $lite[2][0] = 1; $lite[2][1] = 4; $lite[2][2] = 0; $lite[2][3] = 0; $lite[2][4] = 0; my $waste = 0; my $end = 0; sub KNAP { my $bigx = $_[0]; my $bigy = $_[1]; my $locx = $_[2]; my $locy = $_[3]; my @list[][]; my $listnum = 0; for ( $i = 1; i <= $maxlites; $i ++; ) { if ( $lite[$i][2] == 0 ) { if ( ( $lite[$i][0] <= $bigx ) && ( $lite[$i][1] <= $bigy ) ) { $list[$listnum][0] = $i; $list[$listnum][1] = 0; $list[$listnum][2] = 0; $listnum = $listnum + 1; } if ( ( $lite[$i][1] <= $bigx ) && ( $lite[$i][0] <= $bigy ) ) { $list[$listnum][0] = $i; $list[$listnum][1] = 0; $list[$listnum][2] = 1; $listnum = $listnum + 1; } } } for ( $j = 1; $j <= $listnum; $j++ ) { $list[$list[$j][0]][2] = 1; $list[$list[$j][0]][3] = $locx; $list[$list[$j][0]][4] = $locy; if ( $list[$j][2] == 1 ) { my $temp = $lite[$list[$j][0]][0]; $lite[$list[$j][0]][0] = $lite[$list[$j][0]][1]; $list[$list[$j][0]][1] = $temp; } for ( $k = 1; $k <= $maxlites; $k++ ) { $end = 1; if ( $lite[$k][2] == 0 ) { $end = 0; } } if ( $end == 1 ) { $waste = $waste + ( $bigx*$bigy - $lite[$list[$j][0]][0]*$lite[$ +list[$j][0]][1] ); } $newx = $bigx - $lite[$list[$j][0]][0]; $newy = $bigy - $lite[$list[$j][0]][1]; if ( ( $newx != 0 ) && ( $newy != 0 ) && ( $end != 1 ) ) { &KNAP( $bigx, $newy, $locx, $locy + $lite[$list[j][0]][1] ); &KNAP( $newx, $lite[$list[$j][0]][1], $locx + $lite[$list[$j][0] +][0], $locy ); &KNAP( $lite[$list[$j][0]][0], $newy, $locx, $locy + $lite[$list +[$j][0]][1] ); &KNAP( $newx, $bigy, $locx + $lite[$list[$j][0]][0], $locy ); } } }

In reply to Help with attempting to solve cutting stock problem (knapsack) by stu96art

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.