R for Industrial Engineers
Operations Research with R — Diet Problem
Operations Research Miscellaneous Applications

Operations Research
Operations Research is a scientific approach for decision making that seeks for the best design and operation of a system, usually under conditions requiring the allocation of scarce or limited resources. The scientific approach for decision making requires the use of one or more mathematical optimization models (i.e. representations of the actual situation) to make the optimum decision.
Diet Problem
The diet problem is one of the first and most common studied optimization problems. The goal of the diet problem is to determine the number of servings of food to consume (or purchase) so as to minimize the total cost of the food while meeting specific nutritional requirements previously provided.
Interested in learning more about the fundamentals of operations research and linear programming? Feel free to check out the following article:
The lpSolve package from R contains several functions for solving integer programing problems. For the following example, let’s consider the following case related with a diet problem:
The cost and nutrition values per unit of different foods are listed on the table below:

A nutritionist is interested in obtaining the minimum-cost diet for six different foods while satisfying the following requirements:
- The diet must contain at least 300 calories
- The diet must contain no more than 10 grams of protein
- The diet must contain no less than 10 grams of carbohydrates
- The diet must contain no less than 8 grams of fat
- The diet must contain at least 0.5 unit of fish
- The diet must contain at least 1 unit of milk
Questions to be answered:
- What combination of food units will minimize the diet cost?
- Which is the minimum expected cost that can be achieved?
Decision Variables:

Problem Formulation:

R Code:
# Import lpSolve package
library(lpSolve) # Set coefficients of the objective function
f.obj <- c(2, 3.5, 8, 1.5, 11, 1) # Set matrix corresponding to coefficients of constraints by rows
f.con <- matrix(c(90.0, 120.0, 106.0, 97.0, 130.0, 180.0, # number of calories 4.0, 8.0, 7.0, 1.3, 8.0, 9.2, # total grams of proteins 15.0, 11.7, 0.4, 22.6, 0.0, 17.0, # total grams of carbohydrates 1.0, 5.0, 9.0, 0.1, 7.0, 1.0, # total grams of fat 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, # units of fish 0.0, 1.0, 0.0, 0.0, 0.0, 0.0), # units of milk nrow = 6, byrow = TRUE) # Set unequality/equality signs
f.dir <- c(“>=”, “<=”, “>=”, “>=”, “>=”, “<=”) # Set right hand side coefficients
f.rhs <- c(300.0, 10.0, 10.0, 8.0, 0.5, 1.0) # Final value (z)
lp(“min”, f.obj, f.con, f.dir, f.rhs) # Variables final values
lp(“min”, f.obj, f.con, f.dir, f.rhs)$solution
Solution:
Success: the objective function is 12.08134
x1 x2 x3 x4 x5 x6
0.00000000 0.05359877 0.44949882 1.86516776 0.50000000 0.00000000
According to the results, the minimum diet cost that could be obtained is ~ $12.08 by selecting ~0.536 units of milk, ~ 0.449 units of cheese, ~ 1.865 units of potato and 0.5 units of fish.
Concluding Thoughts
Diet problems can be optimized through operation research algorithms to minimize their expected total cost. The lpSolve R package allows solving linear programming and optimization problems of multiple applications with just a few lines of code.
The diet problem represents one of the most trivial linear programming problems and is often one of the first optimization applications taught to engineers learning operations research. Engineers must have reliable and robust decision support systems and use efficient decision making tools to obtain the best (i.e. optimum) outcomes, such as R.
— —
If you found this article useful, feel welcome to download my personal codes on GitHub. You can also email me directly at rsalaza4@binghamton.edu and find me on LinkedIn. Interested in learning more about data analytics, data science and machine learning applications in the engineering field? Explore my previous articles by visiting my Medium profile. Thanks for reading.
- Robert