首页 > 解决方案 > 纸浆总和与目标函数

问题描述

我正在尝试使用纸浆来最小化总成本,以找到每种产品的生产数量和订购更多材料的数量。

下面的代码是每个产品的产品、材料、利润和材料使用的输入。我已经定义了变量。

但我不知道如何为 Total_Excess 成本创建目标函数。因为通常我们使用 lpsum 但我们的约束需要 sum in sum 作为图片中的方程。我尝试编写如下代码,但这样做时显示错误。

总超额成本方程


# List

product = ['S1', 'S2', 'S3', 'S4']     # i

# material list

material = ['A', 'B', 'C', 'D', 'E', 'F', 'I', 'G', 'H', 'J', 'K', 'L', 'M']  # m

# Dictionary

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
# 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='Continuous')

# 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)
Total_ExcessCost = lpSum((inv[m]+order[m] for m in material - lpSum(produce[i]*usage[i][m] for m in material for i in product))
                       * cost[m] for m in material)

objective = Total_ProCost + Total_MatCost + Total_ExcessCost

model.setObjective(objective)
# Define Constraints

# Material Quantity used
for m in material:
  model += lpSum(produce[i]*usage[i][m]for m in material for i in product) <= 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) <= inv_bf

model.solve()

#Output

for v in model.variables():
  print(v.name,'=',v.varValue)

produce = [v.varValue for v in model.variables()]

# The optimised objective function value is printed to the console
print("Total cost = ", pulp.value(model.objective))

标签: pythonpulpminimization

解决方案


推荐阅读