Box::Calc - Packing Algorithm
version 1.0000
use Box::Calc; my $box_calc = Box::Calc->new; # define the possible box types $box_calc->add_box_type( x => 12, y => 12, z => 18, weight => 16, name => 'big box' ); $box_calc->add_box_type( x => 4, y => 6, z => 8, weight => 6, name => 'small box' ); # define the items you want to put into boxes $box_calc->add_item( 3, { x => 6, y => 3, z => 3, weight => 12, name => 'soda' }); $box_calc->add_item( 1, { x => 3.3, y => 3, z => 4, weight => 4.5, name => 'apple' }); $box_calc->add_item( 2, { x => 8, y => 2.5, z => 2.5, weight => 14, name => 'water bottle' }); # figure out what you need to pack this stuff $box_calc->pack_items; # how many boxes do you need my $box_count = $box_calc->count_boxes; # 2 # interogate the boes my $box = $box_calc->get_box(-1); # the last box my $weight = $box->calculate_weight; # get a packing list my $packing_list = $box_calc->packing_list;
Box::Calc helps you determine what can fit into a box for shipping or storage purposes. It will try to use the smallest box possible of the box types. If every item won't fit into your largest box, then it will span the boxes letting you know how many boxes you'll need.
Once it's done packing the boxes, you can get a packing list for each box, as well as the weight of each box.
Box::Calc is intended to pack boxes in the simplest way possible. Here's what it does:
Sort all the items by volume.
Eliminate all boxes that won't fit the largest items.
Choose the smallest box still available.
Place the items in a row starting with the largest items.
When the row runs out of space, add another.
When you run out of space to add rows, add a layer.
When you run out of layers either start over with a bigger box, or if there are no bigger boxes span to a second box.
Repeat from step 3 until all items are packed into boxes.
At The Game Crafter (http://www.thegamecrafter.com) we ship a lot of games and game pieces. We tried using a more complicated system for figuring out which size box to use, or how many boxes would be needed in a spanning situation. The problem was that those algorithms made the boxes pack so tightly that our staff spent a lot more time putting the boxes together. This algorithm is relatively dumb, but dumb in a good way. The boxes are easy and fast to pack. By releasing this, we hope it can help those who are either using too complicated a system, or no system at all for figuring out how many boxes they need for shipping/storing materials.
When adding items, be sure to use the outer most dimensions of oddly shaped items, otherwise they may not fit the box.
When adding box types, be sure to use the inside dimensions of the box. If you plan to line the box with padding, then subtract the padding from the dimensions, and also add the padding to the weight of the box.
What units you use (inches, centimeters, ounces, pounds, grams, kilograms, etc) don't matter as long as you use them consistently.
Constructor.
Returns an array reference of the Box::Calc::BoxTypes registered.
Returns the number of Box::Calc::BoxTypes registered.
Returns a specific Box::Calc::BoxType from the list of box_types
box_types
An array index. For example this would return the last box type added:
$box_calc->get_box_type(-1)
Adds a new Box::Calc::BoxType to the list of box_types. Returns the newly created Box::Calc::BoxType instance.
The list of constructor parameters for Box::Calc::BoxType.
NOTE: You can optionally include an argument of "categories" and a box type will be created for each category so you don't have to do it manually.
Returns an array reference of categories associated with the box types.
Sorts the list of box_types by volume and then returns an array reference of that list.
Optional. Array ref of box types. Will call box_types if not passed in.
Given the list of items and the list of box_types this method rules out box types that cannot hold the largest item, and returns the list of box types that will work sorted by volume.
items
Optional. If this is specified, it will match this category name to the categories attached to the boxes and only provide a list of boxes that match that category.
Returns an array reference of the Box::Calc::Items registered.
Returns the number of Box::Calc::Items registered.
Returns a specific Box::Calc::Item.
The array index of the item as it was registered.
Registers a new item. Returns the new item registered.
How many copies of this item should be included in the package?
The constructor parameters for the Box::Calc::Item.
Allows the loading of an entire dataset.
A hash reference containing the output of the dump method, with two exceptions:
dump
You can create a categories element that is an array ref in each box type rather than creating duplicate box types for each category.
categories
You can create a quantity element in each item rather than creating duplicate items to represent the quantity.
quantity
Returns an array reference of the list of items registered sorted by volume.
Optional. An array reference of items. Will call items if not passed in.
Returns an array reference of the list of items registered sorted by z, then x, then y, ascending.
Returns an array reference of the list of items registered sorted by z ASC, then area DESC
Returns an array reference of the list of items registered sorted by A DESC, then z ASC
Given the registered items, returns the max x, y, and z of all items registered as an array reference.
x
y
z
Returns an array reference of the list of Box::Calc::Boxes needed to pack up the items.
NOTE: This will be empty until you call pack_items.
pack_items
Returns the number of boxes needed to pack up the items.
Fetches a specific box from the list of <boxes>.
The array index of the box you wish to fetc.
Deletes the list of boxes.
boxes
If you wish to rerun the packing you should use this to delete the list of boxes first. This is handy if you needed to add an extra item or extra box type after you already ran pack_items.
Deletes the list of items.
For the sake of speed you may wish to reuse a Box::Calc instance with the box types already pre-loaded. In that case you'll want to use this method to remove the items you've already registered. You'll probably also want to call reset_boxes.
reset_boxes
Determines the median of z across all items in the list.
An array reference of items. Optional. Defaults to items.
Stacks all like-sized items into stacks of stack_height for denser packing. Could be used as an optimizer before running pack_items.
stack_height
A hash.
Optional. If not specified, will be the items list.
Optional. If not specified, will be determined by calling find_tallest_z.
find_tallest_z
Uses the list of box_types and the list of items to create the list of boxes to be packed. This method populates the boxes list.
Optional. If omitted the items list will be populated with whatever the current best general purpose preprocessed item list is. Currently that is sort_items_by_zA.
sort_items_by_zA
Optional. If this is specified, it will match this category name to the categories attached to the boxes and only pack in boxes that match that category.
Returns a data structure with all the item names and quantities packed into boxes. This can be used to generate manifests.
[ { # box one id => "xxx", name => "big box", weight => 30.1, packing_list => { "soda" => 3, "apple" => 1, "water bottle" => 2, } } ]
Returns a data structure with all the item names individually packed into rows, layers, and boxes. This can be used to build documentation on how to pack a set of boxes, and to generate a complete build history.
[ { # box one id => "xxx", name => "big box", layers => [ { # layer one rows => [ { # row one items => [ { # item one name => "apple", ... }, ... ], }, ... ], ... }, ], }, ]
There are some additional optimizations that could be done to speed things up a bit. We might also be able to get a better fill percentage (less void space), although that's not really the intent of Box::Calc.
Moose Ouch Log::Any
http://github.com/rizen/Box-Calc
http://github.com/rizen/Box-Calc/issues
Although these modules don't solve the same problem as this module, they may help you build something that does if Box::Calc doesn't quite help you do what you want.
Box::Calc is Copyright 2012 Plain Black Corporation (http://www.plainblack.com) and is licensed under the same terms as Perl itself.
1 POD Error
The following errors were encountered while parsing the POD:
=back without =over
To install Box::Calc, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Box::Calc
CPAN shell
perl -MCPAN -e shell install Box::Calc
For more information on module installation, please visit the detailed CPAN module installation guide.