in reply to Re: Re: Help with attempting to solve cutting stock problem (knapsack)
in thread Help with attempting to solve cutting stock problem (knapsack)
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; my $maxlites = 3; sub KNAP { my $bigx = $_[0]; my $bigy = $_[1]; my $locx = $_[2]; my $locy = $_[3]; my @list; my $listnum = 0; for my $i ( 0..$#lite ) { 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 my $j ( 0..$#list ) { if ( $list[$j][2] == 1 ) { my $temp = $lite[$list[$j][0]][0]; $lite[$list[$j][0]][0] = $lite[$list[$j][0]][1]; $lite[$list[$j][0]][1] = $temp; } $lite[$list[$j][0]][2] = 1; $lite[$list[$j][0]][3] = $locx; $lite[$list[$j][0]][4] = $locy; select OHA; for my $l ( 0..$#lite ) { print "locx $lite[$l][3] locy $lite[$l][4]\n"; } select STDOUT; for my $k ( 0..$#lite ) { $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] ); } my $newx = $bigx - $lite[$list[$j][0]][0]; my $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 ); } } } &KNAP ( 10, 10, 0, 0 );
|
|---|