【问题标题】:PuLP : What am I wrong in Objective Function?PuLP:我在目标函数中做错了什么?
【发布时间】:2021-11-20 15:17:46
【问题描述】:

我创建了目标函数,发现下面的 Excess 语法错误。

*Excess = (inv[m]+order[m]for m in material) - sum(produce[i]usage[i][m]for m in material for i in product)

超额成本 =(库存 + 数量订单)- 使用的总数量

约束 使用的材料数量 我想确定用于生产的材料数量不会超过我们拥有的总库存加上我们订购的总数量。

所以使用的材料数量

任何人都可以为我的失踪提供建议吗?

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)

【问题讨论】:

  • 您收到的错误信息是什么?
  • 我收到如下信息。回溯(最后一次调用):文件“C:\\PycharmProjects\pythonProject1\sim.py”,第 81 行,在 ExcessCost = (inv[m]+order[m]for m in material) - sum( produce[i]*usage[i][m]for m in material for i in product)
  • 其余的回溯在哪里?请记住,您可以(并且应该)编辑您的帖子以添加它,而不是在评论中发布。
  • @jjramsey 我在帖子中添加了更多细节和问题。可以请教吗?
  • 所有的回溯添加到您的帖子,而不仅仅是最后一行。

标签: python linear-programming pulp minimization


【解决方案1】:

在您为Excess cost 创建表达式的位置,您在其前面有一个括号没有 sum 或更可能是lpSum。所以它是一个前面没有表达式的python生成器,所以这就是你的问题。

此外,在您的后续表达式中,您将 Excess cost * cost[] 相乘,这看起来很奇怪。要么你的数学计算错误,要么变量名很愚蠢,因为成本平方可能是荒谬的。

编辑:

试试这个。我想这就是你想要的。你需要lpSum 在你的总和前面,以构建数学模型。在您的Excess 语句中,您没有检查usage 中是否包含材料,因此当您对所有材料求和时会产生关键错误,即使有些不在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)

【讨论】:

  • 你是对的。一个原因是因为变量名错误。我尝试在下面使用 lpSum 作为您的建议,但我们仍然发现有关 LpAffineExpression 的一些错误。你能建议更多吗? # 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) objective = Total_ProCost + Total_MatCost + lpSum(Excess*cost[m]for m in material)
  • 我已经按照您在目标函数中的建议进行了尝试。但是当我定义与 Excess 相关的约束时,我仍然有疑问。我应该将 if m in usage[i].keys() 放在约束中吗?我在上面编辑并添加了更多有问题的约束。你能建议吗?
  • 您可以通过多种方式进行...取决于您想要做什么以及“数学”是什么。您可以创建一个单独的子集,使用条件(就像我一样)或其他各种东西。您只需要确保您用于参数/变量的键是有效的......或遭受错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
  • 2011-04-15
  • 1970-01-01
  • 1970-01-01
  • 2019-11-13
  • 2018-12-31
  • 1970-01-01
相关资源
最近更新 更多