计算出货箱尺寸的粗略估计

Wil*_*Wil 5 php knapsack-problem shipping

我正在努力寻找计算运输所需箱子尺寸的最佳方法.

我有3个不同尺寸的集装箱.我在数据库中定义了产品的宽度,长度,深度和质量.

我想知道如何找到最小数量的箱子,以及购物车中物品数量的最小尺寸.

我当前的'想法'是找到整个产品阵列的最大宽度,根据它选择一个框,然后根据需要拆分订单...这似乎不起作用.

我的盒子尺寸为: - 8 x 6 x 6 = 228立方英寸 - 10 x 8 x 8 = 640立方英寸 - 12.5 x 12.5 x 12.5 = 1953.125立方英寸

产品定义如下:

 [Product] => Array
                (
                    [STOCK_CODE] => 010003
                    [Product_Slug] => GABA_010003
                    [ItemName] => GABA
                    [WHOLESALE_PRICE] => 17.47
                    [RETAIL_PRICE] => 24.95
                    [Brand] => 
                    [ProductLine] => 
                    [image_name] => 705077000440
                    [MASS] => 0.313
                    [Height] => 4.625
                    [Width] => 2.375
                    [Depth] => 2.375
                    [cubic_inches] => 26.087890625
                )
Run Code Online (Sandbox Code Playgroud)

我已经研究过背包问题,包装问题等,但找不到办法.任何帮助都会很棒.

function shipping(){

        $this->CartProduct->unbindModel(
            array('belongsTo' => array('User'))
        );

        //find all cart products by current logged in user
        $cartItems = $this->CartProduct->find('all', array('conditions' => array('CartProduct.user_id' => $this->Auth->user('id'))));

        $i = 0;

        //get the max width, height, depth
        $maxHeight = 0;
        $maxWidth = 0;
        $maxDepth = 0;
        foreach($cartItems as $c){
            $cartItems[$i]['Product']['cubic_inches'] = $c['Product']['Height'] * $c['Product']['Width'] * $c['Product']['Depth'];
            $cartItems[$i]['CartProduct']['total_cubic_inches'] = ($c['Product']['Height'] * $c['Product']['Width'] * $c['Product']['Depth']) * $c['CartProduct']['qty'];

            if($c['Product']['Height'] > $maxHeight)
            {
                $maxHeight = $c['Product']['Height'];
            }

            if($c['Product']['Width'] > $maxWidth)
            {
                $maxWidth = $c['Product']['Width'];
            }
            if($c['Product']['Depth'] > $maxDepth)
            {
                $maxDepth = $c['Product']['Depth'];
            }
            $i++;
        }

        //possible containers 
        //8 x 6 x 6 = 228 ci
        //10 x 8 x 8 = 640 ci
        //12.5 x 12.5 x 12.5 = 1953.125

        $possibleContainers = array(
            1 => array(
                'Height' => 8,
                'Width' => 6,
                'Depth' => 6,
                'Cubic' => 228),
            2 => array(
                'Height' => 10,
                'Width' => 8,
                'Depth' => 8,
                'Cubic' => 640),
            3 => array(
                'Height' => 12.5,
                'Width' => 12.5,
                'Depth' => 12.5,
                'Cubic' => 1953.125)
        );



        $max = array(
            'Height' => $maxHeight, 
            'Width' => $maxWidth, 
            'Depth' => $maxDepth, 
        );

        pr($cartItems);
        pr($possibleContainers);
        die();  
    }
Run Code Online (Sandbox Code Playgroud)

tho*_*edb 2

至于获得最佳答案,那就是 NP-Hard... http://en.wikipedia.org/wiki/Bin_packing_problem

维基百科上显示的贪婪算法虽然可能相当遥远,但实际上可能适合您的情况。

然而,作为估计,您可以将物品的体积相加,然后应用低效率系数,然后使用尽可能小的盒子。

或者,您可以将物品按照体积递减的方式进行排序,然后查看当前的一组盒子可以放入多少物品,当您无法放入物品时创建一个新盒子。但不确定如何处理不同的盒子尺寸。您还可能会遇到这样的情况:它更改框大小而不是创建新框。

值得深思。

  • 我认为这个包是一个启动解决方案 https://github.com/dvdoug/BoxPacker 仅供将来参考,如果世界上其他人来这里寻求可能的解决方案:D (2认同)