optimizationcvxpyconvex-optimizationcvx

How to express a variable vector to the constant vector in cvxpy?


I want to express X^C in cvxpy where X is a convex expression as a vector and C is constant vector. How should I write this using cvxpy?

I tried cp.power(X,C) but in this function C should be a scalar. I also used a loop to form X^C element wise. However, it increases solving time and vectorization is required.


Solution

  • Since it looks like CVXPY converts the power operation by finding a decomposition into quadratic cones, if your c vector has different entries then for each of them a potentially different decomposition must be applied, so everything has to be done element by element anyway. So you will not be able to save any modeling time by any sort of vectorization. If that makes your solve slow then it is perhaps due to the complexity of the quadratic cone decomposition of the powers.

    (I'm actually a bit surprised by that; I thought powers would be converted to power cones if a solver supports them).

    I would just require that the power cone be used directly (if you are using a solver that supports power cones). Here are two equivalent models of

    minimize sum(x[i]^c[i]) 
    st. sum(x)==5, x>=0
    
    import cvxpy as cp
    
    c = [1.178,3,2.5,4,2]
    
    def model1():
        x = cp.Variable(5, pos=True)
    
        obj = cp.Minimize(cp.sum([cp.power(x[i], c[i]) for i in range(5)]))
    
        prob = cp.Problem(obj, [cp.sum(x) == 5])
    
        prob.solve(solver=cp.MOSEK, verbose=True, save_file="a.ptf")
    
    
    def model2():
        x = cp.Variable(5, pos=True)
        t = cp.Variable(5)
    
        cons = []
        for i in range(5):
            cons.append(cp.PowCone3D(t[i], 1, x[i], 1/c[i]))
    
        obj = cp.Minimize(cp.sum(t))
    
        prob = cp.Problem(obj, cons + [cp.sum(x) == 5])
    
        prob.solve(solver=cp.MOSEK, verbose=True, save_file="b.ptf")
    
    
    model1()
    model2()
    

    You can compare the two ptf files to see the difference in the conic models.