Migrating from CVXPY directly to Solver or How to Make CVXPY run faster

91 Views Asked by At

I am trying to solve a convex optimization problem very quickly with python. My problem looks like this:

enter image description here

with p, m, z and o being arrays of values. Z being the one to solve for. and both p and z being from 0-1 inclusive and the sum of all z adding up to 1

After some searching, people recommended trying cvxpy and using trying their different solvers. Which I went on to do. Here is what my basic solve code looks like in cvxpy format:

z = cp.Variable(num_values_z, nonneg=True)

combo_matrix = cp.multiply(z, o)
combo_matrix2 = cp.matmul(combo_matrix, m.T).T
log_sum = cp.sum(cp.multiply(cp.log(combo_matrix2), p))
obj = cp.Maximize(log_sum)

constraint_zero_one = [z <= 1]
constraint_sum_one = [cp.sum(z) == 1]
constraint_two = [p <= 1]

(also p and z are set to be non-negative)

Thing thing is, when I solve a problem with verbose turned on it tells me that the solution was found in around 200 microseconds, which is very fast and a time that I am trying to get close to. In practice however, running 10k iterations takes about 30 seconds, which is more than ten times slower than the solving alone takes. The vast majority of that time is compiling.

Doing some reading of cvxpy documentation, cvxpy was apparently not designed for speed at all. It also seems that cvxpy is spending time and resources checking if the problem is convex, when I know for a fact mine is by definition. cvxpy itself suggested I mitigate this by setting my inputs as parameters, which I did, but this sped it up an imperceptible amount.

So next I started looking into cutting out the middleman and trying to interface with the solvers that worked fastest on cvxpy (which turned out to be CLARABEL and ECOS) directly. But according to Clarabel documentation it solves problems in the format:

enter image description here

I have no clue how to set my problem in that format. It must be possible because cvxpy is able to do it, but I'm at a loss for how I can do it. The clarabel documentation example looks nothing like my problem and I can't find any more information on it.

Does anyone know how I can input an optimization problem like mine in a format that is directly readable by a solver like Clarabel or ECOS? Or alternately, is there a way to speed up cvxpy to have comparable solving speed? Or is there a third option that I don't know about. I'd welcome any and all information you can give me on the matter. Thanks for your help!

*EDIT: On request, here are some examples of p,m and o data:

p:
[1.         5.5        5.99       5.9        6.4        5.8
 5.9        1.85       2.07       1.21276596 1.19230769 1.19607843
 1.17857143 1.2        1.19607843 2.03092784 1.83333333]

m:
[[1 1 0 0 0 0 0 1 0 0 1 1 1 1 1 0 1]
 [1 0 1 0 0 0 0 0 1 1 0 1 1 1 1 1 0]
 [1 0 0 1 0 0 0 1 0 1 1 0 1 1 1 0 1]
 [1 0 0 0 1 0 0 0 1 1 1 1 0 1 1 1 0]
 [1 0 0 0 0 1 0 1 0 1 1 1 1 0 1 0 1]
 [1 0 0 0 0 0 1 0 1 1 1 1 1 1 0 1 0]]

o:
[0.16666667 0.16666667 0.16666667 0.16666667 0.16666667 0.16666667]
0

There are 0 best solutions below