# Operations Research with R — Diet Problem

## Operations Research Miscellaneous Applications

Nov 28, 2020 · 4 min read

# Diet Problem

`# Import lpSolve packagelibrary(lpSolve) # Set coefficients of the objective functionf.obj <- c(2, 3.5, 8, 1.5, 11, 1) # Set matrix corresponding to coefficients of constraints by rowsf.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 signsf.dir <- c(“>=”, “<=”, “>=”, “>=”, “>=”, “<=”) # Set right hand side coefficientsf.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 valueslp(“min”, f.obj, f.con, f.dir, f.rhs)\$solution`
`Success: the objective function is 12.08134         x1         x2         x3         x4         x5         x60.00000000 0.05359877 0.44949882 1.86516776 0.50000000 0.00000000`

# Concluding Thoughts

## Analytics Vidhya

### By Analytics Vidhya

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

Written by

Written by