bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitsim::maths::optimization::QPSolver Class Reference

A solver of the convex quadratic program. More...

#include <qp_solver.h>

Classes

struct  Sparse

Public Types

using Matrix = RealMat
using Vector = RealColVec

Public Member Functions

 QPSolver (const QPSettings &settings=QPSettings())
 ~QPSolver ()
 QPSolver (const QPSolver &other)
QPSolver & operator= (const QPSolver &other)
 QPSolver (QPSolver &&other) noexcept
QPSolver & operator= (QPSolver &&other) noexcept
void setup (const Matrix &P, const Vector &q, const Matrix &C, const Vector &l, const Vector &u)
 Set the matrices and the bounds, and factorise the matrix of the iterations.
void setup (index_t n, const std::vector< QPEntry > &P, const Vector &q, index_t m, const std::vector< QPEntry > &C, const Vector &l, const Vector &u)
 The same, with P, n x n, and C, m x n, given by their non-zeros: the solver works with sparse matrices inside, so a problem with mostly zeros, as in model predictive control, costs only its non-zeros. Entries at the same place are added up.
void update (const Vector &q, const Vector &l, const Vector &u)
 Change q and the bounds, keeping P and C and the factorisation.
QPStatus solve ()
 Solve, starting from the last solution.
void warm_start (const Vector &x)
 Start the next solve from x, and from no constraint being active.
void reset ()
 Forget the last solution: the next solve starts from zero.
const Vector & solution () const noexcept
const Vector & duals () const noexcept
uint_t iterations () const noexcept
real_t primal_residual () const noexcept
real_t dual_residual () const noexcept
QPStatus status () const noexcept
const QPSettings & settings () const noexcept

Detailed Description

A solver of the convex quadratic program.

\[\min_x \tfrac{1}{2} x^T P x + q^T x \quad \text{subject to} \quad l \le C x \le u , \]

with P symmetric positive semi-definite, by the alternating direction method of multipliers (ADMM) of OSQP (Stellato et al., OSQP: an operator splitting solver for quadratic programs, 2020). A row with \(l_i = u_i\) is an equality, and a bound of \(\pm\infty\) is no bound.

Every iteration solves one linear system with the matrix \(P + \sigma I + C^T \rho C\), factorised with a sparse Cholesky factorisation, so an iteration costs two sparse triangular solves. As in OSQP, the problem is first equilibrated: the variables, the constraints and the cost are scaled so that the rows and the columns of its matrices have similar norms, which ADMM needs to converge in reasonable time when the numbers of the problem span orders of magnitude. And \(\rho\) adapts to balance the primal and the dual residuals; that needs a new factorisation, so it changes only when it is off by a factor of 5. The tolerances apply to the problem as it was given.

It suits the problems of model predictive control, with a few thousand variables at most. The solver keeps its last solution, and the next solve starts from it, which in MPC, where one problem is much like the last, saves most of the iterations. ADMM converges to a modest accuracy quickly and to a high one slowly: the default tolerances, 1e-4, are enough for control, and much tighter ones can take thousands of iterations.

Member Typedef Documentation

◆ Matrix

◆ Vector

Constructor & Destructor Documentation

◆ QPSolver() [1/3]

bitsim::maths::optimization::QPSolver::QPSolver ( const QPSettings & settings = QPSettings())
explicit

◆ ~QPSolver()

bitsim::maths::optimization::QPSolver::~QPSolver ( )
default

◆ QPSolver() [2/3]

bitsim::maths::optimization::QPSolver::QPSolver ( const QPSolver & other)

◆ QPSolver() [3/3]

bitsim::maths::optimization::QPSolver::QPSolver ( QPSolver && other)
defaultnoexcept

Member Function Documentation

◆ dual_residual()

real_t bitsim::maths::optimization::QPSolver::dual_residual ( ) const
inlinenoexcept

◆ duals()

const Vector & bitsim::maths::optimization::QPSolver::duals ( ) const
inlinenoexcept
Returns
The multipliers of the constraints: positive where an upper bound is active, negative where a lower one is

◆ iterations()

uint_t bitsim::maths::optimization::QPSolver::iterations ( ) const
inlinenoexcept

◆ operator=() [1/2]

QPSolver & bitsim::maths::optimization::QPSolver::operator= ( const QPSolver & other)

◆ operator=() [2/2]

QPSolver & bitsim::maths::optimization::QPSolver::operator= ( QPSolver && other)
defaultnoexcept

◆ primal_residual()

real_t bitsim::maths::optimization::QPSolver::primal_residual ( ) const
inlinenoexcept

◆ reset()

void bitsim::maths::optimization::QPSolver::reset ( )

Forget the last solution: the next solve starts from zero.

◆ settings()

const QPSettings & bitsim::maths::optimization::QPSolver::settings ( ) const
inlinenoexcept

◆ setup() [1/2]

void bitsim::maths::optimization::QPSolver::setup ( const Matrix & P,
const Vector & q,
const Matrix & C,
const Vector & l,
const Vector & u )

Set the matrices and the bounds, and factorise the matrix of the iterations.

Exceptions
std::invalid_argumentif the sizes do not match, or l > u somewhere
std::runtime_errorif the matrix cannot be factorised, which means P is not positive semi-definite

◆ setup() [2/2]

void bitsim::maths::optimization::QPSolver::setup ( index_t n,
const std::vector< QPEntry > & P,
const Vector & q,
index_t m,
const std::vector< QPEntry > & C,
const Vector & l,
const Vector & u )

The same, with P, n x n, and C, m x n, given by their non-zeros: the solver works with sparse matrices inside, so a problem with mostly zeros, as in model predictive control, costs only its non-zeros. Entries at the same place are added up.

◆ solution()

const Vector & bitsim::maths::optimization::QPSolver::solution ( ) const
inlinenoexcept

◆ solve()

QPStatus bitsim::maths::optimization::QPSolver::solve ( )

Solve, starting from the last solution.

◆ status()

QPStatus bitsim::maths::optimization::QPSolver::status ( ) const
inlinenoexcept

◆ update()

void bitsim::maths::optimization::QPSolver::update ( const Vector & q,
const Vector & l,
const Vector & u )

Change q and the bounds, keeping P and C and the factorisation.

◆ warm_start()

void bitsim::maths::optimization::QPSolver::warm_start ( const Vector & x)

Start the next solve from x, and from no constraint being active.


The documentation for this class was generated from the following files: