繁体   English   中英

Python PuLP 性能问题 - 解决时间太长

[英]Python PuLP performance issue - taking too much time to solve

我正在使用纸浆创建一个分配器函数,该函数根据重量和体积将物品打包到卡车中。 对于 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()

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 拥有良好的学术执照。

或者,您可能希望查看近似解决方案,具体取决于您的质量限制。

暂无
暂无

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

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