Calc.pm

filter: Statementstmt 89.9% 223 / 248 Branchbran 76.0% 35 / 46 Conditioncond 40.0% 22 / 55 MC/DCmcdc 5.8% 2 / 34 Subroutinesub 91.1% 31 / 34 Podpod 100% 19 / 19 totaltotal 76.1% 332 / 436 CC 57 SCAR 19.6
CC
57
Cov
80%
sort_items_by_zxy
20.0
pack_items
15.3
sort_items_by_z_desc_A
12.0
CRAP 7.1
↑ Summary Calc/Layer.pm »
1 package Box::Calc;
2 8 $Box::Calc::VERSION = '1.0205';
3 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 8 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Box::Calc::BoxType;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 8 ▶use Box::Calc::Item;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 8 ▶use Box::Calc::Box;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 8 ▶use List::MoreUtils qw(natatime);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 8 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use Ouch;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 8 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Box::Calc - Packing Algorithm
16  
17 =head1 VERSION
18  
19 version 1.0205
20  
21 =head1 SYNOPSIS
22  
23 use Box::Calc;
24
25 my $box_calc = Box::Calc->new;
26
27 # define the possible box types
28 $box_calc->add_box_type( x => 12, y => 12, z => 18, weight => 16, name => 'big box' );
29 $box_calc->add_box_type( x => 4, y => 6, z => 8, weight => 6, name => 'small box' );
30  
31 # define the items you want to put into boxes
32 $box_calc->add_item( 3, { x => 6, y => 3, z => 3, weight => 12, name => 'soda' });
33 $box_calc->add_item( 1, { x => 3.3, y => 3, z => 4, weight => 4.5, name => 'apple' });
34 $box_calc->add_item( 2, { x => 8, y => 2.5, z => 2.5, weight => 14, name => 'water bottle' });
35  
36 # figure out what you need to pack this stuff
37 $box_calc->pack_items;
38
39 # how many boxes do you need
40 my $box_count = $box_calc->count_boxes; # 2
41
42 # interrogate the boxes
43 my $box = $box_calc->get_box(-1); # the last box
44 my $weight = $box->calculate_weight;
45
46 # get a packing list
47 my $packing_list = $box_calc->packing_list;
48
49 =head1 DESCRIPTION
50  
51 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.
52  
53 Once it's done packing the boxes, you can get a packing list for each box, as well as the weight of each box.
54  
55 =head2 How The Algorithm Works
56  
57 Box::Calc is intended to pack boxes in the simplest way possible. Here's what it does:
58  
59 =over
60  
61 =item 1
62  
63 Sort all the items by volume.
64  
65 =item 2
66  
67 Eliminate all boxes that won't fit the largest items.
68  
69 =item 3
70  
71 Choose the smallest box still available.
72  
73 =item 4
74  
75 Place the items in a row starting with the largest items.
76  
77 =item 5
78  
79 When the row runs out of space, add another.
80  
81 =item 6
82  
83 When you run out of space to add rows, add a layer.
84  
85 =item 7
86  
87 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.
88  
89 =item 8
90  
91 Repeat from step 3 until all items are packed into boxes.
92  
93 =back
94  
95 =head2 Motivation
96  
97 At The Game Crafter (L<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.
98  
99 =head2 Tips
100  
101 When adding items, be sure to use the outer most dimensions of oddly shaped items, otherwise they may not fit the box.
102  
103 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.
104  
105 What units you use (inches, centimeters, ounces, pounds, grams, kilograms, etc) don't matter as long as you use them consistently.
106  
107 =head1 METHODS
108  
109 =head2 new()
110  
111 Constructor.
112  
113 =head2 box_types()
114  
115 Returns an array reference of the L<Box::Calc::BoxType>s registered.
116  
117 =head2 count_box_types()
118  
119 Returns the number of L<Box::Calc::BoxType>s registered.
120  
121 =head2 get_box_type(index)
122  
123 Returns a specific L<Box::Calc::BoxType> from the list of C<box_types>
124  
125 =over
126  
127 =item index
128  
129 An array index. For example this would return the last box type added:
130  
131 $box_calc->get_box_type(-1)
132  
133 =back
134  
135 =cut
136  
137 has box_types => (
138 is => 'rw',
139 isa => 'ArrayRef[Box::Calc::BoxType]',
140 9 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
141 8 traits => ['Array'],
142 handles => {
143 push_box_types => 'push',
144 count_box_types => 'count',
145 get_box_type => 'get',
146 }
147 );
148  
149 =head2 add_box_type(params)
150  
151 Adds a new L<Box::Calc::BoxType> to the list of C<box_types>. Returns the newly created L<Box::Calc::BoxType> instance.
152  
153 =over
154  
155 =item params
156  
157 The list of constructor parameters for L<Box::Calc::BoxType>.
158  
159 B<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.
160  
161 =back
162  
163 =cut
164  
165 sub add_box_type {
166 29 ▶ my $self = shift;
Subroutine add_box_type: called CC=4 SCAR=13.9
Pod documented
167 29 my $args;
168 29 ▶ if (ref $_[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] eq 'HASH') { } 11 18 29
169 11 $args = shift;
170 }
171 else {
172 18 $args = { @_ };
173 }
174 29 my $categories = delete $args->{categories};
175 29 ▶ if (defined $categories) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $categories) { } 3 26 29
176 3 foreach my $category (@{$categories}) {
177 4 my %copy = %{$args};
178 4 $copy{category} = $category;
179 4 $self->push_box_types(Box::Calc::BoxType->new(%copy));
180 }
181 }
182 else {
183 26 $self->push_box_types(Box::Calc::BoxType->new($args));
184 }
185 29 return $self->get_box_type(-1);
186 }
187  
188  
189 =head2 box_type_categories()
190  
191 Returns an array reference of categories associated with the box types.
192  
193 =cut
194  
195 has box_type_categories => (
196 is => 'rw',
197 lazy => 1,
198 isa => 'ArrayRef',
199 default => sub {
200 1 ▶ my $self = shift;
Subroutine __ANON__: called CC=3 SCAR=11.0
201 1 my %categories = ();
202 1 foreach my $box_type (@{$self->box_types}) {
203 7 ▶ next if $box_type->category eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $box_type->category eq "" 3 4 7
204 4 $categories{$box_type->category} = 1;
205 }
206 1 return [sort keys %categories];
207 },
208 8 );
209  
210  
211 =head2 sort_box_types_by_volume()
212  
213 Sorts the list of C<box_types> by volume and then returns an array reference of that list.
214  
215 =over
216  
217 =item types
218  
219 Optional. Array ref of box types. Will call C<box_types> if not passed in.
220  
221 =back
222  
223 =cut
224  
225 sub sort_box_types_by_volume {
226 18 ▶ my $self = shift;
Subroutine sort_box_types_by_volume: called CC=2 SCAR=7.1
Pod documented
227 18 ▶ my $types = shift || $self->box_types;
Condition33%1/3
shift() || $self->box_types
l!l&&r!l&&!r
0180
MC/DC0%0/2
shift() || $self->box_types
shift() $self->box_types
Truth table33%1/3
A || B
A= shift()B= $self->box_types
ABresult
1X1
011
000
228 18 my @sorted = sort { ($a->volume) <=> ($b->volume ) } @{$types};
229 18 return \@sorted;
230 }
231  
232 =head2 determine_viable_box_types(category)
233  
234 Given the list of C<items> and the list of C<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.
235  
236 =over
237  
238 =item category
239  
240 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.
241  
242 =back
243  
244 =cut
245  
246 sub determine_viable_box_types {
247 17 ▶ my ($self, $category) = @_;
Subroutine determine_viable_box_types: called CC=8 SCAR=20.8
Pod documented
248 17 my ($item_x, $item_y, $item_z) = sort {$b <=> $a} @{$self->find_max_dimensions_of_items};
249 17 my @viable;
250 17 foreach my $box_type (@{$self->sort_box_types_by_volume}) {
251 53 ▶ if (defined $category) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $category) 18 35 53
252 18 ▶ next unless $category eq $box_type->category;
Branch: 1 branch
BranchTrueFalseTotal
unless $category eq $box_type->category 13 5 18
253 }
254 40 my ($box_type_x, $box_type_y, $box_type_z) = @{$box_type->dimensions};
255 40 ▶ if ($item_x <= $box_type_x && $item_y <= $box_type_y && $item_z <= $box_type_z) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item_x <= $box_type_x and $item_y <= $box_type_y and $item_z <= $box_type_z) 34 6 40
Condition66%4/6
$item_x <= $box_type_x and $item_y <= $box_type_y and $item_z <= $box_type_z
!ll&&!rl&&r
6034
$item_x <= $box_type_x and $item_y <= $box_type_y
!ll&&!rl&&r
6034
MC/DC33%1/3
$item_x <= $box_type_x and $item_y <= $box_type_y and $item_z <= $box_type_z
$item_x <= $box_type_x $item_y <= $box_type_y $item_z <= $box_type_z
Truth table50%2/4
A and B and C
A= $item_x <= $box_type_xB= $item_y <= $box_type_yC= $item_z <= $box_type_z
ABCresult
0XX0
10X0
1100
1111
256 34 push @viable, $box_type;
257 }
258 }
259 17 ▶ unless (scalar @viable) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @viable) 1 16 17
260 1 $log->fatal('There are no box types that can fit the items.');
261 1 ouch 'no viable box types', 'There are no box types that can fit the items. ('.join(', ', $item_x, $item_y, $item_z).')', [$item_x, $item_y, $item_z];
262 }
263 16 return \@viable;
264 }
265  
266 =head2 items()
267  
268 Returns an array reference of the L<Box::Calc::Item>s registered.
269  
270 =head2 count_items()
271  
272 Returns the number of L<Box::Calc::Item>s registered.
273  
274 =head2 get_item(index)
275  
276 Returns a specific L<Box::Calc::Item>.
277  
278 =over
279  
280 =item index
281  
282 The array index of the item as it was registered.
283  
284 =back
285  
286 =cut
287  
288 has items => (
289 is => 'rw',
290 isa => 'ArrayRef[Box::Calc::Item]',
291 9 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
292 8 traits => ['Array'],
293 handles => {
294 push_items => 'push',
295 count_items => 'count',
296 get_item => 'get',
297 }
298 );
299  
300 =head2 add_item(quantity, params)
301  
302 Registers a new item. Returns the new item registered.
303  
304 =over
305  
306 =item quantity
307  
308 How many copies of this item should be included in the package?
309  
310 =item params
311  
312 The constructor parameters for the L<Box::Calc::Item>.
313  
314 =back
315  
316 =cut
317  
318 sub add_item {
319 27 ▶ my ($self, $quantity, @params) = @_;
Subroutine add_item: called CC=2 SCAR=6.9
Pod documented
320 27 my $item = Box::Calc::Item->new(@params);
321 27 for (1..$quantity) {
322 20962 $self->push_items($item);
323 }
324 27 return $self->get_item(-1);
325 }
326  
327 =head2 load(payload)
328  
329 Allows the loading of an entire dataset.
330  
331 =over
332  
333 =item payload
334  
335 A hash reference containing the output of the C<dump> method, with two exceptions:
336  
337 =over
338  
339 =item *
340  
341 You can create a C<categories> element that is an array ref in each box type rather than creating duplicate box types for each category.
342  
343 =item *
344  
345 You can create a C<quantity> element in each item rather than creating duplicate items to represent the quantity.
346  
347 =back
348  
349 =back
350  
351 =cut
352  
353 sub load {
354 1 ▶ my ($self, $payload) = @_;
Subroutine load: called CC=4 SCAR=13.9
Pod documented
355 # note that we copy the box type and item to avoid modifying the original
356 1 foreach my $type (@{$payload->{box_types}}) {
357 7 $self->add_box_type(%{$type});
358 }
359 1 foreach my $item (@{$payload->{items}}) {
360 6 ▶ $self->add_item($item->{quantity} || 1, %{$item});
Condition50%1/2
$item->{'quantity'} || 1
l!l
06
MC/DC0%0/1
$item->{'quantity'} || 1
$item->{'quantity'}
Truth table50%1/2
A
A= $item->{'quantity'}
Aresult
11
00
361 }
362 }
363  
364 =head2 dump()
365  
366 =cut
367  
368 sub dump {
369 2 ▶ my ($self) = @_;
Subroutine dump: called CC=3 SCAR=11.0
Pod documented
370 2 my $payload = {};
371 2 foreach my $type (@{$self->box_types}) {
372 14 push @{$payload->{box_types}}, $type->describe;
373 }
374 2 foreach my $item (@{$self->items}) {
375 12 push @{$payload->{items}}, $item->describe;
376 }
377 2 return $payload;
378 }
379  
380  
381 =head2 sort_items_by_volume()
382  
383 Returns an array reference of the list of C<items> registered sorted by volume.
384  
385 =over
386  
387 =item items
388  
389 Optional. An array reference of items. Will call C<items> if not passed in.
390  
391 =back
392  
393 =cut
394  
395 sub sort_items_by_volume {
396 1 ▶ my $self = shift;
Subroutine sort_items_by_volume: called CC=2 SCAR=7.1
Pod documented
397 1 ▶ my $items = shift || $self->items;
Condition33%1/3
shift() || $self->items
l!l&&r!l&&!r
010
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table33%1/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
398 1 my @sorted = sort { ($a->volume) <=> ($b->volume ) } @{$items};
399 1 return \@sorted;
400 }
401  
402 =head2 sort_items_by_zxy()
403  
404 Returns an array reference of the list of C<items> registered sorted by z, then x, then y, ascending.
405  
406 =over
407  
408 =item items
409  
410 Optional. An array reference of items. Will call C<items> if not passed in.
411  
412 =back
413  
414 =cut
415  
416 sub sort_items_by_zxy {
417 0 ▶ my $self = shift;
Subroutine sort_items_by_zxy: not called CC=4 SCAR=30.0
Pod documented
418 0 ▶ my $items = shift || $self->items;
Condition0%0/3
shift() || $self->items
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table0%0/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
419 my @sorted = sort {
420 0 ▶ $a->z <=> $b->z
Branch: 1 branch
BranchTrueFalseTotal
$a->z <=> $b->z or $a->x <=> $b->x or $a->y <=> $b->y 0 0 0
Condition0%0/3
$a->z <=> $b->z or $a->x <=> $b->x
l!l&&r!l&&!r
000
MC/DC0%0/2
$a->z <=> $b->z or $a->x <=> $b->x
$a->z <=> $b->z $a->x <=> $b->x
Truth table0%0/3
A or B
A= $a->z <=> $b->zB= $a->x <=> $b->x
ABresult
1X1
011
000
421 || $a->x <=> $b->x
422 || $a->y <=> $b->y
423 0 } @{$items};
424 0 return \@sorted;
425 }
426  
427 =head2 sort_items_by_z_desc_A()
428  
429 Returns an array reference of the list of C<items> registered sorted by z DESC, then area DESC
430  
431 =over
432  
433 =item items
434  
435 Optional. An array reference of items. Will call C<items> if not passed in.
436  
437 =back
438  
439 =cut
440  
441 sub sort_items_by_z_desc_A {
442 0 ▶ my $self = shift;
Subroutine sort_items_by_z_desc_A: not called CC=3 SCAR=24.8
Pod documented
443 0 ▶ my $items = shift || $self->items;
Condition0%0/3
shift() || $self->items
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table0%0/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
444 0 my @sorted = map { $_->[1] }
445 sort {
446 $b->[0]->{z} <=> $a->[0]->{z}
447 || $b->[0]->{A} <=> $a->[0]->{A}
448 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$Box::Calc::b->[0]{"z"} <=> $Box::Calc::a->[0]{"z"} or $Box::Calc::b->[0]{"A"} <=> $Box::Calc::a->[0]{"A"} 0 0 0
449 ##Fetch Z and calculate A
450 0 map { [ { z=>$_->z, A=>$_->x*$_->y }, $_ ] } @{$items};
451 0 return \@sorted;
452 }
453  
454 =head2 sort_items_by_zA()
455  
456 Returns an array reference of the list of C<items> registered sorted by z ASC, then area DESC
457  
458 =over
459  
460 =item items
461  
462 Optional. An array reference of items. Will call C<items> if not passed in.
463  
464 =back
465  
466 =cut
467  
468 sub sort_items_by_zA {
469 11 ▶ my $self = shift;
Subroutine sort_items_by_zA: called CC=3 SCAR=11.0
Pod documented
470 11 ▶ my $items = shift || $self->items;
Condition66%2/3
shift() || $self->items
l!l&&r!l&&!r
1100
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table66%2/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
471 20925 my @sorted = map { $_->[1] }
472 sort {
473 $a->[0]->{z} <=> $b->[0]->{z}
474 || $b->[0]->{A} <=> $a->[0]->{A}
475 21020 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$Box::Calc::a->[0]{"z"} <=> $Box::Calc::b->[0]{"z"} or $Box::Calc::b->[0]{"A"} <=> $Box::Calc::a->[0]{"A"} 20969 51 21020
476 ##Fetch Z and calculate A
477 11 map { [ { z=>$_->z, A=>$_->x*$_->y }, $_ ] } @{$items};
478 11 return \@sorted;
479 }
480  
481 =head2 sort_items_by_Az()
482  
483 =over
484  
485 =item items
486  
487 Optional. An array reference of items. Will call C<items> if not passed in.
488  
489 =back
490  
491 Returns an array reference of the list of C<items> registered sorted by A DESC, then z ASC
492  
493 =cut
494  
495 sub sort_items_by_Az {
496 0 ▶ my $self = shift;
Subroutine sort_items_by_Az: not called CC=3 SCAR=24.8
Pod documented
497 0 ▶ my $items = shift || $self->items;
Condition0%0/3
shift() || $self->items
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table0%0/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
498 0 my @sorted = map { $_->[1] }
499 sort {
500 $b->[0]->{A} <=> $a->[0]->{A}
501 || $a->[0]->{z} <=> $b->[0]->{z}
502 0 ▶ }
Branch: 1 branch
BranchTrueFalseTotal
$Box::Calc::b->[0]{"A"} <=> $Box::Calc::a->[0]{"A"} or $Box::Calc::a->[0]{"z"} <=> $Box::Calc::b->[0]{"z"} 0 0 0
503 ##Fetch Z and calculate A
504 0 map { [ { z=>$_->z, A=>$_->x*$_->y }, $_ ] } @{$items};
505 0 return \@sorted;
506 }
507  
508 =head2 find_max_dimensions_of_items()
509  
510 Given the registered C<items>, returns the max C<x>, C<y>, and C<z> of all items registered as an array reference.
511  
512 =cut
513  
514 has find_max_dimensions_of_items => (
515 is => 'rw',
516 lazy => 1,
517 isa => 'ArrayRef',
518 clearer => 'clear_max_dimensions_of_items',
519 default => sub {
520 9 ▶ my $self = shift;
Subroutine __ANON__: called CC=5 SCAR=16.1
521 9 my $x = 0;
522 9 my $y = 0;
523 9 my $z = 0;
524 9 foreach my $item (@{$self->items}) {
525 20928 my ($ex, $ey, $ez) = @{$item->dimensions};
526 20928 ▶ $x = $ex if $ex > $x;
Branch: 1 branch
BranchTrueFalseTotal
if $ex > $x 11 20917 20928
527 20928 ▶ $y = $ey if $ey > $y;
Branch: 1 branch
BranchTrueFalseTotal
if $ey > $y 11 20917 20928
528 20928 ▶ $z = $ez if $ez > $z;
Branch: 1 branch
BranchTrueFalseTotal
if $ez > $z 11 20917 20928
529 }
530 9 return [$x, $y, $z];
531 }
532 8 );
533  
534 =head2 boxes()
535  
536 Returns an array reference of the list of L<Box::Calc::Box>es needed to pack up the items.
537  
538 B<NOTE:> This will be empty until you call C<pack_items>.
539  
540 =head2 count_boxes()
541  
542 Returns the number of boxes needed to pack up the items.
543  
544 =head2 get_box(index)
545  
546 Fetches a specific box from the list of <boxes>.
547  
548 =over
549  
550 =item index
551  
552 The array index of the box you wish to fetc.
553  
554 =back
555  
556 =cut
557  
558 has boxes => (
559 is => 'rw',
560 isa => 'ArrayRef[Box::Calc::Box]',
561 9 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
562 8 traits => ['Array'],
563 handles => {
564 push_boxes => 'push',
565 count_boxes => 'count',
566 get_box => 'get',
567 }
568 );
569  
570 =head2 reset_boxes()
571  
572 Deletes the list of C<boxes>.
573  
574 If you wish to rerun the packing you should use this to delete the list of C<boxes> first. This is handy if you needed to add an extra item or extra box type after you already ran C<pack_items>.
575  
576 =cut
577  
578 sub reset_boxes {
579 4 ▶ my $self = shift;
Subroutine reset_boxes: called CC=1 SCAR=0.0
Pod documented
580 4 $self->boxes([]);
581 }
582  
583 =head2 reset_items()
584  
585 Deletes the list of C<items>.
586  
587 For the sake of speed you may wish to reuse a L<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 C<reset_boxes>.
588  
589 =cut
590  
591 sub reset_items {
592 1 ▶ my $self = shift;
Subroutine reset_items: called CC=1 SCAR=0.0
Pod documented
593 1 $self->items([]);
594 1 $self->clear_max_dimensions_of_items;
595 }
596  
597 =head2 make_box($box_type)
598  
599 Handy method to create new box using a specified
600 box type.
601  
602 =cut
603  
604 sub make_box {
605 44 ▶ my ($self, $box_type) = @_;
Subroutine make_box: called CC=1 SCAR=0.0
Pod documented
606 44 return Box::Calc::Box->new(
607 swap_xy => 1,
608 mail_service_name => $box_type->mail_service_name,
609 x => $box_type->x,
610 y => $box_type->y,
611 z => $box_type->z,
612 weight => $box_type->weight,
613 max_weight => $box_type->max_weight,
614 void_weight => $box_type->void_weight,
615 name => $box_type->name,
616 outer_x => $box_type->outer_x,
617 outer_y => $box_type->outer_y,
618 outer_z => $box_type->outer_z,
619 );
620 }
621  
622 =head2 find_tallest_z ( [ items ] )
623  
624 Determines the median of z across all items in the list.
625  
626 =over
627  
628 =item items
629  
630 An array reference of items. Optional. Defaults to C<items>.
631  
632 =back
633  
634 =cut
635  
636 sub find_tallest_z {
637 3 ▶ my $self = shift;
Subroutine find_tallest_z: called CC=2 SCAR=7.0
Pod documented
638 3 ▶ my $items = shift || $self->items;
Condition66%2/3
shift() || $self->items
l!l&&r!l&&!r
210
MC/DC0%0/2
shift() || $self->items
shift() $self->items
Truth table66%2/3
A || B
A= shift()B= $self->items
ABresult
1X1
011
000
639 3 return max map { $_->z } @{$items};
640 }
641  
642  
643 =head2 stack_like_items( options )
644  
645 Stacks all like-sized items into stacks of C<stack_height> for denser packing. Could be used as an optimizer before running C<pack_items>.
646  
647 =over
648  
649 =item options
650  
651 A hash.
652  
653 =over
654  
655 =item items
656  
657 Optional. If not specified, will be the C<items> list.
658  
659 =item stack_height
660  
661 Optional. If not specified, will be determined by calling C<find_tallest_z>.
662  
663 =back
664  
665 =back
666  
667 =cut
668  
669 sub stack_like_items {
670 2 ▶ my ($self, %options) = @_;
Subroutine stack_like_items: called CC=9 SCAR=22.6
Pod documented
671 2 ▶ my $items = $options{items} || $self->items;
Condition33%1/3
$options{'items'} || $self->items
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{'items'} || $self->items
$options{'items'} $self->items
Truth table33%1/3
A || B
A= $options{'items'}B= $self->items
ABresult
1X1
011
000
672 2 ▶ my $stack_height = $options{stack_height} || $self->find_tallest_z($items);
Condition33%1/3
$options{'stack_height'} || $self->find_tallest_z($items)
l!l&&r!l&&!r
020
MC/DC0%0/2
$options{'stack_height'} || $self->find_tallest_z($items)
$options{'stack_height'} $self->find_tallest_z($items)
Truth table33%1/3
A || B
A= $options{'stack_height'}B= $self->find_tallest_z($items)
ABresult
1X1
011
000
673 2 my %like;
674 2 foreach my $item (@{$items}) {
675 110 push @{$like{$item->extent}}, $item;
676 }
677 2 my @stacks;
678 2 foreach my $kind (values %like) {
679 6 ▶ if (scalar @{$kind} == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$kind;} == 1) { } 0 6 6
680 0 push @stacks, $kind->[0];
681 }
682 else {
683 6 ▶ my $items_per_stack = int($stack_height / $kind->[0]->z) || 1;
Condition50%1/2
int $stack_height / $kind->[0]->z || 1
l!l
60
MC/DC0%0/1
int $stack_height / $kind->[0]->z || 1
int $stack_height / $kind->[0]->z
Truth table50%1/2
A
A= int $stack_height / $kind->[0]->z
Aresult
11
00
684 6 my $iterator = natatime($items_per_stack, @{$kind});
685 6 ▶ while (my @items = $iterator->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@items) = &$iterator()) 12 6 18
686 12 my $count = scalar @items;
687 12 ▶ if ($count == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count == 1) { } 6 6 12
688 6 push @stacks, $items[0];
689 }
690 else {
691 6 my $item = $items[0];
692 6 push @stacks, Box::Calc::Item->new(
693 x => $item->x,
694 y => $item->y,
695 z => $item->z * $count,
696 weight => $item->weight * $count,
697 name => 'Stack of '.$count.' '.$item->name,
698 no_sort => 1,
699 );
700 }
701 }
702 }
703 }
704 2 return \@stacks;
705 }
706  
707 =head2 pack_items(options)
708  
709 Uses the list of C<box_types> and the list of C<items> to create the list of boxes to be packed. This method populates the C<boxes> list.
710  
711 =over
712  
713 =item options
714  
715 A hash.
716  
717 =over
718  
719 =item items
720  
721 Optional. If omitted the items list will be populated with whatever the current B<best> general purpose preprocessed item list is. Currently that is C<sort_items_by_zA>.
722  
723 =item category
724  
725 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.
726  
727 =back
728  
729 =back
730  
731 =cut
732  
733 sub pack_items {
734 11 ▶ my ($self, %options) = @_;
Subroutine pack_items: called CC=14 SCAR=27.2
Pod documented
735 11 my $category = $options{category};
736 11 ▶ my $items = $options{items} || $self->sort_items_by_zA;
Condition66%2/3
$options{'items'} || $self->sort_items_by_zA
l!l&&r!l&&!r
1100
MC/DC0%0/2
$options{'items'} || $self->sort_items_by_zA
$options{'items'} $self->sort_items_by_zA
Truth table33%1/3
A || B
A= $options{'items'}B= $self->sort_items_by_zA
ABresult
1X1
011
000
737 11 my $item_count = scalar(@{$items});
738 11 my @box_types = @{$self->determine_viable_box_types($category)};
739 11 my $countdown = scalar(@box_types);
740 11 BOXTYPE: foreach my $box_type (@box_types) {
741 20 $log->info("Box Type: ".$box_type->name);
742 20 $countdown--;
743 20 my $box = $self->make_box($box_type);
744 20 ITEM: foreach my $item (@{$items}) {
745 27834 $log->info("Item: ".$item->name);
746
747 # swap the item's x & y if it will make the item fit tighter
748 27834 ▶ if ($item->x > 0 && $item->y > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->x > 0 and $item->y > 0) { } 27834 0 27834
Condition33%1/3
$item->x > 0 and $item->y > 0
!ll&&!rl&&r
0027834
MC/DC0%0/2
$item->x > 0 and $item->y > 0
$item->x > 0 $item->y > 0
Truth table33%1/3
A and B
A= $item->x > 0B= $item->y > 0
ABresult
0X0
100
111
749 27834 $log->debug("Item's dimensions are not 0.");
750 27834 ▶ if ($box->x >= $item->y && $box->y >= $item->x) { # see if the item would still fit in the box if it swapped
Branch: 1 branch
BranchTrueFalseTotal
if ($box->x >= $item->y and $box->y >= $item->x) 27834 0 27834
Condition33%1/3
$box->x >= $item->y and $box->y >= $item->x
!ll&&!rl&&r
0027834
MC/DC0%0/2
$box->x >= $item->y and $box->y >= $item->x
$box->x >= $item->y $box->y >= $item->x
Truth table33%1/3
A and B
A= $box->x >= $item->yB= $box->y >= $item->x
ABresult
0X0
100
111
751 27834 $log->debug('Item would still fit in the box if we rotated it.');
752 27834 my $original_x_per_layer = int($box->x / $item->x);
753 27834 my $original_y_per_layer = int($box->y / $item->y);
754 27834 my $original_count_per_layer = $original_x_per_layer * $original_y_per_layer;
755 27834 my $new_count_per_layer = int($box->x / $item->y) * int($box->y / $item->x);
756 27834 ▶ if ( $new_count_per_layer > $original_count_per_layer # you can fit more items per layer in a swap
Branch: 1 branch
BranchTrueFalseTotal
if ($new_count_per_layer > $original_count_per_layer or $original_x_per_layer == 0 or $original_y_per_layer == 0) 0 27829 27829
Condition66%4/6
$new_count_per_layer > $original_count_per_layer or $original_x_per_layer == 0 or $original_y_per_layer == 0
l!l&&r!l&&!r
5027829
$new_count_per_layer > $original_count_per_layer or $original_x_per_layer == 0
l!l&&r!l&&!r
5027829
MC/DC33%1/3
$new_count_per_layer > $original_count_per_layer or $original_x_per_layer == 0 or $original_y_per_layer == 0
$new_count_per_layer > $original_count_per_layer $original_x_per_layer == 0 $original_y_per_layer == 0
Truth table50%2/4
A or B or C
A= $new_count_per_layer > $original_count_per_layerB= $original_x_per_layer == 0C= $original_y_per_layer == 0
ABCresult
1XX1
01X1
0011
0000
757 || $original_x_per_layer == 0 || $original_y_per_layer == 0 # if we keep it the current rotation we definitely won't fit, probably due to previous rotation
758 ) {
759 5 $log->info('Rotating '.$item->{name}.', because we can fit more per layer if we rotate.');
760 5 my $temp_x = $item->x;
761 5 $item->x($item->y);
762 5 $item->y($temp_x);
763 }
764 }
765 }
766 else {
767 0 $log->error('Item has a zero (0) dimension. That should not happen.');
768 }
769  
770 # pack the item into the box
771 27834 ▶ unless ($box->pack_item($item)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($box->pack_item($item)) 33 27801 27834
772 33 ▶ if ($countdown) { # we still have other boxes to try
Branch: 1 branch
BranchTrueFalseTotal
if ($countdown) { } 9 24 33
773 9 $log->info("moving to next box type");
774 9 next BOXTYPE;
775 }
776 else { # no more boxes to try, time for spanning
777 24 ▶ if (scalar(@{$self->boxes}) > $item_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->boxes;} > $item_count) 0 24 24
778 0 $log->warn("More boxes than items.");
779 #ouch 'more boxes than items', 'The number of boxes has exceded the number of items, which should never happen.';
780 }
781 24 $log->info("no more box types, spanning");
782 24 $self->push_boxes($box);
783 24 $box = $self->make_box($box_type);
784 24 redo ITEM;
785 }
786 }
787 }
788
789 # we made it through our entire item list, yay!
790 11 $log->info("finished!");
791 11 $self->push_boxes($box);
792 11 last BOXTYPE;
793 }
794 }
795  
796 =head2 packing_list()
797  
798 Returns a data structure with all the item names and quantities packed into boxes. This can be used to generate manifests.
799  
800 [
801 { # box one
802 id => "xxx",
803 name => "big box",
804 weight => 30.1,
805 packing_list => {
806 "soda" => 3,
807 "apple" => 1,
808 "water bottle" => 2,
809 }
810 }
811 ]
812  
813 =cut
814  
815 sub packing_list {
816 2 ▶ my $self = shift;
Subroutine packing_list: called CC=2 SCAR=6.9
Pod documented
817 2 my @boxes;
818 2 foreach my $box (@{$self->boxes}) {
819 26 my ($weight, $list) = $box->packing_list;
820 26 push @boxes, {
821 id => $box->id,
822 name => $box->name,
823 weight => $weight,
824 packing_list => $list,
825 };
826 }
827 2 return \@boxes;
828 }
829  
830 =head2 packing_instructions()
831  
832 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.
833  
834 [
835 { # box one
836 id => "xxx",
837 name => "big box",
838 layers => [
839 { # layer one
840 rows => [
841 { # row one
842 items => [
843 { # item one
844 name => "apple",
845 ...
846 },
847 ...
848 ],
849 },
850 ...
851 ],
852 ...
853 },
854 ],
855 },
856 ]
857  
858 =cut
859  
860 sub packing_instructions {
861 4 ▶ my $self = shift;
Subroutine packing_instructions: called CC=1 SCAR=0.0
Pod documented
862 4 my @boxes = map { $_->packing_instructions} @{ $self->boxes };
863 4 return \@boxes;
864 }
865  
866 =head1 TODO
867  
868 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.
869  
870 =head1 PREREQS
871  
872 L<Moose>
873 L<Ouch>
874 L<Log::Any>
875 L<Data::GUID>
876  
877 =head1 SUPPORT
878  
879 =over
880  
881 =item Repository
882  
883 L<http://github.com/rizen/Box-Calc>
884  
885 =item Bug Reports
886  
887 L<http://github.com/rizen/Box-Calc/issues>
888  
889 =back
890  
891  
892 =head1 SEE ALSO
893  
894 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.
895  
896 =over
897  
898 =item L<Algorithm::Knapsack>
899  
900 =item L<Algorithm::Bucketizer>
901  
902 =item L<Algorithm::Knap01DP>
903  
904 =back
905  
906 =head1 AUTHOR
907  
908 =over
909  
910 =item JT Smith <jt_at_plainblack_dot_com>
911  
912 =item Colin Kuskie <colink_at_plainblack_dot_com>
913  
914 =back
915  
916 =head1 LEGAL
917  
918 Box::Calc is Copyright 2012 Plain Black Corporation (L<http://www.plainblack.com>) and is licensed under the same terms as Perl itself.
919  
920 =cut
921  
922 8 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
923 8 __PACKAGE__->meta->make_immutable;
↑ Summary Calc/Layer.pm »