



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Question
Methodology + Code
#install packages install.packages("readxl") install.packages("lpSolve")
#run library(readxl) library(lpSolve)
diet <- read_excel("diet.xls")
food_data <- diet[!is.na(diet$Foods), ]
req_data <- diet[diet$Serving Size %in% c("Minimum daily intake", "Maximum daily intake"), ]
nutrients <- c( "Calories", "Cholesterol mg", "Total_Fat g", "Sodium mg", "Carbohydrates g", "Dietary_Fiber g", "Protein g", "Vit_A IU", "Vit_C IU", "Calcium mg", "Iron mg" )
#Set food names and number of foods foods <- food_data$Foods
n_foods <- length(foods)
cost <- food_data$Price/ Serving
A <- as.matrix(food_data[, nutrients])
min_req <- as.numeric(req_data[req_data$Serving Size == "Minimum daily intake", nutrients]) max_req <- as.numeric(req_data[req_data$Serving Size == "Maximum daily intake", nutrients])
obj <- cost
#To set the A matrix as transposed, and done twice because it requires min and max const.mat <- rbind(t(A), t(A))
#This tells the solver that the first 11 rows above are min, and second max const.dir <- c(rep(">=", length(nutrients)), rep("<=", length(nutrients)))
#setting the constraints for the function const.rhs <- c(min_req, max_req)
solution1 <- lp( direction = "min", objective.in = obj, const.mat = const.mat, const.dir = const.dir, const.rhs = const.rhs )
selected_lps <- data.frame( Food = foods, Servings = solution1$solution )
selected_lps <- selected1[selected1$Servings, ] print(selected_lps)
nutrition_lower <- cbind(t(A), matrix(0, nrow = length(nutrients), ncol = n_foods)) nutrition_upper <- cbind(t(A), matrix(0, nrow = length(nutrients), ncol = n_foods))
link_lower <- cbind(diag(n_foods), -0.1 * diag(n_foods)) # x_i - 0.1 y_i >= 0 link_upper <- cbind(diag(n_foods), -1000 * diag(n_foods)) # x_i - 1000 y_i <= 0
#returns the row position of the food list celery_idx <- which(foods == "Celery, Raw") broccoli_idx <- which(foods == "Frozen Broccoli")
#This part sets the y_i value to 1 if selected celery_broccoli <- rep(0, n_vars) celery_broccoli[n_foods + celery_idx] <- 1 celery_broccoli[n_foods + broccoli_idx] <- 1
#Set the values that are protein protein_foods <- c( "Roasted Chicken", "Poached Eggs", "Scrambled Eggs", "Bologna,Turkey", "Frankfurter, Beef", "Ham,Sliced,Extralean", "Kielbasa,Prk", "Pork", "Sardines in Oil", "White Tuna in Water" )
protein_idx <- which(foods %in% protein_foods)
#Gives value 1 to where protein meal is selected protein_constraint <- rep(0, n_vars) protein_constraint[n_foods + protein_idx] <- 1
Then LInk all the constraints together
#The constraint values const.mat <- rbind( nutrition_lower, nutrition_upper, link_lower, link_upper, celery_broccoli, protein_constraint )
#The constraint directions const.dir <- c( rep(">=", length(nutrients)), # nutrition minimums rep("<=", length(nutrients)), # nutrition maximums rep(">=", n_foods), # x_i - 0.1 y_i >= 0 rep("<=", n_foods), # x_i - M y_i <= 0 "<=", # celery + broccoli <= 1 ">=" # at least 3 protein foods )
#The constraint directions values const.rhs <- c( min_req, # nutrition minimums max_req, # nutrition maximums rep(0, n_foods), # x_i - 0.1 y_i >= 0 rep(0, n_foods), # x_i - M y_i <= 0 1, # celery + broccoli <= 1 3 # at least 3 protein foods )
And run the solver function and print results
binary_positions <- (n_foods + 1):(2 * n_foods)
solution2 <- lp( direction = "min", objective.in = obj2, const.mat = const.mat, const.dir = const.dir, const.rhs = const.rhs, binary.vec = binary_positions )
#For servings x_sol <- solution2$solution[1:n_foods] #for binary choices y_sol <- solution2$solution[(n_foods + 1):(2 * n_foods)]