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 Psr\Log\LoggerAwareInterface; |
12
|
|
|
use Psr\Log\LoggerInterface; |
13
|
|
|
use Psr\Log\LogLevel; |
14
|
|
|
use Psr\Log\NullLogger; |
15
|
|
|
use WeakMap; |
16
|
|
|
|
17
|
|
|
use function array_filter; |
18
|
|
|
use function array_map; |
19
|
|
|
use function array_merge; |
20
|
|
|
use function array_sum; |
21
|
|
|
use function assert; |
22
|
|
|
use function count; |
23
|
|
|
use function iterator_to_array; |
24
|
|
|
use function usort; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Actual packer. |
28
|
|
|
* @internal |
29
|
|
|
*/ |
30
|
|
|
class WeightRedistributor implements LoggerAwareInterface |
31
|
|
|
{ |
32
|
|
|
private LoggerInterface $logger; |
33
|
|
|
|
34
|
|
|
private BoxList $boxes; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var WeakMap<Box, int> |
38
|
|
|
*/ |
39
|
|
|
private WeakMap $boxQuantitiesAvailable; |
40
|
|
|
|
41
|
|
|
private PackedBoxSorter $packedBoxSorter; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param WeakMap<Box, int> $boxQuantitiesAvailable |
45
|
|
|
*/ |
46
|
13 |
|
public function __construct(BoxList $boxList, PackedBoxSorter $packedBoxSorter, WeakMap $boxQuantitiesAvailable) |
47
|
|
|
{ |
48
|
13 |
|
$this->boxes = $boxList; |
49
|
13 |
|
$this->packedBoxSorter = $packedBoxSorter; |
50
|
13 |
|
$this->boxQuantitiesAvailable = $boxQuantitiesAvailable; |
51
|
13 |
|
$this->logger = new NullLogger(); |
52
|
|
|
} |
53
|
|
|
|
54
|
13 |
|
public function setLogger(LoggerInterface $logger): void |
55
|
|
|
{ |
56
|
13 |
|
$this->logger = $logger; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Given a solution set of packed boxes, repack them to achieve optimum weight distribution. |
61
|
|
|
*/ |
62
|
13 |
|
public function redistributeWeight(PackedBoxList $originalBoxes): PackedBoxList |
63
|
|
|
{ |
64
|
13 |
|
$targetWeight = $originalBoxes->getMeanItemWeight(); |
65
|
13 |
|
$this->logger->log(LogLevel::DEBUG, "repacking for weight distribution, weight variance {$originalBoxes->getWeightVariance()}, target weight {$targetWeight}"); |
66
|
|
|
|
67
|
13 |
|
$boxes = iterator_to_array($originalBoxes); |
68
|
|
|
|
69
|
13 |
|
usort($boxes, static fn (PackedBox $boxA, PackedBox $boxB) => $boxB->getWeight() <=> $boxA->getWeight()); |
70
|
|
|
|
71
|
|
|
do { |
72
|
13 |
|
$iterationSuccessful = false; |
73
|
|
|
|
74
|
13 |
|
foreach ($boxes as $a => &$boxA) { |
75
|
13 |
|
foreach ($boxes as $b => &$boxB) { |
76
|
13 |
|
if ($b <= $a || $boxA->getWeight() === $boxB->getWeight()) { |
77
|
13 |
|
continue; // no need to evaluate |
78
|
|
|
} |
79
|
|
|
|
80
|
9 |
|
$iterationSuccessful = $this->equaliseWeight($boxA, $boxB, $targetWeight); |
81
|
9 |
|
if ($iterationSuccessful) { |
82
|
5 |
|
$boxes = array_filter($boxes, static function (?PackedBox $box) { // remove any now-empty boxes from the list |
83
|
5 |
|
return $box instanceof PackedBox; |
84
|
|
|
}); |
85
|
5 |
|
break 2; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
13 |
|
} while ($iterationSuccessful); |
90
|
|
|
|
91
|
|
|
// Combine back into a single list |
92
|
13 |
|
$packedBoxes = new PackedBoxList($this->packedBoxSorter); |
93
|
13 |
|
$packedBoxes->insertFromArray($boxes); |
94
|
|
|
|
95
|
13 |
|
return $packedBoxes; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Attempt to equalise weight distribution between 2 boxes. |
100
|
|
|
* |
101
|
|
|
* @return bool was the weight rebalanced? |
102
|
|
|
*/ |
103
|
9 |
|
private function equaliseWeight(PackedBox &$boxA, PackedBox &$boxB, float $targetWeight): bool |
104
|
|
|
{ |
105
|
9 |
|
$anyIterationSuccessful = false; |
106
|
|
|
|
107
|
9 |
|
if ($boxA->getWeight() > $boxB->getWeight()) { |
108
|
9 |
|
$overWeightBox = $boxA; |
109
|
9 |
|
$underWeightBox = $boxB; |
110
|
|
|
} else { |
111
|
|
|
$overWeightBox = $boxB; |
112
|
|
|
$underWeightBox = $boxA; |
113
|
|
|
} |
114
|
|
|
|
115
|
9 |
|
$overWeightBoxItems = $overWeightBox->getItems()->asItemArray(); |
116
|
9 |
|
$underWeightBoxItems = $underWeightBox->getItems()->asItemArray(); |
117
|
|
|
|
118
|
9 |
|
foreach ($overWeightBoxItems as $key => $overWeightItem) { |
119
|
9 |
|
if (!self::wouldRepackActuallyHelp($overWeightBoxItems, $overWeightItem, $underWeightBoxItems, $targetWeight)) { |
120
|
8 |
|
continue; // moving this item would harm more than help |
121
|
|
|
} |
122
|
|
|
|
123
|
6 |
|
$newLighterBoxes = $this->doVolumeRepack(array_merge($underWeightBoxItems, [$overWeightItem]), $underWeightBox->getBox()); |
124
|
6 |
|
if ($newLighterBoxes->count() !== 1) { |
125
|
2 |
|
continue; // only want to move this item if it still fits in a single box |
126
|
|
|
} |
127
|
|
|
|
128
|
5 |
|
$underWeightBoxItems[] = $overWeightItem; |
129
|
|
|
|
130
|
5 |
|
if (count($overWeightBoxItems) === 1) { // sometimes a repack can be efficient enough to eliminate a box |
131
|
|
|
$boxB = $newLighterBoxes->top(); |
132
|
|
|
$boxA = null; |
133
|
|
|
--$this->boxQuantitiesAvailable[$underWeightBox->getBox()]; |
134
|
|
|
++$this->boxQuantitiesAvailable[$overWeightBox->getBox()]; |
135
|
|
|
|
136
|
|
|
return true; |
137
|
|
|
} |
138
|
|
|
|
139
|
5 |
|
unset($overWeightBoxItems[$key]); |
140
|
5 |
|
$newHeavierBoxes = $this->doVolumeRepack($overWeightBoxItems, $overWeightBox->getBox()); |
141
|
5 |
|
if (count($newHeavierBoxes) !== 1) { |
142
|
|
|
assert(true, 'Could not pack n-1 items into box, even though n were previously in it'); |
143
|
|
|
continue; |
144
|
|
|
} |
145
|
|
|
|
146
|
5 |
|
++$this->boxQuantitiesAvailable[$overWeightBox->getBox()]; |
147
|
5 |
|
++$this->boxQuantitiesAvailable[$underWeightBox->getBox()]; |
148
|
5 |
|
--$this->boxQuantitiesAvailable[$newHeavierBoxes->top()->getBox()]; |
149
|
5 |
|
--$this->boxQuantitiesAvailable[$newLighterBoxes->top()->getBox()]; |
150
|
5 |
|
$underWeightBox = $boxB = $newLighterBoxes->top(); |
151
|
5 |
|
$overWeightBox = $boxA = $newHeavierBoxes->top(); |
152
|
|
|
|
153
|
5 |
|
$anyIterationSuccessful = true; |
154
|
|
|
} |
155
|
|
|
|
156
|
9 |
|
return $anyIterationSuccessful; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Do a volume repack of a set of items. |
161
|
|
|
* @param iterable<Item> $items |
162
|
|
|
*/ |
163
|
6 |
|
private function doVolumeRepack(iterable $items, Box $currentBox): PackedBoxList |
164
|
|
|
{ |
165
|
6 |
|
$packer = new Packer(); |
166
|
6 |
|
$packer->throwOnUnpackableItem(false); |
167
|
6 |
|
$packer->setBoxes($this->boxes); // use the full set of boxes to allow smaller/larger for full efficiency |
168
|
6 |
|
foreach ($this->boxes as $box) { |
169
|
6 |
|
$packer->setBoxQuantity($box, $this->boxQuantitiesAvailable[$box]); |
170
|
|
|
} |
171
|
6 |
|
$packer->setBoxQuantity($currentBox, $this->boxQuantitiesAvailable[$currentBox] + 1); |
172
|
6 |
|
$packer->setItems($items); |
173
|
|
|
|
174
|
6 |
|
return $packer->doBasicPacking(true); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Not every attempted repack is actually helpful - sometimes moving an item between two otherwise identical |
179
|
|
|
* boxes, or sometimes the box used for the now lighter set of items actually weighs more when empty causing |
180
|
|
|
* an increase in total weight. |
181
|
|
|
* @param array<Item> $overWeightBoxItems |
182
|
|
|
* @param array<Item> $underWeightBoxItems |
183
|
|
|
*/ |
184
|
9 |
|
private static function wouldRepackActuallyHelp(array $overWeightBoxItems, Item $overWeightItem, array $underWeightBoxItems, float $targetWeight): bool |
185
|
|
|
{ |
186
|
9 |
|
$overWeightItemsWeight = array_sum(array_map(static fn (Item $item) => $item->getWeight(), $overWeightBoxItems)); |
187
|
9 |
|
$underWeightItemsWeight = array_sum(array_map(static fn (Item $item) => $item->getWeight(), $underWeightBoxItems)); |
188
|
|
|
|
189
|
9 |
|
if ($overWeightItem->getWeight() + $underWeightItemsWeight > $targetWeight) { |
190
|
8 |
|
return false; |
191
|
|
|
} |
192
|
|
|
|
193
|
6 |
|
$oldVariance = self::calculateVariance($overWeightItemsWeight, $underWeightItemsWeight); |
|
|
|
|
194
|
6 |
|
$newVariance = self::calculateVariance($overWeightItemsWeight - $overWeightItem->getWeight(), $underWeightItemsWeight + $overWeightItem->getWeight()); |
195
|
|
|
|
196
|
6 |
|
return $newVariance < $oldVariance; |
197
|
|
|
} |
198
|
|
|
|
199
|
6 |
|
private static function calculateVariance(int $boxAWeight, int $boxBWeight): float |
200
|
|
|
{ |
201
|
6 |
|
return ($boxAWeight - (($boxAWeight + $boxBWeight) / 2)) ** 2; // don't need to calculate B and ÷ 2, for a 2-item population the difference from mean is the same for each box |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|