Python Scipy Optimization method: global optimization failed

Viewed 301

I am trying to use a Scipy global optimizer to find a global min of a function, but instead of giving the global min as an answer, it stuck in a local min. The code:

def f(x):
    return x**2 + 10*np.sin(x)
x = np.arange(-10, 10, 0.1)
print(optimize.basinhopping(f, 3))

Can anyone tell me why? And which method in Scipy you think is the best one to solve global optimization?

2 Answers

Only SHGO will provide you guarantees. It also has a better performance based on my benchmarking exercise. Basin-hopping can spend too much time on one possible local minima whereas the homology based technique (i.e. shgo, which you can also pip install and use separately of scipy if you want) avoids this in a cunning fashion.

I should add that if you really want to be careful, you should change the default SHGO sampling (it will use Sobol by default, which technically breaks the guarantee).

The simple scipy.optimize is finding the global minimum for your problem. Run the following code:

import math
import numpy as np
from scipy.optimize import minimize

def objective(x):
    # min f(x)
    return x[0]**2+10*math.sin(x[0])

if __name__ == "__main__":
    # initial guesses
    n = 1
    x0 = np.zeros(n)
    x0[0] = 90.0

    # show initial objective
    print('\nInitial Objective: ' + str(objective(x0)))

    # state bounds
    b1 = (-100.0, 100.0)
    bds = (b1,)
    print("\n")
    solution = minimize(objective, x0, method='SLSQP', jac=None, bounds=bds,
                        tol = 1e-20, constraints=(),
                        options={'maxiter': 1000000, 'ftol': 1e-20, 'disp': True})
    
    # z is a numpy.ndarray vector
    z = solution.x

    # show final objective
    print('\nFinal Objective')
    print('f* = ' + str(objective(z)))

    # print solution
    print('\nSolution')
    print('x1* = ' + str(z[0]))

which outputs:

Initial Objective: 10005.063656411097


Optimization terminated successfully.    (Exit mode 0)
            Current function value: -7.9458233756152845
            Iterations: 7
            Function evaluations: 34
            Gradient evaluations: 7

Final Objective
f* = -7.9458233756152845

Solution
x1* = -1.3064400096083357
Related