Ethunxxx
Ethunxxx

Reputation: 1337

Specifying the ranges in scipy.optimize.brute()

When I try to minimize a function with scipy.minimize.brute() using the following code:

import scipy
scipy.optimize.brute(lambda x: x**2, ranges=(-2,3))

I get the following error:

TypeError: object of type 'int' has no len()

I suppose it has to do with the specification of the range, but I don't see why. The documentation says

Each component of the ranges tuple must be either a “slice object” or a range tuple of the form (low, high).

Where is my mistake?

Upvotes: 4

Views: 3208

Answers (1)

obachtos
obachtos

Reputation: 1061

As the documentation says:

Each component of the ranges tuple must be either a “slice object” or a range tuple of the form (low, high).

So the function expects a tuple of tuples, one of the form (low, high) for each dimension. You only have one dimension, so the correct call in your case would be

scipy.optimize.brute(lambda x: x**2, ranges=((-2,3),) )

Upvotes: 7

Related Questions