1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Box packing (3D bin packing, knapsack problem) |
4
|
|
|
* @package BoxPacker |
5
|
|
|
* @author Doug Wright |
6
|
|
|
*/ |
7
|
|
|
declare(strict_types=1); |
8
|
|
|
namespace DVDoug\BoxPacker; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* List of possible packed box choices, ordered by utilisation (item count, volume) |
12
|
|
|
* @author Doug Wright |
13
|
|
|
* @package BoxPacker |
14
|
|
|
*/ |
15
|
|
|
class PackedBoxList extends \SplMinHeap |
16
|
|
|
{ |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Average (mean) weight of boxes |
20
|
|
|
* @var float |
21
|
|
|
*/ |
22
|
|
|
protected $meanWeight; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Compare elements in order to place them correctly in the heap while sifting up. |
26
|
|
|
* @see \SplMinHeap::compare() |
27
|
|
|
* |
28
|
|
|
* @param PackedBox $boxA |
29
|
|
|
* @param PackedBox $boxB |
30
|
|
|
* |
31
|
|
|
* @return int |
32
|
|
|
*/ |
33
|
6 |
View Code Duplication |
public function compare($boxA, $boxB): int |
|
|
|
|
34
|
|
|
{ |
35
|
6 |
|
$choice = $boxA->getItems()->count() - $boxB->getItems()->count(); |
36
|
6 |
|
if ($choice === 0) { |
37
|
3 |
|
$choice = $boxB->getInnerVolume() - $boxA->getInnerVolume(); |
38
|
|
|
} |
39
|
6 |
|
if ($choice === 0) { |
40
|
3 |
|
$choice = $boxA->getWeight() - $boxB->getWeight(); |
41
|
|
|
} |
42
|
6 |
|
return $choice; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Reversed version of compare |
47
|
|
|
* |
48
|
|
|
* @param PackedBox $boxA |
49
|
|
|
* @param PackedBox $boxB |
50
|
|
|
* |
51
|
|
|
* @return int |
52
|
|
|
*/ |
53
|
1 |
View Code Duplication |
public function reverseCompare($boxA, $boxB): int |
|
|
|
|
54
|
|
|
{ |
55
|
1 |
|
$choice = $boxB->getItems()->count() - $boxA->getItems()->count(); |
56
|
1 |
|
if ($choice === 0) { |
57
|
|
|
$choice = $boxA->getInnerVolume() - $boxB->getInnerVolume(); |
58
|
|
|
} |
59
|
1 |
|
if ($choice === 0) { |
60
|
|
|
$choice = $boxB->getWeight() - $boxA->getWeight(); |
61
|
|
|
} |
62
|
1 |
|
return $choice; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Calculate the average (mean) weight of the boxes |
67
|
|
|
* @return float |
68
|
|
|
*/ |
69
|
6 |
|
public function getMeanWeight(): float |
70
|
|
|
{ |
71
|
|
|
|
72
|
6 |
|
if (!is_null($this->meanWeight)) { |
73
|
5 |
|
return $this->meanWeight; |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
foreach (clone $this as $box) { |
77
|
6 |
|
$this->meanWeight += $box->getWeight(); |
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
return $this->meanWeight /= $this->count(); |
81
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Calculate the variance in weight between these boxes |
86
|
|
|
* @return float |
87
|
|
|
*/ |
88
|
6 |
|
public function getWeightVariance(): float |
89
|
|
|
{ |
90
|
6 |
|
$mean = $this->getMeanWeight(); |
91
|
|
|
|
92
|
6 |
|
$weightVariance = 0; |
93
|
6 |
|
foreach (clone $this as $box) { |
94
|
6 |
|
$weightVariance += pow($box->getWeight() - $mean, 2); |
95
|
|
|
} |
96
|
|
|
|
97
|
6 |
|
return $weightVariance / $this->count(); |
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get volume utilisation of the set of packed boxes |
103
|
|
|
* @return float |
104
|
|
|
*/ |
105
|
1 |
|
public function getVolumeUtilisation(): float |
106
|
|
|
{ |
107
|
1 |
|
$itemVolume = 0; |
108
|
1 |
|
$boxVolume = 0; |
109
|
|
|
|
110
|
|
|
/** @var PackedBox $box */ |
111
|
1 |
|
foreach (clone $this as $box) { |
112
|
1 |
|
$boxVolume += $box->getInnerVolume(); |
113
|
|
|
|
114
|
|
|
/** @var PackedItem $item */ |
115
|
1 |
View Code Duplication |
foreach (clone $box->getItems() as $item) { |
|
|
|
|
116
|
1 |
|
$itemVolume += ($item->getItem()->getWidth() * $item->getItem()->getLength() * $item->getItem()->getDepth()); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
return round($itemVolume / $boxVolume * 100, 1); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Do a bulk insert |
125
|
|
|
* @param array $boxes |
126
|
|
|
*/ |
127
|
5 |
|
public function insertFromArray(array $boxes): void |
128
|
|
|
{ |
129
|
5 |
|
foreach ($boxes as $box) { |
130
|
3 |
|
$this->insert($box); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.