Linear Programming in R – What If

In the last post, we did the optimisation of a product line and found that:
The maximum profit is 1880, and the optimal production quantities are alpha = 24 and beta = 16.

Now, a question can arise:
What if we change the objective or the constraints by a little? In other words, how sensitive is the solution to changes in the situation?

One way is to rerun the program with a different coefficient. Let us change alpha’s profit (per kg) from 45 to 47 and see what happens.

library(lpSolve)
f.obj <- c(47, 50)
f.con <- matrix (c(500, 500, 750, 625, 150, 100, 200, 300, 1, 0, 0, 1), nrow = 6, byrow = TRUE)
f.dir <- c("<=", "<=")
f.rhs <- c(20000, 42000, 10400, 9600, 50, 30)

results <- lp ("max", f.obj, f.con, f.dir, f.rhs)
results
results$solution
Success: the objective function is 1928 
24 16

The maximum profit changes to 1928, but the optimal production quantities remain at (24, 16).

What about making it 50? Now, quantities change to (40,0) – produce only alpha!

Rather than making changes manually, the lp function can do it for us using the attribute compute.sens = TRUE . We then use results$sens.coef.from and results1$sens.coef.to to get the range of coefficients.

results <- lp ("max", f.obj, f.con, f.dir, f.rhs, compute.sens=TRUE)
results1$sens.coef.from
results1$sens.coef.to
33.33333 45.00000
50.0 67.5

The solution is optimal as long as the coefficient on X1 stays between [33.33 and 50].
The solution is optimal if the coefficient on X2 is between [45 and 67.5].
Note that you only change one coefficient at a time in sensitivity calculations.