我正在复制此混合问题示例: https://www.coin-or.org/PuLP/CaseStudies/a_blending_problem.html
具有以下数据:
import pulp
from pulp import *
import pandas as pd
food = ["f1","f2","f3","f4"]
KG = [10,20,50,80]
Protein = [18,12,16,18]
Grass = [13,14,13,16]
price_per_kg = [15,11,10,12]
## protein,carbohydrates,kg
df = pd.DataFrame({"tkid":food,"KG":KG,"Protein":Protein,"Grass":Grass,"value":price_per_kg})
这是代码:
deposit = df["tkid"].values.tolist()
factor_volumen = 1
costs = dict((k,v) for k,v in zip(df["tkid"],df["value"]))
Protein = dict((k,v) for k,v in zip(df["tkid"],df["Protein"]))
Grass = dict((k,v) for k,v in zip(df["tkid"],df["Grass"]))
KG = dict((k,v) for k,v in zip(df["tkid"],df["KG"]))
prob = LpProblem("The Whiskas Problem", LpMinimize)
deposit_vars = LpVariable.dicts("Ingr",deposit,0)
prob += lpSum([costs[i]*deposit_vars[i] for i in deposit]), "Total Cost of Ingredients per can"
prob += lpSum([deposit_vars[i] for i in deposit]) == 1.0, "PercentagesSum"
prob += lpSum([Protein[i] * deposit_vars[i] for i in deposit]) >= 17.2, "ProteinRequirement"
prob += lpSum([Grass[i] * deposit_vars[i] for i in deposit]) >= 11.8, "FatRequirement"
prob.writeLP("WhiskasModel.lp")
prob.solve()
# The status of the solution is printed to the screen
print ("Status:", LpStatus[prob.status])
# Each of the variables is printed with it's resolved optimum value
for v in prob.variables():
print (v.name, "=", v.varValue)
# The optimised objective function value is printed to the screen
print ("Total Cost of Ingredients per can = ", value(prob.objective))
这部分很正常,但我需要再添加一个禁忌素,即我要生产多少公斤。
我试图做这两个约束:
## total KG produced == 14
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"
### Can´t not use more that 8KG from deposit 1
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit[0:1]]) <= 8, "KGRequirement1"
我收到此错误:
Status: Infeasible
Ingr_f1 = 0.83636364
Ingr_f2 = 0.11818182
Ingr_f3 = 0.045454545
Ingr_f4 = 0.0
Total Cost of Ingredients per can = 14.30000007
但是使用存款4来满足此要求是可行的,因此我认为约束不正确。
我意识到约束量百分比是错误的,实际上我只需要添加我要生产的量的约束量即可。
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"
成分的平均含量也满足该条件。
prob += lpSum([Protein[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 17.2*14, "ProteinRequirement"
现在这是严格的禁忌症:
prob += lpSum([Protein[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 17.2*14, "ProteinRequirement"
prob += lpSum([Grass[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 11.8*14, "FatRequirement"
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit[0:1]]) <= 8, "KGRequirement1"
答案 0 :(得分:1)
我意识到约束量百分比是错误的,实际上我只需要添加我要生产的量的约束量即可。
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"
成分的平均含量也满足了这一要求。
prob += lpSum([Protein[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 17.2*14, "ProteinRequirement"
这是严格的禁忌症:
prob += lpSum([Protein[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 17.2*14, "ProteinRequirement"
prob += lpSum([Grass[i] *KG[i] * deposit_vars[i] for i in deposit]) >= 11.8*14, "FatRequirement"
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit]) == 14, "KGRequirement"
prob += lpSum([KG[i] * deposit_vars[i] for i in deposit[0:1]]) <= 8, "KGRequirement1"