Robotat Linalg
Loading...
Searching...
No Matches
Quadprog

Convex quadratic problem solver, with direct methods for inequality constrained problems and an active-set method for inequality constrained problems. More...

Data Structures

struct  quadprog_t
 Quadratic Program (QP or quadprogram) problem structure. More...

Enumerations

enum  quadprog_status_t {
  QP_SUCCESS , QP_SIZE_MISMATCH , QP_NOT_RESTRICTED , QP_NOT_CONVEX ,
  QP_BAD_DEFINED
}
 Operation status from quadprog. More...
enum  quadprog_method_t {
  QP_KKT_NULLSPACE , QP_KKT_LDLT , QP_LU , QP_SVD ,
  QP_QR , SQP_ACTIVE_SET
}
 Quadprog-specific methods for solving QPs. More...

Functions

void quadprog_status_print (quadprog_status_t status)
 Print quadprog status.
void quadprog_init (quadprog_t *const p_qp, const matf32_t *const p_Q, const matf32_t *const p_c, const matf32_t *const p_Aeq, const matf32_t *const p_beq, const matf32_t *const p_Ain, const matf32_t *const p_bin, const matf32_t *const p_x0)
 Constructor for the quadratic problem data structure. Set as null any argment that isn't needed.
quadprog_status_t quadprog (quadprog_t *p_qp, matf32_t *const p_x, bool set_linsolve_method, quadprog_method_t method)
 Quadratic convex problem solver, choosing automatically the method to be used.
quadprog_status_t quadprog_qp_linsolve (quadprog_t *p_qp, matf32_t *const p_x, linsolve_method_t method)
 Equality restricted quadratic convex problem solver by constructing a KKT system and then solving it through one of the following linsolve methods: LU, SVD or QR (the method must be specificed in the arguments of the function). The KKT system is constructed as follows:
quadprog_status_t quadprog_qp_nullspace (quadprog_t *p_qp, matf32_t *const p_x)
 Solves a convex quadratic problem with equality restrictions using the null-space method for KKT systems. This method does NOT construct and solve the KKT system directly, instead, it does the following procedure as described in Nocedal, Numerical Optimization:
quadprog_status_t quadprog_qp_ldlt (quadprog_t *p_qp, matf32_t *const p_x)
 Solves a convex quadratic problem with equality restrictions using the LDL' factorization, as explained in Nocedal, Numerical Optimization.
quadprog_status_t quadprog_sqp (quadprog_t *p_qp, matf32_t *const p_x)
 Solves a convex quadratic problem with an active-set binding direction method, based on Martins, Engineering Design Optimization, algorithm 5.4, but divided in four cases depending on the combination of restrictions that can be found in each iteration, so that the KKT system that's built and solved in each iteration is as small as possible (increasing solving speed and stability). As such, the procedure implemented in this routine is as follows:

Detailed Description

Convex quadratic problem solver, with direct methods for inequality constrained problems and an active-set method for inequality constrained problems.

Last modified: 16 Nov. 2026 By: Andrea Pineda


Data Structure Documentation

◆ quadprog_t

struct quadprog_t

Quadratic Program (QP or quadprogram) problem structure.

Collaboration diagram for quadprog_t:
[legend]
Data Fields
const matf32_t * p_Aeq

Equality conditions matrix

const matf32_t * p_Ain

Inequality conditions matrix

const matf32_t * p_beq

Equality conditions vector

const matf32_t * p_bin

Inequality conditions vector

const matf32_t * p_c

Cost function vector

const matf32_t * p_Q

Cost function matrix

const matf32_t * p_x0

Starting point

Enumeration Type Documentation

◆ quadprog_method_t

Quadprog-specific methods for solving QPs.

Enumerator
QP_KKT_NULLSPACE 

Direct method for solving a KKT system using the null-space matrix of Aeq

QP_KKT_LDLT 

Direct method for solving a KKT system using the LDL' symmetric indefinite factorization

QP_LU 

Solve an equality constrained QP with linsolve using LU (method not specific for KKT systems)

QP_SVD 

Solve an equality constrained QP using SVD (method not specific for KKT systems)

QP_QR 

Solve an equality constrained QP using QR (method not specific for KKT systems)

SQP_ACTIVE_SET 

Solve a constrained QP with an active set binding direction method

◆ quadprog_status_t

Operation status from quadprog.

Enumerator
QP_SUCCESS 

Operation completed correctly

QP_SIZE_MISMATCH 

Matrices/vectors are not the correct size

QP_NOT_RESTRICTED 

Missing restrictions

QP_NOT_CONVEX 

Problem is not convex

QP_BAD_DEFINED 

Problem is not correctly defined

Function Documentation

◆ quadprog()

quadprog_status_t quadprog ( quadprog_t * p_qp,
matf32_t *const p_x,
bool set_linsolve_method,
quadprog_method_t method )

Quadratic convex problem solver, choosing automatically the method to be used.

