forked from dvdoug/BoxPacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackedBoxList.php
100 lines (82 loc) · 2.27 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
<?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;
/**
* Variance in weight between boxes
* @var float
*/
protected $weightVariance;
/**
* Compare elements in order to place them correctly in the heap while sifting up.
* @see \SplMinHeap::compare()
*/
public function compare($aBoxA, $aBoxB) {
$choice = $aBoxA->getItems()->count() - $aBoxB->getItems()->count();
if ($choice === 0) {
$choice = $aBoxB->getBox()->getInnerVolume() - $aBoxA->getBox()->getInnerVolume();
}
return $choice;
}
/**
* Reversed version of compare
* @return int
*/
public function reverseCompare($aBoxA, $aBoxB) {
$choice = $aBoxB->getItems()->count() - $aBoxA->getItems()->count();
if ($choice === 0) {
$choice = $aBoxA->getBox()->getInnerVolume() - $aBoxB->getBox()->getInnerVolume();
}
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() {
if (!is_null($this->weightVariance)) {
return $this->weightVariance;
}
$mean = $this->getMeanWeight();
foreach (clone $this as $box) {
$this->weightVariance += pow($box->getWeight() - $mean, 2);
}
return $this->weightVariance /= $this->count();
}
/**
* Do a bulk insert
* @param array $aBoxes
*/
public function insertFromArray(array $aBoxes) {
foreach ($aBoxes as $box) {
$this->insert($box);
}
}
}