4

When I say box I am talking about shipping boxes.

I have a number of random sized, small items that I need to pack into as few boxes as possible. I need to know what box sizes are optimal.

  • All items are rectangular prisms.
  • It's easy to exclude a box size for an item which is too large to fit.
  • I know the box sizes (they are the available box sizes which I have in-stock)
  • Items can be positioned horizontally or vertically, not diagonal.
  • As many boxes as required can be used. The goal is to use as few boxes as possible.
  • Multiple box sizes may be used to optimally fit the varying-sized items.

What algorithm exists that allows me to calculate the box sizes I need to use for optimal space usage? To fit the most items into as few boxes as possible.

The available box sizes come from what I have available, in-stock. You can create a finite number of made up box sizes for example purposes.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
unixman83
  • 9,421
  • 10
  • 68
  • 102
  • So this involves only 2 dimensions? length and width? – Francis P Apr 11 '12 at 20:51
  • 1
    This sounds similar to `Knapsack` which is `NP-Complete`. You could take a look at algorithms to approximate `Knapsack` and see if you can adapt one to your needs. – twain249 Apr 11 '12 at 20:52
  • @FrancisP Cubic, Length, width and height. I don't know the *technical* word for a cubic rectangular shape. – unixman83 Apr 11 '12 at 20:52
  • @FrancisP The word is just rectangular prism. I edited it. – twain249 Apr 11 '12 at 20:54
  • So how did you solve this ? Is their any ready made classes or code in PHP ? Any idea? Writing one myself is going to take days! – John Jun 10 '14 at 14:40

2 Answers2

7

This is a generalization of the Bin packing problem, meaning it is NP-Hard.

To see this, imagine all bins and packages have the same width and height, and additionally all bins (but not packages) have the same length. Then it is a one-dimensional problem: we have bins of size V, and packages of size a1, a2, ..., an. This simplified case is exactly the Bin-packing problem. Thus, a fast solution to your problem gives us a fast solution to bin-packing, so your problem is at least as hard; and since bin-packing is NP-Hard, so is your problem.


There are some approximation algorithms available though; for example, it's easy to show that the simple first-fit algorithm (put every item in the first bin that it fits into) will never do worse than 2x the optimal solution.

The similar "First Fit Decreasing" algorithm (sort the items in descending order, then put every item in the first bin it fits into) is even better, guaranteeing to be within about 25% of the optimal solution. There is also another, slightly more complicated algorithm called MFFD which guarantees to be within about 20%.

And of course, with only 7 boxes, you could always just brute-force the solution. This will require about 7n steps (where n is the number of items), so this solution is infeasible with more than a dozen or so items.

BlueRaja - Danny Pflughoeft
  • 84,206
  • 33
  • 197
  • 283
  • My problem with this is that the bin sizes I will choose from is partially based upon the total volume of the items. Example: Many small items are cheaper to send in a large UPS box than in many small USPS flat rate boxes. i.e. as total volume increases it is *usually* better to use a larger bin. – unixman83 Apr 11 '12 at 21:54
  • @unixman83: -_- The question mentions fitting items into the fewest boxes, it says nothing about value. I think you will find that your new problem is a generalization of Knapsack, though, so you are still out of luck with finding the optimal solution :\ Try looking up some Knapsack approximations. – BlueRaja - Danny Pflughoeft Apr 11 '12 at 21:58
  • Yeah. Thanks for answering the space fitting problem I was having though. This is definitely a help. – unixman83 Apr 11 '12 at 21:58
2

You have described a variation of the knapsack problem. Check out the wikipedia article for more detail about approaches to the problem than could be given here.

Michael Slade
  • 13,802
  • 2
  • 39
  • 44