Parameters
[in]p_qpPoints to the structure representing the problem to solve.
[in,out]p_xPoints to the vector to store the result.
Returns
Execution status.
Here is the call graph for this function:

◆ quadprog_init()

void quadprog_init ( quadprog_t *const p_qp,
const matf32_t *const p_Q,
const matf32_t *const p_c,
const matf32_t *const p_Aeq,
const matf32_t *const p_beq,
const matf32_t *const p_Ain,
const matf32_t *const p_bin,
const matf32_t *const p_x0 )

Constructor for the quadratic problem data structure. Set as null any argment that isn't needed.

Parameters
[in,out]p_qpPoints to structure representing the problem.
[in]p_QCost function matrix.
[in]p_cCost function vector.
[in]p_AinInequality conditions matrix.
[in]p_vinInequality conditions vector.
[in]p_AeqEquality conditions matrix.
[in]p_veqEquality conditions vector.
[in]p_x0Starting point.
Returns
None

◆ quadprog_qp_ldlt()

quadprog_status_t quadprog_qp_ldlt ( quadprog_t * p_qp,
matf32_t *const p_x )

Solves a convex quadratic problem with equality restrictions using the LDL' factorization, as explained in Nocedal, Numerical Optimization.

Starting with the KKT system for the problem with equality restrictions:

\[\begin{bmatrix} \textbf{Q} & \textbf{A}^\top \\ \textbf{A} & \textbf{0} \end{bmatrix} \begin{bmatrix} \textbf{-p} \\ \boldsymbol{\lambda} \end{bmatrix} = \begin{bmatrix} \textbf{c} \\ \textbf{b} \\ \end{bmatrix} \]

The \( \textbf{LDL}^\top \) factorization is applied to the matrix of the system, to get the resulting structure:

\[ \textbf{P}^\top \textbf{MP} = \textbf{LDL}^\top \]

where \( \textbf{P} \) is a permutation matrix, while \( \textbf{L} \) and \( \textbf{D} \) are, respectively, a lower triangular and diagonal matrix, all generated during the procedure of the factorization.

Afterwards, the KKT system is solved as follows:

\[ \textbf{Lz}_1 = \textbf{P}^\top \textbf{b} \]

\[ \textbf{Bz}_2 = \textbf{z}_1 \]

\[ \textbf{L}^\top \textbf{z}_3 = \textbf{z}_2 \]

\[ \textbf{y} = \textbf{Pz}_3 \]

where:

\[ \textbf{b} = \begin{bmatrix} \textbf{g} \\ \textbf{h} \end{bmatrix} \]

\[ \textbf{y} = \begin{bmatrix} - \textbf{p} \\ \boldsymbol{\lambda} \end{bmatrix} \]

The specific algorithm in this routine for the factorization corresponds to the symmetric indefinite factorization with Bunch and Parlett Pivoting as presented in section 4.4.3 of Golub, Matrix Computations. The different linear systems in the procedure are solved using linsolve.

Parameters
[in]p_qpPoints to the structure representing the problem to solve.
[in,out]p_xPoints to the vector to store the solution for the QP.
Returns
Execution status
Here is the call graph for this function:

◆ quadprog_qp_linsolve()

quadprog_status_t quadprog_qp_linsolve ( quadprog_t * p_qp,
matf32_t *const p_x,
linsolve_method_t method )

Equality restricted quadratic convex problem solver by constructing a KKT system and then solving it through one of the following linsolve methods: LU, SVD or QR (the method must be specificed in the arguments of the function). The KKT system is constructed as follows:

\[\begin{bmatrix} \textbf{Q} & \textbf{A}^\top \\ \textbf{A} & \textbf{0} \end{bmatrix} \begin{bmatrix} \textbf{-p} \\ \boldsymbol{\lambda} \end{bmatrix} = \begin{bmatrix} \textbf{c} \\ \textbf{b} \\ \end{bmatrix} \]

according to the matrices of the cost function and restrictions of the QP, and the definition of the KKT system (Nocedal, Numerical Optimization).

Parameters
[in]p_qpPoints to the structure representing the problem to solve.
[out]p_xPoints to the vector to store the solution to the QP.
[in]methodLinsolve method to use for solving the system: LU, SVD or QR.
Returns
Execution status.
Here is the call graph for this function:

◆ quadprog_qp_nullspace()

quadprog_status_t quadprog_qp_nullspace ( quadprog_t * p_qp,
matf32_t *const p_x )

Solves a convex quadratic problem with equality restrictions using the null-space method for KKT systems. This method does NOT construct and solve the KKT system directly, instead, it does the following procedure as described in Nocedal, Numerical Optimization:

Starting from the definition of a KKT system for the problem with equality restrictions:

\[\begin{bmatrix} \textbf{Q} & \textbf{A}^\top \\ \textbf{A} & \textbf{0} \end{bmatrix} \begin{bmatrix} \textbf{-p} \\ \boldsymbol{\lambda} \end{bmatrix} = \begin{bmatrix} \textbf{c} \\ \textbf{b} \\ \end{bmatrix} \]

