4

I am new to LP modelling 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 the model with 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 bigger models.

Ram Narasimhan
  • 22,341
  • 5
  • 49
  • 55
pasternak
  • 361
  • 4
  • 14

2 Answers2

2

Use write.lp to print out larger LPs.

 write.lp(lprec, filename="test.lp")

Will produce a text file, which you can examine with any text editor.

Ram Narasimhan
  • 22,341
  • 5
  • 49
  • 55
0

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 all additional parameters to the print function are going to be ignored:

Usage

## S3 method for class 'lpExtPtr' print(x, ...)

Arguments

x an lpSolve linear program model object.

... additional arguments are ignored.

As a result, your best bet is probably to extract the individual pieces of information and output them. For instance:

# 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
josliber
  • 43,891
  • 12
  • 98
  • 133