PuLP:我的约束有什么错?如何解决这个问题?



我多次尝试用PuLP制作最小化成本模型,但仍然存在错误。我想为下面的两个变量做决定。每种产品将生产多少单位?订单=多少数量。每件材料都要多订一些吗?

from pulp import *
product = ['S1', 'S2', 'S3', 'S4']
material = ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'G', 'H', 'J', 'K', 'L', 'M']
margin = {'S1': 25.68, 'S2': 25.68,
'S3': 25.68, 'S4': 25.68}
capacity = 300
inv_bf = 42753.63
product_cost = {'S1': 4.28, 'S2': 4.28,
'S3': 4.28, 'S4': 4.28}
usage = {'S1': {'A': 12.24,
'D': 12.24,
'E': 0.014,
'F': 0.095,
'G': 12.24,
'H': 0.589,
'J': 24.24,
'K': 0.005,
'L': 0.0105},
'S2': {'A': 12.24,
'D': 12.24,
'E': 0.014,
'F': 0.095,
'G': 12.24,
'H': 0.589,
'J': 24.24,
'K': 0.005,
'L': 0.0105},
'S3': {'H': 0.26,
'K': 0.014,
'B': 12.24,
'C': 12.24,
'I': 0.624,
'G': 12.18,
'J': 24.24,
'M': 0.005},
'S4': {'H': 0.26,
'K': 0.014,
'B': 12.24,
'C': 12.24,
'I': 0.624,
'G': 12.18,
'J': 24.24,
'M': 0.005}}
inv = {'A': 7645.8, 'B': 2470, 'C': 4526,
'D': 6678, 'J': 4180.92, 'G': 6879,
'E': 159.5, 'F': 717.4, 'I': 764.1,
'H': 1302.69, 'K': 248.79, 'L': 235,
'M': 179.4}
cost = {'A': 0.03, 'B': 0.03, 'C': 0.056,
'D': 0.151, 'J': 0.024, 'G': 0.88,
'E': 5.156, 'F': 13.04, 'I': 11.09,
'H': 6.833, 'K': 11.261, 'L': 10.118,
'M': 11.914}
# Define variables
produce = LpVariable.dicts("produce", product, lowBound=0, cat='Integer')
order = LpVariable.dicts("order", material, lowBound=0, cat='Integer')
# Define and initialize model
model = LpProblem("total_cost", LpMinimize)
# Objective Function
Total_ProCost = lpSum(product_cost[i]*produce[i] for i in product)
Total_MatCost = lpSum(order[m]*cost[m] for m in material)
Excess = lpSum(inv[m]+order[m]for m in material) - 
lpSum(produce[i]*usage[i][m]for m in material for i in product if m in usage[i].keys())
objective = Total_ProCost + Total_MatCost + lpSum(Excess*cost[m]for m in material)
model.setObjective(objective)
# Material Quantity used
for i in product:
model += lpSum(produce[i]*usage[i][m]for m in material for i in product if m in usage[i].keys()) 
<= lpSum(inv[m] + order[m] for m in material)
# Capacity limited
for i in product:
model += lpSum(produce[i]) <= capacity
# Profit
for i in product:
model += lpSum(produce[i]*margin[i]) >= Total_ProCost + Total_MatCost
# Reduce inv cost
for i in product:
model += lpSum(inv[m]+order[m] - produce[i]*usage[i][m] for m in material for i in product if m in usage[i].keys()) <= inv_bf
model.solve()
#Output
for v in model.variables():
print(v.name,'=',v.varValue)
produce = [v.varValue for v in model.variables()]

我发现结果显示0,订购所有材料,生产4个产品。这是不可能的,也是奇怪的。因为实际上,我们需要使用的材料在字典使用生产每一个产品。例如,当我们有足够的材料来生产所有项目时,我们可以生产S1。这意味着需要使用A、D、E、F、G、H、J、K和l

order_A = 0.0
order_B = 0.0
order_C = 0.0
order_D = 0.0
order_E = 0.0
order_F = 0.0
order_G = 0.0
order_H = 0.0
order_I = 0.0
order_J = 0.0
order_K = 0.0
order_L = 0.0
order_M = 0.0
produce_S1 = 97.11436
produce_S2 = 97.11436
produce_S3 = 97.11436
produce_S4 = 291.34308
Total cost =  2493.887783819344

请告知修复我的约束的方法或其他方法。提前谢谢。

你这里有几个问题…

  • 首先,它很可能是"生产"。从你现有的库存中,不需要订购更多

  • 在几个约束条件下,当您的for i in product然后对约束内的相同集合求和时,您将创建冗余约束

  • 如果你正在做一个最小化模型,什么会激励模型生产任何东西?需求在哪里?

  • 你可能需要再看看orderinv。当你现在这样做时,你不知道订单或库存在哪里使用,所以你可以使用相同的库存来构建5个不同的东西…

最新更新