1 回答
TA贡献1815条经验 获得超13个赞
下面的代码版本假设每辆卡车都有总载重量和总载重量限制。注意:这不能保证适合卡车的物品组合(8m x 1m x 1m 的物品体积为 8 立方米,但不适合内部空间为 2m x 2m x 2m = 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
添加回答
举报