遍历和组合产品的最佳方法 [英] best way to loop through and combine products

查看:53
本文介绍了遍历和组合产品的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找您对实现此目标的最佳方式的个人想法,因为我可以看到很多达到同一目的的方式.

I'm looking for your personal thoughts on the best way to do this as there a number of ways that I can see to get to the same end.

我的店面有多个小册子,可以任意组合订购.

I have a store front that has multiple different booklets that can be ordered in any combination.

每个小册子项目在数据库中具有L xW xH和重量.它们实际上都是L x W一样的,只是厚度和重量在变化.

Each booklet item has L x W x H and weight in the database. They're all really the same L x W, it's the thickness and weight that varies.

我还设置了最大盒子尺寸.

I also have a set max box size.

我知道我可以在最大的盒子中容纳10.5的高度.

I know I can fit a height of 10.5 in the largest box size.

但是我可以合并这些书以进行运输.因此,我想尽可能地填充盒子,然后重复直到我只有部分盒子为止.

I can combine the books however for shipping. So I want to fill the box to the max I can, then repeat until I only have a partial box.

我需要的最终结果是每个盒子及其重量的数组.

The final result I need is an array of each of the boxes and their weight.

示例:

订购的商品

  1. 第1本书的100张,厚度为.05,重量为.15
  2. 第二本书200的厚度为.07,重量为.23

高度

  1. 100 * .05 = 5
  2. 200 * .07 = 14

重量

  1. 100 * .15 = 15
  2. 200 * .23 = 46

第1栏包含

  1. 第二本书150个
  2. 高度:150 * .07 = 10.5
  3. 重量:150 * .23 = 34.5

第2栏包含

  1. 第二本书50本书和第一本书100本书
  2. 高度:(100 * .05)+(50 * .07)= 8.5
  3. 重量:(100 * .15)+(50 * .25)= 27.5

您将如何遍历信息以获取输出?从一个产品开始,计算高度,将其分成整箱,计算出这些箱的重量,然后将每个整箱添加到阵列中,获取部分箱的高度,然后移至下一个产品?还是...... ??

How would you go about looping through the information to get the output? Start with one product, figure the height, divide it out to full boxes, calculate the weight of those boxes and add each full box to the array, take the height of the partial box and move on to the next product? Or......??

静态编码将是容易的",但是我正在尝试对其进行动态编码,这样我每次添加新产品时都不必回来.

Static coding would be "easy", but I'm trying to code this dynamically so that I don't have to come back in every time I add a new product.

-编辑-

这是我当前的循环,但这只是合计数量并分成几个框.当前产品的尺寸/形状都相同,因此效果很好.

