[SciPy-user] Minimizing functions of two variables withfmin_bfgs

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


THANKS A LOT !!!! It works fine !!!

Adrián.

-----Original Message-----
From: scipy-user-bounces at scipy.net on behalf of Nils Wagner
Sent: Mon 14/11/2005 15:02
To: SciPy Users List
Subject: Re: [SciPy-user] Minimizing functions of two variables withfmin_bfgs
 
LOPEZ GARCIA DE LOMANA, ADRIAN wrote:
>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.
>
>_______________________________________________
>SciPy-user mailing list
>SciPy-user at scipy.net
>http://www.scipy.net/mailman/listinfo/scipy-user
>  
Try

import scipy
from scipy.optimize import fmin_bfgs

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

def fitness_der(p):
    return scipy.array(([2 * p[0] , 2.0*p[1]] ))

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


 

_______________________________________________
SciPy-user mailing list
SciPy-user at scipy.net
http://www.scipy.net/mailman/listinfo/scipy-user



More information about the SciPy-user mailing list