ToB企服应用市场:ToB评测及商务社交产业平台

标题: 利用Python实现供应链管理中的线性规划与资源优化——手机生产操持2:利润最 [打印本页]

作者: 惊落一身雪    时间: 2024-8-18 00:42
标题: 利用Python实现供应链管理中的线性规划与资源优化——手机生产操持2:利润最
写在开头

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

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

市场分析
公司在两个主要市场(Market1 和 Market2)销售智能手机:

数据输入
1. materials 工作表
记载了生产智能手机所需的原材料和供应量:
MaterialCoefficient_ACoefficient_BSupplyM1212000M2322500M3131500M4423000M5242000M6514000M7322500M8232000M9423000M10151000 2. production_cost 工作表
记载了生产每款智能手机的成本:
ModelCostA5B7 3. logistics_cost 工作表
记载了不同市场和型号的物流成本:
MarketModelCostMarket1A10Market1B12Market2A8Market2B11 4. selling_price 工作表
记载了不同市场中每款智能手机的售价:
MarketModelSelling_PriceMarket1A25Market1B30Market2A28Market2B32 业务目标
为了实现利润最大化,公司必要通过以下步骤来优化其生产和销售策略:
模型设定
我们将使用线性规划模型来办理这个题目。目标是最大化总利润,此中利润由产物的销售收入减去生产和物流成本。
模型参数

2. 模型构建思绪

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 效果分析

最优解
生产与分配策略

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企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4