我想最小化线性约束“均等”的线性编程系统。
该系统总结在以下代码“Python 3”中
>>> obj_func = [1,1,1]
>>> const = [[[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 1, 1]]]
>>> constraints= np.reshape(const, (-1, 3))
>>> constraints
array([[1, 0, 0],
[0, 1, 0],
[0, 0, 1],
[1, 1, 1]])
>>> rhs = [0.4498162176582741, 0.4498162176582741, 0.10036756468345168, 1.0]
使用scipy.optimization.linprg
:
>>> res = linprog(obj_func, constraints, rhs, method="interior-point", options={"disp":True})
>>> res
con: array([], dtype=float64)
fun: 1.4722956444515663e-09
message: 'Optimization terminated successfully.'
nit: 4
slack: array([0.44981622, 0.44981622, 0.10036756, 1. ])
status: 0
success: True
x: array([4.34463075e-10, 4.34463075e-10, 6.03369494e-10])
R中汇总的相同系统使用lpSolve
最小化:
> obj.func = c(1,1,1)
> constraints = matrix(c(1,0,0,0,1,0,0,0,1,1,1,1), nrow= 4, byrow = TRUE)
> rhs = c(0.4498162+0i, 0.4498162+0i, 0.1003676+0i, 1.0000000+0i)
> f.dir = c("=","=","=","=")
>
> res = lp("min",obj.func,constraints,f.dir,rhs,compute.sens=FALSE)
> res
Success: the objective function is 1
如上所述,虽然它是相同的系统,但结果并不相互接近,所以我对其他系统做了同样的工作,但结果也很远。
我的问题:我知道每个LP都没有必要有一个独特的解决方案,但我认为它们应该产生接近的价值!在我的情况下,我试图使用两个解算器最小化许多系统,但结果太过分了。例如,
First system: linprog gave 1.4722956444515663e-09 while lpSolve gave 1
Another system: linprog gave 1.65952852061376e-11 while lpSolve gave 0.8996324
Another system: linprog gave 3.05146726445553e-12 while lpSolve gave 0.8175745
你正在解决不同的模型。
res = linprog(obj_func, constraints, rhs, method="interior-point", options={"disp":True})
手段
res = linprog(obj_func, A_ub=constraints, b_ub=rhs, method="interior-point", options={"disp":True})
影响约束:
x0 <= 0.4498162176582741
...
代替
x0 == 0.4498162176582741
所以linprog只使用不等式,而lpsolve只使用等式(没有我检查f.dir = c("=","=","=","=")
是否正在做我认为它正在做的事情;但结果显示这或多或少)。
linprog结果:
x: array([4.34463075e-10, 4.34463075e-10, 6.03369494e-10])
是内点法的典型零矢量输出(仅近似积分解)!与commercial solvers like Gurobi相比,没有交叉步骤。
阅读docs(包含此信息)时要小心。