|
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 ArrayIterator; |
|
12
|
|
|
use Countable; |
|
13
|
|
|
use IteratorAggregate; |
|
14
|
|
|
use JsonSerializable; |
|
15
|
|
|
use Traversable; |
|
16
|
|
|
|
|
17
|
|
|
use function count; |
|
18
|
|
|
use function json_encode; |
|
19
|
|
|
use function reset; |
|
20
|
|
|
use function round; |
|
21
|
|
|
use function usort; |
|
22
|
|
|
use function array_map; |
|
23
|
|
|
use function iterator_to_array; |
|
24
|
|
|
use function spl_object_id; |
|
25
|
|
|
|
|
26
|
|
|
use const JSON_THROW_ON_ERROR; |
|
27
|
|
|
use const JSON_NUMERIC_CHECK; |
|
28
|
|
|
use const JSON_UNESCAPED_UNICODE; |
|
29
|
|
|
use const JSON_UNESCAPED_SLASHES; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* List of packed boxes. |
|
33
|
|
|
*/ |
|
34
|
|
|
class PackedBoxList implements IteratorAggregate, Countable, JsonSerializable |
|
35
|
|
|
{ |
|
36
|
56 |
|
/** |
|
37
|
|
|
* @var PackedBox[] |
|
38
|
56 |
|
*/ |
|
39
|
|
|
private array $list = []; |
|
40
|
|
|
|
|
41
|
|
|
private bool $isSorted = false; |
|
42
|
|
|
|
|
43
|
33 |
|
public function __construct(private readonly PackedBoxSorter $sorter = new DefaultPackedBoxSorter()) |
|
44
|
|
|
{ |
|
45
|
33 |
|
} |
|
46
|
33 |
|
|
|
47
|
33 |
|
/** |
|
48
|
|
|
* @return Traversable<PackedBox> |
|
49
|
|
|
*/ |
|
50
|
33 |
|
public function getIterator(): Traversable |
|
51
|
|
|
{ |
|
52
|
|
|
if (!$this->isSorted) { |
|
53
|
|
|
usort($this->list, $this->sorter->compare(...)); |
|
54
|
|
|
$this->isSorted = true; |
|
55
|
|
|
} |
|
56
|
44 |
|
|
|
57
|
|
|
return new ArrayIterator($this->list); |
|
58
|
44 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
52 |
|
* Number of items in list. |
|
62
|
|
|
*/ |
|
63
|
52 |
|
public function count(): int |
|
64
|
52 |
|
{ |
|
65
|
|
|
return count($this->list); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function insert(PackedBox $item): void |
|
69
|
|
|
{ |
|
70
|
|
|
$this->list[] = $item; |
|
71
|
|
|
$this->isSorted = false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
13 |
|
/** |
|
75
|
|
|
* Do a bulk insert. |
|
76
|
13 |
|
* |
|
77
|
13 |
|
* @internal |
|
78
|
|
|
* |
|
79
|
|
|
* @param PackedBox[] $boxes |
|
80
|
|
|
*/ |
|
81
|
|
|
public function insertFromArray(array $boxes): void |
|
82
|
|
|
{ |
|
83
|
|
|
foreach ($boxes as $box) { |
|
84
|
10 |
|
$this->insert($box); |
|
85
|
|
|
} |
|
86
|
10 |
|
} |
|
87
|
9 |
|
|
|
88
|
9 |
|
/** |
|
89
|
|
|
* @internal |
|
90
|
|
|
*/ |
|
91
|
10 |
|
public function top(): PackedBox |
|
92
|
|
|
{ |
|
93
|
|
|
if (!$this->isSorted) { |
|
94
|
|
|
usort($this->list, $this->sorter->compare(...)); |
|
95
|
|
|
$this->isSorted = true; |
|
96
|
|
|
} |
|
97
|
14 |
|
|
|
98
|
|
|
return reset($this->list); |
|
99
|
14 |
|
} |
|
100
|
|
|
|
|
101
|
14 |
|
/** |
|
102
|
14 |
|
* Calculate the average (mean) weight of the boxes. |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getMeanWeight(): float |
|
105
|
14 |
|
{ |
|
106
|
|
|
$meanWeight = 0; |
|
107
|
|
|
|
|
108
|
|
|
foreach ($this->list as $box) { |
|
109
|
|
|
$meanWeight += $box->getWeight(); |
|
110
|
|
|
} |
|
111
|
12 |
|
|
|
112
|
|
|
return $meanWeight / count($this->list); |
|
113
|
12 |
|
} |
|
114
|
|
|
|
|
115
|
12 |
|
/** |
|
116
|
12 |
|
* Calculate the average (mean) weight of the boxes. |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getMeanItemWeight(): float |
|
119
|
12 |
|
{ |
|
120
|
|
|
$meanWeight = 0; |
|
121
|
|
|
|
|
122
|
|
|
foreach ($this->list as $box) { |
|
123
|
|
|
$meanWeight += $box->getItemWeight(); |
|
124
|
|
|
} |
|
125
|
13 |
|
|
|
126
|
|
|
return $meanWeight / count($this->list); |
|
127
|
13 |
|
} |
|
128
|
|
|
|
|
129
|
13 |
|
/** |
|
130
|
13 |
|
* Calculate the variance in weight between these boxes. |
|
131
|
13 |
|
*/ |
|
132
|
|
|
public function getWeightVariance(): float |
|
133
|
|
|
{ |
|
134
|
13 |
|
$mean = $this->getMeanWeight(); |
|
135
|
|
|
|
|
136
|
|
|
$weightVariance = 0; |
|
137
|
|
|
foreach ($this->list as $box) { |
|
138
|
|
|
$weightVariance += ($box->getWeight() - $mean) ** 2; |
|
139
|
|
|
} |
|
140
|
1 |
|
|
|
141
|
|
|
return round($weightVariance / count($this->list), 1); |
|
142
|
1 |
|
} |
|
143
|
1 |
|
|
|
144
|
|
|
/** |
|
145
|
1 |
|
* Get volume utilisation of the set of packed boxes. |
|
146
|
1 |
|
*/ |
|
147
|
|
|
public function getVolumeUtilisation(): float |
|
148
|
1 |
|
{ |
|
149
|
1 |
|
$itemVolume = 0; |
|
150
|
|
|
$boxVolume = 0; |
|
151
|
|
|
|
|
152
|
|
|
foreach ($this as $box) { |
|
153
|
1 |
|
$boxVolume += $box->getInnerVolume(); |
|
154
|
|
|
|
|
155
|
|
|
foreach ($box->items as $item) { |
|
156
|
|
|
$itemVolume += ($item->item->getWidth() * $item->item->getLength() * $item->item->getDepth()); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
1 |
|
|
|
160
|
|
|
return round($itemVolume / $boxVolume * 100, 1); |
|
161
|
1 |
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
2 |
|
* Create a custom website visualiser URL for this packing. |
|
165
|
|
|
*/ |
|
166
|
2 |
|
public function generateVisualisationURL(): string |
|
167
|
|
|
{ |
|
168
|
|
|
$items = []; |
|
169
|
|
|
foreach ($this->list as $packedBox) { |
|
170
|
|
|
$items = [...$items, ...$packedBox->items->asItemArray()]; |
|
171
|
|
|
} |
|
172
|
|
|
$dedupedItems = $splIdToIntMap = []; |
|
173
|
|
|
$splIdIndex = 0; |
|
174
|
|
|
foreach ($items as $item) { |
|
175
|
|
|
if (!isset($splIdToIntMap[spl_object_id($item)])) { |
|
176
|
|
|
$splIdToIntMap[spl_object_id($item)] = $splIdIndex++; |
|
177
|
|
|
} |
|
178
|
|
|
$dedupedItems[$splIdToIntMap[spl_object_id($item)]] = $item; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
foreach ($dedupedItems as $item) { |
|
182
|
|
|
$data['items'][$splIdToIntMap[spl_object_id($item)]] = [$item->getDescription(), $item->getWidth(), $item->getLength(), $item->getDepth()]; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
$data['boxes'] = []; |
|
186
|
|
|
foreach ($this->list as $packedBox) { |
|
187
|
|
|
$data['boxes'][] = [ |
|
188
|
|
|
$packedBox->box->getReference(), |
|
189
|
|
|
$packedBox->box->getInnerWidth(), |
|
190
|
|
|
$packedBox->box->getInnerLength(), |
|
191
|
|
|
$packedBox->box->getInnerDepth(), |
|
192
|
|
|
array_map( |
|
193
|
|
|
fn (PackedItem $item) => [$splIdToIntMap[spl_object_id($item->item)], $item->x, $item->y, $item->z, $item->width, $item->length, $item->depth], |
|
194
|
|
|
iterator_to_array($packedBox->items) |
|
195
|
|
|
), |
|
196
|
|
|
]; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
return 'https://boxpacker.io/en/master/visualiser.html?packing=' . json_encode($data, flags: JSON_THROW_ON_ERROR | JSON_NUMERIC_CHECK | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
|
|
|
|
|
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
public function jsonSerialize(): array |
|
203
|
|
|
{ |
|
204
|
|
|
return $this->list; |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|