当前位置: 首页 > news >正文

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

目录

  • 写在开头
  • 1.背景描述
  • 2. 模型构建思路
  • 3.实现代码
    • 3.1 数据准备
    • 3.2 进行建模
    • 3.3 结果解析
  • 4.代码的优化
  • 写在最后

写在开头

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

1.背景描述

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

产品和市场概述

  • 产品A:一款高性能智能手机,适用于追求高技术和大屏幕体验的用户。
  • 产品B:一款性价比高的智能手机,目标客户是预算有限但仍希望拥有现代技术的消费者。

市场分析

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

  • Market1:对高性能和技术要求较高的用户市场,售价较高。
  • Market2:对预算敏感的用户市场,售价较为亲民。

数据输入

1. materials 工作表
记录了生产智能手机所需的原材料和供应量:

MaterialCoefficient_ACoefficient_BSupply
M1212000
M2322500
M3131500
M4423000
M5242000
M6514000
M7322500
M8232000
M9423000
M10151000

2. production_cost 工作表
记录了生产每款智能手机的成本:

ModelCost
A5
B7

3. logistics_cost 工作表
记录了不同市场和型号的物流成本:

MarketModelCost
Market1A10
Market1B12
Market2A8
Market2B11

4. selling_price 工作表
记录了不同市场中每款智能手机的售价:

MarketModelSelling_Price
Market1A25
Market1B30
Market2A28
Market2B32

业务目标

为了实现利润最大化,公司需要通过以下步骤来优化其生产和销售策略:

  1. 优化生产量:决定生产型号A和型号B的最优数量,以满足市场需求并最大化利润。
  2. 分配销售:根据不同市场的售价和物流成本,优化智能手机的市场分配。

模型设定

我们将使用线性规划模型来解决这个问题。目标是最大化总利润,其中利润由产品的销售收入减去生产和物流成本。

模型参数

  • 生产能力

    • 型号A的生产能力上限为400单位。
    • 型号B的生产能力上限为300单位。
  • 市场需求

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

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

2. 模型构建思路

  1. 确定决策变量

    • 生产变量

      • 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的数量
  2. 定义目标函数

    我们的目标是最大化利润,利润计算公式为:
    Profit = Total Revenue − Total Cost \text{Profit} = \text{Total Revenue} - \text{Total Cost} Profit=Total RevenueTotal 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 RevenueTotal Cost

  3. 设定约束条件

    • 原材料约束:每种原材料的消耗不能超过其供应量。设原材料的需求系数为Coefficient_ACoefficient_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×productionBSupply

    • 生产能力约束
      production A ≤ 400 \text{production}_A \leq 400 productionA400
      production B ≤ 300 \text{production}_B \leq 300 productionB300

    • 市场需求与分配约束

      • 总生产需求约束:
        production A + production B ≥ 500 \text{production}_A + \text{production}_B \geq 500 productionA+productionB500

      • 市场分配约束:
        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 数据准备

为方便进行演示,构建实例数据代码如下:

import pandas as pd# 创建数据
materials_data = {'Material': ['M1', 'M2', 'M3', 'M4', 'M5', 'M6', 'M7', 'M8', 'M9', 'M10'],'Coefficient_A': [2, 3, 1, 4, 2, 5, 3, 2, 4, 1],'Coefficient_B': [1, 2, 3, 2, 4, 1, 2, 3, 2, 5],'Supply': [2000, 2500, 1500, 3000, 2000, 4000, 2500, 2000, 3000, 1000]
}production_cost_data = {'Model': ['A', 'B'],'Cost': [5, 7]
}logistics_cost_data = {'Market': ['Market1', 'Market1', 'Market2', 'Market2'],'Model': ['A', 'B', 'A', 'B'],'Cost': [10, 12, 8, 11]
}selling_price_data = {'Market': ['Market1', 'Market1', 'Market2', 'Market2'],'Model': ['A', 'B', 'A', 'B'],'Selling_Price': [25, 30, 28, 32]
}# 将数据转换为DataFrame
materials_df = pd.DataFrame(materials_data)
production_cost_df = pd.DataFrame(production_cost_data)
logistics_cost_df = pd.DataFrame(logistics_cost_data)
selling_price_df = pd.DataFrame(selling_price_data)# # 保存到Excel文件
# with pd.ExcelWriter('parameters.xlsx') as writer:
#     materials_df.to_excel(writer, sheet_name='materials', index=False)
#     production_cost_df.to_excel(writer, sheet_name='production_cost', index=False)
#     logistics_cost_df.to_excel(writer, sheet_name='logistics_cost', index=False)
#     selling_price_df.to_excel(writer, sheet_name='selling_price', index=False)# print("数据文件 'parameters.xlsx' 已成功创建。")

