“LpVariable”对象不支持索引

问题描述

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

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

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


这是我的 Python 代码的关键部分: 根据LocationNumber对我的数据进行排序后,我的数据的前几行和最后几行如下: enter image description here

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])

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)