Skip to content

Commit

Permalink
Backport WeightRedistributor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
dvdoug committed Jul 26, 2023
1 parent a243d15 commit 8e4c447
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
12 changes: 6 additions & 6 deletions src/Packer.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Packer implements LoggerAwareInterface
/**
* Quantities available of each box type.
*/
protected SplObjectStorage $boxesQtyAvailable;
protected SplObjectStorage $boxQuantitiesAvailable;

protected PackedBoxSorter $packedBoxSorter;

Expand All @@ -55,7 +55,7 @@ public function __construct()
{
$this->items = new ItemList();
$this->boxes = new BoxList();
$this->boxesQtyAvailable = new SplObjectStorage();
$this->boxQuantitiesAvailable = new SplObjectStorage();
$this->packedBoxSorter = new DefaultPackedBoxSorter();

$this->logger = new NullLogger();
Expand Down Expand Up @@ -112,7 +112,7 @@ public function setBoxes(BoxList $boxList): void
*/
public function setBoxQuantity(Box $box, int $qty): void
{
$this->boxesQtyAvailable[$box] = $qty;
$this->boxQuantitiesAvailable[$box] = $qty;
}

/**
Expand Down Expand Up @@ -150,7 +150,7 @@ public function pack(): PackedBoxList

// If we have multiple boxes, try and optimise/even-out weight distribution
if (!$this->beStrictAboutItemOrdering && $packedBoxes->count() > 1 && $packedBoxes->count() <= $this->maxBoxesToBalanceWeight) {
$redistributor = new WeightRedistributor($this->boxes, $this->packedBoxSorter, $this->boxesQtyAvailable);
$redistributor = new WeightRedistributor($this->boxes, $this->packedBoxSorter, $this->boxQuantitiesAvailable);
$redistributor->setLogger($this->logger);
$packedBoxes = $redistributor->redistributeWeight($packedBoxes);
}
Expand Down Expand Up @@ -203,7 +203,7 @@ public function doVolumePacking(bool $singlePassMode = false, bool $enforceSingl
$this->items->removePackedItems($bestBox->getItems());

$packedBoxes->insert($bestBox);
$this->boxesQtyAvailable[$bestBox->getBox()] = $this->boxesQtyAvailable[$bestBox->getBox()] - 1;
$this->boxQuantitiesAvailable[$bestBox->getBox()] = $this->boxQuantitiesAvailable[$bestBox->getBox()] - 1;
}

return $packedBoxes;
Expand All @@ -224,7 +224,7 @@ protected function getBoxList(bool $enforceSingleBox = false): iterable
$preferredBoxes = [];
$otherBoxes = [];
foreach ($this->boxes as $box) {
if ($this->boxesQtyAvailable[$box] > 0) {
if ($this->boxQuantitiesAvailable[$box] > 0) {
if ($box->getInnerWidth() * $box->getInnerLength() * $box->getInnerDepth() >= $itemVolume) {
$preferredBoxes[] = $box;
} elseif (!$enforceSingleBox) {
Expand Down
20 changes: 10 additions & 10 deletions src/WeightRedistributor.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ class WeightRedistributor implements LoggerAwareInterface
*
* @var SplObjectStorage|int[]
*/
private $boxesQtyAvailable;
private $boxQuantitiesAvailable;

private PackedBoxSorter $packedBoxSorter;

public function __construct(BoxList $boxList, PackedBoxSorter $packedBoxSorter, SplObjectStorage $boxQuantitiesAvailable)
{
$this->boxes = $boxList;
$this->packedBoxSorter = $packedBoxSorter;
$this->boxesQtyAvailable = $boxQuantitiesAvailable;
$this->boxQuantitiesAvailable = $boxQuantitiesAvailable;
$this->logger = new NullLogger();
}

Expand Down Expand Up @@ -128,8 +128,8 @@ private function equaliseWeight(PackedBox &$boxA, PackedBox &$boxB, float $targe
if (count($overWeightBoxItems) === 1) { // sometimes a repack can be efficient enough to eliminate a box
$boxB = $newLighterBoxes->top();
$boxA = null;
$this->boxesQtyAvailable[$underWeightBox->getBox()] = $this->boxesQtyAvailable[$underWeightBox->getBox()] - 1;
$this->boxesQtyAvailable[$overWeightBox->getBox()] = $this->boxesQtyAvailable[$overWeightBox->getBox()] + 1;
$this->boxQuantitiesAvailable[$underWeightBox->getBox()] = $this->boxQuantitiesAvailable[$underWeightBox->getBox()] - 1;
$this->boxQuantitiesAvailable[$overWeightBox->getBox()] = $this->boxQuantitiesAvailable[$overWeightBox->getBox()] + 1;

return true;
}
Expand All @@ -140,10 +140,10 @@ private function equaliseWeight(PackedBox &$boxA, PackedBox &$boxB, float $targe
continue; // this should never happen, if we can pack n+1 into the box, we should be able to pack n
}

$this->boxesQtyAvailable[$overWeightBox->getBox()] = $this->boxesQtyAvailable[$overWeightBox->getBox()] + 1;
$this->boxesQtyAvailable[$underWeightBox->getBox()] = $this->boxesQtyAvailable[$underWeightBox->getBox()] + 1;
$this->boxesQtyAvailable[$newHeavierBoxes->top()->getBox()] = $this->boxesQtyAvailable[$newHeavierBoxes->top()->getBox()] - 1;
$this->boxesQtyAvailable[$newLighterBoxes->top()->getBox()] = $this->boxesQtyAvailable[$newLighterBoxes->top()->getBox()] - 1;
$this->boxQuantitiesAvailable[$overWeightBox->getBox()] = $this->boxQuantitiesAvailable[$overWeightBox->getBox()] + 1;
$this->boxQuantitiesAvailable[$underWeightBox->getBox()] = $this->boxQuantitiesAvailable[$underWeightBox->getBox()] + 1;
$this->boxQuantitiesAvailable[$newHeavierBoxes->top()->getBox()] = $this->boxQuantitiesAvailable[$newHeavierBoxes->top()->getBox()] - 1;
$this->boxQuantitiesAvailable[$newLighterBoxes->top()->getBox()] = $this->boxQuantitiesAvailable[$newLighterBoxes->top()->getBox()] - 1;
$underWeightBox = $boxB = $newLighterBoxes->top();
$overWeightBox = $boxA = $newHeavierBoxes->top();

Expand All @@ -162,9 +162,9 @@ private function doVolumeRepack(iterable $items, Box $currentBox): PackedBoxList
$packer->setLogger($this->logger);
$packer->setBoxes($this->boxes); // use the full set of boxes to allow smaller/larger for full efficiency
foreach ($this->boxes as $box) {
$packer->setBoxQuantity($box, $this->boxesQtyAvailable[$box]);
$packer->setBoxQuantity($box, $this->boxQuantitiesAvailable[$box]);
}
$packer->setBoxQuantity($currentBox, $this->boxesQtyAvailable[$currentBox] + 1);
$packer->setBoxQuantity($currentBox, $this->boxQuantitiesAvailable[$currentBox] + 1);
$packer->setItems($items);

return $packer->doVolumePacking(true, true);
Expand Down

0 comments on commit 8e4c447

Please sign in to comment.