I'm trying to write a program that calculates the most efficient way to cut some small pieces of lumber from a standard 2" by 4" by 8' piece. I'm having trouble searching up the algorithm used for this. Please help! I use Python but just want to get the general idea of how an algorithm would work.
The situation is, I need to make some cuts of lumber that are shorter than 8', and are different in length. I want to cut the short lumber in from the smallest number of 8' pieces available. The pieces I need are:
- 3* 34"
- 2* 25"
- 2* 30"
- 2* 39"
My thoughts so far: Method 1: set the shortest length as the base, and try to fit the longest length of lumber into the first original 2by4. as soon as the leftover is smaller than the shortest, remove the pieces used in the first calculation and add the number of 2by4 needed by 1. Repeat until all pieces are used. Problem with this one is there is no way to prove this gives the most efficient cut. It could be that a different arrangement of pieces would save a 2by4 altogether.
Any idea is appreciated.
Edit: This is a real problem I have with trying to design a winter tire rack from lumber. In Canada and the States, lumber come in 2"by4"by8' pieces, so given the number of pieces I need to build the rack and their length, I want to optimize the number of 2by4s I have to buy. Let's call the pieces I want 'pieces' and the 2by4by8 'standard'.
My method 1 above is basically the brute force idea. i.e. A queue is formed with the pieces in them, then I try to fit as many pieces into a standard as I can, removing the 'used' pieces from the queue, and quit when the leftover length in the standard is shorter than my shortest piece left, and add 1 to the number of standards I need. This will provide a reasonably efficient solution and would suffice for my purposes, as lumber is not that expensive after all. Thinking about this issue got me thinking tho: what if the lumber was expensive, or if I get this question in an coding interview,
How do we mathematically or algorithmically prove one solution is the most efficient solution? I'm taking whichever piece that fits into the leftover standard, so there's no math proof that the solution is the most efficient. Say the pieces we need are categorized into long, medium, and short lengths, even if we sort the original pieces queue in order of large to small, and start fitting the longest pieces into standard before the shorter ones, there could still be a case where the standard can be used by
long, medium, waste
medium, medium, short, waste,
short, waste
which takes 3 standards and causes a huge waste by the third standard only used by one short,
as opposed to:
long, short, short, waste
medium, medium, medium, waste,
which takes 2 standards and the waste are minimized.
Again, question is, is there a way to prove mathematically that a solution, or a set of solutions, is/are the most efficient in terms of minimized waste?
Thanks to the answers so far, they are the brute force solutions I had above. Please keep the proofs coming :)
Aucun commentaire:
Enregistrer un commentaire