如何解决“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 举报,一经查实,本站将立刻删除。