The solution vector \( \textbf{p} \) is calculated through partial solutions, which are as follows:

\[ \textbf{p} = \textbf{Yp}_y + \textbf{Zp}_z \]

where \( \textbf{Y} \) is (n x m) and \( \textbf{Z} \) is (n x (n-m)), and are generated from the \( \textbf{Q} \) matrix of the QR factorization.

Then, the vectors \( \textbf{p}_y \) and \( \textbf{p}_z \) of the partial solutions are calculated, by solving the following linear systems:

\[ \textbf{AYp}_y = - \textbf{h} \]

\[ \textbf{Z}^\top \textbf{QZp}_z = -\textbf{Z}^\top \textbf{Q} \textbf{Yp}_y - \textbf{Z}^\top \textbf{g} \]

After which, \( \textbf{p} \) can be calculated.

Currently, this routine does NOT calculate the Lagrange multipliers associated to the solution of the QP, but if you need them, the following operation should be added:

\[ \textbf{AY}^\top \boldsymbol{\lambda} = \textbf{Y}^\top (\textbf{g} + \textbf{Gp}) \]

Parameters
[in]p_qpPoints to the structure representing the problem to solve.
[in,out]p_xPoints to the vector to store the solution for the QP.
Returns
Execution status.
Here is the call graph for this function:

◆ quadprog_sqp()

quadprog_status_t quadprog_sqp ( quadprog_t * p_qp,
matf32_t *const p_x )

Solves a convex quadratic problem with an active-set binding direction method, based on Martins, Engineering Design Optimization, algorithm 5.4, but divided in four cases depending on the combination of restrictions that can be found in each iteration, so that the KKT system that's built and solved in each iteration is as small as possible (increasing solving speed and stability). As such, the procedure implemented in this routine is as follows:

Case 1: There are both equality and inequality restrictions

\[\begin{bmatrix} \textbf{Q} & \textbf{A}_{eq}^\top & \textbf{A}_w^\top\\ \textbf{A}_{eq} & 0 & 0 \\ \textbf{A}_w & 0 & 0 \end{bmatrix} \begin{bmatrix} \textbf{p}_k \\ \boldsymbol{\lambda}_{eq} \\ \boldsymbol{\lambda}_w \end{bmatrix} = - \begin{bmatrix} \textbf{Qx}_k + \textbf{c} \\ \textbf{A}_{eq}\textbf{x}_k - \textbf{b}_{eq} \\ \textbf{A}_w \textbf{x}_k - \textbf{b}_w \end{bmatrix} \]

Case 2: There are only equality restrictions

\[\begin{bmatrix} \textbf{Q} & \textbf{A}_{eq}^\top \\ \textbf{A}_{eq} & 0 \end{bmatrix} \begin{bmatrix} \textbf{p}_k \\ \boldsymbol{\lambda}_eq \end{bmatrix} = - \begin{bmatrix} \textbf{Qx}_k + \textbf{c} \\ \textbf{A}_{eq} - \textbf{b}_{eq} \end{bmatrix} \]

Case 3: There are only inequality restrictions (and at least one is active)

\[\begin{bmatrix} \textbf{Q} & \textbf{A}_w^\top \\ \textbf{A}_w & 0 \end{bmatrix} \begin{bmatrix} \textbf{p}_k \\ \boldsymbol{\lambda}_w \end{bmatrix} = - \begin{bmatrix} \textbf{Qx}_k + \textbf{c} \\ \textbf{A}_w - \textbf{b}_w \end{bmatrix} \]

Case 4: There are no restrictions (either the problem was unconstrained or had only inequality restrictions such as, currently, all are inactive)

\[ \textbf{Qp}_k = -(\textbf{Qx}_k + \textbf{c}) \]

Warning
This routine currently uses quadprog_qp_linsolve to solve the KKT system inside each iteration of the active-set method, and it's currently set to use the LU factorization to solve the system. this can be changed to use SVD or QR by modifying the corresponding argument in quadprog_qp_linsolve. Take into consideration that using the SVD makes the routine slower. On the other hand, quadprog_qp_nullspace and quadprog_qp_ldlt are not currently used nor tested in the active-set method, so if you want to use those routines inside the active-set, further modifications might be needed to quadprog_sqp. Though, for devices with very limited FLASH memory capacity, quadprog_qp_ldlt is NOT recommended as it's the most memory-expensive method of the available quadprog routines and in combination with quadprog_sqp, could increase the memory usage considerably.
Parameters
[in]p_qpPoints to the structure representing the problem to solve.
[in,out]p_xPoints to the vector to store the result.
Returns
Execution status.
Here is the call graph for this function:

◆ quadprog_status_print()

void quadprog_status_print ( quadprog_status_t status)

Print quadprog status.

Parameters
[in]p_qpquadprog execution status.
Returns
None.