I’m very new to CVX and saw it used in a paper for real-time solving of Control Barrier Functions (CBFs) with an updating speed of 1kHz. I’m trying to use it for the same purpose to solve for the control input of N(=50) agents with N constraints.

Here’s the code snippet I’m using in MATLAB:

%%
cvx_begin quiet
variable x(2 * N)

% Objective function
minimize(0.5 * quad_form(x, H) + f' * x);
% Constraints
subject to
A * x <= b;

cvx_end
%%

However, when I implement this, it runs about 10 times slower than using MATLAB’s quadprog. Is this normal? Am I implementing something wrong or is there a way to optimize this?

For context:

My Hessian matrix is H=speye(2∗N)

My constraint matrix A is N×2N.

Any insights or suggestions to improve the performance would be greatly appreciated!

CVX takes time to process and transform the problem into the form needed by the solver, and a little time to transform back. If quadprog is called directly from MATLAB, the model processing and transformation time will be saved.

Remove quiet so you can see the solver and CVX output, and see how long the solver takes, which may depend on which solver is used.

CVX is designed to save human model creation and debugging time, at the expense of computer time. It seems that you already know how to form all the needed input matrices and vectors in terms of a single vector optimization variable, so CVX is not providing much benefit in this case. Only if a solver is called from CVX which is faster than quadprog, would there be any chance of overall CVX computer time being competitive with directly calling quadprog.

In addition, the problem is tiny which means setup time and various overhead will dominate.
Almost surely cvx was not optimized to be as fast as possible for case like this.