5

lpSolve と lpSolveAPI を使用しています。制約行列、目的関数などを作成し、lp 関数にフィードすると、これは問題なく機能します。問題を write.lp を使用して lp ファイルとして保存したいのですが、問題が発生しています。オブジェクトが lp オブジェクトではないというエラーが表示され続けます。何か案は?

> x1 = lp(direction = "min", cost, A , ">=",r,,3:13, , , ,FALSE)
> class(x1)
[1] "lp"
>write.lp(x1, filename, type = "lp",use.names = c(TRUE, TRUE))

Error in write.lp(x1, filename, type = "lp", use.names = c(TRUE, TRUE)) : 
the lp argument does not appear to be a valid linear program record
4

1 に答える 1

5

これら 2 つのパッケージを混在させることはできないと思います (lpSolveAPIはインポートも依存もしませんlpSolve)。の単純な LP を考えてみましょうlpSolve:

library(lpSolve)
costs <- c(1, 2)
mat <- diag(2)
dirs <- rep(">=", 2)
rhs <- c(1, 1)
x1 = lp("min", costs, mat, dirs, rhs)
x1
# Success: the objective function is 3

のプロジェクト Web サイトに基づいて、lpSolveAPI次のようなもので同じことを行います。

library(lpSolveAPI)
x2 = make.lp(0, ncol(mat))
set.objfn(x2, costs)
for (idx in 1:nrow(mat)) {
  add.constraint(x2, mat[idx,], dirs[idx], rhs[idx])
}

これで、解を解いて観察できます。

x2
# Model name: 
#             C1    C2       
# Minimize     1     2       
# R1           1     0  >=  1
# R2           0     1  >=  1
# Kind       Std   Std       
# Type      Real  Real       
# Upper      Inf   Inf       
# Lower        0     0       
solve(x2)
# [1] 0
get.objective(x2)
# [1] 3
get.variables(x2)
# [1] 1 1

質問に戻ると、ファイルに書き出すことができます。

write.lp(x2, "myfile.lp")

ファイルの内容は次のとおりです。

/* Objective function */
min: +C1 +2 C2;

/* Constraints */
R1: +C1 >= 1;
R2: +C2 >= 1;
于 2014-04-24T21:14:09.753 に答える