pythoncvxoptquadratic-programming

inputting specific constraints into cvxopt QP


I have a rather complex quadratic problem that I am trying to solve in python, however, for the sake of this question - i am significantly simplifying the problem at hand.

I have the following quadratic function I am trying to minimize while satisfying the following constraints:

minimize 0.5 * x.T * P * x + q.T * x

where: 

x >= 0
x[0] >= 1.5
x[n] >= 1.5 # n = last element

I have written the equivalent in scipy.optimize :

def minimize_func(x,y,P):
    return 0.5*np.dot(x.T,np.dot(P,x)) + np.dot(y.T,x)

cons = ({'type':'ineq','fun': lambda x: x},
        {'type':'ineq','fun': lambda x: x[0] - 1.5},
        {'type':'ineq','fun': lambda x: x[n] - 1.5})

However, my question is how do I input specific constraints in cvxopt quadratic solver?

I have looked into the cvxopt documentation page and none of the examples they give seem related to my question.I am looking to input element wise constraints. Any help is greatly appreciated.


Solution

  • cvxopt is focusing on the natural matrix-form, which might look quite low-level for people without any knowledge about the internals.

    All you need is documented in the user-manual

    cvxopt.solvers.qp(P, q[, G, h[, A, b[, solver[, initvals]]]])
    

    solves:

    enter image description here

    Assuming n=3 and your constraints (i assume 0-indexing -> n-1 is the last element):

    x >= 0
    x[0] >= 1.5
    x[n-1] >= 1.5 # n-1 = last element 
    

    this will look like:

    G = 
    -1   0   0
     0  -1   0
     0   0  -1
    
    h = -1.5
            0
        -1,5
    

    The reasoning is simple:

         - 1 * x[0] + 0 * x[1] + 0 * x[2] <= -1.5
    <->  -     x[0]                       <= -1.5
    <->        x[0]                       >=  1.5  
    

    (We ignored the non-negative constraints for x[0] and x[1] here as >= 1.5 is more restrictive)

    There are lots of helper functions in numpy/scipy and co. to do this more easily (e.g. np.eye(n)).

    In general, i would recommend to use cvxpy which is a more high-level modelling-tool allowing to call cvxopt's solvers too.