2 回答

TA貢獻1831條經驗 獲得超9個贊
您可以使用月份、工廠和產品的元組作為變量字典的鍵,也可以使用它從數據幀中獲取生產輸出 Mt。
import pulp
months = range(1,12)
plants = ['A', 'B', 'C', 'D', 'E']
products = ['AFS', 'GDF', 'POD', 'PPI']
# set up binary variables for plant-month-product
var_dict = {}
for month in months:
for plant in plants:
for product in product:
combo = (month, plant, product)
var_name = '_'.join([str(c) for c in combo])
var_dict[combo] = LpVariable(var_name, cat=LpBinary)
prob = LpProblem('Schedule', LpMinimize)
# objective function
# assume data in df and has index of month, plant, and product
prob += lpSum([var * df.loc[('at', k), 'Production Output (Mt)']
for k, v in var_dict.items()]
# then add the relevant constraints
# for example, one and only one product per plant per month
# remember that in var_dict the key is a tuple of month, plant, product
# and the value is the binary variable
for month in months:
for plant in plants:
prob += lpSum([v for k, v in var_dict.items()
if k[0] == month and k[1] == plant]) == 1

TA貢獻1820條經驗 獲得超10個贊
我認為你想做的是有決策變量,即每個工廠每個月每種產品的供應量。換句話說,你有指數:(月份,植物,產品)。
這當然會創建一個變量總數,在示例中為12 * 5 * 4 = 240個變量。len(months)*len(plants)*len(products)
我會通過將該工廠的產品的產能設置為零來處理無法生產某種產品的工廠的情況。
import pulp
months = range(1,12)
plants = ['A', 'B', 'C', 'D', 'E']
products = ['AFS', 'GDF', 'POD', 'PPI']
supply = pulp.LpVariable.dicts("supply", (months, plants, products))
print(supply)
這將返回可以引用為的變量,例如:supply[3]['A']['POD']
添加回答
舉報