Basin-hopping helps find the lowest point (global minimum) of a complex function. It avoids getting stuck in small dips (local minima) by jumping around.
0
0
Basin-hopping for global minima in SciPy
Introduction
When you want to find the best solution in a problem with many ups and downs.
When simple methods get stuck in a local low point and miss the true lowest point.
When optimizing functions that are not smooth or have many peaks and valleys.
When you want a reliable way to explore different areas of the solution space.
When you have a function where guessing the starting point is hard.
Syntax
SciPy
from scipy.optimize import basinhopping result = basinhopping(func, x0, niter=100, stepsize=0.5)
func is the function you want to minimize.
x0 is the starting guess for the solution.
Examples
Finds the minimum of a simple parabola starting from 0.
SciPy
from scipy.optimize import basinhopping def func(x): return (x - 3)**2 + 5 result = basinhopping(func, 0) print(result.x)
Minimizes a function with two variables starting from [1,1].
SciPy
from scipy.optimize import basinhopping def func(x): return x[0]**2 + x[1]**2 + 10 result = basinhopping(func, [1, 1], niter=50, stepsize=1) print(result.x)
Sample Program
This code tries to find the lowest point of a wavy function that has many dips. Basin-hopping jumps around to avoid getting stuck in small dips and finds the lowest one.
SciPy
from scipy.optimize import basinhopping import numpy as np def func(x): # A function with many local minima return np.sin(3 * x) + (x - 1)**2 # Start at x=0 result = basinhopping(func, 0, niter=100, stepsize=0.5) print(f"Global minimum found at x = {result.x:.4f}") print(f"Function value at minimum = {result.fun:.4f}")
OutputSuccess
Important Notes
Basin-hopping combines random jumps with local minimization to explore the function better.
You can adjust niter (number of jumps) and stepsize (jump size) to improve results.
It works well for functions with many local minima but can be slower than simple methods.
Summary
Basin-hopping helps find the lowest point in tricky functions with many dips.
It uses random jumps plus local searches to avoid getting stuck.
You can control how many jumps and how big they are to balance speed and accuracy.