|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Box packing (3D bin packing, knapsack problem). |
|
4
|
|
|
* |
|
5
|
|
|
* @author Doug Wright |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace DVDoug\BoxPacker; |
|
10
|
|
|
|
|
11
|
|
|
use function count; |
|
12
|
|
|
use DVDoug\BoxPacker\Exception\ItemTooLargeException; |
|
13
|
|
|
use DVDoug\BoxPacker\Exception\NoBoxesAvailableException; |
|
14
|
|
|
use Psr\Log\LoggerAwareInterface; |
|
15
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
16
|
|
|
use Psr\Log\LogLevel; |
|
17
|
|
|
use Psr\Log\NullLogger; |
|
18
|
|
|
use function usort; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Actual packer. |
|
22
|
|
|
* |
|
23
|
|
|
* @author Doug Wright |
|
24
|
|
|
*/ |
|
25
|
|
|
class Packer implements LoggerAwareInterface |
|
26
|
|
|
{ |
|
27
|
|
|
use LoggerAwareTrait; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Number of boxes at which balancing weight is deemed not worth it. |
|
31
|
|
|
* |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $maxBoxesToBalanceWeight = 12; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* List of items to be packed. |
|
38
|
|
|
* |
|
39
|
|
|
* @var ItemList |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $items; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* List of box sizes available to pack items into. |
|
45
|
|
|
* |
|
46
|
|
|
* @var BoxList |
|
47
|
|
|
*/ |
|
48
|
|
|
protected $boxes; |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Quantities available of each box type. |
|
52
|
|
|
* |
|
53
|
|
|
* @var array<int, int> |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $boxesQtyAvailable = []; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Constructor. |
|
59
|
|
|
*/ |
|
60
|
42 |
|
public function __construct() |
|
61
|
|
|
{ |
|
62
|
42 |
|
$this->items = new ItemList(); |
|
63
|
42 |
|
$this->boxes = new BoxList(); |
|
64
|
|
|
|
|
65
|
42 |
|
$this->logger = new NullLogger(); |
|
66
|
42 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Add item to be packed. |
|
70
|
|
|
*/ |
|
71
|
40 |
|
public function addItem(Item $item, int $qty = 1): void |
|
72
|
|
|
{ |
|
73
|
40 |
|
for ($i = 0; $i < $qty; ++$i) { |
|
74
|
40 |
|
$this->items->insert($item); |
|
75
|
|
|
} |
|
76
|
40 |
|
$this->logger->log(LogLevel::INFO, "added {$qty} x {$item->getDescription()}", ['item' => $item]); |
|
77
|
40 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Set a list of items all at once. |
|
81
|
|
|
* @param iterable<Item>|ItemList $items |
|
82
|
|
|
*/ |
|
83
|
6 |
|
public function setItems(iterable $items): void |
|
84
|
|
|
{ |
|
85
|
6 |
|
if ($items instanceof ItemList) { |
|
86
|
|
|
$this->items = clone $items; |
|
87
|
|
|
} else { |
|
88
|
6 |
|
$this->items = new ItemList(); |
|
89
|
6 |
|
foreach ($items as $item) { |
|
90
|
6 |
|
$this->items->insert($item); |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
6 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Add box size. |
|
97
|
|
|
*/ |
|
98
|
38 |
|
public function addBox(Box $box): void |
|
99
|
|
|
{ |
|
100
|
38 |
|
$this->boxes->insert($box); |
|
101
|
38 |
|
$this->setBoxQuantity($box, $box instanceof LimitedSupplyBox ? $box->getQuantityAvailable() : PHP_INT_MAX); |
|
102
|
38 |
|
$this->logger->log(LogLevel::INFO, "added box {$box->getReference()}", ['box' => $box]); |
|
103
|
38 |
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Add a pre-prepared set of boxes all at once. |
|
107
|
|
|
*/ |
|
108
|
6 |
|
public function setBoxes(BoxList $boxList): void |
|
109
|
|
|
{ |
|
110
|
6 |
|
$this->boxes = $boxList; |
|
111
|
6 |
|
foreach ($this->boxes as $box) { |
|
112
|
6 |
|
$this->setBoxQuantity($box, $box instanceof LimitedSupplyBox ? $box->getQuantityAvailable() : PHP_INT_MAX); |
|
113
|
|
|
} |
|
114
|
6 |
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Set the quantity of this box type available. |
|
118
|
|
|
*/ |
|
119
|
38 |
|
public function setBoxQuantity(Box $box, int $qty): void |
|
120
|
|
|
{ |
|
121
|
38 |
|
$this->boxesQtyAvailable[spl_object_id($box)] = $qty; |
|
122
|
38 |
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Number of boxes at which balancing weight is deemed not worth the extra computation time. |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function getMaxBoxesToBalanceWeight(): int |
|
128
|
|
|
{ |
|
129
|
2 |
|
return $this->maxBoxesToBalanceWeight; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Number of boxes at which balancing weight is deemed not worth the extra computation time. |
|
134
|
|
|
*/ |
|
135
|
4 |
|
public function setMaxBoxesToBalanceWeight(int $maxBoxesToBalanceWeight): void |
|
136
|
|
|
{ |
|
137
|
4 |
|
$this->maxBoxesToBalanceWeight = $maxBoxesToBalanceWeight; |
|
138
|
4 |
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Pack items into boxes. |
|
142
|
|
|
*/ |
|
143
|
40 |
|
public function pack(): PackedBoxList |
|
144
|
|
|
{ |
|
145
|
40 |
|
$this->sanityPrecheck(); |
|
146
|
36 |
|
$packedBoxes = $this->doVolumePacking(); |
|
147
|
|
|
|
|
148
|
|
|
//If we have multiple boxes, try and optimise/even-out weight distribution |
|
149
|
34 |
|
if ($packedBoxes->count() > 1 && $packedBoxes->count() <= $this->maxBoxesToBalanceWeight) { |
|
150
|
14 |
|
$redistributor = new WeightRedistributor($this->boxes, $this->boxesQtyAvailable); |
|
151
|
14 |
|
$redistributor->setLogger($this->logger); |
|
152
|
14 |
|
$packedBoxes = $redistributor->redistributeWeight($packedBoxes); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
34 |
|
$this->logger->log(LogLevel::INFO, "[PACKING COMPLETED], {$packedBoxes->count()} boxes"); |
|
156
|
|
|
|
|
157
|
34 |
|
return $packedBoxes; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Pack items into boxes using the principle of largest volume item first. |
|
162
|
|
|
* |
|
163
|
|
|
* @throws NoBoxesAvailableException |
|
164
|
|
|
*/ |
|
165
|
36 |
|
public function doVolumePacking(bool $singlePassMode = false, bool $enforceSingleBox = false): PackedBoxList |
|
166
|
|
|
{ |
|
167
|
36 |
|
$packedBoxes = new PackedBoxList(); |
|
168
|
|
|
|
|
169
|
|
|
//Keep going until everything packed |
|
170
|
36 |
|
while ($this->items->count()) { |
|
171
|
36 |
|
$packedBoxesIteration = []; |
|
172
|
|
|
|
|
173
|
|
|
//Loop through boxes starting with smallest, see what happens |
|
174
|
36 |
|
foreach ($this->getBoxList($enforceSingleBox) as $box) { |
|
175
|
36 |
|
$volumePacker = new VolumePacker($box, $this->items); |
|
176
|
36 |
|
$volumePacker->setLogger($this->logger); |
|
177
|
36 |
|
$volumePacker->setSinglePassMode($singlePassMode); |
|
178
|
36 |
|
$packedBox = $volumePacker->pack(); |
|
179
|
36 |
|
if ($packedBox->getItems()->count()) { |
|
180
|
36 |
|
$packedBoxesIteration[] = $packedBox; |
|
181
|
|
|
|
|
182
|
|
|
//Have we found a single box that contains everything? |
|
183
|
36 |
|
if ($packedBox->getItems()->count() === $this->items->count()) { |
|
184
|
34 |
|
break; |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
try { |
|
190
|
|
|
//Find best box of iteration, and remove packed items from unpacked list |
|
191
|
36 |
|
$bestBox = $this->findBestBoxFromIteration($packedBoxesIteration); |
|
192
|
4 |
|
} catch (NoBoxesAvailableException $e) { |
|
193
|
4 |
|
if ($enforceSingleBox) { |
|
194
|
2 |
|
return new PackedBoxList(); |
|
195
|
|
|
} |
|
196
|
2 |
|
throw $e; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
36 |
|
$this->items->removePackedItems($bestBox->getItems()); |
|
200
|
|
|
|
|
201
|
36 |
|
$packedBoxes->insert($bestBox); |
|
202
|
36 |
|
--$this->boxesQtyAvailable[spl_object_id($bestBox->getBox())]; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
34 |
|
return $packedBoxes; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Get a "smart" ordering of the boxes to try packing items into. The initial BoxList is already sorted in order |
|
210
|
|
|
* so that the smallest boxes are evaluated first, but this means that time is spent on boxes that cannot possibly |
|
211
|
|
|
* hold the entire set of items due to volume limitations. These should be evaluated first. |
|
212
|
|
|
* |
|
213
|
|
|
* @return iterable<Box> |
|
214
|
|
|
*/ |
|
215
|
36 |
|
protected function getBoxList(bool $enforceSingleBox = false): iterable |
|
216
|
|
|
{ |
|
217
|
36 |
|
$itemVolume = 0; |
|
218
|
36 |
|
foreach ($this->items as $item) { |
|
219
|
36 |
|
$itemVolume += $item->getWidth() * $item->getLength() * $item->getDepth(); |
|
220
|
|
|
} |
|
221
|
|
|
|
|
222
|
36 |
|
$preferredBoxes = []; |
|
223
|
36 |
|
$otherBoxes = []; |
|
224
|
36 |
|
foreach ($this->boxes as $box) { |
|
225
|
36 |
|
if ($this->boxesQtyAvailable[spl_object_id($box)] > 0) { |
|
226
|
36 |
|
if ($box->getInnerWidth() * $box->getInnerLength() * $box->getInnerDepth() >= $itemVolume) { |
|
227
|
34 |
|
$preferredBoxes[] = $box; |
|
228
|
12 |
|
} elseif (!$enforceSingleBox) { |
|
229
|
12 |
|
$otherBoxes[] = $box; |
|
230
|
|
|
} |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
36 |
|
return array_merge($preferredBoxes, $otherBoxes); |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
/** |
|
238
|
|
|
* @param PackedBox[] $packedBoxes |
|
239
|
|
|
*/ |
|
240
|
36 |
|
protected function findBestBoxFromIteration(array $packedBoxes): PackedBox |
|
241
|
|
|
{ |
|
242
|
36 |
|
if (count($packedBoxes) === 0) { |
|
243
|
4 |
|
throw new NoBoxesAvailableException("No boxes could be found for item '{$this->items->top()->getDescription()}'", $this->items->top()); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
36 |
|
usort($packedBoxes, [$this, 'compare']); |
|
247
|
|
|
|
|
248
|
36 |
|
return $packedBoxes[0]; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
40 |
|
private function sanityPrecheck(): void |
|
252
|
|
|
{ |
|
253
|
|
|
/** @var Item $item */ |
|
254
|
40 |
|
foreach ($this->items as $item) { |
|
255
|
40 |
|
$possibleFits = 0; |
|
256
|
|
|
|
|
257
|
|
|
/** @var Box $box */ |
|
258
|
40 |
|
foreach ($this->boxes as $box) { |
|
259
|
38 |
|
if ($item->getWeight() <= ($box->getMaxWeight() - $box->getEmptyWeight())) { |
|
260
|
38 |
|
$possibleFits += count((new OrientatedItemFactory($box))->getPossibleOrientationsInEmptyBox($item)); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
40 |
|
if ($possibleFits === 0) { |
|
265
|
6 |
|
throw new ItemTooLargeException("Item '{$item->getDescription()}' is too large to fit into any box", $item); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
36 |
|
} |
|
269
|
|
|
|
|
270
|
6 |
|
private static function compare(PackedBox $boxA, PackedBox $boxB): int |
|
271
|
|
|
{ |
|
272
|
6 |
|
$choice = $boxB->getItems()->count() <=> $boxA->getItems()->count(); |
|
273
|
|
|
|
|
274
|
6 |
|
if ($choice === 0) { |
|
275
|
6 |
|
$choice = $boxB->getVolumeUtilisation() <=> $boxA->getVolumeUtilisation(); |
|
276
|
|
|
} |
|
277
|
6 |
|
if ($choice === 0) { |
|
278
|
6 |
|
$choice = $boxB->getUsedVolume() <=> $boxA->getUsedVolume(); |
|
279
|
|
|
} |
|
280
|
|
|
|
|
281
|
6 |
|
return $choice; |
|
282
|
|
|
} |
|
283
|
|
|
} |
|
284
|
|
|
|