Search code examples
pythoncvxpy

solving a non-linear convex minimization problem in cvxpy


I have a non-linear convex optimization problem, which can be expressed in a cost function like this: Objective Function The task of the solver should be to find values for the variables which minimize the value. constraint are : constants

How would the minimize function for cvxpy look in that case?

I do this but it doesn't work

import cvxpy as cp
ppMax = 1

Tmax = [1.19, 1.99, 4.16, 1.98, 2.53]
d = [2648000, 5552000, 4744000, 4056000, 6168000]
p = [0.19952623149688797, 0.00018021843172751523, 0.0020210434604112652, 0.001602417432034276, 0.003647501823979989]
r = [8574212.020483451, 6619470.077787987, 7521159.373986546, 7135440.631765847, 6832684.423897811]
c = [430000000.0, 700000000.0, 400000000.0, 220000000.0, 170000000.0]
fc = [40000000000, 40000000000, 40000000000, 40000000000, 40000000000]
ff = [4000000000, 4000000000, 4000000000, 4000000000, 4000000000]
W = [0.7, 0.2, 0.3, 0.7, 0.5]
wt = [0.609, 0.04000000000000001, 0.255, 0.308, 0.43]
we = [4.336742687028045, 10.647756980938421, 8.263103073749088, 7.675258157093112, 6.322105707432189]


pp = cp.Variable(5)
cons = [cp.sum(pp) <= ppMax, d/r+c/fc+d/(W*cp.log(1+pp)) <= Tmax]
object = cp.Minimize(wt*(2*d/r+c/ff+c/fc+d/(W*cp.log(1+pp)))+we*(2*p*(d/r)+pp*(d/(W*cp.log(1+pp)))))
prob = cp.Problem(object, cons)
prob.solve()
print(prob.value, pp.value)

I get this error for constants and object

Exception has occurred: TypeError
unsupported operand type(s) for /: 'list' and 'list'

The output should be an array of length 5 of the pp variable that minimizes the object


Solution

  • The error comes from the fact, that division / is not implemented for generic lists like you are using.

    d = [2648000, 5552000, 4744000, 4056000, 6168000]
    r = [8574212.020483451, 6619470.077787987, 7521159.373986546, 7135440.631765847, 6832684.423897811]
    
    print(d/r)
    

    Results in the error:

    TypeError: unsupported operand type(s) for /: 'list' and 'list'
    

    You can try to use numpy-arrays instead of the lists as those define the division operation:

    import numpy as np
    
    
    d = np.array([2648000, 5552000, 4744000, 4056000, 6168000])
    r = np.array([8574212.020483451, 6619470.077787987, 7521159.373986546, 7135440.631765847, 6832684.423897811])
    
    print(d/r)
    

    Works as expected with the output:

    [0.30883304 0.83873783 0.63075382 0.56843021 0.90271987]