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