1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Box packing (3D bin packing, knapsack problem). |
4
|
|
|
* |
5
|
|
|
* @author Doug Wright |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace DVDoug\BoxPacker; |
9
|
|
|
|
10
|
|
|
use Psr\Log\LoggerAwareInterface; |
11
|
|
|
use Psr\Log\LoggerAwareTrait; |
12
|
|
|
use Psr\Log\LogLevel; |
13
|
|
|
use Psr\Log\NullLogger; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Actual packer. |
17
|
|
|
* |
18
|
|
|
* @author Doug Wright |
19
|
|
|
*/ |
20
|
|
|
class WeightRedistributor implements LoggerAwareInterface |
21
|
|
|
{ |
22
|
|
|
use LoggerAwareTrait; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* List of box sizes available to pack items into. |
26
|
|
|
* |
27
|
|
|
* @var BoxList |
28
|
|
|
*/ |
29
|
|
|
private $boxes; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Constructor. |
33
|
|
|
* |
34
|
|
|
* @param BoxList $boxList |
35
|
|
|
*/ |
36
|
2 |
|
public function __construct(BoxList $boxList) |
37
|
|
|
{ |
38
|
2 |
|
$this->boxes = clone $boxList; |
39
|
2 |
|
$this->logger = new NullLogger(); |
40
|
2 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Given a solution set of packed boxes, repack them to achieve optimum weight distribution. |
44
|
|
|
* |
45
|
|
|
* @param PackedBoxList $originalBoxes |
46
|
|
|
* |
47
|
|
|
* @return PackedBoxList |
48
|
|
|
*/ |
49
|
2 |
|
public function redistributeWeight(PackedBoxList $originalBoxes) |
50
|
|
|
{ |
51
|
2 |
|
$targetWeight = $originalBoxes->getMeanWeight(); |
52
|
2 |
|
$this->logger->log(LogLevel::DEBUG, "repacking for weight distribution, weight variance {$originalBoxes->getWeightVariance()}, target weight {$targetWeight}"); |
53
|
|
|
|
54
|
|
|
/** @var PackedBox[] $boxes */ |
55
|
2 |
|
$boxes = iterator_to_array($originalBoxes); |
56
|
|
|
|
57
|
2 |
|
usort($boxes, function (PackedBox $boxA, PackedBox $boxB) { |
58
|
2 |
|
return $boxB->getWeight() - $boxA->getWeight(); |
59
|
2 |
|
}); |
60
|
|
|
|
61
|
|
|
do { |
62
|
2 |
|
$iterationSuccessful = false; |
63
|
|
|
|
64
|
2 |
|
foreach ($boxes as $a => &$boxA) { |
65
|
2 |
|
foreach ($boxes as $b => &$boxB) { |
66
|
2 |
|
if ($b <= $a || $boxA->getWeight() === $boxB->getWeight()) { |
67
|
2 |
|
continue; //no need to evaluate |
68
|
|
|
} |
69
|
|
|
|
70
|
2 |
|
$iterationSuccessful = $this->equaliseWeight($boxA, $boxB, $targetWeight); |
71
|
2 |
|
if ($iterationSuccessful) { |
72
|
1 |
|
$boxes = array_filter($boxes, function ($box) { //remove any now-empty boxes from the list |
73
|
1 |
|
return $box instanceof PackedBox; |
74
|
1 |
|
}); |
75
|
2 |
|
break 2; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
} |
79
|
2 |
|
} while ($iterationSuccessful); |
80
|
|
|
|
81
|
|
|
//Combine back into a single list |
82
|
2 |
|
$packedBoxes = new PackedBoxList(); |
83
|
2 |
|
$packedBoxes->insertFromArray($boxes); |
84
|
|
|
|
85
|
2 |
|
return $packedBoxes; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Attempt to equalise weight distribution between 2 boxes. |
90
|
|
|
* |
91
|
|
|
* @param PackedBox $boxA |
92
|
|
|
* @param PackedBox $boxB |
93
|
|
|
* @param float $targetWeight |
94
|
|
|
* |
95
|
|
|
* @return bool was the weight rebalanced? |
96
|
|
|
*/ |
97
|
2 |
|
private function equaliseWeight(PackedBox &$boxA, PackedBox &$boxB, $targetWeight) |
98
|
|
|
{ |
99
|
2 |
|
$anyIterationSuccessful = false; |
100
|
|
|
|
101
|
2 |
|
if ($boxA->getWeight() > $boxB->getWeight()) { |
102
|
2 |
|
$overWeightBox = $boxA; |
103
|
2 |
|
$underWeightBox = $boxB; |
104
|
|
|
} else { |
105
|
|
|
$overWeightBox = $boxB; |
106
|
|
|
$underWeightBox = $boxA; |
107
|
|
|
} |
108
|
|
|
|
109
|
2 |
|
$overWeightBoxItems = $overWeightBox->getItems()->asArray(); |
110
|
2 |
|
$underWeightBoxItems = $underWeightBox->getItems()->asArray(); |
111
|
|
|
|
112
|
2 |
|
foreach ($overWeightBoxItems as $key => $overWeightItem) { |
113
|
2 |
|
if ($overWeightItem->getWeight() + $boxB->getWeight() > $targetWeight) { |
114
|
2 |
|
continue; // moving this item would harm more than help |
115
|
|
|
} |
116
|
|
|
|
117
|
1 |
|
$newLighterBoxes = $this->doVolumeRepack(array_merge($underWeightBoxItems, [$overWeightItem])); |
118
|
1 |
|
if (count($newLighterBoxes) !== 1) { |
119
|
|
|
continue; //only want to move this item if it still fits in a single box |
120
|
|
|
} |
121
|
|
|
|
122
|
1 |
|
$underWeightBoxItems[] = $overWeightItem; |
123
|
|
|
|
124
|
1 |
|
if (count($overWeightBoxItems) === 1) { //sometimes a repack can be efficient enough to eliminate a box |
125
|
|
|
$boxB = $newLighterBoxes->top(); |
126
|
|
|
$boxA = null; |
127
|
|
|
|
128
|
|
|
return true; |
129
|
|
|
} else { |
130
|
1 |
|
unset($overWeightBoxItems[$key]); |
131
|
1 |
|
$newHeavierBoxes = $this->doVolumeRepack($overWeightBoxItems); |
132
|
1 |
|
if (count($newHeavierBoxes) !== 1) { |
133
|
|
|
continue; |
134
|
|
|
} |
135
|
|
|
|
136
|
1 |
|
if ($this->didRepackActuallyHelp($boxA, $boxB, $newHeavierBoxes->top(), $newLighterBoxes->top())) { |
137
|
1 |
|
$boxB = $newLighterBoxes->top(); |
138
|
1 |
|
$boxA = $newHeavierBoxes->top(); |
139
|
1 |
|
$anyIterationSuccessful = true; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
2 |
|
return $anyIterationSuccessful; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Do a volume repack of a set of items. |
149
|
|
|
* |
150
|
|
|
* @param array $items |
151
|
|
|
* |
152
|
|
|
* @return PackedBoxList |
153
|
|
|
*/ |
154
|
1 |
|
private function doVolumeRepack($items) |
155
|
|
|
{ |
156
|
1 |
|
$packer = new Packer(); |
157
|
1 |
|
$packer->setBoxes($this->boxes); // use the full set of boxes to allow smaller/larger for full efficiency |
158
|
1 |
|
$packer->setItems($items); |
159
|
|
|
|
160
|
1 |
|
return $packer->doVolumePacking(); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Not every attempted repack is actually helpful - sometimes moving an item between two otherwise identical |
165
|
|
|
* boxes, or sometimes the box used for the now lighter set of items actually weighs more when empty causing |
166
|
|
|
* an increase in total weight. |
167
|
|
|
* |
168
|
|
|
* @param PackedBox $oldBoxA |
169
|
|
|
* @param PackedBox $oldBoxB |
170
|
|
|
* @param PackedBox $newBoxA |
171
|
|
|
* @param PackedBox $newBoxB |
172
|
|
|
* |
173
|
|
|
* @return bool |
174
|
|
|
*/ |
175
|
1 |
|
private function didRepackActuallyHelp(PackedBox $oldBoxA, PackedBox $oldBoxB, PackedBox $newBoxA, PackedBox $newBoxB) |
176
|
|
|
{ |
177
|
1 |
|
$oldList = new PackedBoxList(); |
178
|
1 |
|
$oldList->insertFromArray([$oldBoxA, $oldBoxB]); |
179
|
|
|
|
180
|
1 |
|
$newList = new PackedBoxList(); |
181
|
1 |
|
$newList->insertFromArray([$newBoxA, $newBoxB]); |
182
|
|
|
|
183
|
1 |
|
return $newList->getWeightVariance() < $oldList->getWeightVariance(); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|