robotology / osqp-eigen

Simple Eigen-C++ wrapper for OSQP library
https://robotology.github.io/osqp-eigen/
BSD 3-Clause "New" or "Revised" License
395 stars 118 forks source link

In the QP problem solving, why the lower triangular matrix using the same data can be solved successfully, but the solution of the upper triangular matrix fails #114

Closed Syk-yr closed 2 years ago

Syk-yr commented 2 years ago

In the QP problem solving, why the lower triangular matrix using the same data can be solved successfully, but the solution of the upper triangular matrix fails

Syk-yr commented 2 years ago

Matrix P=[ 2 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 26 -50 0 0 0 0 0 0 0 0 0 0 0 51 -50 0 0 0 0 0 0 0 0 0 0 0 51 -50 0 0 0 0 0 0 0 0 0 0 0 26 ]

S-Dafarra commented 2 years ago

Hy @Syk-yr, thanks for opening the issue. Just to make sure we are aligned, is the matrix P the cost hessian?

S-Dafarra commented 2 years ago

Hy @Syk-yr, thanks for opening the issue. Just to make sure we are aligned, is the matrix P the cost hessian?

To answer myself, I remembered later that OSQP calls the hessian matrix P. Then, to answer your initial question

In the QP problem solving, why the lower triangular matrix using the same data can be solved successfully, but the solution of the upper triangular matrix fails

In general, the matrix P is supposed to be symmetric matrix. You can check why this is necessary at https://math.stackexchange.com/questions/307381/why-do-we-assume-that-a-matrix-in-quadratic-form-is-symmetric.

For this reason, OSPQ requires the user to only insert the upper triangular part of the matrix, because it assumes it to be symmetric. If you pass only the upper triangular part of the matrix, it basically assumes you have the following matrix P

P =

     2     0     0     0     0     0     0     0     0     0     0     0
     0     2     0     0     0     0     0     0     0     0     0     0
     0     0     2     0     0     0     0     0     0     0     0     0
     0     0     0     2     0     0     0     0     0     0     0     0
     0     0     0     0     1     0     0     0     0     0     0     0
     0     0     0     0     0     1     0     0     0     0     0     0
     0     0     0     0     0     0     1     0     0     0     0     0
     0     0     0     0     0     0     0     1     0     0     0     0
     0     0     0     0     0     0     0     0    26   -50     0     0
     0     0     0     0     0     0     0     0   -50    51   -50     0
     0     0     0     0     0     0     0     0     0   -50    51   -50
     0     0     0     0     0     0     0     0     0     0   -50    26

This matrix is not positive definite (you can simply check the determinant), that instead is a requirement https://osqp.org/docs/index.html. This si why the QP fails

S-Dafarra commented 2 years ago

Note that you can obtain the symmetric version of the matrix you have in https://github.com/robotology/osqp-eigen/issues/114#issuecomment-1054208677 by doing (P + P')/2, obtaining


P =

     2     0     0     0     0     0     0     0     0     0     0     0
     0     2     0     0     0     0     0     0     0     0     0     0
     0     0     2     0     0     0     0     0     0     0     0     0
     0     0     0     2     0     0     0     0     0     0     0     0
     0     0     0     0     1     0     0     0     0     0     0     0
     0     0     0     0     0     1     0     0     0     0     0     0
     0     0     0     0     0     0     1     0     0     0     0     0
     0     0     0     0     0     0     0     1     0     0     0     0
     0     0     0     0     0     0     0     0    26   -25     0     0
     0     0     0     0     0     0     0     0   -25    51   -25     0
     0     0     0     0     0     0     0     0     0   -25    51   -25
     0     0     0     0     0     0     0     0     0     0   -25    26

That is positive definite.

Syk-yr commented 2 years ago

Thank you very much for your answer, I see where my mistake is.

------------------ 原始邮件 ------------------ 发件人: "Stefano @.>; 发送时间: 2022年3月4日(星期五) 凌晨0:27 收件人: @.>; 抄送: @.>; @.>; 主题: Re: [robotology/osqp-eigen] In the QP problem solving, why the lower triangular matrix using the same data can be solved successfully, but the solution of the upper triangular matrix fails (Issue #114)

请注意,您可以获得矩阵的对称版本(评论)通过做( P + P ')/ 2 ,获得 P = 2 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 26 -25 0 0 0 0 0 0 0 0 0 0 -25 51 -25 0 0 0 0 0 0 0 0 0 0 -25 51 -25 0 0 0 0 0 0 0 0 0 0 -25 26 这是肯定的。

— 直接回复此邮件,在 GitHub 上查看或取消订阅. 使用 GitHub Mobile 随时随地发送分流通知IOS 系统或机器人. 你收到这个,因为你被提到了。留言 ID @.***与>