繁体   English   中英

纸浆需要太多时间解决

[英]Pulp taking too much time to solve

我正在尝试使用纸浆求解器在卡车中包装物品,当物品数量少(即<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笔记本和测试文件的链接

默认情况下,您使用的是开源MIP求解器CBC。
2种可能的方法:

  1. 使用更好的求解器,例如CPLEX或GUROBI(商业软件,但对学生和学者免费)。 PuLP都有针对它们的API。
  2. 您需要最佳解决方案吗? 如果不是这种情况,请设置时间限制。

例:

prob.solve(pulp.COIN(maxSeconds=your_time_limit))

我怀疑您的问题过于对称。 即有多辆卡车是完全一样的。

当这种情况发生时,CBC可以花时间寻找“最佳”解决方案。

您在这里有2个选项:

  • 设置时间限制或界限,这将尽早退出求解过程,但仍返回“良好”的解决方案。

  • 设置次要成本函数,以减少事物的对称性,即,将编号最小的项目分配给编号最小的卡车。 这种对称性在古罗比等商业求解器中自动发生。

暂无
暂无

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

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