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']
添加回答
举报