An R interface for lp_solve, a Mixed Integer Linear Programming (MILP) solver with support for pure linear, (mixed) integer/binary, semi-continuous and special ordered sets (SOS) models.
R is a language and environment for statistical computing and graphics. It is a GNU project which is similar to the S language and environment which was developed at Bell Laboratories (formerly AT&T, now Lucent Technologies) by John Chambers and colleagues. R can be considered as a different implementation of S. There are some important differences, but much code written for S runs unaltered under R. For more information or to download R please visit the R website.
There are currently two R packages based on lp_solve. The lpSolve package provides high-level functions for solving general linear/integer problems, assignment problems and transportation problems. The lpSolveAPI package provides a complete implementation of the lp_solve API. The lpSolveAPI package has a lot more functionality than lpSolve, however, it also has a slightly more difficult learning curve. Both packages are available from CRAN.
Caveat (19.04.2011): the lpSolve package is based on lp_solve version 22.214.171.124 which was released on 27.12.2007. The current version of lp_solve (used in the lpSolveAPI package) is 126.96.36.199 and was released on 22.08.2010.
You can find the project summary page here.
To install the lpSolve package use the command:
> install.packages("lpSolve")and to install the lpSolveAPI package use the command:
> install.packages("lpSolveAPI")After the packages have been downloaded and installed, you can load them into your R session using the library function, e.g.,
> library(lpSolveAPI)This needs to be done once in each R session (i.e., every time you launch R).
The > shown before each R command is the R prompt. Only the text after > should be entered.
Documentation for the lpSolve and lpSolveAPI packages is provided using R's built-in help system. For example, the command
> ?make.lpwill display the documentation for the make.lp function. You can list all of the functions in the lpSolveAPI package with the following command.
> ls("package:lpSolveAPI")The documentation for each of these functions can be accessed using the ? operator. Note that you must append .lpExtPtr to the names of the generic functions (dim, dimnames, plot, print and solve), otherwise you will get the documentation for the standard generic function.
The lpSolveAPI package provides an API for building and solving linear programs that mimics the lp_solve C API. This approach allows greater flexibility but also has a few caveats. The most important is that the lpSolve linear program model objects created by make.lp and read.lp are not actually R objects. Rather, they are pointers to lp_solve 'lprec' structures which are created and store externally. R does not know how to deal with these structures. In particular, R cannot duplicate them. You should never assign an lpSolve linear program model object in R code.
Consider the following example. First we create an empty model x.
> x <- make.lp(2, 2)Then we assign x to y.
> y <- xNext we set some columns in x.
> set.column(x, 1, c(1, 2)) > set.column(x, 2, c(3, 4))And finally, take a look at y.
> y Model name: C1 C2 Minimize 0 0 R1 1 3 free 0 R2 2 4 free 0 Type Real Real upbo Inf Inf lowbo 0 0The changes we made in x appear in y as well. Although x and y are two distinct objects in R, they both refer to the same lp_solve 'lprec' structure.
The safest way to use the lpSolve API is inside an R function - do not return the lpSolve linear program model object.
> lprec <- make.lp(0, 4) > set.objfn(lprec, c(1, 3, 6.24, 0.1)) > add.constraint(lprec, c(0, 78.26, 0, 2.9), ">=", 92.3) > add.constraint(lprec, c(0.24, 0, 11.31, 0), "<=", 14.8) > add.constraint(lprec, c(12.68, 0, 0.08, 0.9), ">=", 4) > set.bounds(lprec, lower = c(28.6, 18), columns = c(1, 4)) > set.bounds(lprec, upper = 48.98, columns = 4) > RowNames <- c("THISROW", "THATROW", "LASTROW") > ColNames <- c("COLONE", "COLTWO", "COLTHREE", "COLFOUR") > dimnames(lprec) <- list(RowNames, ColNames)Lets take a look at what we have done so far.
> lprec # or equivalently print(lprec) Model name: COLONE COLTWO COLTHREE COLFOUR Minimize 1 3 6.24 0.1 THISROW 0 78.26 0 2.9 >= 92.3 THATROW 0.24 0 11.31 0 <= 14.8 LASTROW 12.68 0 0.08 0.9 >= 4 Type Real Real Real Real Upper Inf Inf Inf 48.98 Lower 28.6 0 0 18Now lets solve the model.
> solve(lprec)  0 > get.objective(lprec)  31.78276 > get.variables(lprec)  28.60000 0.00000 0.00000 31.82759 > get.constraints(lprec)  92.3000 6.8640 391.2928