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