in reply to Re: (2) Knapsack problem
in thread Knapsack problem

What's this business of a horizontal or vertical line dividing the area, by the way? That's just confusing the issue. Can I solve the problem for you by saying "put that line one millimetre from the edge and then solve the knapsack thing with your MaxX x MaxY area one millimetre smaller than before?"

The practical side of this knapsack business, by the way, is that you can spend enormous amounts of time finding better and better solutions, but they'll only be slightly better. If you find a solution that's about 80% better than random, then stop looking and go with it. People who really understand math, please correct me if I'm wrong, but there's no point continuing trying to find a slightly-more-perfect solution, unless you're chopping up solid gold, I guess.



($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print

Replies are listed 'Best First'.
Re: (4) Knapsack problem
by stu96art (Scribe) on Dec 08, 2003 at 21:01 UTC
    I am working with glass, and so, I need to have at least one cut that goes through the entire piece. It can be either vertical or horizontal. This is an important piece and why I am having an issue with it.
      The more detail you give, the better.

      Here's some quick thoughts off the top...

      Rescale your coordinates by 4 so that 8.5 x 11 becomes 34 x 44, and your rectangles of 2.75 x 3 become 11 x 12.

      Now you can draw the line vertically anywhere from 0 to 34, and call the horizontal function with the 2 pieces. The horizontal function then chooses, on each piece, anywhere between 1 and 44, and calls the vertical function on the 2 pieces. [No need to use 0 instead of 1, because it's time to make a cut!]

      You need to work out how to represent these, and how to avoid infinite recursion.

      Perhaps like this:

      sub vert { my @{@piece[0]} = @_[0,1]; my @{@piece[1]} = @_[2,3]; my $depth = @_[4] + 1; for my $piece ( 0..1 ) { for my $x ( 0 .. $piece[$piece][0] ) { next unless ( $x or ( $depth > 1 )); horz( $x, $piece[$piece][1], $piece[$piece][0] - $x, $piece[$piece][1], $depth ); } } } # sub vert sub horz { # similar code for horz } my ($scrap, $count1, $count2) = vert( 34, 44, 0, 0, 0 );
      That's the basic idea.

      Now, you'll have to work out when to stop recursing, like when it gets smaller than your target rectangle, or when you have enough of both rectangles. And vert and horz should count up how what the minimum scrap is at each recursion level, and pass that back, along with how many of each rectangle are available, etc.

      There's really quite a bit more work to do here, and some of it is rather subtle. Maybe this will get you started, so you can ask the bigger questions.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of