我一直在使用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发布于 2014-04-24 21:14:09
我不认为您可以混合这两个包(lpSolveAPI不导入或依赖于lpSolve)。考虑lpSolve中的简单LP
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基于项目网站 for 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;https://stackoverflow.com/questions/23279581
复制相似问题