PuLP:我在 Objective 函数中有什么问题?
PuLP : What am I wrong in Objective Function?
我创建了 objective 函数,发现下面的 Excess 语法错误。
*多余 = (inv[m]+order[m]for m in material) - sum(produce[i]usage[ i][m]for m in material for i in product)
超额成本=(库存+订单数量)-使用总量
约束
Material 使用数量
我想确定用于生产的 material 数量不会超过我们拥有的总库存加上我们订购的总数量。
所以Material使用数量<=总库存+总订单
任何人都可以为我的失踪提出建议吗?
product = ['S1', 'S2']
material = ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'G', 'H', 'J', 'K', 'L', 'M']
inv_bf = 40000
product_cost = {'S1': 4.28, 'S2': 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}}
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
# How many unit will produce ?
produce = LpVariable.dicts("produce", product, lowBound=0, cat='Integer')
# How many quantity of material to order more ?
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)
# Define Constraints
# 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)
在为 Excess cost
创建表达式的地方,前面有一个没有 sum
或更可能是 lpSum
的括号 。所以它是一个 python 生成器,前面没有表达式,这就是你的问题。
此外,在您的后续表达式中,您乘以 Excess cost
* cost[]
这看起来很奇怪。要么你的数学有误,要么变量名很傻,因为成本平方可能是荒谬的。
编辑:
试试这个。我想这就是你想要的。您需要在求和前加上 lpSum
才能构建数学模型。在您的 Excess
语句中,您没有检查 usage
中是否包含 material,因此在对所有 material 求和时会产生关键错误,即使有些不在 usage
中。我在正确的求和中加入了条件...
# 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)
我创建了 objective 函数,发现下面的 Excess 语法错误。
*多余 = (inv[m]+order[m]for m in material) - sum(produce[i]usage[ i][m]for m in material for i in product)
超额成本=(库存+订单数量)-使用总量
约束 Material 使用数量 我想确定用于生产的 material 数量不会超过我们拥有的总库存加上我们订购的总数量。
所以Material使用数量<=总库存+总订单
任何人都可以为我的失踪提出建议吗?
product = ['S1', 'S2']
material = ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'G', 'H', 'J', 'K', 'L', 'M']
inv_bf = 40000
product_cost = {'S1': 4.28, 'S2': 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}}
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
# How many unit will produce ?
produce = LpVariable.dicts("produce", product, lowBound=0, cat='Integer')
# How many quantity of material to order more ?
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)
# Define Constraints
# 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)
在为 Excess cost
创建表达式的地方,前面有一个没有 sum
或更可能是 lpSum
的括号 。所以它是一个 python 生成器,前面没有表达式,这就是你的问题。
此外,在您的后续表达式中,您乘以 Excess cost
* cost[]
这看起来很奇怪。要么你的数学有误,要么变量名很傻,因为成本平方可能是荒谬的。
编辑:
试试这个。我想这就是你想要的。您需要在求和前加上 lpSum
才能构建数学模型。在您的 Excess
语句中,您没有检查 usage
中是否包含 material,因此在对所有 material 求和时会产生关键错误,即使有些不在 usage
中。我在正确的求和中加入了条件...
# 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)