利用Python实现供应链管理中的线性规划与资源优化——手机生产操持2:利润最 ...

打印 上一主题 下一主题

主题 537|帖子 537|积分 1611

写在开头

在上篇文章中,我们探讨了如何利用生产束缚条件实现成本的最小化,这为优化运营奠基了基础。然而,实际世界中的商业环境每每充满变数,单一的成本控制策略大概无法满足复杂的红利需求。在本篇文章中,我们将深入分析如安在多重挑战下实现利润的最大化。我们将探讨先辈的利润优化策略,发掘隐藏的红利机会,并提供实际操作的最佳实践,资助你在竞争激烈的市场中取得最终的成功。
1.背景描述

在激烈的市场竞争中,智能手机制造公司面对着生产和销售的挑战。公司目宿世产两种型号的智能手机:型号A和型号B。公司希望通过优化生产和销售策略来最大化利润。我们将探讨如何利用线性规划模型来实现这一目标,具体包括生产优化、成本控制和利润最大化。
产物和市场概述


  • 产物A:一款高性能智能手机,适用于追求高技能和大屏幕体验的用户。
  • 产物B:一款性价比高的智能手机,目标客户是预算有限但仍希望拥有现代技能的消费者。
市场分析
公司在两个主要市场(Market1 和 Market2)销售智能手机:


  • Market1:对高性能和技能要求较高的用户市场,售价较高。
  • Market2:对预算敏感的用户市场,售价较为亲民。
数据输入
1. materials 工作表
记载了生产智能手机所需的原材料和供应量:
MaterialCoefficient_ACoefficient_BSupplyM1212000M2322500M3131500M4423000M5242000M6514000M7322500M8232000M9423000M10151000 2. production_cost 工作表
记载了生产每款智能手机的成本:
ModelCostA5B7 3. logistics_cost 工作表
记载了不同市场和型号的物流成本:
MarketModelCostMarket1A10Market1B12Market2A8Market2B11 4. selling_price 工作表
记载了不同市场中每款智能手机的售价:
MarketModelSelling_PriceMarket1A25Market1B30Market2A28Market2B32 业务目标
为了实现利润最大化,公司必要通过以下步骤来优化其生产和销售策略:

  • 优化生产量:决定生产型号A和型号B的最优数量,以满足市场需求并最大化利润。
  • 分配销售:根据不同市场的售价和物流成本,优化智能手机的市场分配。
模型设定
我们将使用线性规划模型来办理这个题目。目标是最大化总利润,此中利润由产物的销售收入减去生产和物流成本。
模型参数


  • 生产本领

    • 型号A的生产本领上限为400单位。
    • 型号B的生产本领上限为300单位。

  • 市场需求

    • 总生产需求至少为500单位(型号A和B的总和)。

  • 市场分配

    • 型号A和B的市场分配必须即是生产量。

