gpt4 book ai didi

python - 为 Python 的 Scipy 线性规划寻找严格大于零的解决方案的方法

转载 作者:太空宇宙 更新时间:2023-11-03 13:14:01 26 4
gpt4 key购买 nike

Scipy NNLS 执行此操作:

Solve argmin_x || Ax - b ||_2 for x>=0.

如果我寻求,有什么替代方法可以做到这一点严格非零解(即 x > 0)?

这是我使用 Scipy 的 NNLS 的 LP 代码:

import numpy as np
from numpy import array
from scipy.optimize import nnls

def by_nnls(A=None, B=None):
""" Linear programming by NNLS """
#print "NOF row = ", A.shape[0]
A = np.nan_to_num(A)
B = np.nan_to_num(B)

x, rnorm = nnls(A,B)
x = x / x.sum()
# print repr(x)
return x

B1 = array([ 22.133, 197.087, 84.344, 1.466, 3.974, 0.435,
8.291, 45.059, 5.755, 0.519, 0. , 30.272,
24.92 , 10.095])
A1 = array([[ 46.35, 80.58, 48.8 , 80.31, 489.01, 40.98,
29.98, 44.3 , 5882.96],
[ 2540.73, 49.53, 26.78, 30.49, 48.51, 20.88,
19.92, 21.05, 19.39],
[ 2540.73, 49.53, 26.78, 30.49, 48.51, 20.88,
19.92, 21.05, 19.39],
[ 30.95, 1482.24, 100.48, 35.98, 35.1 , 38.65,
31.57, 87.38, 33.39],
[ 30.95, 1482.24, 100.48, 35.98, 35.1 , 38.65,
31.57, 87.38, 33.39],
[ 30.95, 1482.24, 100.48, 35.98, 35.1 , 38.65,
31.57, 87.38, 33.39],
[ 15.99, 223.27, 655.79, 1978.2 , 18.21, 20.51,
19. , 16.19, 15.91],
[ 15.99, 223.27, 655.79, 1978.2 , 18.21, 20.51,
19. , 16.19, 15.91],
[ 16.49, 20.56, 19.08, 18.65, 4568.97, 20.7 ,
17.4 , 17.62, 25.51],
[ 33.84, 26.58, 18.69, 40.88, 19.17, 5247.84,
29.39, 25.55, 18.9 ],
[ 42.66, 83.59, 99.58, 52.11, 46.84, 64.93,
43.8 , 7610.12, 47.13],
[ 42.66, 83.59, 99.58, 52.11, 46.84, 64.93,
43.8 , 7610.12, 47.13],
[ 41.63, 204.32, 4170.37, 86.95, 49.92, 87.15,
51.88, 45.38, 42.89],
[ 81.34, 60.16, 357.92, 43.48, 36.92, 39.13,
1772.07, 68.43, 38.07]])

用法:

In [9]: by_nnls(A=A1,B=B1)
Out[9]:
array([ 0.70089761, 0. , 0.06481495, 0.14325696, 0.01218972,
0. , 0.02125942, 0.01906576, 0.03851557]

注意上面的零解。

最佳答案

如果你真的确定你想要严格正解,你可以使用lsq_linear在最新的 scipy 版本中可用。它比 nnls 允许更多地控制边界。

In [37]: from scipy.optimize import lsq_linear

In [38]: lsq_linear(A1, B1, bounds=(0.001, np.inf))
Out[38]:
active_mask: array([ 0, -1, 0, 0, -1, -1, 0, 0, 0])
cost: 3784.3150152135881
fun: array([ -0.06189388, -56.45892624, 56.28407376, 2.97647016,
0.46847016, 4.00747016, 18.24947887, -18.51852113,
0.19599207, 7.32663679, 15.0829264 , -15.1890736 ,
-0.14570891, -0.24341795])
message: 'The first-order optimality measure is less than `tol`.'
nit: 17
optimality: 5.4491449547056092e-11
status: 1
success: True
x: array([ 0.05506904, 0.001 , 0.00501077, 0.01112669, 0.001 ,
0.001 , 0.00154812, 0.00147833, 0.00300156])

关于python - 为 Python 的 Scipy 线性规划寻找严格大于零的解决方案的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35426444/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com