A pig farmer needs to work out the cheapest combination feed for his animals. He has access to three types of foodstuff, Corn, Grunge, and Alfalfa. The nutrient content of these (grams per kilogram) and cost (cents per kilogram) are detailed below.
Corn | Grunge | Alfalfa | |
Carbs | 90 | 20 | 40 |
Protein | 30 | 80 | 30 |
Vitamins | 10 | 20 | 60 |
Cost (c) | 84 | 92 | 60 |
Each pig requires at least 200 grams of Carbs, 180 grams of Protein and 150 grams of Vitamins.
[4 marks] Write this as a linear program.
[2 marks] Find the optimal kilos of each foodstuff each pig should be fed that minimises cost whilst satisfying the nutritional requirements
[2 marks] What price would Alfalfa have to be (in whole cents) for the optimal solution to not include any Alfalfa?
(solution using R)
ANSWER:
I have provided the properly commented
and indented code so you can easily copy the code as well as check
for correct indentation.
I have provided the output image of the code so you can easily
cross-check for the correct output of the code.
Have a nice and healthy day!!
CODE
require(lpSolve)
# linear programing eqns
# defining variables to find i.e. Corn, Grunge, Alfalfa quantities in kgs
# Min -> 84*Corn + 92*Grunge + 60*Alfalfa
# Constraints ->
# for at least 200 grams of Cargs=> 90*Corn + 20*Grunge + 40*Alfalfs >= 200
# for at least 180 grams of Protein=> 30*Corn + 80*Grunge + 30*Alfalfs >= 180
# for at least 150 grams of Vitamins=> 10*Corn + 20*Grunge + 60*Alfalfs >= 150
# defining function, and writing linear program in it
# input as coefficients of the decision variables -> Coffs
findOptimum <- function(Coffs){
# Creating constraint martix consMatrix
consMatrix <- matrix(c(90, 20, 40,
30, 80, 30,
10, 20, 60), nrow=3, byrow=TRUE)
# Right hand side for the constraints
B <- c(200, 180, 150)
# Defining Direction of the constraints
consDirection <- c(">=", ">=", ">=")
# Finding the optimal solution using lp function
optimum <- lp(direction="min",
objective.in = Coffs,
const.mat = consMatrix,
const.dir = consDirection,
const.rhs = B,
all.int = T)
# if status: 0 = success, 2 = no feasible solution
#ifelse(optimum$status == 0,print("Solution Found!"),print("no feasible solution!"))
# displaying optimum solution
# fetching and returning solutions
optimum$solution
}
# displaying optimum solution by calling findOptimum
# fetching solutions
opt_sol <- findOptimum(c(84, 92, 60))
# giving them names
names(opt_sol) <- c("Corn", "Grunge", "Alfalfa")
# displaying result
print("Optimal Solutions is(in Kgs):")
print(opt_sol)
# to find cost of Alfalfa to not be included in optimal solution
# looping through each possible cost and fetching the first alfalfa cost where alfalfa quant is 0
for (i in seq(61,1000)){
Coffs <- c(84, 92, i)
# fetching solution by calling function findOptimum
opt_sol <- findOptimum(Coffs)
# if alfalfa is 0 breaking loop and displaying result
if(opt_sol[3]==0){
# using cat function to display result
cat("For Alfalfa cost =",i,", optimal solution doesnot include Alfalfa\n")
cat("Solution is:\n",opt_sol)
break
}
}
OUTPUT IMAGE
Get Answers For Free
Most questions answered within 1 hours.