简体   繁体   中英

Pyomo + Mosek trouble: Coefficient matrix is not positive semidefinite

We are formulating a QP optimization problem in Pyomo + Mosek (commercial).

Unexpectedly, mosek complains the quadratic coefficient is not 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:

  • All its eigenvalues are positive
  • CPLEX (commercial) is able to solve it

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. 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):

  1. Clone the Pyomo github repository (master branch) and use that as your pyomo installation. 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. #lifehack

  2. If you have Pyomo 5.7.3, then you can make the fix yourself. 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) .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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