You are on page 1of 8

lpSolveAPI Package Users Guide

Kjell Konis

Contents
1 Introduction
1.1 Installation . . . . . . . . . . . . .
1.2 Getting Help . . . . . . . . . . . .
1.3 Conventions used in the lpSolveAPI
1.4 Caveats . . . . . . . . . . . . . . .
1.5 A Detailed Example . . . . . . . .

. . . . .
. . . . .
Package
. . . . .
. . . . .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

1
1
1
2
4
5

a mixed integer linear programming (MILP) solver with support for pure linear. http://lpsolve.constraint) 1 .0. semi-continuous and special ordered sets (SOS) models.1 Installation Use the following command to install the package from CRAN > install. The lp solve library uses the revised simplex method to solve pure linear programs and uses the branch-and-bound algorithm to handle integer variables. For example.5. The lpSolve package is based on an older version of lp solve (5.8) and does not include the API. (mixed) integer/binary. 1. the command > help(add. The > shown before each R command is the R prompt. Note there is separate package called lpSolve available on CRAN that provides a few high-level functions for solving specific types of linear programs.2 Getting Help Documentation is provided for each function in the lpSolve package using R’s built-in help system. > library(lpSolveAPI) Additionally.r-project.1 1 Introduction The lpSolveAPI package provides an R API for the lp solve library.packages("lpSolveAPI") then load it into your R session. development versions of the package can be obtained from the R-Forge lpSolveAPI project website. semi-continuous variables and special ordered sets. Only the text after > should be entered.r-forge.org 1.

