optimality_check <- function(t) {
  if (min(t[nrow(t), 1:(ncol(t) - 1)]) < 0) {
    return(FALSE) # not optimal
  }
  else {
    return(TRUE) # optimal
  }
}
simplex <- function(tableau, max_iter = 100) {
  print("Initial Tableau (Tableau 0)")
  print(tableau)
  iter <- 1
  while (!optimality_check(tableau) & iter < max_iter) {
    print("----------------------------------------------")
    print(paste("Iteration", iter))
    print("----------------------------------------------")
    pivot_column <- get_pivot_column(tableau)
    print(paste("Pivot column:", pivot_column))
    pivot_row <- get_pivot_row(tableau, pivot_column)
    if (pivot_row == -1) {
      break()
    }
    print(paste("Pivot row:", pivot_row))
    tableau <- pivot(tableau, pivot_row, pivot_column)
    print(paste("New tableau at the end of iteration", iter))
    print(tableau)
    iter <- iter + 1
  }
  print("----------------------------------------------")
  print("Status: End")
  print("----------------------------------------------")
}
simplexR <- function(A, b, c, sense = 1, relation = rep("<=", length(b)), max_iter = 100) {
  tableau <- construct_tableau(A, b, c, sense, relation)
  simplex(tableau = tableau, max_iter = max_iter)
}