Skip to content

Instantly share code, notes, and snippets.

@teldridge11
Created November 12, 2016 15:34
Show Gist options
  • Save teldridge11/e12dd3b1f7dc8bd3a8d0f81293054652 to your computer and use it in GitHub Desktop.
Save teldridge11/e12dd3b1f7dc8bd3a8d0f81293054652 to your computer and use it in GitHub Desktop.
Problem 2
Part A:
i.
MINIMIZE e
to = 21e + 0.85p + 0.33f + 4.64c + 9s + 1cs
le = 16e + 1.62p + 0.2f + 2.37c + 28s + 0.75cs
sp = 40e + 2.86p + 0.39f + 3.63c + 65s + 0.50cs
ca = 41e + 0.93p + 0.24f + 9.58c + 69.0s + 0.50cs
ss = 585e + 23.4p + 48.7f + 15c + 3.80s + 0.45cs
st = 120e + 16p + 5f + 3c + 120s + 2.15cs
cp = 164e + 9p + 2.6f + 27c + 78s + 0.95cs
ol = 884e + 0p + 100f + 0c + 0s + 2.00cs
ST
p >= 15
8 >= f >= 2
c >= 4
s <= 200
le+sp/to+le+ca+ss+st+cp+ol >= 0.40
ii.
from pulp import *
# Creates a list of the Ingredients
Ingredients = ['TOMATO', 'LETTUCE', 'SPINACH', 'CARROT', 'SUNFLOWER', 'TOFU', 'CHICKPEA', 'OIL']
kcal = {'TOMATO': 21,
'LETTUCE': 16,
'SPINACH': 40,
'CARROT': 41,
'SUNFLOWER': 585,
'TOFU': 120,
'CHICKPEA': 164,
'OIL': 884}
protein = {'TOMATO': 0.85,
'LETTUCE': 1.62,
'SPINACH': 2.86,
'CARROT': 0.93,
'SUNFLOWER': 23.4,
'TOFU': 16,
'CHICKPEA': 9,
'OIL': 0}
fat = {'TOMATO': 0.33,
'LETTUCE': 0.20,
'SPINACH': 0.39,
'CARROT': 0.24,
'SUNFLOWER': 48.7,
'TOFU': 5.0,
'CHICKPEA': 2.6,
'OIL': 100.0}
carbs = {'TOMATO': 4.64,
'LETTUCE': 2.37,
'SPINACH': 3.63,
'CARROT': 9.58,
'SUNFLOWER': 15.0,
'TOFU': 3.0,
'CHICKPEA': 27.0,
'OIL': 0.0}
sodium = {'TOMATO': 9.0,
'LETTUCE': 28.0,
'SPINACH': 65.0,
'CARROT': 69.0,
'SUNFLOWER': 3.80,
'TOFU': 120.0,
'CHICKPEA': 78.0,
'OIL': 0.0}
cost = {'TOMATO': 1.0,
'LETTUCE': 0.75,
'SPINACH': 0.50,
'CARROT': 0.50,
'SUNFLOWER': 0.45,
'TOFU': 2.15,
'CHICKPEA': 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 += ingredient_vars['LETTUCE'] + ingredient_vars['SPINACH'] >= (0.4 * pulp.lpSum([ingredient_vars[i] for i in Ingredients]))
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))
Status: Optimal
Ingr_CARROT = 0.0
Ingr_CHICKPEA = 0.0
Ingr_LETTUCE = 0.58548009
Ingr_OIL = 0.0
Ingr_SPINACH = 0.0
Ingr_SUNFLOWER = 0.0
Ingr_TOFU = 0.87822014
Ingr_TOMATO = 0.0
Total kCal of Ingredients per salad = 114.75409824
iii.
Cost: $2.3272833685
Part B
i.
MINIMIZE cs
to = 21e + 0.85p + 0.33f + 4.64c + 9s + 1cs
le = 16e + 1.62p + 0.2f + 2.37c + 28s + 0.75cs
sp = 40e + 2.86p + 0.39f + 3.63c + 65s + 0.50cs
ca = 41e + 0.93p + 0.24f + 9.58c + 69.0s + 0.50cs
ss = 585e + 23.4p + 48.7f + 15c + 3.80s + 0.45cs
st = 120e + 16p + 5f + 3c + 120s + 2.15cs
cp = 164e + 9p + 2.6f + 27c + 78s + 0.95cs
ol = 884e + 0p + 100f + 0c + 0s + 2.00cs
ST
p >= 15
8 >= f >= 2
c >= 4
s <= 200
le+sp/to+le+ca+ss+st+cp+ol >= 0.40
ii.
from pulp import *
# Creates a list of the Ingredients
Ingredients = ['TOMATO', 'LETTUCE', 'SPINACH', 'CARROT', 'SUNFLOWER', 'TOFU', 'CHICKPEA', 'OIL']
kcal = {'TOMATO': 21,
'LETTUCE': 16,
'SPINACH': 40,
'CARROT': 41,
'SUNFLOWER': 585,
'TOFU': 120,
'CHICKPEA': 164,
'OIL': 884}
protein = {'TOMATO': 0.85,
'LETTUCE': 1.62,
'SPINACH': 2.86,
'CARROT': 0.93,
'SUNFLOWER': 23.4,
'TOFU': 16,
'CHICKPEA': 9,
'OIL': 0}
fat = {'TOMATO': 0.33,
'LETTUCE': 0.20,
'SPINACH': 0.39,
'CARROT': 0.24,
'SUNFLOWER': 48.7,
'TOFU': 5.0,
'CHICKPEA': 2.6,
'OIL': 100.0}
carbs = {'TOMATO': 4.64,
'LETTUCE': 2.37,
'SPINACH': 3.63,
'CARROT': 9.58,
'SUNFLOWER': 15.0,
'TOFU': 3.0,
'CHICKPEA': 27.0,
'OIL': 0.0}
sodium = {'TOMATO': 9.0,
'LETTUCE': 28.0,
'SPINACH': 65.0,
'CARROT': 69.0,
'SUNFLOWER': 3.80,
'TOFU': 120.0,
'CHICKPEA': 78.0,
'OIL': 0.0}
cost = {'TOMATO': 1.0,
'LETTUCE': 0.75,
'SPINACH': 0.50,
'CARROT': 0.50,
'SUNFLOWER': 0.45,
'TOFU': 2.15,
'CHICKPEA': 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([cost[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 += ingredient_vars['LETTUCE'] + ingredient_vars['SPINACH'] >= (0.4 * pulp.lpSum([ingredient_vars[i] for i in Ingredients]))
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))
Status: Optimal
Ingr_CARROT = 0.0
Ingr_CHICKPEA = 0.0
Ingr_LETTUCE = 0.0
Ingr_OIL = 0.0
Ingr_SPINACH = 0.39515279
Ingr_SUNFLOWER = 0.59272919
Ingr_TOFU = 0.0
Ingr_TOMATO = 0.0
Total cost of Ingredients per salad = 0.4643045305
iii.
Calories: 362.55268775
Part C
i.
Status: Optimal
Ingr_CARROT = 0.0
Ingr_CHICKPEA = 0.0
Ingr_LETTUCE = 0.0
Ingr_OIL = 0.0
Ingr_SPINACH = 0.47251402
Ingr_SUNFLOWER = 0.31192885
Ingr_TOFU = 0.39684217
Ingr_TOMATO = 0.0
Total cost of Ingredients per salad = 1.2298356579999998
Total kcal of Ingredients per salad = 248.99999845
Status: Near Optimal
Ingr_CARROT = 0.0
Ingr_CHICKPEA = 0.0
Ingr_LETTUCE = 0.0
Ingr_OIL = 0.0
Ingr_SPINACH = 0.55034345
Ingr_SUNFLOWER = 0.029429036
Ingr_TOFU = 0.79608614
Ingr_TOMATO = 0.0
Total cost of Ingredients per salad = 1.9999999922
Total kcal of Ingredients per salad = 134.76006086
ii.
I would choose to sell the first salad, with 0.47251402 spinach, 0.31192885 sunflower seeds, and 0.39684217 smoked tofu. This salad has a total cost of $1.23 and 249 calories.
iii.
I chose this salad because it minimized the cost of the salad while still fulfilling the nutritional constraints as well as being under 250 calories. Caloric reduction likely has diminishing returns on sales once under 250 calories, but additional profit never has diminishing returns. So I decided that once the constraints were met, I wanted the cheapest possible salad. If sold for $5.00, this salad gives a profit of $3.77, which is a profit margin of 75.4%.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment