import pulp
from pulp import *
prob = LpProblem("The Whiskas Problem",LpMinimize)
LpVariable("example", None, 100)
example
x1=LpVariable("ChickenPercent",0,None,LpInteger)
x2=LpVariable("BeefPercent",0)
prob += 0.013*x1 + 0.008*x2, "Total Cost of Ingredients per can"
prob += x1 + x2 == 100, "PercentagesSum"
prob += 0.100*x1 + 0.200*x2 >= 8.0, "ProteinRequirement"
prob += 0.080*x1 + 0.100*x2 >= 6.0, "FatRequirement"
prob += 0.001*x1 + 0.005*x2 <= 2.0, "FibreRequirement"
prob += 0.002*x1 + 0.005*x2 <= 0.4, "SaltRequirement"
prob.writeLP("WhiskasModel.lp")
prob.solve()
1
print("Status:", LpStatus[prob.status])
Status: Optimal
for v in prob.variables():
print(v.name, "=", v.varValue)
BeefPercent = 66.0
ChickenPercent = 34.0
print("Total Cost of Ingredients per can = ", value(prob.objective))
Total Cost of Ingredients per can = 0.97
#The 2 outputs above tell us that chicken makes up 33.33% and Beef makes up 66.67%. The total cost of ingredients per can is 96 cents.
#list of ingredients:
Ingredients = ['CHICKEN', 'BEEF', 'MUTTON', 'RICE', 'WHEAT', 'GEL']
#cost of each of the ingredients:
costs = {'CHICKEN': 0.013,
'BEEF': 0.008,
'MUTTON': 0.010,
'RICE': 0.002,
'WHEAT': 0.005,
'GEL': 0.001}
#percentage of protein in each of the ingredients:
proteinPercent = {'CHICKEN': 0.100,
'BEEF': 0.200,
'MUTTON': 0.150,
'RICE': 0.000,
'WHEAT': 0.040,
'GEL': 0.000}
#percentage of fat in each of the ingredients:
fatPercent = {'CHICKEN': 0.080,
'BEEF': 0.100,
'MUTTON': 0.110,
'RICE': 0.010,
'WHEAT': 0.010,
'GEL': 0.000}
#percentage of fiber in each of the ingredients:
fibrePercent = {'CHICKEN': 0.001,
'BEEF': 0.005,
'MUTTON': 0.003,
'RICE': 0.100,
'WHEAT': 0.150,
'GEL': 0.000}
#percentage of salt in each of the ingredients:
saltPercent = {'CHICKEN': 0.002,
'BEEF': 0.005,
'MUTTON': 0.007,
'RICE': 0.002,
'WHEAT': 0.008,
'GEL': 0.000}
prob = LpProblem("The Whiskas Problem", LpMinimize)
ingredient_vars = LpVariable.dicts("Ingr",Ingredients,0)
#The objective function of the problem:
prob += lpSum([costs[i]*ingredient_vars[i] for i in Ingredients]), "Total Cost of Ingredients per can"
# The five constraints of the problem:
prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 100, "PercentagesSum"
prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 8.0, "ProteinRequirement"
prob += lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 6.0, "FatRequirement"
prob += lpSum([fibrePercent[i] * ingredient_vars[i] for i in Ingredients]) <= 2.0, "FiberRequirement"
prob += lpSum([saltPercent[i] * ingredient_vars[i] for i in Ingredients]) <= 0.4, "SaltRequirement"
prob.writeLP("WhiskasModel.lp")
prob.solve()
1
print("Status:", LpStatus[prob.status])
Status: Optimal
for v in prob.variables():
print(v.name, "=", v.varValue)
Ingr_BEEF = 60.0
Ingr_CHICKEN = 0.0
Ingr_GEL = 40.0
Ingr_MUTTON = 0.0
Ingr_RICE = 0.0
Ingr_WHEAT = 0.0
print("Total Cost of Ingredients per can = ", value(prob.objective))
Total Cost of Ingredients per can = 0.52
#The 2 outputs above tell us that the optimal solution is 60% Beef and 40% Gel. The total cost of ingredients per can is 52 cents.