[SciPy-user] basic usage of fmin_tnc and fmin_l_bfgs_b
Gilles Rochefort
gilles.rochefort at gmail.com
Sun May 31 17:43:30 EDT 2009
physeco a écrit :
> I'm new to multidimensional optimization with scipy. Sorry for asking the
> simple question, but I can't figure out the syntax for fmin_tnc and
> fmin_l_bfgs_b. Here's a simple example of the error I'm getting:
>
> Executing:
>
>> def f(x):
>>
> return (x[0]*x[1]-1)**2+1
>
>> g=0.1,0.1
>> b=[(-10,10),(-10,10)]
>> so.fmin_tnc(f,g,bounds=b)
>>
>
> Leads to the error:
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> /usr/lib/python2.5/site-packages/scipy/optimize/tnc.py in fmin_tnc(func, x0,
> fprime, args, approx_grad, bounds, epsilon, scale, offset, messages,
> maxCGit, maxfun, eta, stepmx, accuracy, fmin, ftol, xtol, pgtol, rescale)
> 244 rc, nf, x = moduleTNC.minimize(func_and_grad, x0, low, up,
> scale, offset,
> 245 messages, maxCGit, maxfun, eta, stepmx, accuracy,
> --> 246 fmin, ftol, xtol, pgtol, rescale)
> 247 return array(x), nf, rc
> 248
>
> /usr/lib/python2.5/site-packages/scipy/optimize/tnc.py in func_and_grad(x)
> 203 def func_and_grad(x):
> 204 x = asarray(x)
> --> 205 f, g = func(x, *args)
> 206 return f, list(g)
> 207 else:
>
> <type 'exceptions.TypeError'>: 'numpy.float64' object is not iterable
> ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
>
> I get a similar error from so.fmin_l_bfgs_b(f,g,bounds=b). If you can point
> out my mistake, it would be greatly appreciate.
>
> Thank you!
>
Hi, function to be minimized is supposed to return both function value
and gradient.
You can eventually provide a gradient function separately.
If you do not want or do not have a gradient function to provide, you
can always turn to True the approx_gradient
argument, which compute a numerical approximation of your function.
To take back your example :
fmin_l_bfgs_b(f,g,approx_grad=True, bounds=b)
(array([ 0.99999789, 0.99999789]),
1.0000000000178644,
{'funcalls': 8,
'grad': array([ -8.45989945e-06, -8.45989945e-06]),
'nbiter': 4,
'task': 'CONVERGENCE: NORM OF PROJECTED GRADIENT <= PGTOL',
'warnflag': 0})
fmin_tnc works the same as fmin_l_bfgs_b.
Gilles.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.scipy.org/pipermail/scipy-user/attachments/20090531/9d70196a/attachment.html>
More information about the SciPy-User
mailing list