python - 解释为什么会发生错误:ValueError: 输入数组的所有大小,除了连接轴,必须完全匹配
问题描述
我正在使用scipy.optimize.minimize在 Python 中优化一个函数。我按照文档中的示例编写了代码:
import numpy as np
from scipy.optimize import minimize
# I set bounds and constraints
bnds = ((10e-6, 2000), (10e-6, 16000), (10e-6, 120), (10e-6, 5000), (10e-6, 2000), (85, 93), (90, 95), (3, 12), (1.2, 4), (145, 162))
eq_cons = {'type': 'eq',
'fun': lambda x: np.array([1.22 * x[3] - x[0] - x[4],
(98000 * x[2]) / x[3] * x[8] + 1000 * x[2] - x[5],
((x[1] + x[4]) / x[0]) - x[7]]),
'jac': lambda x: np.array([[1.22, -1, -1],
[98000, 1, 1, 1000, -1],
[1, 1, 1, -1]])
}
ineq_cons = {'type': 'ineq',
'fun': lambda x: np.array([x[0] * (1.12 + 0.13167 * x[7] - 0.0067 * x[7] ** 2) - 0.99 * x[3],
-(x[0] * (1.12 + 0.13167 * x[7] - 0.0067 * x[7] ** 2) + (100 / 99) * x[3]),
(86.35 + 1.098 * x[7] - 0.038 * x[7] ** 2 + 0.325 * (x[5] - 89)) - 0.99 * x[6],
-(86.35 + 1.098 * x[7] - 0.038 * x[7] ** 2 + 0.325 * (x[5] - 89)) + (100 / 99) * x[6],
(35.82 - 0.222 * x[9]) - 0.9 * x[8],
-(35.82 - 0.222 * x[9]) + (10 / 9) * x[8],
(-133 + 3 * x[6]) - 0.99 * x[9],
-(-133 + 3 * x[6]) + (100 / 99) * x[9]]),
'jac': lambda x: np.array([[1, 0, 0.13167, -0.0134 * x[7], -0.99],
[-1, 0, -0.13167, 0.0134 * x[7], -(100 / 99)],
[0, 1.098, -0.076 * x[7], 0.325, -0.99],
[0, -1.098, 0.076 * x[7], -0.325, -(100 / 99)],
[0, -0.222, -0.9],
[0, 0.222, -(10 / 9)],
[0, 3, -0.99],
[0, -3, -(100 / 99)]])
}
# I set the initial values of variables
x0 = np.array([1745, 12000, 110, 3048, 1974, 89.2, 92.8, 8, 3.6, 145])
def f(x):
return 0.063 * x[3] * x[6] - 5.04 * x[0] - 0.035 * x[1] - 10 * x[2] - 3.36 * x[4]
res = minimize(f, x0, method='SLSQP', constraints=[ineq_cons, eq_cons], bounds=bnds, options={'ftol': 1e-9, 'disp': True})
print(res.x)
这是回溯:
Traceback (most recent call last):
File "C:\Users\user\Desktop\Python\optimize.py", line 41, in <module>
res = minimize(f, x0, method='SLSQP', constraints=[ineq_cons, eq_cons], bounds=bnds, options={'ftol': 1e-9, 'disp': True})
File "C:\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py", line 611, in minimize
constraints, callback=callback, **options)
File "C:\Anaconda3\lib\site-packages\scipy\optimize\slsqp.py", line 422, in _minimize_slsqp
a = vstack((a_eq, a_ieq))
File "C:\Anaconda3\lib\site-packages\numpy\core\shape_base.py", line 234, in vstack
return _nx.concatenate([atleast_2d(_m) for _m in tup], 0)
ValueError: all the input array dimensions except for the concatenation axis must match exactly
当我不在 eq_cons 和 ineq_cons 中使用 jac 时,计算会进行,但不正确。我得到输出:
C:\Anaconda3\python.exe C:\Users\user\Desktop\Python\optimize.py
Positive directional derivative for linesearch (Exit mode 8)
Current function value: 0.00610273865507208
Iterations: 82
Function evaluations: 1050
Gradient evaluations: 78
fun: 0.00610273865507208
jac: array([-5.04000000e+00, -3.49999999e-02, -1.00000000e+01, 5.98500000e+00,
-3.36000000e+00, 0.00000000e+00, 4.70466912e-04, 0.00000000e+00,
0.00000000e+00, 0.00000000e+00])
message: 'Positive directional derivative for linesearch'
nfev: 1050
nit: 82
njev: 78
status: 8
success: False
x: array([3.91761754e-03, 3.69776009e-02, 1.00000000e-05, 7.46772900e-03,
5.19422947e-03, 9.30000000e+01, 9.50000000e+01, 1.10436932e+01,
1.56130612e+00, 1.53536361e+02])
维度有什么问题?我不明白哪些数组的维度不匹配以及如何修复它?
解决方案
推荐阅读
- django - 在 django 视图中使用 URL 传递的值并将其呈现给模板
- swift - Swift 5:向 GestureRecognizer 发送函数时更改标签错误
- reactjs - 使用 React.js 的真正优势是什么?是虚拟 DOM 还是单向数据流?还是别的什么?
- xml - 如何使用 xml 在电子邮件主题中插入昨天的日期?
- arrays - 如何在 Bash 中创建哈希等价物
- python - OpenCV 图像处理 GUI:TypeError:on_click_diff_per_button() 采用 1 个位置参数,但给出了 2 个
- amazon-web-services - AWS 拥有的 CMK 多久轮换一次?
- json - 如何从具有Golang中json对象列表的文件中读取单个json对象
- swift - 聊天备份和恢复
- swift - 为什么我的 AVCapturePhotoOutput 文件输出这么大?