简体   繁体   English

Pyomo + Mosek 麻烦:系数矩阵不是半正定的

[英]Pyomo + Mosek trouble: Coefficient matrix is not positive semidefinite

We are formulating a QP optimization problem in Pyomo + Mosek (commercial).我们正在 Pyomo + Mosek(商业)中制定 QP 优化问题。

Unexpectedly, mosek complains the quadratic coefficient is not PSD.出乎意料的是,mosek 抱怨二次系数不是 PSD。

Error: rescode.err_obj_q_not_psd(1295): The quadratic coefficient matrix in the objective is not positive semidefinite as expected for a minimization problem.

Minimal reproducible example:最小的可重现示例:

import pyomo.kernel as pmo
import numpy as np; np.random.seed(1)

n = 5
Q1 = np.random.randn(n, n)
Q1 = Q1.T @ Q1            # theoretically always PSD
m = 5
A1 = np.random.randn(m, n)
b1 = np.random.randn(m)

problem = pmo.block()

problem.x = pmo.variable_list()
for i in range(n):
    problem.x.append(pmo.variable())

problem.OBJ = pmo.objective(expr = problem.x @ Q1 @ problem.x, sense = pmo.minimize)

problem.cons = pmo.constraint_list()
tmp_lhs = A1 @ problem.x
for i in range(len(b1)):
    problem.cons.append(pmo.constraint(expr= tmp_lhs[i] <= b1[i]))

opt = pmo.SolverFactory("mosek")
opt.solve(problem)

Reasons we think Q1 is PSD:我们认为 Q1 是 PSD 的原因:

  • All its eigenvalues are positive它的所有特征值都是正的
  • CPLEX (commercial) is able to solve it CPLEX(商业)能够解决它

Kindly help!请帮忙!

I fixed this bug in a PR back in Feb 2021. However, it seems that the current release (v 5.7.3) does not have the fix yet.我在 2021 年 2 月的 PR 中修复了这个错误。但是,当前版本(v 5.7.3)似乎还没有修复。 You can do two things (for both of these you would need to know where pyomo is installed, find that out using print(pyomo.__file__) in the python console):您可以做两件事(对于这两件事,您需要知道 pyomo 的安装位置,在 python 控制台中使用print(pyomo.__file__)找到它):

  1. Clone the Pyomo github repository (master branch) and use that as your pyomo installation.克隆 Pyomo github 存储库(主分支)并将其用作您的 pyomo 安装。 Hint: you can install pyomo using pip, and then replace the pyomo installation (somewhere in env/lib/site-packages/pyomo) with a symbolic link the repo clone.提示:您可以使用 pip 安装 pyomo,然后将 pyomo 安装(在 env/lib/site-packages/pyomo 中的某处)替换为 repo 克隆的符号链接。 #lifehack #生活黑客

  2. If you have Pyomo 5.7.3, then you can make the fix yourself.如果你有 Pyomo 5.7.3,那么你可以自己修复。 If you go to file: python3.8/site-packages/pyomo/solvers/plugins/solvers/mosek_direct.py then you only need to change line number 253 from mosek_qexp = (qsubi, qsubj, qvals) to mosek_qexp = (qsubj, qsubi, qvals) .如果您将 go 归档: python3.8/site-packages/pyomo/solvers/plugins/solvers/mosek_direct.py那么您只需将第 253 行从mosek_qexp = (qsubi, qsubj, qvals)更改为mosek_qexp = (qsubj, qsubi, qvals)

The second option should be quicker.第二个选项应该更快。

Sorry for the inconvenience.带来不便敬请谅解。 It is a bit confusing why the release does not have this fix yet, but I will raise this issue with the maintainers of the repo.为什么这个版本还没有这个修复有点令人困惑,但我会向 repo 的维护者提出这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM