forked from phux/BoxPacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Packer.php
175 lines (152 loc) · 4.77 KB
/
Packer.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* Box packing (3D bin packing, knapsack problem)
* @package BoxPacker
* @author Doug Wright
*/
namespace DVDoug\BoxPacker;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerAwareTrait;
use Psr\Log\LogLevel;
use Psr\Log\NullLogger;
/**
* Actual packer
* @author Doug Wright
* @package BoxPacker
*/
class Packer implements LoggerAwareInterface
{
use LoggerAwareTrait;
/**
* List of items to be packed
* @var ItemList
*/
protected $items;
/**
* List of box sizes available to pack items into
* @var BoxList
*/
protected $boxes;
/**
* Constructor
*/
public function __construct()
{
$this->items = new ItemList();
$this->boxes = new BoxList();
$this->logger = new NullLogger();
}
/**
* Add item to be packed
* @param Item $item
* @param int $qty
*/
public function addItem(Item $item, $qty = 1)
{
for ($i = 0; $i < $qty; $i++) {
$this->items->insert($item);
}
$this->logger->log(LogLevel::INFO, "added {$qty} x {$item->getDescription()}");
}
/**
* Set a list of items all at once
* @param \Traversable|array $items
*/
public function setItems($items)
{
if ($items instanceof ItemList) {
$this->items = clone $items;
} else {
$this->items = new ItemList();
foreach ($items as $item) {
$this->items->insert($item);
}
}
}
/**
* Add box size
* @param Box $box
*/
public function addBox(Box $box)
{
$this->boxes->insert($box);
$this->logger->log(LogLevel::INFO, "added box {$box->getReference()}");
}
/**
* Add a pre-prepared set of boxes all at once
* @param BoxList $boxList
*/
public function setBoxes(BoxList $boxList)
{
$this->boxes = clone $boxList;
}
/**
* Pack items into boxes
*
* @throws \RuntimeException
* @return PackedBoxList
*/
public function pack()
{
$packedBoxes = $this->doVolumePacking();
//If we have multiple boxes, try and optimise/even-out weight distribution
if ($packedBoxes->count() > 1) {
$redistributor = new WeightRedistributor($this->boxes);
$redistributor->setLogger($this->logger);
$packedBoxes = $redistributor->redistributeWeight($packedBoxes);
}
$this->logger->log(LogLevel::INFO, "packing completed, {$packedBoxes->count()} boxes");
return $packedBoxes;
}
/**
* Pack items into boxes using the principle of largest volume item first
*
* @throws \RuntimeException
* @return PackedBoxList
*/
public function doVolumePacking()
{
$packedBoxes = new PackedBoxList;
//Keep going until everything packed
while ($this->items->count()) {
$boxesToEvaluate = clone $this->boxes;
$packedBoxesIteration = new PackedBoxList;
//Loop through boxes starting with smallest, see what happens
while (!$boxesToEvaluate->isEmpty()) {
$box = $boxesToEvaluate->extract();
$volumePacker = new VolumePacker($box, clone $this->items);
$volumePacker->setLogger($this->logger);
$packedBox = $volumePacker->pack();
if ($packedBox->getItems()->count()) {
$packedBoxesIteration->insert($packedBox);
//Have we found a single box that contains everything?
if ($packedBox->getItems()->count() === $this->items->count()) {
break;
}
}
}
//Check iteration was productive
if ($packedBoxesIteration->isEmpty()) {
throw new \RuntimeException('Item ' . $this->items->top()->getDescription() . ' is too large to fit into any box');
}
//Find best box of iteration, and remove packed items from unpacked list
$bestBox = $packedBoxesIteration->top();
$unPackedItems = $this->items->asArray();
foreach (clone $bestBox->getItems() as $packedItem) {
foreach ($unPackedItems as $unpackedKey => $unpackedItem) {
if ($packedItem === $unpackedItem) {
unset($unPackedItems[$unpackedKey]);
break;
}
}
}
$unpackedItemList = new ItemList();
foreach ($unPackedItems as $unpackedItem) {
$unpackedItemList->insert($unpackedItem);
}
$this->items = $unpackedItemList;
$packedBoxes->insert($bestBox);
}
return $packedBoxes;
}
}