Check Packages
if(!require('lpSolveAPI')){install.packages('lpSolveAPI')}
## Loading required package: lpSolveAPI
if(!require('dplyr')){install.packages('dplyr')}
## Loading required package: dplyr
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
Set working directory Read and combine data Rename variables
#Set working directory
getwd()
## [1] "/Users/valenlizardo/Desktop/MLizardoRStudio files"
setwd("~/Desktop/MLizardoRStudio files")
#Read in data
nutrition=read.csv("nutrition.csv")
prices=read.csv("prices.csv")
#Combine data
diet=cbind(nutrition,prices)
#Rename variables
colnames(diet)
## [1] "Food" "Portion.Size"
## [3] "Energy..kcal." "Protein..g."
## [5] "Calcium..mg." "Price..cents.per.portion."
## [7] "Limit..portions.per.day."
colnames(diet)=c("Food","Portion","Energy","Protein","Calcium","Price","Limit")
colnames(diet)
## [1] "Food" "Portion" "Energy" "Protein" "Calcium" "Price" "Limit"
glimpse(diet)
## Observations: 7
## Variables: 7
## $ Food <fct> Beans, Chicken, Cookies, Eggs, Milk, Oats,
## $ Portion <fct> 260 g, 100 g, 3, 2, 237 cc, 28 g,
## $ Energy <int> 260, 205, 420, 160, 160, 110, NA
## $ Protein <fct> 14, 32, 4, 13, 8, 4, .
## $ Calcium <int> 80, 12, 22, 54, 285, 2, NA
## $ Price <int> 60, 240, 200, 130, 90, 30, NA
## $ Limit <fct> 2, 3, 2, 2, 8, 4, .
Converting variable types
#Convert variable types
diet$Protein <- as.numeric(as.character(diet$Protein))
## Warning: NAs introduced by coercion
diet$Limit <- as.numeric(as.character(diet$Limit))
## Warning: NAs introduced by coercion
glimpse(diet)
## Observations: 7
## Variables: 7
## $ Food <fct> Beans, Chicken, Cookies, Eggs, Milk, Oats,
## $ Portion <fct> 260 g, 100 g, 3, 2, 237 cc, 28 g,
## $ Energy <int> 260, 205, 420, 160, 160, 110, NA
## $ Protein <dbl> 14, 32, 4, 13, 8, 4, NA
## $ Calcium <int> 80, 12, 22, 54, 285, 2, NA
## $ Price <int> 60, 240, 200, 130, 90, 30, NA
## $ Limit <dbl> 2, 3, 2, 2, 8, 4, NA
#Remove blank row
diet=diet[1:6,]
diet
## Food Portion Energy Protein Calcium Price Limit
## 1 Beans 260 g 260 14 80 60 2
## 2 Chicken 100 g 205 32 12 240 3
## 3 Cookies 3 420 4 22 200 2
## 4 Eggs 2 160 13 54 130 2
## 5 Milk 237 cc 160 8 285 90 8
## 6 Oats 28 g 110 4 2 30 4
# set up lp
lp <- make.lp(0, 6)
# objective function coef
set.objfn(lp, diet$Price)
### constraints
# >=2000 calories
add.constraint(lp, diet$Energy, ">=", 2000)
# >=55 gms of protein
add.constraint(lp, diet$Protein, ">=", 55)
# >= 800 mg of calcium
add.constraint(lp, diet$Calcium, ">=", 800)
dimnames(lp)
## [[1]]
## [1] "R1" "R2" "R3"
##
## [[2]]
## [1] "C1" "C2" "C3" "C4" "C5" "C6"
# re-naming dimnames of lp object
dimnames(lp) <- list(c('Energy', 'Protein', 'Calcium'), diet$Food)
dimnames(lp)
## [[1]]
## [1] "Energy" "Protein" "Calcium"
##
## [[2]]
## [1] "Beans" "Chicken" "Cookies" "Eggs" "Milk" "Oats"
lp
## Model name:
## Beans Chicken Cookies Eggs Milk Oats
## Minimize 60 240 200 130 90 30
## Energy 260 205 420 160 160 110 >= 2000
## Protein 14 32 4 13 8 4 >= 55
## Calcium 80 12 22 54 285 2 >= 800
## Kind Std Std Std Std Std Std
## Type Real Real Real Real Real Real
## Upper Inf Inf Inf Inf Inf Inf
## Lower 0 0 0 0 0 0
solve(lp)
## [1] 0
Minimum price devided by 100, because we want the answer in cents
get.objective(lp)/100
## [1] 5.030995
print(paste(dimnames(lp)[[2]], ": ", get.variables(lp)))
## [1] "Beans : 7.21044045676998" "Chicken : 0"
## [3] "Cookies : 0" "Eggs : 0"
## [5] "Milk : 0.783034257748777" "Oats : 0"
print(paste(dimnames(lp)[[1]], ": ", get.constraints(lp)))
## [1] "Energy : 2000" "Protein : 107.21044045677"
## [3] "Calcium : 800"
Unlimited Diet 7.21 servings beans, 0.78 servings milk Nutritional Content 2,000 calories 55 grams protein 800 mg calcium Cost $5.03
diet[,c(1, 7)]
## Food Limit
## 1 Beans 2
## 2 Chicken 3
## 3 Cookies 2
## 4 Eggs 2
## 5 Milk 8
## 6 Oats 4
# set up lp
lp <- make.lp(0, 6)
# objective function coef
set.objfn(lp, diet$Price)
# add constraints
add.constraint(lp, diet$Energy, ">=", 2000)
add.constraint(lp, diet$Protein, ">=", 55)
add.constraint(lp, diet$Calcium, ">=", 800)
# setting the upper bounds using the limits
set.bounds(lp, upper = diet$Limit)
dimnames(lp) <- list(c('Energy', 'Protein', 'Calcium'), diet$Food)
dimnames(lp)
## [[1]]
## [1] "Energy" "Protein" "Calcium"
##
## [[2]]
## [1] "Beans" "Chicken" "Cookies" "Eggs" "Milk" "Oats"
lp
## Model name:
## Beans Chicken Cookies Eggs Milk Oats
## Minimize 60 240 200 130 90 30
## Energy 260 205 420 160 160 110 >= 2000
## Protein 14 32 4 13 8 4 >= 55
## Calcium 80 12 22 54 285 2 >= 800
## Kind Std Std Std Std Std Std
## Type Real Real Real Real Real Real
## Upper 2 3 2 2 8 4
## Lower 0 0 0 0 0 0
solve(lp)
## [1] 0
get.objective(lp)/100
## [1] 7.640695
print(paste(dimnames(lp)[[2]], ": ", get.variables(lp)))
## [1] "Beans : 2" "Chicken : 0"
## [3] "Cookies : 1.68084007574453" "Eggs : 0"
## [5] "Milk : 2.0877948011706" "Oats : 4"
print(paste(dimnames(lp)[[1]], ": ", get.constraints(lp)))
## [1] "Energy : 2000" "Protein : 67.4257187123429"
## [3] "Calcium : 800"
Limited Diet 2.0 servings beans, 1.68 servings cookies, 2.08 servings milk, 4 servings oats Nutritional Content 2,000 calories 55 grams protein 1345 mg calcium Cost $7.64