[SciPy-User] quadratic programming with fmin_slsqp

josef.pktd@gmai... josef.pktd@gmai...
Fri Mar 16 11:45:33 CDT 2012

scipy is missing a fmin_quadprog


Did anyone ever try to see if fmin_slsqp can be used for this?
It looks flexible and targeted enough to be the base for a quadratic
programming wrapper.

So far I only got a quick experiment


# -*- coding: utf-8 -*-

Created on Thu Mar 15 20:25:27 2012

Author: Josef Perktold

just a first try

write a function fmin_quadprog(func, A,B,C,b,c)

max xAx
st Bx = b
   Cx >= c

check if there is a standard notation for Matrices

can work with f_eqcons and f_ieqcons

I'm not sure how good the checking is
I thought I had f_ieqcons that conflicted with eqcons, but got
normal successful convergence. f_ineqcons was binding but didn't satisfy eqcons

If f_ieqcons is defined, then ieqcons is ignored. see docstring


from time import time
import numpy as np
from scipy.optimize import fmin_slsqp

def func(x, args):
    A = np.eye(len(x))
    A[0,0] = 2
    x = np.atleast_2d(x)
    return np.dot(np.dot(x,A), x.T)

B = np.eye(2)[0]
b = np.ones(2)[0]
f_ieqcons = lambda x, *args: np.atleast_1d(np.dot(x, B) - b)

t0 = time()
xres = fmin_slsqp(func,[2.0,1.0],  args=(-1.0,),
               eqcons=[lambda x, args: x[0]+x[1] + 4 ],
#               ieqcons=[lambda x, args: x[0]+.5,
#                        lambda x, args: x[0]],
               iprint=2, full_output=1)

print "Elapsed time:", 1000*(time()-t0), "ms"
print "Results",xres
print "\n\n"

More information about the SciPy-User mailing list