coin-or / qpOASES

Open-source C++ implementation of the recently proposed online active set strategy
GNU Lesser General Public License v2.1
384 stars 129 forks source link

Optimal solution off compared to analytical solution when Equality constraints are used #134

Closed MaximilianDio closed 2 years ago

MaximilianDio commented 2 years ago

Hi, while trying to solve a general QP problem I have some issues. Therefore, I reduced the problem to only contain equality constraints, where I can calculate the solution analytically. However, when I try to solve the problem with qpOASES, the solution is completely off although the solution is found.

The Problem min_x 1/2x^T H x + x^Tg s.t. A*x+b=0

The analytical solution including the dual Soulution:

[H A^T; A 0]⁻¹*[-g;-b]=[x,y]

My qp setup using Eigen:

lbA = -b;
ubA = -b;

qproblem.init(H.data(),g.data(), A.data(),nullptr,nullptr,lbA.data(),ubA.data(),nWSR,&cputime)

qproblem.getPrimalSolution(xOpt);
qproblem.getDualSolution(yOpt);

I have no Idea why I get a completely different solution compared to the analytical solutions. Any Suggestions?

apotschka commented 2 years ago

By convention, qpOASES uses a negative sign in the Lagrangian (see manual p. 15, footnote). Does swapping the sign of yOpt yield your expected solution?

MaximilianDio commented 2 years ago

I had a look at the dual solution, and it is zero. And the analytical is not.

MaximilianDio commented 2 years ago

Hey, I found my problem. I thought you only need to allocate n_C elements for yOpt because I do not include the basic bounds (nullptr). However, this will overwrite the primal solution. When I allocate enough space everything works fine.