在Prestashop中计算托架模块的多种产品的盒子尺寸

Jul*_*don 1 algorithm prestashop

我目前正在为Kuroneko开发运输模块,这是一家日本上门送货公司。该载体以及重量考虑了箱子的尺寸。但是它不使用音量,这太容易了。它使用三个维度(高度,重量和深度)的总和。

如果只装一种产品,这仍然很容易,但是当您需要装2种产品时会发生什么呢?假设您有尺寸为(x1,y1,z1)和(x2,y2,z2)的两个乘积,如何以使X + Y + Z最小的方式来计算最终盒子尺寸X,Y,Z?

这是我的临时解决方案,但请告诉我是否有更好的计算方法:令m1为第一个乘积的最小尺寸,min(x1,y1,z1)和m2为第二个乘积。您必须考虑包装盒中产品的旋转,以使其以最佳方式适应,从而为这两个产品定义新的尺寸nx,ny,nz。假设nx = m。如果m = x,则ny = y,nz = z,否则,如果m = y,则ny = x,nz = z,否则,如果m = z,ny = y,nz = x。因此,盒子的总大小变为2 * nx,max(ny1,ny2),max(nz1,nz2)。

但据我所知,我认为这种方法不适用于2种以上的产品。任何的想法?

Mac*_*408 7

我一直在寻找这个算法,但链接已关闭,但是我可以使用回程机找到它。

无论如何,我会将其发布在这里,因为它可能对其他人有用

<?php

$test = '1,2,3|4,2,1|0.1,0.9,0.01';

$dimensions = explode("|", $test);

//1. Find total volume
$volume = 0;
//2. Find WHD ranges
$widthRange     = array();
$heightRange    = array();
$depthRange     = array();
foreach($dimensions as $dimension) {
    list($width, $height, $depth) = explode(',', $dimension);

    $volume += $width * $height * $depth;

    $widthRange[] = $width;

    $heightRange[] = $height;

    $depthRange[] = $depth;
}

//3. Order the WHD ranges
sort($widthRange);
sort($heightRange);
sort($depthRange);

echo 'Volume: '.$volume.'<br />';
echo 'Width Range: '.implode(', ', $widthRange).'<br />';
echo 'Height Range: '.implode(', ', $heightRange).'<br />';
echo 'Depth Range: '.implode(', ', $depthRange).'<br />';

//4. Figure out every combination with WHD
$widthCombination   = array();
$heightCombination  = array();
$depthCombination   = array();

function combination($list) {
    $combination = array();
    $total = pow(2, count($list)); 
    for ($i = 0; $i < $total; $i++) {   
        $set = array();
        //For each combination check if each bit is set  
        for ($j = 0; $j < $total; $j++) {  
           //Is bit $j set in $i?  
            if (pow(2, $j) & $i) $set[] = $list[$j];       
        }  

        if(empty($set) || in_array(array_sum($set), $combination)) {
            continue;
        }

        $combination[] = array_sum($set);
    }

    sort($combination);

    return $combination;
}

$widthCombination = combination($widthRange);
$heightCombination = combination($heightRange);
$depthCombination = combination($depthRange);

echo 'Width Combination: '.implode(', ', $widthCombination).'<br />';
echo 'Height Combination: '.implode(', ', $heightCombination).'<br />';
echo 'Depth Combination: '.implode(', ', $depthCombination).'<br />';

$stacks = array();
foreach($widthCombination as $width) {
    foreach($heightCombination as $height) {
        foreach($depthCombination as $depth) {
            $v = $width*$height*$depth;
            if($v >= $volume) {
                $stacks[$v][$width+$height+$depth] = array($width, $height, $depth);
            }
        }
    }
}

ksort($stacks);

foreach($stacks as $i => $dims) {
    ksort($stacks[$i]);
    foreach($stacks[$i] as $j => $stack) {
        rsort($stack);
        break;
    }

    break;
}

echo '<pre>'.print_r($stacks, true).'</pre>';
Run Code Online (Sandbox Code Playgroud)

所有功劳都属于克里斯蒂安·布兰克拉


小智 5

  • 在此处回答演示。
  • 您可以在此处检查代码(我添加了画布以进行可视化)

逻辑:

  1. 找到总体积(w * h * d)[+(w * h * d)..]

  2. 收集所有可能的宽度高度和深度值,从最低到最高排序

  3. 查找宽度,高度,宽度的所有可能的总和排列

    3a。示例:宽度范围1,2,3的总和将为1、2、3、4、5、6

    3b。我们需要这样做,因为根据示例(3a),width的最终值不可能为1.5。

  4. 根据在(3.)上计算出的排列,找到宽度,高度和深度的所有可能组合。

  5. 存储总体积等于或大于(1.)中的总体积的所有组合

    5a。这是因为最终音量不可能小于实际音量(1.)

    5b。对于大于(1.)的体积,则表示死空间。

  6. 从(5.)升序排序所有组合,第一个结果将是最准确的音量
  7. 最准确的体积可能仍可能具有不同的尺寸

    7a。示例:第16卷可以是2x2x4或4x4x1或2x1x8或16x1x1

    7b。找出每个W + H + D的总和,最小的总和就是更精确的尺寸。

    7c。(7a。)中的示例2 + 2 + 4 = 8,4 + 4 + 1 = 9,2 + 1 + 8 = 11,16 + 1 + 1 = 18 ....因此我们的脚本将选择2 x 2 x 4