我如何更改我的脚本,以便将成本降至最低,同时计算出所选的体积和重量是否可行?

我使用纸浆创建了一个函数,它接收一系列物品和卡车。每件物品都有容量,每辆卡车都有容量和成本。我使用纸浆求解器来计算所需的最少卡车数量,同时保持最低成本。约束因素是容量,优化因素是仓位成本。但是现在,我想添加另一个体积限制因素。可以做到吗?你能建议一种使用求解器本身来做到这一点的方法吗?我试图在不使用求解器的情况下完成那部分,但它根本没有帮助。非常感谢。

我试图创建一个函数,每当我收到消息“卡车不够用”时,它就会用下一个最大的卡车替换最小的卡车

更新:@kabdulla 帮助我得到了答案,但也指出它有一个很大的缺陷,它只考虑了项目的整体体积,而不是项目的几何形状以适应内部;一个 2 X 2 X 2 的物品应该很容易装进卡车,但是一个 8 X 1 X 1 的物品有不同的几何形状和不同的限制。这是我更新的代码(全部归功于 @kabdulla),其中包含物品和卡车的详细信息。

这是我的输出:


Status: Optimal

Total Cost is:  126.5

x_soln

[[1. 0. 0. 0.]

 [0. 1. 0. 0.]

 [0. 0. 1. 0.]

 [0. 0. 0. 1.]]

y_soln

[1. 1. 1. 1.]

Trucks used: 4.0

Item 0 is packed in vehicle 0

Item 1 is packed in vehicle 1

Item 2 is packed in vehicle 2

Item 3 is packed in vehicle 3

The volume of used trucks is 93.22999999999999

Trucks are sufficient

这是一个可以玩的 colab 链接。 https://colab.research.google.com/drive/1uKuS2F4Xd1Lbps8yAwivLbhzZrD_Hv0-


慕虎7371278
浏览 163回答 1
1回答

萧十郎

下面的代码版本假设每辆卡车都有总载重量和总载重量限制。注意:这不能保证适合卡车的物品组合(8m x 1m x 1m 的物品体积为 8 立方米,但不适合内部空间为 2m x 2m x 2m = 8 立方的卡车例如米,但使用低于此的体积约束不会违反约束)。from pulp import *import numpy as np# Item masses, volumesitem_mass = [16, 10, 5]item_vol = [25, 12, 1]n_items = len(item_vol)set_items = range(n_items)# Mass & volume capacities of truckstruck_mass = [7, 15, 15, 15, 18, 38, 64, 100]truck_vol = [25, 50, 50, 50, 100, 125, 250, 500]# Cost of using each trucktruck_cost = [7, 1.5, 1.5, 1.5, 18, 380, 640, 1000]n_trucks = len(truck_cost)set_trucks = range(n_trucks)y = pulp.LpVariable.dicts('truckUsed', set_trucks,&nbsp; &nbsp; lowBound=0, upBound=1, cat=LpInteger)x = pulp.LpVariable.dicts('itemInTruck', (set_items, set_trucks),&nbsp;&nbsp; &nbsp; lowBound=0, upBound=1, cat=LpInteger)# Model formulationprob = LpProblem("Truck allocatoin problem", LpMinimize)# Objectiveprob += lpSum([truck_cost[i] * y[i] for i in set_trucks])# Constraintsfor j in set_items:&nbsp; &nbsp; # Every item must be taken in one truck&nbsp; &nbsp; prob += lpSum([x[j][i] for i in set_trucks]) == 1for i in set_trucks:&nbsp; &nbsp; # Respect the mass constraint of trucks&nbsp; &nbsp; prob += lpSum([item_mass[j] * x[j][i] for j in set_items]) <= truck_mass[i]*y[i]&nbsp; &nbsp; # Respect the volume constraint of trucks&nbsp; &nbsp; 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:&nbsp; &nbsp; for i in set_trucks:&nbsp; &nbsp; &nbsp; &nbsp; x[j][i] <= y[i]prob.solve()x_soln = np.array([[x[i][j].varValue for i in set_items] for j in set_trucks])y_soln = np.array([y[i].varValue for i in set_trucks])print (("Status:"), LpStatus[prob.status])print ("Total Cost is: ", value(prob.objective))print("x_soln"); print(x_soln)print("y_soln"); print(y_soln)print("Trucks used: " + str(sum(([y_soln[i] for i in set_trucks]))))for i in set_items:&nbsp; &nbsp; for j in set_trucks:&nbsp; &nbsp; &nbsp; &nbsp; if x[i][j].value() == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Item " + str(i) + " is packed in vehicle "+ str(j))totalitemvol = sum(item_vol)totaltruckvol = sum([y[i].value() * truck_vol[i] for i in set_trucks])print("Volume of used trucks is " + str(totaltruckvol))if(totaltruckvol >= totalitemvol):&nbsp; print("Trucks are sufficient")else:&nbsp; print("Items cannot fit")应该返回以下内容:('Status:', 'Optimal')('Total Cost is: ', 19.5)x_soln[[0. 0. 0.]&nbsp;[0. 0. 0.]&nbsp;[0. 0. 0.]&nbsp;[0. 1. 1.]&nbsp;[1. 0. 0.]&nbsp;[0. 0. 0.]&nbsp;[0. 0. 0.]&nbsp;[0. 0. 0.]]y_soln[0. 0. 0. 1. 1. 0. 0. 0.]Trucks used: 2.0Item 0 is packed in vehicle 4Item 1 is packed in vehicle 3Item 2 is packed in vehicle 3Volume of used trucks is 150.0Trucks are sufficient
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python