我正在尝试使用纸浆解算器在卡车中打包物品,当物品数量少(即<25)时,它可以很好地工作,但是当我将数量增加到30-32时,它永远需要解决。
这是纸浆求解器的代码:
def allocator(item_mass, item_vol, truck_mass, truck_vol, truck_cost, id_series):
n_items = len(item_vol)
set_items = range(n_items)
n_trucks = len(truck_cost)
set_trucks = range(n_trucks)
y = pulp.LpVariable.dicts('truckUsed', set_trucks,
lowBound=0, upBound=1, cat=LpInteger)
x = pulp.LpVariable.dicts('itemInTruck', (set_items, set_trucks),
lowBound=0, upBound=1, cat=LpInteger)
# Model formulation
prob = LpProblem("Truck allocation problem", LpMinimize)
# Objective
prob += lpSum([truck_cost[i] * y[i] for i in set_trucks])
# Constraints
for j in set_items:
# Every item must be taken in one truck
prob += lpSum([x[j][i] for i in set_trucks]) == 1
for i in set_trucks:
# Respect the mass constraint of trucks
prob += lpSum([item_mass[j] * x[j][i] for j in set_items]) <= truck_mass[i]*y[i]
# Respect the volume constraint of trucks
prob += lpSum([item_vol[j] * x[j][i] for j in set_items]) <= truck_vol[i]*y[i]
# Ensure y variables have to be set to make use of x variables:
for j in set_items:
for i in set_trucks:
x[j][i] <= y[i]
s = id_series # id_series
prob.solve()
我在做错什么吗?
这是jupyter笔记本和测试文件的link。
答案 0 :(得分:0)
默认情况下,您使用的是开源MIP求解器CBC。
2种可能的方法:
示例:
prob.solve(pulp.COIN(maxSeconds=your_time_limit))
答案 1 :(得分:0)
我怀疑您的问题过于对称。即,有多辆卡车完全一样。
当这种情况发生时,CBC可以花时间寻找“最佳”解决方案。
您在这里有2个选择:
设置时间限制或界限,这将尽早退出求解过程,但仍返回“良好”解决方案。
设置次要成本函数以使事物不那么对称,即,希望将编号最小的项目分配给编号最小的卡车。这种对称性会在古罗比等商业求解器中自动发生。