Search code examples
optimizationcvxoptquadratic-programming

Quadratic Programming with a large number of variables using CVXOPT


I am new to CVXOPT. I have tried out the example quadratic program (with 2 variables) in CVXOPT documentation, and I am able to understand it. Now I need to solve a quadratic programming problem with a large number of variables (eg: 100 variables). How can I do this using CVXOPT?

The problem that I want to solve is shown below.

Minimize

Σ [ d(t) + x(t) ]²        ; t=1, ....., 100

such that,

0 <= x(t) <= 10

Σ x(t) = 600

Here, d(t) is known for t=(1, ...,100).

x(t) for t=(1, ...,100) are the decision variables.  

Cheers !!! 


Solution

  • cvxpy may be a bit easier:

    import numpy as np
    import cvxpy as cvx
    
    N = 100
    d = np.random.uniform(-500,500,N)
    
    x = cvx.Variable(N)
    prob = cvx.Problem(cvx.Minimize(cvx.norm(x+d)), [x >= 0, x <= 10, sum(x) == 600])
    prob.solve()
    print(prob.status)
    v = x.value
    print(v)
    

    This gives

    optimal
    [[  3.65513295e-09]
     [  4.89791266e-09]
     [  3.05045765e-09]
     [  9.99999999e+00]
     . . .
     [  1.00000000e+01]
     [  2.85640643e-09]
     [  5.42473434e-09]]