3.2 进行建模

现利用Python进行建模,具体代码如下:

import pandas as pd
import pulp# 读取数据
xls = pd.ExcelFile('parameters.xlsx')
materials_df = pd.read_excel(xls, 'materials')
production_cost_df = pd.read_excel(xls, 'production_cost')
logistics_cost_df = pd.read_excel(xls, 'logistics_cost')
selling_price_df = pd.read_excel(xls, 'selling_price')# 初始化模型
prob = pulp.LpProblem("Profit_Maximization", pulp.LpMaximize)# 定义生产和分配变量
production_A = pulp.LpVariable('production_A', lowBound=0, cat='Integer')
production_B = pulp.LpVariable('production_B', lowBound=0, cat='Integer')
dist_A_market1 = pulp.LpVariable('dist_A_market1', lowBound=0, cat='Integer')
dist_B_market1 = pulp.LpVariable('dist_B_market1', lowBound=0, cat='Integer')
dist_A_market2 = pulp.LpVariable('dist_A_market2', lowBound=0, cat='Integer')
dist_B_market2 = pulp.LpVariable('dist_B_market2', lowBound=0, cat='Integer')# 获取数据
cost_A = production_cost_df.loc[production_cost_df['Model'] == 'A', 'Cost'].values[0]
cost_B = production_cost_df.loc[production_cost_df['Model'] == 'B', 'Cost'].values[0]
logistics_cost_A_market1 = logistics_cost_df.loc[(logistics_cost_df['Market'] == 'Market1') & (logistics_cost_df['Model'] == 'A'), 'Cost'].values[0]
logistics_cost_B_market1 = logistics_cost_df.loc[(logistics_cost_df['Market'] == 'Market1') & (logistics_cost_df['Model'] == 'B'), 'Cost'].values[0]
logistics_cost_A_market2 = logistics_cost_df.loc[(logistics_cost_df['Market'] == 'Market2') & (logistics_cost_df['Model'] == 'A'), 'Cost'].values[0]
logistics_cost_B_market2 = logistics_cost_df.loc[(logistics_cost_df['Market'] == 'Market2') & (logistics_cost_df['Model'] == 'B'), 'Cost'].values[0]
selling_price_A_market1 = selling_price_df.loc[(selling_price_df['Market'] == 'Market1') & (selling_price_df['Model'] == 'A'), 'Selling_Price'].values[0]
selling_price_B_market1 = selling_price_df.loc[(selling_price_df['Market'] == 'Market1') & (selling_price_df['Model'] == 'B'), 'Selling_Price'].values[0]
selling_price_A_market2 = selling_price_df.loc[(selling_price_df['Market'] == 'Market2') & (selling_price_df['Model'] == 'A'), 'Selling_Price'].values[0]
selling_price_B_market2 = selling_price_df.loc[(selling_price_df['Market'] == 'Market2') & (selling_price_df['Model'] == 'B'), 'Selling_Price'].values[0]# 定义目标函数(利润最大化)
prob += (selling_price_A_market1 * dist_A_market1 +selling_price_B_market1 * dist_B_market1 +selling_price_A_market2 * dist_A_market2 +selling_price_B_market2 * dist_B_market2-(cost_A * production_A +cost_B * production_B +logistics_cost_A_market1 * dist_A_market1 +logistics_cost_B_market1 * dist_B_market1 +logistics_cost_A_market2 * dist_A_market2 +logistics_cost_B_market2 * dist_B_market2)
), "Total Profit"# 添加约束条件
for index, row in materials_df.iterrows():prob += (row['Coefficient_A'] * production_A +row['Coefficient_B'] * production_B) <= row['Supply'], f"{row['Material']}_Supply"
prob += production_A <= 400, "Production Capacity A"
prob += production_B <= 300, "Production Capacity B"
prob += production_A + production_B >= 500, "Total Production Demand"
prob += dist_A_market1 + dist_A_market2 == production_A, "Total Distribution A"
prob += dist_B_market1 + dist_B_market2 == production_B, "Total Distribution B"# 求解问题
prob.solve()# 输出结果
print(f"Optimal Production for Model A (Production Line 1): {pulp.value(production_A)} units")
print(f"Optimal Production for Model B (Production Line 2): {pulp.value(production_B)} units")
print(f"Optimal Distribution to Market 1 (Model A): {pulp.value(dist_A_market1)} units")
print(f"Optimal Distribution to Market 1 (Model B): {pulp.value(dist_B_market1)} units")
print(f"Optimal Distribution to Market 2 (Model A): {pulp.value(dist_A_market2)} units")
print(f"Optimal Distribution to Market 2 (Model B): {pulp.value(dist_B_market2)} units")

