We all have different sets of goal in our life, and most of the time, there are different approaches to achieve these goals. One of such important goal is to have a balanced diet, and it has additional constraint of the tight budget in this pandemic.
In this article, I will discuss the way we can model our diet requirement with different constraints like budget, energy and fat requirements etc. and get the most optimised diet plan recommendation.
Let us assume that I like to have meat, eggs, broccoli, milk, apple, rice and potatoes. Below table shows the calorie and nutrient content of these food items.
Objective: I would like to have the most economical lunch to get a minimum of 1100 calories energy, less than 30 gm fat, less than 80 gm of carbohydrate, less than 5 gm of sugar.
We will PuLP to solve our diet optimisation goal. PuLP is an LP modeller written in Python.
from pulp import *
We aim to have a balanced diet lunch most economically, hence it is LpMinimize optimisation problem.
To model problem, we will define each food items I like as a variable. As we cannot have negative apples or eggs etc. hence lowBound is mentioned as zero. Also, the restaurant doesn’t allow to order half egg or three-quarter apple etc. hence we have put a constraint that the solution should be an integer.
meat=LpVariable("Meat", lowBound=0, cat="Integer")
broccoli=LpVariable("Broccoli", lowBound=0, cat="Integer")
milk=LpVariable("Milk", lowBound=0, cat="Integer")
apple=LpVariable("Apple", lowBound=0, cat="Integer")
rice=LpVariable("Rice", lowBound=0, cat="Integer")
potato=LpVariable("Potato", lowBound=0, cat="Integer")
egg=LpVariable("Egg", lowBound=0, cat="Integer")
As our objective is to have the most economical lunch and within all the dietary constraints hence first we will append the objective function i.e. price of each food item multiplied by the number of food items to the model.
Next, we will append the dietary constraints to the model like the minimum energy requirement of 1100 calories, less than 30 gms of fats etc.
prob +=0*meat+7*broccoli+5*milk+14*apple+28*rice+17*potato+1.1*egg <=80
After the modelling is completed, we will initiate the optimisation with the solve () method.
In the below code, the objective function i.e. meal price is rounded to two decimal places.
print("Balanced Lunch can be done with £ ", round(value(prob.objective),2))
To order a meal satisfying all the dietary requirement we need to spend a minimum of £ 18.3.
In the below code we are checking the values of each decision variable to get the itemised menu which we need to order.
print("Portion of Meat is ",value(meat))
print("Portion of Broccoli is ",value(broccoli))
print("Portion of Milk is ",value(milk))
print("Portion of Apple is ",value(apple))
print("Portion of Rice is ",value(rice))
print("Portion of Potato is ",value(potato))
print("Portion of Egg is ",value(egg))
It appears that five portions of meat ( we consider each portion of 100 gm), two portions of rice and one egg is the most economical order to meet the dietary requirement we mentioned earlier.
The parameter which we want to optimise is the objective function. In the scenario discussed in this article, our goal was to order a balanced diet most economically. If we want to optimise profit then naturally we want to maximise it as much possible, and we build the model with LpMaximize.
The values of the parameters which we need to set to meet the objective functions are known as decision variables. In the scenario discussed in this article, it is the number of food items to order.
Constraints, as the name suggests, are the restrictions which need to respect while finding the most optimal solution.
We can scale the model illustrated in this article to include everyday grocery we buy, prices and daily recommended nutrient requirements, constraints as discussed, and then use optimisation solver to get optimal shopping recommendation.
I hope this article provided you with an approach to shop and order food to meet the dietary requirement most economically from a budgetary standpoint.