[SciPy-user] basic usage of fmin_tnc and fmin_l_bfgs_b

josef.pktd@gmai... josef.pktd@gmai...
Sun May 31 13:18:11 CDT 2009


On Sun, May 31, 2009 at 1:03 PM,  <josef.pktd@gmail.com> wrote:
> On Sun, May 31, 2009 at 12:54 PM,  <josef.pktd@gmail.com> wrote:
>> On Sun, May 31, 2009 at 12:17 PM, physeco <andrewenoble@gmail.com> wrote:
>>>
>>> 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!
>>> --
>>> View this message in context: http://www.nabble.com/basic-usage-of-fmin_tnc-and-fmin_l_bfgs_b-tp23798939p23798939.html
>>> Sent from the Scipy-User mailing list archive at Nabble.com.
>>>
>>> _______________________________________________
>>> SciPy-user mailing list
>>> SciPy-user@scipy.org
>>> http://mail.scipy.org/mailman/listinfo/scipy-user
>>>
>>
>> from the description, the function needs to return both, the function
>> value and gradient values:
>>
>> func : callable func(x, *args)
>> Function to minimize. Should return f and g, where f is the value of
>> the function and g its gradient (a list of floats). If the function
>> returns None, the minimization is aborted.
>>
>>
>> import scipy.optimize as so
>> def f(x):
>>    return (x[0]*x[1]-1)**2+1, [(x[0]*x[1]-1)*x[1], (x[0]*x[1]-1)*x[0]]
>> g = np.array([0.1,0.1])
>> b=[(-10,10),(-10,10)]
>> so.fmin_tnc(f,g,bounds=b)
>>
>
> I usually check whether there are any good usage examples in the test
> files, for these cases see
> scipy\optimize\tests\test_optimize.py
>

with numerical gradient

def f0(x):
    return (x[0]*x[1]-1)**2+1
print so.fmin_tnc(f0, g, bounds=b, approx_grad=1)

note: my gradients are missing factor 2

Josef


More information about the SciPy-user mailing list