2. 模型构建思绪


  • 确定决策变量

    • 生产变量

      • production_A:生产型号A的数量
      • production_B:生产型号B的数量

    • 分配变量

      • dist_A_market1:分配给Market1的型号A的数量
      • dist_B_market1:分配给Market1的型号B的数量
      • dist_A_market2:分配给Market2的型号A的数量
      • dist_B_market2:分配给Market2的型号B的数量


  • 定义目标函数
    我们的目标是最大化利润,利润计算公式为:
                                                       Profit                                  =                                  Total Revenue                                  −                                  Total Cost                                          \text{Profit} = \text{Total Revenue} - \text{Total Cost}                           Profit=Total Revenue−Total Cost

    • 总收入
                                                                   Total Revenue                                        =                                        (                                                       Selling Price                                           A                                                           M                                              1                                                                     ×                                                       dist                                           A                                                           M                                              1                                                                     )                                        +                                        (                                                       Selling Price                                           B                                                           M                                              1                                                                     ×                                                       dist                                           B                                                           M                                              1                                                                     )                                        +                                        (                                                       Selling Price                                           A                                                           M                                              2                                                                     ×                                                       dist                                           A                                                           M                                              2                                                                     )                                        +                                        (                                                       Selling Price                                           B                                                           M                                              2                                                                     ×                                                       dist                                           B                                                           M                                              2                                                                     )                                                  \text{Total Revenue} = (\text{Selling Price}_A^{M1} \times \text{dist}_A^{M1}) + (\text{Selling Price}_B^{M1} \times \text{dist}_B^{M1}) + (\text{Selling Price}_A^{M2} \times \text{dist}_A^{M2}) + (\text{Selling Price}_B^{M2} \times \text{dist}_B^{M2})                                 Total Revenue=(Selling PriceAM1​×distAM1​)+(Selling PriceBM1​×distBM1​)+(Selling PriceAM2​×distAM2​)+(Selling PriceBM2​×distBM2​)
    • 总成本
                                                                   Total Cost                                        =                                        (                                                       Cost                                           A                                                      ×                                                       production                                           A                                                      )                                        +                                        (                                                       Cost                                           B                                                      ×                                                       production                                           B                                                      )                                        +                                        (                                                       Logistics Cost                                           A                                                           M                                              1                                                                     ×                                                       dist                                           A                                                           M                                              1                                                                     )                                        +                                        (                                                       Logistics Cost                                           B                                                           M                                              1                                                                     ×                                                       dist                                           B                                                           M                                              1                                                                     )                                        +                                        (                                                       Logistics Cost                                           A                                                           M                                              2                                                                     ×                                                       dist                                           A                                                           M                                              2                                                                     )                                        +                                        (                                                       Logistics Cost                                           B                                                           M                                              2                                                                     ×                                                       dist                                           B                                                           M                                              2                                                                     )                                                  \text{Total Cost} = (\text{Cost}_A \times \text{production}_A) + (\text{Cost}_B \times \text{production}_B) + (\text{Logistics Cost}_A^{M1} \times \text{dist}_A^{M1}) + (\text{Logistics Cost}_B^{M1} \times \text{dist}_B^{M1}) + (\text{Logistics Cost}_A^{M2} \times \text{dist}_A^{M2}) + (\text{Logistics Cost}_B^{M2} \times \text{dist}_B^{M2})                                 Total Cost=(CostA​×productionA​)+(CostB​×productionB​)+(Logistics CostAM1​×distAM1​)+(Logistics CostBM1​×distBM1​)+(Logistics CostAM2​×distAM2​)+(Logistics CostBM2​×distBM2​)
    • 目标函数
                                                                   Profit                                        =                                        Total Revenue                                        −                                        Total Cost                                                  \text{Profit} = \text{Total Revenue} - \text{Total Cost}                                 Profit=Total Revenue−Total Cost

  • 设定束缚条件

    • 原材料束缚:每种原材料的消耗不能高出其供应量。设原材料的需求系数为Coefficient_A和Coefficient_B,原材料供应量为Supply:
                                                                                  Coefficient                                           A                                                      ×                                                       production                                           A                                                      +                                                       Coefficient                                           B                                                      ×                                                       production                                           B                                                      ≤                                        Supply                                                  \text{Coefficient}_A \times \text{production}_A + \text{Coefficient}_B \times \text{production}_B \leq \text{Supply}                                 CoefficientA​×productionA​+CoefficientB​×productionB​≤Supply
    • 生产本领束缚
                                                                                  production                                           A                                                      ≤                                        400                                                  \text{production}_A \leq 400                                 productionA​≤400
                                                                                  production                                           B                                                      ≤                                        300                                                  \text{production}_B \leq 300                                 productionB​≤300
    • 市场需求与分配束缚

      • 总生产需求束缚:
                                                                                                production                                                 A                                                              +                                                               production                                                 B                                                              ≥                                              500                                                          \text{production}_A + \text{production}_B \geq 500                                       productionA​+productionB​≥500
      • 市场分配束缚:
                                                                                                dist                                                 A                                                                   M                                                    1                                                                               +                                                               dist                                                 A                                                                   M                                                    2                                                                               =                                                               production                                                 A                                                                          \text{dist}_A^{M1} + \text{dist}_A^{M2} = \text{production}_A                                       distAM1​+distAM2​=productionA​
                                                                                                dist                                                 B                                                                   M                                                    1                                                                               +                                                               dist                                                 B                                                                   M                                                    2                                                                               =                                                               production                                                 B                                                                          \text{dist}_B^{M1} + \text{dist}_B^{M2} = \text{production}_B                                       distBM1​+distBM2​=productionB​


3.实现代码

3.1 数据准备

