简体   繁体   English

带有多个袋子和仅重量的物品的背包

[英]Knapsack with multiple bags and items having only weight

I am trying to solve this problem and I wanted to know if there are known existing algorithms / solutions to solve this. 我正在尝试解决此问题,我想知道是否存在已知的算法/解决方案来解决此问题。

Problem: 问题:

I have n bags and n items (which are either equal or different weights) to fill into these bag. 我有n个袋子和n个物品(相等或不同的重量)可装入这些袋子。 Each of these bags have a certain weight limit and the n items needs to be put into these bags in such a way that I can use the maximum space in each of these bags. 这些袋子中的每个袋子都有一定的重量限制,因此需要将n个物品放入这些袋子中,这样我才能在每个袋子中使用最大的空间。

The bags are of equal size. 袋子大小相等。 Will also like to know how to solve with bags of unequal size too. 也将想知道如何解决尺寸不等的袋子。

Most of the solutions I read was trying to solve a 0/1 knapsack with a weight and value. 我读过的大多数解决方案都在尝试解决具有重量和价值的0/1背包。 Should I consider the weight and value as same? 我应该认为重量和价值相同吗? Am I on the right track? 我在正确的轨道上吗?

This is not a homework problem. 这不是作业问题。

This is known as the bin packing problem (which is NP-hard). 这被称为垃圾箱包装问题 (NP难题)。

By simply sorting the decreasing order by their sizes, and then inserting each item into the first bin in the list with sufficient remaining space, we get 11/9 OPT + 6/9 bins (where OPT is the number of bins used in the optimal solution). 通过简单地按其大小对降序进行排序,然后将每个项目插入具有足够剩余空间的列表中的第一个箱中,我们得到11/9 OPT + 6/9箱(其中OPT是最优中使用的箱数解)。 This would easily take O(n²) , or possibly O(n log n) with an efficient implementation. 通过有效的实现,这很容易获得O(n²)O(n log n)

In terms of optimal solutions, there isn't a dynamic programming solution that's as well-known as for the knapsack problem. 就最佳解决方案而言,没有一个背包问题众所周知的动态编程解决方案。 This resource has one option - the basic idea is: 该资源有一个选项-基本思想是:

 D[{set}] = the minimum number of bags using each of the items in {set} Then: D[{set1}] = the minimum of all D[{set1} - {set2}] where set2 fits into 1 bag and is a subset of set1 

The array index above is literally a set - think of this as a map of set to value, a bitmap or a multi-dimensional array where each index is either 1 or 0 to indicate whether we include the item corresponding to that dimensional or not. 上面的数组索引实际上是一个集合-可以将其视为集合到值的映射,位图或多维数组,其中每个索引为1或0以指示我们是否包括与该维度相对应的项。

The linked resource actually considers multiple types, which can occur multiple times - I derived the above solution from that. 链接的资源实际上考虑了多种类型,这些类型可以多次出现-我从中得出了上述解决方案。

The running time will greatly depend on the number of items that can fit into a bag - it will be O(minimumBagsUsed.2 maxItemsPerBag ) . 运行时间将在很大程度上取决于可放入袋子的物品数-它将是O(minimumBagsUsed.2 maxItemsPerBag )

In the case of 1 bag, this is essentially the subset sum problem . 在1袋的情况下,这本质上是子集和问题 For this, you can consider the weight the same as value and solve using a knapsack algorithm, but this won't really work too well for multiple bags. 为此,您可以将重量与值视为相同,并使用背包算法求解,但是对于多个袋子来说,这实际上效果不佳。

Why not? 为什么不? Consider items 5,5,5,9,9,9 with a bag size of 16 . 考虑袋大小为16物品5,5,5,9,9,9 If you just solve subset sum, you're left with 5,5,5 in one bag and 9 in one bag each (for a total of 4 bags), rather than 5,9 in each of 3 bags. 如果仅求解子集总和,则每个袋子剩下5,5,5 ,每个袋子剩下9个(总共4袋子),而不是3个袋子中的每个5,9

Subset sum / knapsack is already a difficult problem - if using it's not going to give you an optimal solution, you may as well use the sorting / greedy approach above. 子集总和/背包的问题已经很棘手-如果使用它不能为您提供最佳解决方案,那么您也可以使用上面的排序/贪婪方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM