Python PuLP性能问题-需要太多时间来解决

时间:2019-05-06 07:16:05

标签: python linear-programming pulp

我正在使用纸浆创建分配器功能,该功能根据重量和体积将卡车中的物品包装起来。对于10-15个项目来说,它可以正常工作(耗时10-15秒),但是当我将其翻倍时,要花半个多小时才能解决。

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)

    print("working1")

    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)
    print("working2")

    # Model formulation
    prob = LpProblem("Truck allocation problem", LpMinimize)

    # Objective
    prob += lpSum([truck_cost[i] * y[i] for i in set_trucks])
    print("working3")
    # 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]
    print("working4")
    # 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]
    print("working5")

    s = id_series          #id_series

    prob.solve()

    print("working6")

这是我正在运行的数据

项目:

   Name  Pid  Quantity  Length  Width  Height  Volume  Weight     t_type 
0     A    1         1    4.60   4.30     4.3   85.05    1500       Open   
1     B    2         1    4.60   4.30     4.3   85.05    1500       Open   
2     C    3         1    6.00   5.60     9.0  302.40   10000  Container   
3     D    4         1    8.75   5.60     6.6  441.00    1000       Open   
4     E    5         1    6.00   5.16     6.6  204.33    3800       Open   
5     C    6         1    6.00   5.60     9.0  302.40   10000        All   
6     C    7         1    6.00   5.60     9.0  302.40   10000  Container   
7     D    8         1    8.75   5.60     6.6  441.00    6000       Open   
8     E    9         1    6.00   5.16     6.6  204.33    3800       Open   
9     C   10         1    6.00   5.60     9.0  302.40   10000        All   
.... times 5

卡车(仅排在前5行,我总共有54种卡车):

  Category       Name  TruckID  Length(ft)  Breadth(ft)  Height(ft)   Volume  \
0      LCV  Tempo 407        0         9.5          5.5         5.5  287.375   
1      LCV  Tempo 407        1         9.5          5.5         5.5  287.375   
2      LCV  Tempo 407        2         9.5          5.5         5.5  287.375   
3      LCV    13 Feet        3        13.0          5.5         7.0  500.500   
4      LCV    14 Feet        4        14.0          6.0         6.0  504.000   

   Weight  Price  
0    1500      1  
1    2000      1  
2    2500      2  
3    3500      3  
4    4000      3  

其中的ItemId是:

data["ItemId"] = data.index + 1
id_series = data["ItemId"].tolist()

1 个答案:

答案 0 :(得分:0)

PuLP可以处理多个求解器。看看您拥有什么:

pulp.pulpTestAll()

这将给出如下列表:

Solver pulp.solvers.PULP_CBC_CMD unavailable.
Solver pulp.solvers.CPLEX_DLL unavailable.
Solver pulp.solvers.CPLEX_CMD unavailable.
Solver pulp.solvers.CPLEX_PY unavailable.
    Testing zero subtraction
    Testing continuous LP solution
    Testing maximize continuous LP solution
    ...
* Solver pulp.solvers.COIN_CMD passed.
Solver pulp.solvers.COINMP_DLL unavailable.
    Testing zero subtraction
    Testing continuous LP solution
    Testing maximize continuous LP solution
    ...
* Solver pulp.solvers.GLPK_CMD passed.
Solver pulp.solvers.XPRESS unavailable.
Solver pulp.solvers.GUROBI unavailable.
Solver pulp.solvers.GUROBI_CMD unavailable.
Solver pulp.solvers.PYGLPK unavailable.
Solver pulp.solvers.YAPOSIB unavailable.

然后您可以使用来解决问题,例如:

lp_prob.solve(pulp.COIN_CMD())

Gurobi和CPLEX是商业解决方案,它们通常工作得很好。也许您可以访问它们?古罗比(Gurobi)具有良好的学历。

或者,您可能希望根据质量限制来寻找一种近似的解决方案。