3.3 结果解析

最优解

  1. 最优目标值

    • Objective value: 7680
      • 最终计算出的目标值为 7680。这是设定的利润最大化目标函数的最优解。
  2. 生产决策

    • 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单位。
  3. 市场分配

    • 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.代码的优化

在原始代码的基础上,我可以对模型进行了几项关键优化,以增强其灵活性和实用性。

import pandas as pd
import pulpclass ProfitMaximizationModel:def __init__(self, file_path, config):"""初始化模型类,读取数据并进行基本配置。参数:file_path (str): Excel文件的路径config (dict): 包含生产限制和总需求的配置字典"""self.file_path = file_pathself.config = configself.models = ['A', 'B']self.markets = ['Market1', 'Market2']self.materials_df, self.production_cost_df, self.logistics_cost_df, self.selling_price_df = self._read_data()self.prob = pulp.LpProblem("Profit_Maximization", pulp.LpMaximize)self.variables = self._define_variables()self.parameters = self._extract_parameters()def _read_data(self):"""内部方法,用于读取Excel数据并返回各数据表的DataFrame对象。"""xls = pd.ExcelFile(self.file_path)materials_df = pd.read_excel(xls, 'materials')production_cost_df = pd.read_excel(xls, 'production_cost')logistics_cost_df = pd.read_excel(xls, 'logistics_cost')selling_price_df = pd.read_excel(xls, 'selling_price')return materials_df, production_cost_df, logistics_cost_df, selling_price_dfdef _define_variables(self):"""内部方法,用于定义生产和分配的决策变量。"""variables = {}for model in self.models:variables[f'production_{model}'] = pulp.LpVariable(f'production_{model}', lowBound=0, cat='Integer')for market in self.markets:variables[f'dist_{model}_{market}'] = pulp.LpVariable(f'dist_{model}_{market}', lowBound=0, cat='Integer')return variablesdef _extract_parameters(self):"""内部方法,从数据表中提取成本和价格参数。"""parameters = {}for model in self.models:parameters[f'cost_{model}'] = self.production_cost_df.loc[self.production_cost_df['Model'] == model, 'Cost'].values[0]for market in self.markets:parameters[f'logistics_cost_{model}_{market}'] = self.logistics_cost_df.loc[(self.logistics_cost_df['Market'] == market) & (self.logistics_cost_df['Model'] == model), 'Cost'].values[0]parameters[f'selling_price_{model}_{market}'] = self.selling_price_df.loc[(self.selling_price_df['Market'] == market) & (self.selling_price_df['Model'] == model), 'Selling_Price'].values[0]return parametersdef _define_objective_function(self):"""内部方法,定义目标函数(利润最大化)。"""profit = []cost = []for model in self.models:for market in self.markets:profit.append(self.parameters[f'selling_price_{model}_{market}'] * self.variables[f'dist_{model}_{market}'])cost.append(self.parameters[f'logistics_cost_{model}_{market}'] * self.variables[f'dist_{model}_{market}'])cost.append(self.parameters[f'cost_{model}'] * self.variables[f'production_{model}'])self.prob += pulp.lpSum(profit) - pulp.lpSum(cost), "Total Profit"def _define_constraints(self):"""内部方法,添加模型的约束条件。"""for index, row in self.materials_df.iterrows():self.prob += pulp.lpSum([row[f'Coefficient_{model}'] * self.variables[f'production_{model}'] for model in self.models]) <= row['Supply'], f"{row['Material']}_Supply"for model in self.models:self.prob += self.variables[f'production_{model}'] <= self.config['production_limits'][model], f"Production Capacity {model}"self.prob += pulp.lpSum([self.variables[f'production_{model}'] for model in self.models]) >= self.config['total_demand'], "Total Production Demand"for model in self.models:self.prob += pulp.lpSum([self.variables[f'dist_{model}_{market}'] for market in self.markets]) == self.variables[f'production_{model}'], f"Total Distribution {model}"def solve(self):"""执行求解过程并返回结果。"""self._define_objective_function()self._define_constraints()self.prob.solve()return {v.name: pulp.value(v) for v in self.prob.variables()}# 示例使用方法
if __name__ == "__main__":config = {"production_limits": {"A": 400,"B": 300},"total_demand": 500}# 初始化模型并求解model = ProfitMaximizationModel('parameters.xlsx', config)results = model.solve()print(results)

