The return type of Dual Annealing OptimiseResult 'fun' is float64 and sometimes it is ndarray with single float64

45 Views Asked by At

I am using the scipy optimise function dual annealing to solve a gradient descent problem of a single variable. aka find the input that minimises the value of the cost function.

The min value of the cost function is stored in the'fun' attribute of the result returned from dual_annealing which is an OptimiseResult class. I have added links to the classes below.

My issue is that this 'fun' attribute which is the optimised output, is a single numpy.float64 but occasionally it is a numpy.ndarray which contains a single numpy.float64. i.e. fun is usually 10.32 sometimes [10.32].

I have been unable to find anything that suggests what may cause this. For the moment I will write some checks to ensure it is a numpy.float64 but I wish to know the root cause.

Can anyone please suggest what may be the cause of the fun attribute being an ndarray?

For context this arose as I am running this optimisation continuously and it occasionally just hit an error. After some debugging found it wasn't consistent in its return type.

An example code block is

result = dual_annealing(cost_function, bounds)
optimised_input = result.x[0]
optimised_output = -result.fun
#sometimes optimised_output is 10.32, sometimes [10.32] - why?

Dual Annealing function https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.dual_annealing.html

OptimiseResult (Dual Annealing return type) https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.OptimizeResult.html#scipy.optimize.OptimizeResult

0

There are 0 best solutions below