> ls("package:lpSolveAPI") 1.lp function.lp <. > my. The general approach used in the lpSolveAPI package is to create an lpSolve linear program model object (LPMO). The first argument to almost all of the functions in the lpSolveAPI package is therefore the lpSolve linear program model object that the function is meant to operate on.constraint function. Assignment via the dim function is not supported. > my. For example. to change the dimension of an LPMO it is necessary to use the resize. 2) The number of constraints m and the number of decision variables n can be retrieved using the generic dim function.3 Conventions used in the lpSolveAPI Package 2 will display the documentation for the add.lp(3. Use the following command to list all of the functions provided by the lpSolveAPI package. use the set accessor methods to define a linear program (which will be contained in the LPMO). the following command creates an lpSolve linear program model object with 3 constraints and 2 decision variables.lp Model name: Minimize R1 R2 R3 C1 0 0 0 0 C2 0 0 0 0 free free free 0 0 0 2 .1.3 Conventions used in the lpSolveAPI Package The syntax used to program lp solve via the lpSolveAPI package is different from that normally used in R. solve the linear program and finally use the get accessor methods to retrieve elements of the solution. The LPMO is now initialized but contains no data.lp function. A new lpSolve linear program model object with m constraints and n decision variables can be created using the make. Note that the objective function and right-hand-side are not counted in the dimension of the LPMO.make.

Suppose we wish to solve the following linear program minimize: −2x1 subject to: x1 x1 2x1 − x2 + 3x2 ≤ 4 + x2 ≤ 2 + ≤ 3 with x1 ≥ 0 and x2 ≥ 0. c(4. c(1.objfn(my. 1.lp. 0)) set. 3)) my.column(my.1. 2. Do not assign the output of a set accessor method and expect an lpSolve linear program model object.type(my.lp.lp.3 Conventions used in the lpSolveAPI Package Kind Type Upper Lower Std Real Inf 0 3 Std Real Inf 0 The next step is to use the set accessor methods to define a linear programming problem. c(-2. rep("<=".rhs(my. 1. The best way to build a model in lp solve is columnwise.lp Model name: Minimize R1 R2 R3 Kind Type Upper Lower C1 -2 1 1 2 Std Real Inf 0 C2 -1 3 1 0 Std Real Inf 0 <= <= <= 4 2 3 The set accessor methods (the functions with names beginning with the word set) operate directly on an lpSolve linear program model object. 3)) set. 2. that is to set the columns then the objective function then the constraint types and finally the right-hand-side.lp.column(my. c(3.lp. 1. 2)) set. 3 . -1)) set. > > > > > > set.constr. The (invisible) return value of these functions is generally a logical status code indicating whether the function executed sucessfully.

2) Then we assign x to y. 1. take a look at y.4 4 Caveats The lpSolveAPI package provides an API for building and solving linear programs that mimics the lp solve C API. > y Model name: Minimize R1 R2 Kind Type Upper Lower C1 0 1 2 Std Real Inf 0 C2 0 3 4 Std Real Inf 0 free free 0 0 The changes made to x appear in y as well. In particular. they both refer to the same external lp solve ’lprec’ structure.lp(2.x Next we set some columns in x. 2)) > set.lp and read. The most important is that the lpSolve linear program model objects created by make. This approach allows much greater flexibility but also has a few caveats. 4)) And finally. c(1. 4 .4 Caveats 1. > set. R does not know how to deal with these structures. 2.column(x. Although x and y are two distinct objects in R.column(x. First we create an empty model x. > y <.lp are not actually R objects but external pointers to lp solve ’lprec’ structures. > x <. Consider the following example. Thus you should avoid assigning LPMOs in R code. R cannot duplicate them.make.1. c(3.

0. > lprec <. constraint types and right-hand-side. we create an LPMO with 3 constraints and 4 decision variables. > set. c(2. 12.68x1 3x2 + 6.9.31x3 ≤ 14. indices = 1) > set. 3. 3.6.9x4 ≥ 92.26x2 + 2. indices = c(1.5 5 A Detailed Example Lets solve the mixed integer linear program minimize: subject to: 1x1 + 0.make.column(lprec.constr. "integer") > set.9).lp(3.column(lprec. c(0. > set.3.0.column(lprec. "<=". 0.24. all decision variables are created as real with range [0.1.3 + 11. "binary") 5 .08x3 + 0. c(1. 0. > set.column(lprec.rhs(lprec.24x1 12.type(lprec.08).5 A Detailed Example 1.31. 4) Next we set the values in the first column. 3)) Next. only the nonzero entries are set.type(lprec. we set the objective function. First. ∞). c(92. c(11. 48. 78. 2.1)) > set. 1.1x4 78. x2 is a nonnegative integer-valued decision variable. 6.98].8 + 0. 4)) By default.0. ">=")) > set.24. 3. > set. 0.0.8.26. c(">=".type(lprec. 2. In the remaining three columns. indices = 2:3) > set. 4.objfn(lprec.68)) The lp solve library is capable of using a sparse vectors to represent the constraints matrix.24x3 + 0. 14.9x4 ≥ 4 where x1 is a real decision variable with a minimum value of 28. Thus we must change the type of the decision variables x2 and x3 . x3 is a binary decision variable and x4 is a real decision variable restricted to the range [18.

"COLTHREE". We still need to set the range constraints on x1 and x4 . "COLTWO".26 0 0 Std Int Inf 0 COLTHREE 6.3 14.5 A Detailed Example 6 Setting the type to binary automatically changes the range to [0.60000 0.9 0 0.8 4 Now we can solve the model and retrieve some results. lower = c(28. > solve(lprec) [1] 0 A return value of 0 indicates that the model was successfully solved.24 12.bounds(lprec.6 COLTWO 3 78. "THATROW".98. columns = c(1. > lprec # or equivalently print(lprec) Model name: Minimize THISROW THATROW LASTROW Kind Type Upper Lower COLONE 1 0 0.6.1 2.08 Std Int 1 0 COLFOUR 0. upper = 48.68 Std Real Inf 28.1. > RowNames <.24 0 11. 4)) > set.objective(lprec) [1] 31.98 18 >= <= >= 92.78276 > get. > get.9 Std Real 48.00000 31. > set.c("THISROW".31 0. 18).00000 0.list(RowNames. we name the decision variables and the constraints.variables(lprec) [1] 28. "COLFOUR") > dimnames(lprec) <. columns = 4) Finally. "LASTROW") > ColNames <.bounds(lprec. ColNames) Lets take a look at what we have done so far.82759 6 . 1].c("COLONE".

Since solve is generic in R.3000 6. 7 .1.constraints(lprec) [1] 92. The set accessor functions (the functions with names beginning with the word set) do not have return values.. solve).lpExtPtr) to view its documentation.2928 Note that there are some commands that return an answer. you may assume that they completed successfully.5 A Detailed Example 7 > get. If the return without error. For the get accessor functions (the functions with names beginning with the word get) the output should be clear. For other functions (e. the interpretation of the returned value is described in the documentation.8640 391.g. use the command > help(solve.