I am trying to perform simplex method of linear programming for the following:
The code does produces correct result on the first iteration. But it won't go beyond result4. Any help on how to perform this iteration would be helpful.
I tried applying while loop within the function to check if the first four columns of last row are negative using any() function. But it did not help. Thank you.
row1 <- c(-4,6,5,-4,1,0,0,0,20)
row2 <- c(3,-2,4,1,0,1,0,0,10)
row3 <- c(8,-3,3,2,0,0,1,0,20)
row4 <- c(-4,-1,-4,-5,0,0,0,1,0)
matrix <- rbind(row1,row2,row3,row4)
matrix
func_simplex <- function(matrix){
#finding the pivot column
pvt_col <- which(matrix[4,1:4]==min(matrix[4,1:4]))
#after division by the coefficient
Coef_div <- matrix[,9]/matrix[,pvt_col]
#finding the minimum coef division and setting the pivot element
pvt_row <- which(Coef_div==min(Coef_div))
pvt_ele <- matrix[pvt_row,pvt_col]
#Divide the row by Pivot Element value
matrix[pvt_row, ] <- matrix[pvt_row, ]/pvt_ele
for (i in 1:nrow(matrix)){
if (i != pvt_row){
matrix[i,] <- matrix[i,]-matrix[i,pvt_col]*matrix[pvt_row,]
}
}
return(matrix)
}
result <- func_simplex(matrix)
result
result2 <- func_simplex(result1)
result2
result3 <- func_simplex(result2)
result3
result4 <- func_simplex(result3)
result4