写在最后

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

相关文章:

  • 北京网站建设多少钱?
  • 辽宁网页制作哪家好_网站建设
  • 高端品牌网站建设_汉中网站制作
  • Unity 求坐标点在扇形区域内的投影
  • 从零开始学数据结构系列之第四章《拓扑排序代码详解》
  • 22 mysql数据库主从搭建
  • 国外机器人相关网站推荐
  • Unity AB包
  • 【计算机网络】网络版本计算器
  • CentOS 7使用RPM安装MySQL
  • Linux 网站服务器的搭建教程
  • js使用run编码计算region的交集并集差集
  • WHAT - 前端跨端识别
  • 图神经网络教程2——循环图神经网络-1
  • Linux ubuntu 使用 wine 安装迅雷不限速版本,并添加快捷方式,解决 desktop 桌面快捷方式不能启动的问题!
  • 鸿蒙关于手机全局本地文件读取,写入
  • The Sandbox 新提案: 2024 年亚洲和拉丁美洲区块链活动预算
  • 一文读懂 服务器
  • [Vue CLI 3] 配置解析之 css.extract
  • HashMap ConcurrentHashMap
  • Javascript 原型链
  • Java面向对象及其三大特征
  • Python_OOP
  • Ruby 2.x 源代码分析:扩展 概述
  • Solarized Scheme
  • Spring声明式事务管理之一:五大属性分析
  • STAR法则
  • 不用申请服务号就可以开发微信支付/支付宝/QQ钱包支付!附:直接可用的代码+demo...
  • 前端工程化(Gulp、Webpack)-webpack
  • 通过git安装npm私有模块
  • 通过几道题目学习二叉搜索树
  • 一起来学SpringBoot | 第十篇:使用Spring Cache集成Redis
  • 用Canvas画一棵二叉树
  • 自定义函数
  • 【干货分享】dos命令大全
  • 阿里云IoT边缘计算助力企业零改造实现远程运维 ...
  • ​Kaggle X光肺炎检测比赛第二名方案解析 | CVPR 2020 Workshop
  • ‌‌雅诗兰黛、‌‌兰蔻等美妆大品牌的营销策略是什么?
  • ###STL(标准模板库)
  • #define MODIFY_REG(REG, CLEARMASK, SETMASK)
  • #NOIP 2014# day.1 T2 联合权值
  • (2)(2.10) LTM telemetry
  • (2024,LoRA,全量微调,低秩,强正则化,缓解遗忘,多样性)LoRA 学习更少,遗忘更少
  • (八)Flink Join 连接
  • (八)Spring源码解析:Spring MVC
  • (二十四)Flask之flask-session组件
  • (附源码)SSM环卫人员管理平台 计算机毕设36412
  • (六) ES6 新特性 —— 迭代器(iterator)
  • (一)十分简易快速 自己训练样本 opencv级联haar分类器 车牌识别
  • (转)IIS6 ASP 0251超过响应缓冲区限制错误的解决方法
  • .net2005怎么读string形的xml,不是xml文件。
  • .NetCore 如何动态路由
  • .NET程序员迈向卓越的必由之路
  • .NET构架之我见
  • .NET框架
  • .Net转Java自学之路—SpringMVC框架篇六(异常处理)
  • /3GB和/USERVA开关
  • :=