【发布时间】:2021-10-09 07:55:48
【问题描述】:
我正在尝试用最小化(算法 = SLSQP)求解线性方程组,具有一组约束:解向量分量的总和必须为 1(或至少非常接近它,以最小化误差)第二个约束强制对向量分量进行排序,其中 x_0 最大,x_n 最小。此外,我设置了界限,因为每个向量分量都有
这是我目前的代码:
from scipy.optimize import minimize
import numpy as np
from scipy.sparse import rand
from scipy.optimize import lsq_linear
#Linear equation Ax = b
def fmin(x,A,b):
y = np.dot(A, x) - b
return np.dot(y, y)
#Test data
b = np.array([172,8,7.4,24,21,0.8,0.1])
A_t = np.array(
[[188,18.4,16.5,3.4,2.1,1.77,0.075],
[405,0,0,99.8,99.8,0,0.0054],
[90.5,0.4,0.009,19.7,15.6,1.06,0.012],
[322,0,0,79,79,0.3,0.3],
[0,0,0,0,0,0,0],
[362,0.25,0.009,89.2,0,0.43,0.019],
[37,1.4,0.2,7.3,1,4.5,0.1],
[26,0.29,0.038,6.1,2.4,0.4,0.053]])
A = A_t.T
#=========================
m = np.shape(A)[0]
n = np.shape(A)[1]
x0 = np.full(n, 0.5)
args = (A,b)
bnds = (( (1*10e-100, 1), )*n) #all x_i must be between 0 and 1
cons = [{'type': 'eq', 'fun': lambda x: 1.0-np.sum(x) }] #sum(x_i) = 1
#consider order of vectors as constraints
for i in range(n-1):
cons = cons + [{'type': 'ineq', 'fun': lambda x : x[i] - x[i+1] }]
res = minimize(fmin, x0, args, method='SLSQP',
bounds=bnds,constraints=cons,tol=1e-2,options={'disp': False})
print ("\n res\n", res)
print("Sum of coefficients {}".format(np.sum(res.x)))
print("Difference vector:\n{}".format(np.dot(A,res.x) - b))
不幸的是算法错误
res
fun: 317820.09898084006
jac: array([205597.34765625, 481389.625 , 105853.7265625 , 382592.76953125,
0. , 416196.953125 , 42268.78125 , 30196.81640625])
message: 'Positive directional derivative for linesearch'
nfev: 10
nit: 5
njev: 1
status: 8
success: False
x: array([0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5])
Sum of coefficients 4.0
Difference vector:
[5.4325e+02 2.3700e+00 9.7800e-01 1.2825e+02 7.8950e+01 3.4300e+00
1.8220e-01]
如果有人能帮我解决这个问题,我将不胜感激。事实上,对于这个例子中的测试数据,我知道正确的解决方案应该是 x_0 的 0.58 和 x_2 的 0.12。 非常感谢!
【问题讨论】:
-
欢迎来到 SO!这不是一个完整的可重现示例。你的目标是什么
fmin?另请注意,minimize解决了非线性问题,即至少目标或约束之一不是线性的。如果您的目标函数是线性的,您将面临一个可以通过scipy.optimize.linprog 解决的线性问题 (LP)。 -
嗨,对不起,我刚刚添加了上面缺少的
fmin并更新了最初的帖子。你是对的,这是一个线性问题。我可能需要一些时间来弄清楚如何为新求解器转换上述设置的约束,如果同时没有更容易的适应建议的话。到目前为止谢谢! -
如果我的回答有帮助,请考虑accepting and/or upvoting it。