7

I'm trying to nest material with the least drop or waste.

Table A

Qty Type Description Length

2   W    16x19       16'
3   W    16x19       12'
5   W    16x19       5'
2   W    5x9         3'


Table B

Type Description StockLength

W    16X19       20'
W    16X19       25'
W    16X19       40'
W    5X9         20'

I've looked all over looking into Greedy Algorithms, Bin Packing, Knapsack, 1D-CSP, branch and bound, Brute force, and others. I'm pretty sure it is a Cutting stock problem. I just need help coming up with the function(s) to run this. I don't just have one stock length but multiple and a user may enter his own inventory of less common lengths. Any help at figuring a function or algorithm to use in PHP to come up with the optimized cutting pattern and stock lengths needed with the least waste would be greatly appreciated.

Thanks

Nicole
  • 32,841
  • 11
  • 75
  • 101
Arik Lewis
  • 71
  • 1
  • 4
  • 1
    I once played with http://cutting-optimization.optimalprograms.com/ that solves this kind of problem for 1D and 2D. Worked pretty well, you might be able to derive something from their website. – fvu Jul 14 '11 at 00:03
  • @Arik Lewis: pretend I'm an idiot (an in scope of this question, I actually am). Could you provide input and output examples? – bob-the-destroyer Jul 14 '11 at 00:11
  • @Arik Lewis: and, though this may sound a _bit_ odd, have you looked at calendar/event problems, where partial time frames (ie generally integer ranges in the world of PHP) are being juggled? – bob-the-destroyer Jul 14 '11 at 00:21
  • Hey bob, the example is to nest table A (cut lengths) against Table B (stock lengths) and come up with a buy list (ie qty of particular stock lengths) with the least waste or drop. so (1) W 16X19 16', (1) W 16X19 12', AND (2) W 16X19 5' would get me 38' and can be cut out of a 40' piece with a 2' drop. – Arik Lewis Jul 14 '11 at 00:33
  • What's stopping you from looking up a greedy algorithm solving the (known) problem and implementing it? – You Jul 14 '11 at 00:45
  • @Arik Lewis: So say I want 1 16X19 @ 19'. Choosing your 3 available boards of those dimensions (from table B), I would leave either a 1', a 6', or 21' piece depending on which 16X19 stocked board I cut. What is your minimum threshold length for a reusable leftover piece? – bob-the-destroyer Jul 14 '11 at 00:54
  • bob, at the moment, I'm not concerned with minimum threshold. Ideally there would be no drop. If there were to be a minimum threshold, it would be dynamic (a value that would change from type to type). – Arik Lewis Jul 14 '11 at 01:05
  • @You, the only thing that's stopping me is an application to a PHP function. – Arik Lewis Jul 14 '11 at 01:06
  • 2
    You're going to have to write that function yourself. PHP isn't a massive computational toolkit, it's a server-side scripting language. Implementing things is up to you. – You Jul 14 '11 at 06:57
  • Have you looked at [solving the 2D Packing problem](http://www.devx.com/dotnet/Article/36005#codeitemarea)? Page two of that link seems to have a reasonably simple and close to optimal solution. – rossum Jul 16 '11 at 16:12
  • https://cutting-optimization.optimalprograms.com is *amazing* – smirkingman Feb 23 '22 at 22:24

2 Answers2

4

If your question is "gimme the code", I am afraid that you have not given enough information to implement a good solution. If you read the whole of this answer, you will see why.

If your question is "gimme the algorithm", I am afraid you are looking for an answer in the wrong place. This is a technology-oriented site, not an algorithms-oriented one. Even though we programmers do of course understand algorithms (e.g., why it is inefficient to pass the same string to strlen in every iteration of a loop, or why bubble sort is not okay except for very short lists), most questions here are like "how do I use API X using language/framework Y?".

Answering complex algorithm questions like this one requires a certain kind of expertise (including, but not limited to, lots of mathematical ability). People in the field of operations research have worked in this kind of problems more than most of us ever has. Here is an introductory book on the topic.

As an engineer trying to find a practical solution to a real-world problem, I would first get answers for these questions:

  • How big is the average problem instance you are trying to solve? Since your generic problem is NP-complete (as Jitamaro already said), moderately big problem instances require the use of heuristics. If you are only going to solve small problem instances, you might be able to get away with implementing an algorithm that finds the exact optimum, but of course you would have to warn your users that they should not use your software to solve big problem instances.

  • Are there any patterns you could use to reduce the complexity of the problem? For example, do the items always or almost always come in specific sizes or quantities? If so, you could implementing a greedy algorithm that focuses on yielding high-quality solutions for common scenarios.

  • What would be your optimality vs. computational efficiency tradeoff? If you only need a good answer, then you should not waste mental or computational effort in trying to provide an optimal answer. Information, whether provided by a person of by a computer, is only useful if it is available when it is needed.

  • How much are your customers willing to pay for a high-quality solution? Unlike database or Web programming, which can be done by practically everyone because algorithms are kept to a minimum (e.g. you seldom code the exact procedure by which a SQL database provides the result of a query), operations research does require both mathematical and engineering skills. If you are not charging for them, you are losing money.

isekaijin
  • 19,076
  • 18
  • 85
  • 153
  • 1
    I actually think Phpdna has the "better" answer here. The linked tutorial in that answer is a great introductory resource for a non-engineer to understand how to tackle a basic solution to the problem (and in my case it was just what I was looking for). I think a lot of the time, what we non-experts are looking for is help taking a step forward, rather than a definitive statement of how difficult the path ahead is. – user364902 Oct 29 '14 at 02:30
2

This looks to me like a variation of a 1d bin-packing. You may try a best-fit and then try it with different sorting of the table b. Anyway there doesn't exist an solution in 3/2 of the optimum and because this is a NP-complete problem. Here is a nice tutorial: http://m.developerfusion.com/article/5540/bin-packing. I used a lot to solve my problem.

Micromega
  • 12,486
  • 7
  • 35
  • 72
  • 2
    I was going to upvote until I read "there doesn't exist an optimal solution because...". Optimal solutions _do_ exist. It's just that the process by which said solutions can be found might be grossly expensive from a computational point of view. – isekaijin Jul 14 '11 at 00:56
  • I'm aware that it is a NP-complete, however what I'm looking for is a function of some sort. Something that I can plug into PHP – Arik Lewis Jul 14 '11 at 01:02
  • @Leon: I'm not sure what you mean but the 1d-bin-packing is proof to achieve only 85% of what is optimal regardless of what function you throw at it. – Micromega Jul 14 '11 at 01:07
  • @Jitamaro: There _is_ an optimal solution. The optimal might be different than using all available space, because using all available space might be infeasible. (Solutions, by definition, must be feasible.) – isekaijin Jul 14 '11 at 13:30
  • @Leon: In fact I'm confusing this with the christofides algorithm where there is a guarantee of a solution to be within 3/2 of the optimal solution. Maybe a first-fit and a best-fit 1d bin-packing has this guarantee, too? Wikipedia says it's impossible to pack within 3/2. – Micromega Jul 14 '11 at 14:05
  • @Jitamaro: "Optimal" means "the best that you can _possibly_ get", with an emphasis on the word "possibly". // I do not know about the specific algorithms you could use, but I do know that I would not use the same algorithm to solve small and big instances of the same problem, precisely because it is NP-complete. That is something every computer scientist worth his salt should understand. – isekaijin Jul 14 '11 at 15:29
  • @Leon: Maybe you are right but in my answer I told OP to check for different sorting in table b. – Micromega Jul 14 '11 at 15:48
  • @Leon: I've corrected my answer. Thank you for your fast reply. – Micromega Jul 15 '11 at 13:22