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