Inside AI

Optimising Diet Plan In Tight Budget-Modelling In Python

A balanced recommended diet with the constraint of the tight budget in this pandemic is a challenge. It can be addressed to some extent with linear optimisation model

Photo by on

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.

Sample Nutrition Data Used In the Scenario Discussed ( Photo By Author)

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.

prob=LpProblem("Diet", LpMinimize)

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.

155*egg >=1100
prob+=3.5*meat+0.4*broccoli+1*milk+0.2*apple+0.3*rice+0.1*potato+11*egg <=30
prob +=0*meat+7*broccoli+5*milk+14*apple+28*rice+17*potato+1.1*egg <=80
1.1*egg <=5

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.

Cost of Lunch For Optimised Meal —Output of the code discussed in this article ( photo by author)

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.

Itemised most economical meal portion — Output Of The Code Discussed In the Program( photo by author)

Key Takeaways

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.

Kaushik Choudhury is an experienced Supply Chain Strategy and Digital Transformation manager in one of the Big 4 Consulting firm in the UK.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store