Lpsolve does not print lp model in R
I am new to LP modeling in R. I am using lpSolveAPI. When I try a small example with two decision variables and print the model, then it prints the complete model.
library(lpSolveAPI)
lprec <- make.lp(nrow=0,ncol=2,verbose="full")
set.objfn(lprec,c(6,5))
lp.control(lprec,sense="max")
add.constraint(lprec,c(1,1),"<=",5)
add.constraint(lprec,c(3,2),"<=",12)
set.bounds(lprec,lower=c(0,0),columns = c(1,2))
RowNames <- c("A","B")
ColNames <- c("R1","R2")
dimnames(lprec) <- list(RowNames, ColNames)
print(lprec)
# Model name:
# R1 R2
#Maximize 6 5
#A 1 1 <= 5
#B 3 2 <= 12
#Kind Std Std
#Type Real Real
#Upper Inf Inf
#Lower 0 0
But when I try to simulate 25 decision variables and after adding some constraints, if I try to print the model then it just says:
Model name:
a linear program with 25 decision variables and 5 constraints
Please suggest how to display large models.
source to share
I can reproduce this with a simple LP:
library(lpSolveAPI)
lprec <- make.lp(nrow=0,ncol=25,verbose="full")
add.constraint(lprec, rep(1, 25), "<=", 1)
add.constraint(lprec, c(1, rep(0, 24)), "<=", 5)
print(lprec)
# Model name:
# a linear program with 25 decision variables and 2 constraints
From ?print.lpExtPtr
it seems that all additional parameters of the print function will be ignored:
Using
## S3 method for class 'lpExtPtr' print (x, ...)
Arguments
x an object of the linear programming model lpSolve.
... additional arguments are ignored.
As a result, your best bet is probably to extract individual pieces of information and output them. For example:
# Rows and columns
nr <- dim(lprec)[1]
nc <- dim(lprec)[2]
# Constraint matrix
sapply(1:(dim(lprec)[2]), function(x) {
ret <- rep(0, nr)
c <- get.column(lprec, x)
ret[c$nzrow] <- c$column
ret
})
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16] [,17] [,18] [,19]
# [1,] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
# [2,] 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [,20] [,21] [,22] [,23] [,24] [,25]
# [1,] 1 1 1 1 1 1
# [2,] 0 0 0 0 0 0
# Constraint directions
get.constr.type(lprec)
# [1] "<=" "<="
# Right-hand sides
get.constr.value(lprec)
# [1] 1 5
source to share