LpsolveAPI in RStudio

I am using lpsolveAPI in RStudio. When I type a model name with several decision variables, I can read a listing of the current constraints in the model. for instance

> 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 with more than 9 solution variables, it no longer gives a complete summary, and I see:

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

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

Bonus question: is RStudio the best console for working with R?

Here is an example:

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

This creates 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 

Columns C correspond to 5 variables. There are no restrictions right now, and the objective function is 0.

You can add a restriction with

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

This is the restriction C1 + 3 * C2 + 4 * C3 + 2 * C4 - 8 * C5 = 0. Now the printout:

 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 

In any case, the fact is that no matter how many restrictions, if there are more than 9 variables, I do not get the full listing.

 >lprec <- make.lp(0,15) >lprec Model name: a linear program with 15 decision variables and 0 constraints 
+4
source share
2 answers

Write it to a file for verification

When I work with LP using lpSolveAPI , I prefer to write them to a file. The lp format is great for my needs. Then I will explore the LP model using any text editor. If you click on the output file in the Files panel in RStudio, it will also open it and you can check it.

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

You can also record it in MPS format if you want to.

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

Hope this helps.

+4
source

Since this 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 in different ways depending on its size - the details for very large objects will 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 an object using various get.* Functions, as the print method does.

Alternatively, you can simply 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 
+5
source

Source: https://habr.com/ru/post/1490994/


All Articles