I'am doing solve the linear program by lpSolveAPI and I want to substitute object function's variable for subject to's variable.
for example, i mimic orignal my lp like this
x5 <- x1 + x2
x6 <- x2 + x3 + x4
object funtion
min x5 + x6
subject to
x1 + x2 <= 2
x3 + x4 <= 5
x2 + x4 <= 3
x1, x2, x3, x4 = binary
x5, x6 = integer
to do this by lpSolveAPI, I try it like below
lprec <- make.lp(3, 4) # make.lp
var_lp <- matrix(c( rep(1, 12)), nrow = 3) # variable as var_lp
set.column(lprec, 1, var_lp[1,1], indices = 1) # s.t.
set.column(lprec, 2, c(var_lp[1,2], var_lp[3,2], indices = c(1,3))
set.column(lprec, 3, var_lp[2,3], indices = 2)
set.column(lprec, 4, c(var_lp[2,4], var_lp[3,4], indices = c(2,3))
i don't know any more how to express x5, x6 at lpSolveAPI
Thank you for your leading. i hope someone give me a answer about that
*plus, my orignal data has about 5,000 constraint. is it possible to analyze by lpSolveAPI??
The set.type
function allows you to specify integer domains
set.type(lprec, 5, type="integer")
You would need to increase your definition of the problem, though:
lprec <- make.lp(5,6)
Edit: Code fragment for this problem
library(lpSolveAPI)
lprec <- make.lp(5,6)
set.objfn(lprec, obj=c(1,1), indices=c(5,6)) # sense defaults to "min"
set.row(lprec, 1, xt=c(1,1), indices=c(1,2))
set.row(lprec, 2, xt=c(1,1), indices=c(3,4))
set.row(lprec, 3, xt=c(1,1), indices=c(2,4))
set.row(lprec, 4, xt=c(1,1,-1), indices=c(1,2,5))
set.row(lprec, 5, xt=c(1,1,1,-1), indices=c(2,3,4,6))
set.type(lprec, 1, type="binary")
set.type(lprec, 2, type="binary")
set.type(lprec, 3, type="binary")
set.type(lprec, 4, type="binary")
set.type(lprec, 5, type="integer")
set.type(lprec, 6, type="integer")
set.constr.type(lprec, types=c(rep("<=",3), rep("=",2)))
set.rhs(lprec, b=c(2,5,3,0,0))
You can use print
or write.lp
to check that the specified program is the program you want to solve. With solve
you calculate the solution which would be in this all zero:
solve(lprec)
get.variables(lprec)
(I believe that implicitly, lpSolve assumes non-negativity of all variables.)