public function redistributeWeight(PackedBoxList $originalBoxes): PackedBoxList
    {
        $targetWeight = $originalBoxes->getMeanWeight();
        $this->logger->log(LogLevel::DEBUG, "repacking for weight distribution, weight variance {$originalBoxes->getWeightVariance()}, target weight {$targetWeight}");

        /** @var PackedBox[] $boxes */
        $boxes = iterator_to_array($originalBoxes);

        usort($boxes, function (PackedBox $boxA, PackedBox $boxB) {
            return $boxB->getWeight() <=> $boxA->getWeight();
        });

        do {
            $iterationSuccessful = false;

            foreach ($boxes as $a => &$boxA) {
                foreach ($boxes as $b => &$boxB) {
                    if ($b <= $a || $boxA->getWeight() === $boxB->getWeight()) {
                        continue; //no need to evaluate
                    }

                    $iterationSuccessful = $this->equaliseWeight($boxA, $boxB, $targetWeight);
                    if ($iterationSuccessful) {
                        $boxes = array_filter($boxes, function (?PackedBox $box) { //remove any now-empty boxes from the list
                            return $box instanceof PackedBox;
                        });
                        break 2;
                    }
                }
            }
        } while ($iterationSuccessful);

        //Combine back into a single list
        $packedBoxes = new PackedBoxList();
        $packedBoxes->insertFromArray($boxes);

        return $packedBoxes;
    }
public function redistributeWeight(PackedBoxList $originalBoxes): PackedBoxList
    {
        $targetWeight = $originalBoxes->getMeanWeight();
        $this->logger->log(LogLevel::DEBUG, "repacking for weight distribution, weight variance {$originalBoxes->getWeightVariance()}, target weight {$targetWeight}");

        /** @var PackedBox[] $boxes */
        $boxes = iterator_to_array($originalBoxes);

        usort($boxes, function (PackedBox $boxA, PackedBox $boxB) {
            return $boxB->getWeight() <=> $boxA->getWeight();
        });

        do {
            $iterationSuccessful = false;

            foreach ($boxes as $a => &$boxA) {
                foreach ($boxes as $b => &$boxB) {
                    if ($b <= $a || $boxA->getWeight() === $boxB->getWeight()) {
                        continue; //no need to evaluate
                    }

                    $iterationSuccessful = $this->equaliseWeight($boxA, $boxB, $targetWeight);
                    if ($iterationSuccessful) {
                        $boxes = array_filter($boxes, function (?PackedBox $box) { //remove any now-empty boxes from the list
                            return $box instanceof PackedBox;
                        });
                        break 2;
                    }
                }
            }
        } while ($iterationSuccessful);

        //Combine back into a single list
        $packedBoxes = new PackedBoxList();
        $packedBoxes->insertFromArray($boxes);

        return $packedBoxes;
    }
public function redistributeWeight ( PackedBoxList $ originalBoxes ) : PackedBoxList { $ targetWeight = $ originalBoxes -> getMeanWeight ( ) ; $ this -> logger -> log ( LogLevel :: DEBUG , "repacking for weight distribution, weight variance {$originalBoxes->getWeightVariance()}, target weight {$targetWeight}" ) ; /** @var PackedBox[] $boxes */ $ boxes = iterator_to_array ( $ originalBoxes ) ; usort ( $ boxes , function ( PackedBox $ boxA , PackedBox $ boxB ) { return $ boxB -> getWeight ( ) <=> $ boxA -> getWeight ( ) ; } ) ; do { $ iterationSuccessful = false ; foreach ( $ boxes as $ a => & $ boxA ) { foreach ( $ boxes as $ b => & $ boxB ) { if ( $ b <= $ a || $ boxA -> getWeight ( ) === $ boxB -> getWeight ( ) ) { continue ; //no need to evaluate } $ iterationSuccessful = $ this -> equaliseWeight ( $ boxA , $ boxB , $ targetWeight ) ; if ( $ iterationSuccessful ) { $ boxes = array_filter ( $ boxes , function ( ? PackedBox $ box ) { //remove any now-empty boxes from the list return $ box instanceof PackedBox ; } ) ; break 2 ; } } } } while ( $ iterationSuccessful ) ; //Combine back into a single list $ packedBoxes = new PackedBoxList ( ) ; $ packedBoxes -> insertFromArray ( $ boxes ) ; return $ packedBoxes ; }
Given a solution set of packed boxes, repack them to achieve optimum weight distribution.

@param PackedBoxList $originalBoxes

@return PackedBoxList
Given a solution set of packed boxes repack them to achieve optimum weight distribution .