首页 > 解决方案 > “LpVariable”对象不支持索引

问题描述

我遇到了“LpVariable”对象不支持索引的错误。这是因为我的数据是如何为我的纸浆优化设置的吗?

基本上,我试图通过将补货数量乘以每个 SKU (UPC) 的售价来获得优化的销售。补货数量将基于售出的数量(在数据集中可用),并将成为优化问题的约束条件。我不应该补充太多比我卖的东西。

有纸浆经验的人可以帮我解决我的错误吗?我设置 For-Loop 的方式是否合乎逻辑?


这是我的Python代码的关键部分:根据LocationNumber对我的数据进行排序后,我的数据的第一行和最后几行如下: 在此处输入图像描述

df.drop(['LowlawWeekYear'], axis=1, inplace=True) # Drop the WeekYear column

store_list = {108} # This is LocationNumber. Will only run one store for now

for store_number in sorted(store_list):
  specific_store = df[df['LocationNumber'] == store_number] 
  Qty_Price_df = specific_store.groupby('UPC', as_index = False)['AvgSellingPricewoTax', 'Units'].mean()

# get the mean of the AvgSellingPricewoTax and Units. Ultimately, I only need one row for each UPC with the average values of AvgSellingPricewoTax and Units.

SKU_list = sorted(list(set(Qty_Price_df.UPC))) # List of SKU numbers

Variable_list = dict(zip(Qty_Price_df.UPC, Qty_Price_df.UPC)) # Variables which I am looking to optimize

Price_list = dict(zip(Qty_Price_df.UPC, Qty_Price_df.AvgSellingPricewoTax))

Qty_list = dict(zip(Qty_Price_df.UPC, Qty_Price_df.Units)) # Quantity sold per UPC. This will be used in the constraint.

from pulp import *

optimization = LpProblem("Perfect_Store", LpMaximize)

Variable_list = LpVariable("SKU", lowBound=0) # Continuous by default

# Define objective function
optimization += lpSum([Price_list[type]*Variable_list[type] for type in SKU_list]), "Total Sales by multiplying Price with Variable Qty"
***# Here is where I ran into the error message: 'LpVariable' object does not support indexing***

# Set constraint for each SKU
for c in SKU_list:
  optimization += (Qty_list[c] <= Qty_list[c]*1.05), "Constraints for each SKU is the replenishment quantity which should not be more than 5% of the quantity sold"

print("Status:", LpStatus[optimization.status])

标签: for-loopoptimizationconstraintspulp

解决方案


推荐阅读