使用线性规划优化商超进货策略
import itertools import pandas as pd from scipy.optimize import linprog
读取数据
data = pd.read_csv('data4.csv')
计算每种单品的收益
data['单品收益'] = data['销量'] * data['成本加成定价'] * (1 - data['单品损耗'] * 0.01) - data['销量'] * data['批发价格']
按分类名称进行分组,并计算每种分类的收益之和
grouped_data = data.groupby('分类名称')['单品收益'].sum()
进货单品名称数的范围
num_items = range(27, 34)
初始化最大收益和对应的单品名称组合
max_profit = 0 max_profit_combination = []
遍历所有进货单品名称数的组合
for r in num_items: # 获取所有可能的单品名称组合 combinations = itertools.combinations(data['单品名称'].unique(), r)
# 遍历每种单品名称组合
for combination in combinations:
# 过滤出包含指定单品名称的数据
filtered_data = data[data['单品名称'].isin(combination)]
# 构建线性规划问题的约束条件和目标函数
c = -filtered_data['单品收益'].values
A_ub = filtered_data['销量'].values.reshape(1, -1)
b_ub = [filtered_data['最小陈列量'].sum()]
# 使用线性规划算法求解最大收益
result = linprog(c, A_ub=A_ub, b_ub=b_ub, method='highs')
profit = -result.fun
# 更新最大收益和对应的单品名称组合
if profit > max_profit:
max_profit = profit
max_profit_combination = combination
输出结果
print('进货量最大利润的单品名称组合是:', max_profit_combination) print('商超的最大收益是:', max_profit)
原文地址: https://www.cveoy.top/t/topic/eqBM 著作权归作者所有。请勿转载和采集!