Created
November 11, 2016 18:49
-
-
Save teldridge11/616deaa680b696b85d71936404f693ec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pulp import * | |
# Creates a list of the Ingredients | |
Ingredients = ['TOMATO', 'LETTUCE', 'SPINACH', 'CARROT', 'SUNFLOWER', 'TOFU', 'CHICKPEAS', 'OIL'] | |
kcal = {'TOMATO': 21, | |
'LETTUCE': 16, | |
'SPINACH': 40, | |
'CARROT': 41, | |
'SUNFLOWER': 585, | |
'TOFU': 120, | |
'CHICKPEAS': 164, | |
'OIL': 884} | |
protein = {'TOMATO': 0.85, | |
'LETTUCE': 1.62, | |
'SPINACH': 2.86, | |
'CARROT': 0.93, | |
'SUNFLOWER': 23.4, | |
'TOFU': 16, | |
'CHICKPEAS': 9, | |
'OIL': 0} | |
fat = {'TOMATO': 0.33, | |
'LETTUCE': 0.20, | |
'SPINACH': 0.39, | |
'CARROT': 0.24, | |
'SUNFLOWER': 48.7, | |
'TOFU': 5.0, | |
'CHICKPEAS': 2.6, | |
'OIL': 100.0} | |
carbs = {'TOMATO': 4.64, | |
'LETTUCE': 2.37, | |
'SPINACH': 3.63, | |
'CARROT': 9.58, | |
'SUNFLOWER': 15.0, | |
'TOFU': 3.0, | |
'CHICKPEAS': 27.0, | |
'OIL': 0.0} | |
sodium = {'TOMATO': 9.0, | |
'LETTUCE': 28.0, | |
'SPINACH': 65.0, | |
'CARROT': 69.0, | |
'SUNFLOWER': 3.80, | |
'TOFU': 120.0, | |
'CHICKPEAS': 78.0, | |
'OIL': 0.0} | |
cost = {'TOMATO': 1.0, | |
'LETTUCE': 0.75, | |
'SPINACH': 0.50, | |
'CARROT': 0.50, | |
'SUNFLOWER': 0.45, | |
'TOFU': 2.15, | |
'CHICKPEAS': 0.95, | |
'OIL': 2.00} | |
# Create the 'prob' variable to contain the problem data | |
prob = LpProblem("The Salad Problem", LpMinimize) | |
# A dictionary called 'ingredient_vars' is created to contain the referenced Variables | |
ingredient_vars = LpVariable.dicts("Ingr",Ingredients,0) | |
# The objective function is added to 'prob' first | |
prob += lpSum([kcal[i]*ingredient_vars[i] for i in Ingredients]), "Total kCal of Ingredients per salad" | |
# The constraints are added to 'prob' | |
prob += lpSum([protein[i] * ingredient_vars[i] for i in Ingredients]) >= 15.0, "ProteinRequirement" | |
prob += 8.0 >= lpSum([fat[i] * ingredient_vars[i] for i in Ingredients]) >= 2.0, "FatRequirement" | |
prob += lpSum([carbs[i] * ingredient_vars[i] for i in Ingredients]) >= 4.0, "CarbRequirement" | |
prob += lpSum([sodium[i] * ingredient_vars[i] for i in Ingredients]) <= 200.0, "SodiumRequirement" | |
prob += lpSum([lettuce[i] * ingredient_vars[i] + spinach[i] * ingredient_vars[i] for i in Ingredients]) / lpSum([lettuce[i] * ingredient_vars[i] + spinach[i] * ingredient_vars[i] + [tomato[i] * ingredient_vars[i] + carrot[i] * ingredient_vars[i] + sunflower[i] * ingredient_vars[i] + tofu[i] * ingredient_vars[i] + chickpeas[i] * ingredient_vars[i] + oil[i] * ingredient_vars[i] for i in Ingredients]) >= 0.40, "GreensRequirement" | |
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 kCal of Ingredients per salad = ", value(prob.objective)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment