我正在将 MATLAB 程序的一部分转换为 Python 和 Octave。
我使用 Octave 生成两个矩阵,然后使用 oct2py
将这些矩阵导入到 python 中。 .我的问题的根源是 MATLAB 中的这些行(下面的 H_combined
和 f_combined
)
handles.options =optimset('algorithm','interior-point-convex','Display','off','TolFun',1e-15,'TolX',1e-10,'MaxFunEvals', 1E5);
handles.x_ridge_combined = quadprog(H_combined, f_combined, [], [], [], [], handles.lb_re, handles.ub_re, handles.x_re_0, handles.options);
目前,我正在寻找 Python 或 Octave 中的解决方案,该解决方案会产生类似的输出但无济于事。
我曾尝试使用
quadprog
来自 Octave 的 optim
但是我得到了 120, 1, 1, 1, ..., 1
的输出在 x_ridge_combined
,而不是我期望的各种浮点值。我已经确认 H_combined
和 f_combined
与在 MATLAB 中运行时的完全相同,但我想 quadprog
在 Octave 中不一样。在尝试 Octave 方法后,我尝试将值导入 Python 以尝试使用
quadprog
包裹。正在尝试
quadprog
,print(quadprog.solve_qp(H,f))
产生错误
ValueError: Buffer has wrong number of dimensions (expected 1, got 2)
H
的种类和形状和 f
如下面所述:<class 'numpy.ndarray'> #H
(123, 123)
<class 'numpy.ndarray'> #f
(1, 123)
有谁知道为什么我可能会收到这些错误?或者关于如何从 MATLAB 翻译该行的任何其他建议?
最佳答案
尝试使用 cvxopt_quadprog .作者声称它模仿了 MATLAB quadprog
,它应该以同样的方式接受参数:
def quadprog(H, f, L=None, k=None, Aeq=None, beq=None, lb=None, ub=None):
"""
Input: Numpy arrays, the format follows MATLAB quadprog function: https://www.mathworks.com/help/optim/ug/quadprog.html
Output: Numpy array of the solution
"""
最有可能的错误是因为您的
f
是矩阵 [1x123],而它应该是长度为 [123] 的向量。你可以尝试 reshape 它:f = f.reshape(f.shape[1])
关于python - 使用来自 Octave 音程的数据在python中进行二次编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57360462/