R lpSolve 包找不到最佳解决方案
R lpSolve package does not find optimal solution
我正在尝试使用 R lpSolve 包解决以下优化问题:
1.007825*x1 +12.000000*x2 +15.99492*x3 +14.00307*x4 +31.97207*x5 +30.97376*x6 >= 10
1.007825*x1 +12.000000*x2 +15.99492*x3 +14.00307*x4 +31.97207*x5 +30.97376*x6 <= 15
1*x1 - 2*x2 + 0*x3 -1*x4 +0*x5 -3*x6 <= 2
xi >= 0, where i = [1,2,3,4,5,6]
我的objective函数是:
1.007825*x1 +12.000000*x2 +15.99492*x3 +14.00307*x4 +31.97207*x5 +30.97376*x6
我以标准形式 (A) 创建约束矩阵:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -1.007825 -12 -15.99492 -14.00307 -31.97207 -30.97376
[2,] 1.007825 12 15.99492 14.00307 31.97207 30.97376
[3,] 1.000000 -2 0.00000 -1.00000 0.00000 -3.00000
[4,] -1.000000 0 0.00000 0.00000 0.00000 0.00000
[5,] 0.000000 -1 0.00000 0.00000 0.00000 0.00000
[6,] 0.000000 0 -1.00000 0.00000 0.00000 0.00000
[7,] 0.000000 0 0.00000 -1.00000 0.00000 0.00000
[8,] 0.000000 0 0.00000 0.00000 -1.00000 0.00000
[9,] 0.000000 0 0.00000 0.00000 0.00000 -1.00000
我创建了边界矩阵 (b):
[,1]
[1,] -10
[2,] 15
[3,] 2
[4,] 0
[5,] 0
[6,] 0
[7,] 0
[8,] 0
[9,] 0
和 objective 函数 (f):
f = c(1.007825, 12.000000, 15.99492, 14.00307, 31.97207, 30.97376)
当我把它放入代码中时:
out = lp("min",f,A,rep("<=",9),b,all.int=TRUE),
我得到解 c(0,0,0,1,0,0),虽然我知道解是 c(0,1,0,0,0,0)。如果我更改右边框(而不是 15,我改为 13),一切正常。可能是什么问题?
用lpSolveAPI
,问题很好解决:
library(lpSolveAPI)
lprec <- make.lp(0, ncol=6)
set.type(lprec, columns=seq(1,6), type="integer")
set.objfn(lprec, obj=c(1.007825, 12, 15.99492, 14.00307, 31.97207, 30.97376))
add.constraint(lprec, xt=c(1.007825, 12, 15.99492, 14.0030, 31.97207, 30.97376), type=">=", rhs=10)
add.constraint(lprec, xt=c(1.007825, 12, 15.99492, 14.0030, 31.97207, 30.97376), type="<=", rhs=15)
add.constraint(lprec, xt=c(1, -2, 0, -1, 0, -3), type="<=", rhs=2)
solve(lprec)
get.variables(lprec)
returns
[1] 0 1 0 0 0 0
我正在尝试使用 R lpSolve 包解决以下优化问题:
1.007825*x1 +12.000000*x2 +15.99492*x3 +14.00307*x4 +31.97207*x5 +30.97376*x6 >= 10
1.007825*x1 +12.000000*x2 +15.99492*x3 +14.00307*x4 +31.97207*x5 +30.97376*x6 <= 15
1*x1 - 2*x2 + 0*x3 -1*x4 +0*x5 -3*x6 <= 2
xi >= 0, where i = [1,2,3,4,5,6]
我的objective函数是:
1.007825*x1 +12.000000*x2 +15.99492*x3 +14.00307*x4 +31.97207*x5 +30.97376*x6
我以标准形式 (A) 创建约束矩阵:
[,1] [,2] [,3] [,4] [,5] [,6]
[1,] -1.007825 -12 -15.99492 -14.00307 -31.97207 -30.97376
[2,] 1.007825 12 15.99492 14.00307 31.97207 30.97376
[3,] 1.000000 -2 0.00000 -1.00000 0.00000 -3.00000
[4,] -1.000000 0 0.00000 0.00000 0.00000 0.00000
[5,] 0.000000 -1 0.00000 0.00000 0.00000 0.00000
[6,] 0.000000 0 -1.00000 0.00000 0.00000 0.00000
[7,] 0.000000 0 0.00000 -1.00000 0.00000 0.00000
[8,] 0.000000 0 0.00000 0.00000 -1.00000 0.00000
[9,] 0.000000 0 0.00000 0.00000 0.00000 -1.00000
我创建了边界矩阵 (b):
[,1]
[1,] -10
[2,] 15
[3,] 2
[4,] 0
[5,] 0
[6,] 0
[7,] 0
[8,] 0
[9,] 0
和 objective 函数 (f):
f = c(1.007825, 12.000000, 15.99492, 14.00307, 31.97207, 30.97376)
当我把它放入代码中时:
out = lp("min",f,A,rep("<=",9),b,all.int=TRUE),
我得到解 c(0,0,0,1,0,0),虽然我知道解是 c(0,1,0,0,0,0)。如果我更改右边框(而不是 15,我改为 13),一切正常。可能是什么问题?
用lpSolveAPI
,问题很好解决:
library(lpSolveAPI)
lprec <- make.lp(0, ncol=6)
set.type(lprec, columns=seq(1,6), type="integer")
set.objfn(lprec, obj=c(1.007825, 12, 15.99492, 14.00307, 31.97207, 30.97376))
add.constraint(lprec, xt=c(1.007825, 12, 15.99492, 14.0030, 31.97207, 30.97376), type=">=", rhs=10)
add.constraint(lprec, xt=c(1.007825, 12, 15.99492, 14.0030, 31.97207, 30.97376), type="<=", rhs=15)
add.constraint(lprec, xt=c(1, -2, 0, -1, 0, -3), type="<=", rhs=2)
solve(lprec)
get.variables(lprec)
returns
[1] 0 1 0 0 0 0