您要描述的具有可变装箱尺寸的装箱问题的变体至少是np-hard。
我不知道软件包openopt,项目网站似乎已关闭。Openopt似乎使用GLPK作为混合整数程序来解决该问题。您没有直接访问模型公式的权限,因为BPP()是抽象的。您可能需要修改openopt软件包以为各个容器添加约束。
通常,添加可变仓位大小作为约束很容易,如果需要扩展此公式,则需要将索引i添加到容量V,以便每个仓位具有不同的容量。
我建议查看一些维护的库来建模和解决此问题:有库PuLP,CyLP和SCIP。我认为,后者并非免费用于商业用途。
由于装箱是一个非常普遍的问题,所以我找到了PuLP库的示例。我认为它默认使用CoinOR解算器,您也可以使用其他商业解决方案。
easy_install pulp
在安装PuLP之后(可以使用easy_install实现该功能),可以扩展此示例。我根据您的问题修改了示例:
from pulp import *items = [("a", 5), ("b", 6), ("c", 7)]itemCount = len(items)maxBins = 3binCapacity = [11, 15, 10]binCost = [10, 30, 20]y = pulp.LpVariable.dicts('BinUsed', range(maxBins), lowBound = 0, upBound = 1, cat = pulp.LpInteger)possible_ItemInBin = [(itemTuple[0], binNum) for itemTuple in items for binNum in range(maxBins)]x = pulp.LpVariable.dicts('itemInBin', possible_ItemInBin, lowBound = 0, upBound = 1, cat = pulp.LpInteger)# Model formulationprob = LpProblem("Bin Packing Problem", LpMinimize)# Objectiveprob += lpSum([binCost[i] * y[i] for i in range(maxBins)])# Constraintsfor j in items: prob += lpSum([x[(j[0], i)] for i in range(maxBins)]) == 1for i in range(maxBins): prob += lpSum([items[j][1] * x[(items[j][0], i)] for j in range(itemCount)]) <= binCapacity[i]*y[i]prob.solve()print("Bins used: " + str(sum(([y[i].value() for i in range(maxBins)]))))for i in x.keys(): if x[i].value() == 1: print("Item {} is packed in bin {}.".format(*i))此实现的强大优势在于,您可以完全控制模型的制定,并且在openopt的情况下,不受BPP()等抽象层的限制。



