| linp {limSolve} | R Documentation |
Solves a linear programming problem,
min(sum {Cost_i.x_i})
subject to
x_i>=0
Ex=f
Gx>=h
Note: this function is simply a wrapper around lp from package lpSolve
This R-code may fail and terminate R for very small problems that are repeated frequently...
Sometimes multiple solutions exist for the same problem.
linp(E=NULL, F=NULL, G=NULL, H=NULL, Cost, verbose=TRUE, ...)
E |
numeric matrix containing the coefficients of the equality constraints Ex=F; if the columns of E have a names attribute, they will be used to label the output |
F |
numeric vector containing the right-hand side of the equality constraints |
G |
numeric matrix containing the coefficients of the inequality constraints Gx>=H; if the columns of G have a names attribute, and the columns of E do not, they will be used to label the output |
H |
numeric vector containing the right-hand side of the inequality constraints |
Cost |
numeric vector containing the coefficients of the cost function; if the Cost a names attribute, and neither the columns of E nor G have a name, they will be used to label the output |
verbose |
logical to print error messages |
... |
extra arguments passed to R-function lp |
a list containing:
X |
vector containing the solution of the linear programming problem. |
residualNorm |
scalar, the sum of absolute values of residuals of equalities and violated inequalities. Should be very small or zero for a feasible linear programming problem |
solutionNorm |
scalar, the value of the minimised Cost function, i.e. the value of sum {Cost_i.x_i} |
IsError |
logical, TRUE if an error occurred |
type |
the string "linp", such that how the solution was obtained can be traced |
Karline Soetaert <k.soetaert@nioo.knaw.nl>
ldei, lsei,
lp the original function from package lpSolve
Blending
#--------------------------------------------
# Linear programming problem 1, not feasible
#--------------------------------------------
# maximise x1 + 3*x2
# subject to
#-x1 -x2 < -3
#-x1 + x2 <-1
# x1 + 2*x2 < 2
G <- matrix(nrow=3,data=c(-1,-1,1, -1,1,2))
H <- c(3,-1,2)
Cost <- c(-1,-3)
(L<-linp(E=NULL,F=NULL,Cost=Cost,G=G,H=H))
L$residualNorm
#--------------------------------------------
# Linear programming problem 2, feasible
#--------------------------------------------
# minimise x12 + 8*x13 + 9*x14 + 2*x23 + 7*x24 + 3*x34
# subject to:
#-x12 + x23 + x24 = 0
# - x13 - x23 + x34 = 0
# x12 + x13 + x14 > 1
# x14 + x24 + x34 < 1
A <- matrix(nrow=2,byrow=TRUE,data=c(-1,0,0,1,1,0,
0,-1,0,-1,0,1))
B <- c(0,0)
G <- matrix(nrow=2,byrow=TRUE,data=c(1,1,1,0,0,0,
0,0,-1,0,-1,-1))
H <- c(1,-1)
Cost <- c(1,8,9,2,7,3)
(L<-linp(E=A,F=B,Cost=Cost,G=G,H=H))
L$residualNorm