2013-07-11 28 views
3

我在RStudio中使用lpsolveAPI。當我輸入一個決策變量很少的模型的名稱時,我可以讀取模型中當前約束的打印輸出。例如lpsolveAPI in RStudio

> 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 

但是,當我作出這樣的已經超過9個決策變量的模型,它不再給予充分總結和我,而不是看:

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

有誰知道我怎麼能看到的當有大量決策變量時,同樣詳細的模型摘要?

紅利問題:RStudio是與R一起工作的最佳控制檯嗎?

下面是一個例子:

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

這使得一個所謂的lprec新模型,用0約束和5個變量。即使你現在叫你的名字,你會得到:

>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 

C列對應於5個變量。現在不存在約束條件和目標函數爲0

可以添加一個約束與

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

這是約束C1 + 3 * C2 + 4 * C3 + 2 * C4 - 8 * C5 < = 0。現在打印出來的是:

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  

反正一點是,不管有多少的限制,如果有超過9個變量,然後我沒有得到完整的打印出來。

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

以前從未使用過此庫。你能提供一個可重複的例子(即問題設置),所以我可以四處探察嗎? –

+0

我在底部添加了一個示例。 – Gabe

回答

3

寫出來的文件進行檢查

當我使用lpSolveAPI LP的工作,我喜歡他們寫出來的文件。 lp格式適合我的需求。然後我使用任何文本編輯器檢查LP模型。如果您在RStudio的「文件」面板中單擊輸出文件,它也會打開它,您可以檢查它。

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

如果您願意,還可以將其寫爲MPS格式。

以下是write.lp()的幫助文件。

希望有幫助。

4

既然是lpExtPtr類, 稱爲以顯示它是print.lpExtPtr的功能的S3對象。 如果你檢查它的代碼,你會發現它顯示的對象 取決於它的大小 - 非常大的對象的細節不會很有用。 不幸的是,閾值不能改變。

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)) 
#  } 
# (...) 

可以作爲print方法確實與各個get.*功能訪問該對象的內容, 。

或者,您可以更改print方法。

# 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