为方便进行演示,构建实例数据代码如下:
  1. import pandas as pd
  2. # 创建数据
  3. materials_data = {
  4.     'Material': ['M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9', 'M10'],
  5.     'Coefficient_A': [2, 3, 1, 4, 2, 5, 3, 2, 4, 1],
  6.     'Coefficient_B': [1, 2, 3, 2, 4, 1, 2, 3, 2, 5],
  7.     'Supply': [2000, 2500, 1500, 3000, 2000, 4000, 2500, 2000, 3000, 1000]
  8. }
  9. production_cost_data = {
  10.     'Model': ['A', 'B'],
  11.     'Cost': [5, 7]
  12. }
  13. logistics_cost_data = {
  14.     'Market': ['Market1', 'Market1', 'Market2', 'Market2'],
  15.     'Model': ['A', 'B', 'A', 'B'],
  16.     'Cost': [10, 12, 8, 11]
  17. }
  18. selling_price_data = {
  19.     'Market': ['Market1', 'Market1', 'Market2', 'Market2'],
  20.     'Model': ['A', 'B', 'A', 'B'],
  21.     'Selling_Price': [25, 30, 28, 32]
  22. }
  23. # 将数据转换为DataFrame
  24. materials_df = pd.DataFrame(materials_data)
  25. production_cost_df = pd.DataFrame(production_cost_data)
  26. logistics_cost_df = pd.DataFrame(logistics_cost_data)
  27. selling_price_df = pd.DataFrame(selling_price_data)
  28. # # 保存到Excel文件
  29. # with pd.ExcelWriter('parameters.xlsx') as writer:
  30. #     materials_df.to_excel(writer, sheet_name='materials', index=False)
  31. #     production_cost_df.to_excel(writer, sheet_name='production_cost', index=False)
  32. #     logistics_cost_df.to_excel(writer, sheet_name='logistics_cost', index=False)
  33. #     selling_price_df.to_excel(writer, sheet_name='selling_price', index=False)
  34. # print("数据文件 'parameters.xlsx' 已成功创建。")
复制代码
3.2 进行建模

现利用Python进行建模,具体代码如下:
  1. import pandas as pd
  2. import pulp
  3. # 读取数据
  4. xls = pd.ExcelFile('parameters.xlsx')
  5. materials_df = pd.read_excel(xls, 'materials')
  6. production_cost_df = pd.read_excel(xls, 'production_cost')
  7. logistics_cost_df = pd.read_excel(xls, 'logistics_cost')
  8. selling_price_df = pd.read_excel(xls, 'selling_price')
  9. # 初始化模型
  10. prob = pulp.LpProblem("Profit_Maximization", pulp.LpMaximize)
  11. # 定义生产和分配变量
  12. production_A = pulp.LpVariable('production_A', lowBound=0, cat='Integer')
  13. production_B = pulp.LpVariable('production_B', lowBound=0, cat='Integer')
  14. dist_A_market1 = pulp.LpVariable('dist_A_market1', lowBound=0, cat='Integer')
  15. dist_B_market1 = pulp.LpVariable('dist_B_market1', lowBound=0, cat='Integer')
  16. dist_A_market2 = pulp.LpVariable('dist_A_market2', lowBound=0, cat='Integer')
  17. dist_B_market2 = pulp.LpVariable('dist_B_market2', lowBound=0, cat='Integer')
  18. # 获取数据
  19. cost_A = production_cost_df.loc[production_cost_df['Model'] == 'A', 'Cost'].values[0]
  20. cost_B = production_cost_df.loc[production_cost_df['Model'] == 'B', 'Cost'].values[0]
  21. logistics_cost_A_market1 = logistics_cost_df.loc[(logistics_cost_df['Market'] == 'Market1') & (logistics_cost_df['Model'] == 'A'), 'Cost'].values[0]
  22. logistics_cost_B_market1 = logistics_cost_df.loc[(logistics_cost_df['Market'] == 'Market1') & (logistics_cost_df['Model'] == 'B'), 'Cost'].values[0]
  23. logistics_cost_A_market2 = logistics_cost_df.loc[(logistics_cost_df['Market'] == 'Market2') & (logistics_cost_df['Model'] == 'A'), 'Cost'].values[0]
  24. logistics_cost_B_market2 = logistics_cost_df.loc[(logistics_cost_df['Market'] == 'Market2') & (logistics_cost_df['Model'] == 'B'), 'Cost'].values[0]
  25. selling_price_A_market1 = selling_price_df.loc[(selling_price_df['Market'] == 'Market1') & (selling_price_df['Model'] == 'A'), 'Selling_Price'].values[0]
  26. selling_price_B_market1 = selling_price_df.loc[(selling_price_df['Market'] == 'Market1') & (selling_price_df['Model'] == 'B'), 'Selling_Price'].values[0]
  27. selling_price_A_market2 = selling_price_df.loc[(selling_price_df['Market'] == 'Market2') & (selling_price_df['Model'] == 'A'), 'Selling_Price'].values[0]
  28. selling_price_B_market2 = selling_price_df.loc[(selling_price_df['Market'] == 'Market2') & (selling_price_df['Model'] == 'B'), 'Selling_Price'].values[0]
  29. # 定义目标函数(利润最大化)
  30. prob += (
  31.     selling_price_A_market1 * dist_A_market1 +
  32.     selling_price_B_market1 * dist_B_market1 +
  33.     selling_price_A_market2 * dist_A_market2 +
  34.     selling_price_B_market2 * dist_B_market2
  35.     -
  36.     (cost_A * production_A +
  37.     cost_B * production_B +
  38.     logistics_cost_A_market1 * dist_A_market1 +
  39.     logistics_cost_B_market1 * dist_B_market1 +
  40.     logistics_cost_A_market2 * dist_A_market2 +
  41.     logistics_cost_B_market2 * dist_B_market2)
  42. ), "Total Profit"
  43. # 添加约束条件
  44. for index, row in materials_df.iterrows():
  45.     prob += (row['Coefficient_A'] * production_A +
  46.              row['Coefficient_B'] * production_B) <= row['Supply'], f"{row['Material']}_Supply"
  47. prob += production_A <= 400, "Production Capacity A"
  48. prob += production_B <= 300, "Production Capacity B"
  49. prob += production_A + production_B >= 500, "Total Production Demand"
  50. prob += dist_A_market1 + dist_A_market2 == production_A, "Total Distribution A"
  51. prob += dist_B_market1 + dist_B_market2 == production_B, "Total Distribution B"
  52. # 求解问题
  53. prob.solve()
  54. # 输出结果
  55. print(f"Optimal Production for Model A (Production Line 1): {pulp.value(production_A)} units")
  56. print(f"Optimal Production for Model B (Production Line 2): {pulp.value(production_B)} units")
  57. print(f"Optimal Distribution to Market 1 (Model A): {pulp.value(dist_A_market1)} units")
  58. print(f"Optimal Distribution to Market 1 (Model B): {pulp.value(dist_B_market1)} units")
  59. print(f"Optimal Distribution to Market 2 (Model A): {pulp.value(dist_A_market2)} units")
  60. print(f"Optimal Distribution to Market 2 (Model B): {pulp.value(dist_B_market2)} units")
