有界背包问题的设置。想要:所有可能的包装清单
我不想优化任何东西,而是想列出所有可能的——包括“不完整的”——背包的包装。当然,我可以循环遍历对象集的所有子集,并选择满足权重约束的子集(可以通过在要查看的子集的大小上设置上限来改进),但我真的想要更多高效的。
谢谢。
Rather than optimize anything, I want to list all the possible - including "incomplete" - packings of the knapsack. Of course, I could loop through all subsets of the set of objects and pick the ones satisfying the weight constraint (can be improved by placing an upper bound on the size of the subsets to look through), but I'd really like something more efficient.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先按重量对物品进行分类。然后递归打包背包。如果尚未考虑的最小重量物体不适合,或者我们没有剩余物体,则将当前背包添加到我们的列表中并返回,否则将当前背包添加到我们的列表中,对于列表中适合的每个物体将其放入并尝试将背包的其余部分装入比我们最后打包的物体更重的物体。
如果我们可以打包多个给定类型的项目,则将小于替换为小于或等于。
如果我们有多个相同重量的物体,我们需要首先按重量排序,然后按任意顺序排序,然后使用它。
First sort your objects by weight. Then recursively pack the knapsack. If the smallest weight object not yet considered does not fit, or we have no objects remaining, then add the current knapsack to our list and return, else add the current knapsack to our list for each object in the list that fits put it in and try to pack the rest of the knapsack with objects heavier than the last object we packed.
If we can pack more than one item of a given type then replace less than with less than or equal to.
If we have multiple objects of the same weight we need to sort first by weight, then by some arbitrary order, and use that.