for-loop - “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])
解决方案
推荐阅读
- python - 如何使用 Raspberry Pi 读取 RFID 卡上的纯文本
- javascript - IE11中的网格布局问题
- javascript - 在 Roomle 配置器中更改语言
- amazon-s3 - 如何查看创建多个资源的 terraform 模块的价值?
- c# - 如何在不提供 C# 管理员凭据的情况下从标准用户启动进程
- php - 如何获取 Google Analytics(分析)GA4 帐户的所有属性
- python - 获取所有成员 discord.py
- vue.js - VueJS - 如何为滑块的每 10 个单位设置一个标记/刻度
- jestjs - mocking the internal function of another function using jestjs
- cupy - 索引为零维 cp.array 的 cp.array 慢切片(基于 cp.argmin 结果)