pythonmatlabnumpyoptimizationquadratic-programming

Quadratic Programming in Python using Numpy?


I am in the process of translating some MATLAB code into Python. There is one line that is giving me a bit of trouble:

[q,f_dummy,exitflag, output] = quadprog(H,f,-A,zeros(p*N,1),E,qm,[],[],q0,options);

I looked up the documentation in MATLAB to find that the quadprog function is used for optimization (particularly minimization).

I attempted to find a similar function in Python (using numpy) and there does not seem to be any.

Is there a better way to translate this line of code into Python? Or are there other packages that can be used? Do I need to make a new function that accomplishes the same task?

Thanks for your time and help!


Solution

  • There is a library called CVXOPT that has quadratic programming in it.

    def quadprog_solve_qp(P, q, G=None, h=None, A=None, b=None):
        qp_G = .5 * (P + P.T)   # make sure P is symmetric
        qp_a = -q
        if A is not None:
            qp_C = -numpy.vstack([A, G]).T
            qp_b = -numpy.hstack([b, h])
            meq = A.shape[0]
        else:  # no equality constraint
            qp_C = -G.T
            qp_b = -h
            meq = 0
        return quadprog.solve_qp(qp_G, qp_a, qp_C, qp_b, meq)[0]