复制代码
3.3 效果分析

最优解

  • 最优目标值

    • Objective value: 7680

      • 最终计算出的目标值为 7680。这是设定的利润最大化目标函数的最优解。


  • 生产决策

    • Optimal Production for Model A (Production Line 1): 400.0 units

      • 产物A的最优生产量为400单位。

    • Optimal Production for Model B (Production Line 2): 120.0 units

      • 产物B的最优生产量为120单位。


  • 市场分配

    • Optimal Distribution to Market 1 (Model A): 0.0 units

      • 产物A分配到市场1的最优量为0单位。

    • Optimal Distribution to Market 1 (Model B): 0.0 units

      • 产物B分配到市场1的最优量为0单位。

    • Optimal Distribution to Market 2 (Model A): 400.0 units

      • 产物A分配到市场2的最优量为400单位。

    • Optimal Distribution to Market 2 (Model B): 120.0 units

      • 产物B分配到市场2的最优量为120单位。


生产与分配策略


  • 产物A完全分配到市场2,而产物B也全部分配到市场2。这大概是由于市场2的售价或需求条件更加有利于利润最大化。
  • 市场1的分配量为0大概是由于市场1的物流成本或售价不利于整体利润最大化。
4.代码的优化

在原始代码的基础上,我可以对模型进行了几项关键优化,以增强其灵活性和实用性。
  1. import pandas as pd
  2. import pulp
  3. class ProfitMaximizationModel:
  4.     def __init__(self, file_path, config):
  5.         """
  6.         初始化模型类,读取数据并进行基本配置。
  7.         
  8.         参数:
  9.         file_path (str): Excel文件的路径
  10.         config (dict): 包含生产限制和总需求的配置字典
  11.         """
  12.         self.file_path = file_path
  13.         self.config = config
  14.         self.models = ['A', 'B']
  15.         self.markets = ['Market1', 'Market2']
  16.         self.materials_df, self.production_cost_df, self.logistics_cost_df, self.selling_price_df = self._read_data()
  17.         self.prob = pulp.LpProblem("Profit_Maximization", pulp.LpMaximize)
  18.         self.variables = self._define_variables()
  19.         self.parameters = self._extract_parameters()
  20.     def _read_data(self):
  21.         """内部方法,用于读取Excel数据并返回各数据表的DataFrame对象。"""
  22.         xls = pd.ExcelFile(self.file_path)
  23.         materials_df = pd.read_excel(xls, 'materials')
  24.         production_cost_df = pd.read_excel(xls, 'production_cost')
  25.         logistics_cost_df = pd.read_excel(xls, 'logistics_cost')
  26.         selling_price_df = pd.read_excel(xls, 'selling_price')
  27.         return materials_df, production_cost_df, logistics_cost_df, selling_price_df
  28.     def _define_variables(self):
  29.         """内部方法,用于定义生产和分配的决策变量。"""
  30.         variables = {}
  31.         for model in self.models:
  32.             variables[f'production_{model}'] = pulp.LpVariable(f'production_{model}', lowBound=0, cat='Integer')
  33.             for market in self.markets:
  34.                 variables[f'dist_{model}_{market}'] = pulp.LpVariable(f'dist_{model}_{market}', lowBound=0, cat='Integer')
  35.         return variables
  36.     def _extract_parameters(self):
  37.         """内部方法,从数据表中提取成本和价格参数。"""
  38.         parameters = {}
  39.         for model in self.models:
  40.             parameters[f'cost_{model}'] = self.production_cost_df.loc[self.production_cost_df['Model'] == model, 'Cost'].values[0]
  41.             for market in self.markets:
  42.                 parameters[f'logistics_cost_{model}_{market}'] = self.logistics_cost_df.loc[
  43.                     (self.logistics_cost_df['Market'] == market) & (self.logistics_cost_df['Model'] == model), 'Cost'
  44.                 ].values[0]
  45.                 parameters[f'selling_price_{model}_{market}'] = self.selling_price_df.loc[
  46.                     (self.selling_price_df['Market'] == market) & (self.selling_price_df['Model'] == model), 'Selling_Price'
  47.                 ].values[0]
  48.         return parameters
  49.     def _define_objective_function(self):
  50.         """内部方法,定义目标函数(利润最大化)。"""
  51.         profit = []
  52.         cost = []
  53.         for model in self.models:
  54.             for market in self.markets:
  55.                 profit.append(self.parameters[f'selling_price_{model}_{market}'] * self.variables[f'dist_{model}_{market}'])
  56.                 cost.append(self.parameters[f'logistics_cost_{model}_{market}'] * self.variables[f'dist_{model}_{market}'])
  57.             cost.append(self.parameters[f'cost_{model}'] * self.variables[f'production_{model}'])
  58.         
  59.         self.prob += pulp.lpSum(profit) - pulp.lpSum(cost), "Total Profit"
  60.     def _define_constraints(self):
  61.         """内部方法,添加模型的约束条件。"""
  62.         for index, row in self.materials_df.iterrows():
  63.             self.prob += pulp.lpSum([row[f'Coefficient_{model}'] * self.variables[f'production_{model}'] for model in self.models]) <= row['Supply'], f"{row['Material']}_Supply"
  64.         for model in self.models:
  65.             self.prob += self.variables[f'production_{model}'] <= self.config['production_limits'][model], f"Production Capacity {model}"
  66.         
  67.         self.prob += pulp.lpSum([self.variables[f'production_{model}'] for model in self.models]) >= self.config['total_demand'], "Total Production Demand"
  68.         for model in self.models:
  69.             self.prob += pulp.lpSum([self.variables[f'dist_{model}_{market}'] for market in self.markets]) == self.variables[f'production_{model}'], f"Total Distribution {model}"
  70.     def solve(self):
  71.         """执行求解过程并返回结果。"""
  72.         self._define_objective_function()
  73.         self._define_constraints()
  74.         self.prob.solve()
  75.         return {v.name: pulp.value(v) for v in self.prob.variables()}
  76. # 示例使用方法
  77. if __name__ == "__main__":
  78.     config = {
  79.         "production_limits": {
  80.             "A": 400,
  81.             "B": 300
  82.         },
  83.         "total_demand": 500
  84.     }
  85.     # 初始化模型并求解
  86.     model = ProfitMaximizationModel('parameters.xlsx', config)
  87.     results = model.solve()
  88.     print(results)
复制代码
写在最后

通过本文的介绍,您已经相识了如何使用Python中的线性规划工具快速构建并求解一个利润最大化模型,并通过灵活的配置和自定义束缚条件,轻松应对各种复杂的业务场景。无论您是希望优化企业的生产操持,还是精细化管理资源分配,这个模型都能为您提供强有力的支持。将来,您可以根据自身需求,继续扩展和优化这个模型,实现更加精准和高效的决策支持。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

惊落一身雪

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表