R for Industrial Engineers

Operations Research with R — Diet Problem

Operations Research Miscellaneous Applications

Roberto Salazar
Nov 28, 2020 · 4 min read
Image for post
Image for post
Image by Brooke Lark available at Unsplash

Operations Research

Diet Problem

Image for post
Image for post
Image for post
Image for post
Image for post
Image for post
# 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
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

Concluding Thoughts

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Sign up for Analytics Vidhya News Bytes

By Analytics Vidhya

Latest news from Analytics Vidhya on our Hackathons and some of our best articles! Take a look

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

Roberto Salazar

Written by

Industrial and Systems Engineer | LinkedIn: linkedin.com/in/roberto-salazar-reyna/

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Roberto Salazar

Written by

Industrial and Systems Engineer | LinkedIn: linkedin.com/in/roberto-salazar-reyna/

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

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