如何更改脚本以使成本最小化,同时计算所选的体积和重量是可行的?

2024-05-19 09:47:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

我试图创建一个函数,每当我收到消息“Trucks not enough”时,它就会用下一个最大的truck替换最小的truck

更新:@kabdulla帮我得到了答案,但也指出它有一个很大的缺陷,那就是它只考虑了物品的整体体积,而没有考虑物品的几何结构来装进去;一个2×2×2的物品应该很容易装进卡车,但一个8×1×1的物品有不同的几何结构,有不同的约束条件。这里是我的更新代码(全部感谢@kabdulla)与项目和卡车的细节。你知道吗

from pulp import *
import numpy as np

# Item details
item_name = ["Mudguard1","Mudguard2","Mudguard3","Mudguard4",]
item_id = ["1001474185","1001474401","1001474182","1001474154"]
item_length = [14,22,16,16]
item_breadth = [12,24,14,12]
item_height = [9,26,12,17]
item_quantity = [14,8,10,1]
item_vol = [12.25,63.55,15.55,1.88]
item_mass= [160,528,83,5]
n_items = len(item_id)
set_items = range(n_items)

# Details of trucks
item_name = ["Tata-407-9.5","Tata-407-13","Tata-407-17","Tata-407-16",]
item_id = ["1","2","3","4"]
truck_length = [14,22,16,16]
truck_breadth = [12,24,14,12]
truck_height = [9,26,12,17]
truck_quantity = [14,8,10,1]
truck_vol = [12.25,63.55,15.55,1.88]
truck_mass= [160,528,83,5]
truck_cost = [7,1.5,18,100]


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]


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:
    for j in set_trucks:
        if x[i][j].value() == 1:
            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):
  print("Trucks are sufficient")
else:
  print("Items cannot fit")

这是我的输出:

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-


Tags: ofinforisitemsitem物品print
1条回答
网友
1楼 · 发布于 2024-05-19 09:47:45

以下代码的版本假定每辆卡车上都有一个总运载质量和总运载量限制。注:这并不保证适合卡车的项目组合(例如,8米x 1米x 1米的项目体积为8立方米,但不适合内部空间为2米x 2米x 2米=8立方米的卡车,但使用下面的体积约束不会违反约束)。你知道吗

from pulp import *
import numpy as np

# Item masses, volumes
item_mass = [16, 10, 5]
item_vol = [25, 12, 1]
n_items = len(item_vol)
set_items = range(n_items)

# Mass & volume capacities of trucks
truck_mass = [7, 15, 15, 15, 18, 38, 64, 100]
truck_vol = [25, 50, 50, 50, 100, 125, 250, 500]

# Cost of using each truck
truck_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,
    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 allocatoin 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]


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:
    for j in set_trucks:
        if x[i][j].value() == 1:
            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):
  print("Trucks are sufficient")
else:
  print("Items cannot fit")

应返回以下内容:

('Status:', 'Optimal')
('Total Cost is: ', 19.5)
x_soln
[[0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 1. 1.]
 [1. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]
 [0. 0. 0.]]
y_soln
[0. 0. 0. 1. 1. 0. 0. 0.]
Trucks used: 2.0
Item 0 is packed in vehicle 4
Item 1 is packed in vehicle 3
Item 2 is packed in vehicle 3
Volume of used trucks is 150.0
Trucks are sufficient

相关问题 更多 >

    热门问题