Here is my current loop, but this is only totaling the quantity and dividing into boxes. The current products are all the same size / shape, so this worked fine then.

    if($zip_code != '')
    {
        my $item_list = enc_sql_select_multi("SELECT * FROM `$PREF{shopping_carts_table}` WHERE `cart_id` = '$cart_id' AND `sold` = '0' AND `deleted_from_cart` = '0'");
        my $item_qty = '0';
        foreach my $k (sort { $a <=> $b } keys %$item_list)
        {
            $item_qty =~ s/,//g;
            my $item_id = $$item_list{$k}{id};
            my $item_qty_new = $$item_list{$k}{option1value};
            $item_qty_new =~ s/,//g;
            $item_qty = $item_qty + $item_qty_new;
        }
        if ($item_qty != '0')
            {
                $item_qty =~ s/,//g;
                if ( $item_qty == 25 )
                {
                    my $tempCount = $carton_specs{25}{boxNo};
                    $tempCount++;
                    $carton_specs{25}{boxNo} = $tempCount;
                }
                elsif ( $item_qty == 50 )
                {
                    my $tempCount = $carton_specs{50}{boxNo};
                    $tempCount++;
                    $carton_specs{50}{boxNo} = $tempCount;
                }
                elsif ( $item_qty == 100 )
                {
                    my $tempCount = $carton_specs{100}{boxNo};
                    $tempCount++;
                    $carton_specs{100}{boxNo} = $tempCount;
                }
                elsif ($item_qty > 100 && $item_qty < 5000)
                {
                    my $fullBoxCt = int($item_qty / 200);
                    my $tempCountFull = $carton_specs{200}{boxNo};
                    $tempCountFull = $tempCountFull + $fullBoxCt;
                    $carton_specs{200}{boxNo} = $tempCountFull;
                    my $partBoxQty = $item_qty - (200 * $fullBoxCt);
                    if ($partBoxQty != 0)
                    {
                        my $tempCountPart = $carton_specs{$partBoxQty}{boxNo};
                        $tempCountPart++;
                        $carton_specs{$partBoxQty}{boxNo} = $tempCountPart;
                    }
                }
                else
                {
                    @shipDetails =
                    (
                        {
                            Code => 1000,
                            Price => '0.00'
                        },
                        {
                            Code => 1500,
                            Price => '0.00'
                        }
                    );
                    return (@shipDetails);
                }
            }

推荐答案

这是一个关于装箱问题的经典例子,尽管我也不是数学家,但很多人这个可观的想法.所以:

There is no getting away from this being a classic example of the bin-packing problem and while I am no mathematician either, a lot of people who are have given this considerable thought. So:

没有已知的最优解决方案,可以通过多项式时间来计算,并且看来您输入的大小可能会很大.因此,您应该采用以下启发式之一:

There is no known optimal solution to this problem that can be computed in polynomial time and it would appear the size of your inputs can be quite large. Therefore, you should adopt one of the following heuristics:

  1. First Fit算法:逐个处理小册子,将其放在具有足够容纳小册子能力的第一个盒子中.如果没有此类框,则必须启动一个新框并将其添加到可用框列表中.
  2. 最佳拟合算法:逐一处理小册子,将其放入最适合的书本盒(剩余容量最小).如果没有可容纳小册子的盒子,则必须启动一个新盒子并将其添加到可用盒子列表中.
  3. First Fit减少算法:首先通过降低高度对小册子进行排序,然后执行First Fit算法.在您的示例中,您将打包"Book 2"首先是小册子.
  4. 最佳适合度降低算法:首先通过降低高度对小册子进行排序,然后执行最佳适合度算法.
  1. First Fit Algorithm: Process the booklets one by one placing it in the first box that has enough capacity to hold the booklet. If there are no such boxes, a new box must be started and added to the list of boxes available.
  2. Best Fit Algorithm: Process the booklets one by one placing it in the box where it fits the tightest (has the smallest capacity left). If there are no boxes that can hold the booklet, a new box must be started and added to the list of boxes available.
  3. First Fit Decreasing Algorithm: First order the booklets by decreasing height and then perform the First Fit Algorithm. In your example you would be packing up the "Book 2" booklets first.
  4. Best Fit Decreasing Algorithm: First order the booklets by decreasing height and then perform the Best Fit Algorithm.

但是,当然,您的订单包含N种特定类型小册子的项目.我们将以每次一次"的处方服用.在上述算法中松散地因此,例如,如果您正在执行第一拟合算法",那么当您找到第一个有能力容纳小册子的盒子时,就很容易计算出该盒子可以容纳的最大小册子数M.一次最多处理 min(M,N_LEFT)本小册子,其中 N_LEFT 是您仍然需要打包的那种大小的小册子数.对于其他三种算法中的任何一种也是如此.

But, of course, your orders consist of N items of of a certain type of booklet. We will take the prescription "one at a time" in the above algorithms loosely. So if you are doing, for example, the First Fit Algorithm, then when you find the first box that has the capacity to hold the booklet, it is easy enough to calculate the maximum number of those booklets, M, that box could hold and process up to min(M, N_LEFT) booklets at one time where N_LEFT is the number of booklets of that size you still have left to pack up. Likewise for any of the other three algorithms.

您可以调查其他上面只是示例4的其他装箱启发式方法.我不能说一个案例在任何情况下都必须优于其他案例.我认为对它们中的任何一个进行合理的实现都可以满足您的目的.无论如何,这是我的看法.

You can survey other bin-packing heuristics of which the above are just a sample four. I can't say one is necessarily superior to the others in all cases. I think any reasonable implementation of any of them will be satisfactory for your purposes. Anyway, this is my take.

这篇关于遍历和组合产品的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