Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lpsolveAPI in RStudio

I am using the lpsolveAPI in RStudio. When I type the name of a model with few decision variables, I can read a printout of the current constraints in the model. For example

> lprec  
Model name: 
          COLONE    COLTWO  COLTHREE   COLFOUR          
Minimize         1         3      6.24       0.1          
THISROW          0     78.26         0       2.9  >=  92.3
THATROW       0.24         0     11.31         0  <=  14.8
LASTROW      12.68         0      0.08       0.9  >=     4
Type          Real      Real      Real      Real          
Upper          Inf       Inf       Inf     48.98          
Lower         28.6         0         0        18  

But when I make a model that has more than 9 decision variables, it no longer gives the full summary and I instead see:

> lprec
Model name:
    a linear program with 13 decision variables and 258 constraints

Does anyone know how I can see the same detailed summary of the model when there are large numbers of decision variables?

Bonus Question: Is RStudio the best console for working with R?

Here is an example:

>lprec <- make.lp(0,5) 

This makes a new model called lprec, with 0 constraints and 5 variables. Even if you call the name now you get:

>lprec
Model name: 
        C1    C2    C3    C4    C5    
Minimize     0     0     0     0     0    
Kind       Std   Std   Std   Std   Std    
Type      Real  Real  Real  Real  Real    
Upper      Inf   Inf   Inf   Inf   Inf    
Lower        0     0     0     0     0 

The C columns correspond to the 5 variables. Right now there are no constraints and the objective function is 0.

You can add a constraint with

>add.constraint(lprec, c(1,3,4,2,-8), "<=", 0)

This is the constraint C1 + 3*C2 + 4*C3 + 2*C4 - 8*C5 <= 0. Now the print out is:

Model name: 
            C1    C2    C3    C4    C5       
Minimize     0     0     0     0     0       
R1           1     3     4     2    -8  <=  0
Kind       Std   Std   Std   Std   Std       
Type      Real  Real  Real  Real  Real       
Upper      Inf   Inf   Inf   Inf   Inf       
Lower        0     0     0     0     0    

Anyway the point is that no matter how many constraints, if there are more than 9 variables then I don't get the full print out.

>lprec <- make.lp(0,15)
>lprec
Model name:
    a linear program with 15 decision variables and 0 constraints
like image 352
Gabe Conant Avatar asked Dec 21 '22 03:12

Gabe Conant


2 Answers

Write it out to a file for examination

When I work with LPs using lpSolveAPI, I prefer to write them out to a file. The lp format works fine for my needs. I then examine the LP model using any text editor. If you click on the output file in the "Files" panel in RStudio, it will open it too, and you can inspect it.

 write.lp(lprec, "lpfilename.lp", "lp") #write it to a file in LP format

You can also write it out as MPS format if you so choose.

Here's the help file on write.lp().

Hope that helps.

like image 143
Ram Narasimhan Avatar answered Jan 16 '23 04:01

Ram Narasimhan


Since it is an S3 object of class lpExtPtr, the function called to display it is print.lpExtPtr. If you check its code, you will see that it displays the object differently depending on its size -- details for very big objects would not be very useful. Unfortunately, the threshold cannot be changed.

class(r)
# [1] "lpExtPtr"
print.lpExtPtr
# function (x, ...) 
# {
# (...)
#     if (n > 8) {
#         cat(paste("Model name: ", name.lp(x), "\n", "  a linear program with ", 
#             n, " decision variables and ", m, " constraints\n", 
#             sep = ""))
#         return(invisible(x))
#     }
# (...)

You can access the contents of the object with the various get.* functions, as the print method does.

Alternatively, you can just change the print method.

# A function to modify functions
patch <- function( f, before, after ) { 
  f_text <- capture.output(dput(f))
  g_text <- gsub( before, after, f_text )
  g <- eval( parse( text = g_text ) )
  environment(g) <- environment(f)
  g
}

# Sample data
library(lpSolveAPI)
r <- make.lp(0,5) 
r  # Shows the details
r <- make.lp(0,20) 
r  # Does not show the details

# Set the threshold to 800 variables instead of 8
print.lpExtPtr <- patch( print.lpExtPtr, "8", "800" )
r  # Shows the details
like image 23
Vincent Zoonekynd Avatar answered Jan 16 '23 04:01

Vincent Zoonekynd