[SciPy-user] Minimizing functions of two variables with fmin_bfgs

LOPEZ GARCIA DE LOMANA, ADRIAN alopez at imim.es
Mon Nov 14 08:51:54 CST 2005


Hi all, 

I have a problem using the optimization modules. I'm using fmin_bfgs. It works very well for minimizing functions of just one parameter:

import Numeric
import scipy
from scipy.optimize import fmin_bfgs

def fitness(p):
    return p**2 

def fitness_der(p):
    return 2 * p 

p = [158.0]
popt = fmin_bfgs(fitness, p, fprime = fitness_der)
print popt

but while I pretend to expand it to a multiparameter function using a vector,

import Numeric
import scipy
from scipy.optimize import fmin_bfgs

def fitness(p):
    return p[0]**2 + p[1]

def fitness_der(p):
    return [2 * p[0] + 1, 1] 

p = [158.0, 314.0]
popt = fmin_bfgs(fitness, p, fprime = fitness_der)
print popt

it crashes:  

Traceback (most recent call last):
  File "hybrid.py", line 12, in ?
    popt = fmin_bfgs(fitness, p, fprime = fitness_der)
  File "/usr/local/lib/python2.4/site-packages/scipy/optimize/optimize.py", line 675, in fmin_bfgs
    yk = gfkp1 - gfk
TypeError: unsupported operand type(s) for -: 'list' and 'list'

Some ideas? How can I minimize a function of several parameters? "fprime = fitness_der" does no understand that the partial derivatives are a list of same dimension of "p"?

Thanks a lot for your comments in advance, 

Adrián.



More information about the SciPy-user mailing list