forked from dvdoug/BoxPacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackedBoxList.php
123 lines (105 loc) · 2.89 KB
/
PackedBoxList.php
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
<?php
/**
* Box packing (3D bin packing, knapsack problem)
* @package BoxPacker
* @author Doug Wright
*/
namespace DVDoug\BoxPacker;
/**
* List of possible packed box choices, ordered by utilisation (item count, volume)
* @author Doug Wright
* @package BoxPacker
*/
class PackedBoxList extends \SplMinHeap
{
/**
* Average (mean) weight of boxes
* @var float
*/
protected $meanWeight;
/**
* Compare elements in order to place them correctly in the heap while sifting up.
* @see \SplMinHeap::compare()
*/
public function compare($boxA, $boxB)
{
$choice = $boxA->getItems()->count() - $boxB->getItems()->count();
if ($choice === 0) {
$choice = $boxB->getBox()->getInnerVolume() - $boxA->getBox()->getInnerVolume();
}
if ($choice === 0) {
$choice = $boxA->getWeight() - $boxB->getWeight();
}
return $choice;
}
/**
* Reversed version of compare
* @return int
*/
public function reverseCompare($boxA, $boxB)
{
$choice = $boxB->getItems()->count() - $boxA->getItems()->count();
if ($choice === 0) {
$choice = $boxA->getBox()->getInnerVolume() - $boxB->getBox()->getInnerVolume();
}
if ($choice === 0) {
$choice = $boxB->getWeight() - $boxA->getWeight();
}
return $choice;
}
/**
* Calculate the average (mean) weight of the boxes
* @return float
*/
public function getMeanWeight()
{
if (!is_null($this->meanWeight)) {
return $this->meanWeight;
}
foreach (clone $this as $box) {
$this->meanWeight += $box->getWeight();
}
return $this->meanWeight /= $this->count();
}
/**
* Calculate the variance in weight between these boxes
* @return float
*/
public function getWeightVariance()
{
$mean = $this->getMeanWeight();
$weightVariance = 0;
foreach (clone $this as $box) {
$weightVariance += pow($box->getWeight() - $mean, 2);
}
return $weightVariance / $this->count();
}
/**
* Get volume utilisation of the set of packed boxes
* @return float
*/
public function getVolumeUtilisation()
{
$itemVolume = 0;
$boxVolume = 0;
/** @var PackedBox $box */
foreach (clone $this as $box) {
$boxVolume += $box->getBox()->getInnerVolume();
/** @var Item $item */
foreach (clone $box->getItems() as $item) {
$itemVolume += $item->getVolume();
}
}
return round($itemVolume / $boxVolume * 100, 1);
}
/**
* Do a bulk insert
* @param array $boxes
*/
public function insertFromArray(array $boxes)
{
foreach ($boxes as $box) {
$this->insert($box);
}
}
}