The lpSolveAPI
package in R lets you specify and solve a linear program. However calling solve()
on an already solved LP is significantly slower than solving the LP to begin with. Why does this happen, and how can this problem be avoided?
Here is a minimal example showing the problem:
set.seed(42)
library(lpSolveAPI)
#------------
# Set up LP
#------------
m <- 2 ** 15
lps.model <- make.lp(nrow = m, ncol = 3)
set.column(lps.model, 1, rep(1,m))
set.column(lps.model, 2, rep(1,m))
set.column(lps.model, 3, rep(-1,m))
set.rhs(lps.model, rnorm(m) + rep(5,m))
set.objfn(lps.model,c(1,0,0))
set.constr.type(lps.model, rep(">=",m))
#-----------------
# Solve it twice
#-----------------
system.time(solve(lps.model)) # 0.02 elapsed
system.time(solve(lps.model)) # 0.14 elapsed
Why does it matter?
I am doing some sequential statistical tests; each test involves solving an LP, and each LP differs from the last in only O(log m) constraints. In theory it should be possible to do this very quickly by solving the first LP and saving the solution, editing the constraints slightly, then using the solution as a warm start for the next LP.
The problem above extends to editing the LP with delete.constraint()
/ add.constraint()
, which makes 'efficient' solution significantly slower than the naive approach of solving each LP cold.