How to write a model in CVX to run efficiently?(both definition time and running time)

I wrote a model in CVX in two different forms. The model contains a matrix which parts of it is in a equality constraint, i.e. L(1:n-1,1:n-1)=H \odot R , where R and L are two semidefinite matrix variables.
In the first form, I define the matrix L based on its parts, i.e.
H\odot R &d_{Y}\
d_{Y}^\mathsf{T}&(h-\eta_t^\mathsf{T} p -\beta_t^\mathsf{T} r-d_t)
\end{matrix}\Bigr] \succcurlyeq 0\notag\
without any equality constraint. In the second form, I define the above matrix L and defined element-by-element equality constraints between L(1:n-1,1:n-1) and H \odot R using for loop. The second model takes much time till cvx_end, and before definition of the model to cvx completes.
My question is why is so? And how to define my model to CVX such that using the second form it works rapidly? And more general question, How to define such constraints efficiently to solvers directly?

Well, for loops are always to be avoided whenever possible. That’s actually an important principle for MATLAB in general, but for CVX it is much more important, because the objects that CVX uses to represent variables, expressions, and constraints can be a bit expensive to work with. So the fewer steps you can take to build your model, the better.

The fact of the matter is, though, that CVX is not designed for efficiency. It’s designed for convenience. Sure, you will learn tricks and tips to make models more efficient to build. But the primary benefit to CVX is that it saves you time. Your first priority is to build what works.

1 Like

Thank you. CVX is an excellent tool for rapidly building what works. After that you need to make it fast. Currently, my model in CVX works fine but now I need to make it faster by calling directly a solver, MOSEK. Is there any guideline about how to migrate your model from CVX to solver, in my case MOSEK. My model works fine in CVX but till now I can’t find why my MOSEK code doesn’t work. It is feasible with a different objective function from that returned by CVX.
I suggest that if there were any kind of support for this task(I don’t know if there is), i.e. migrating a working model to other solvers which built for speed, that would be excellent too and adds to the power of CVX.

Hey I hear you. There is only so much that a free tool can offer! In fact, one of the ways that I make money from CVX, which allows me to continue to support and maintain it, is for people to hire me as a consultant to help them with modeling and optimization issues that go beyond what CVX can do for free. Unfortunately, short of that, there is not going to be much that I can offer you.

YALMIP has an optimizer command, which is designed, subject to the limits of its applicability, to allow repeated solution of different instantiations of more or less the same optimization problem (including convex SDPs) without reincurring the modeling time for the various instantiations after the optimizer command is performed once. Unfortunately though, for complicated models, I don’t think there is a way to save the results of the optimizer command (the “one-time” translation of model into solver input) between MATLAB sessions.


Thank you, Mark_L_Stone, That is very interesting.

Hi, I was looking for something like this: Print the problem that is sent to the external solver