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.
Basin-hopping for global minima in SciPy
Start learning this pattern below
Jump into concepts and practice - no test required
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.
from scipy.optimize import basinhopping def func(x): return (x - 3)**2 + 5 result = basinhopping(func, 0) print(result.x)
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)
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.
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}")
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.
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.
Practice
Solution
Step 1: Understand the goal of basin-hopping
Basin-hopping is designed to find the lowest point (global minimum) in complex functions that have many dips (local minima).Step 2: Compare with other options
Options A, B, and C describe unrelated tasks: differentiation, regression, and sorting, which are not the purpose of basin-hopping.Final Answer:
To find the global minimum of a function with many local minima -> Option DQuick Check:
Basin-hopping = global minimum search [OK]
- Confusing basin-hopping with simple optimization methods
- Thinking it sorts or differentiates functions
- Assuming it only finds local minima
Solution
Step 1: Recall correct import syntax in Python
To import a specific function from a module, use 'from module import function'.Step 2: Match with scipy.optimize and basinhopping
The basin-hopping function is inside scipy.optimize, so the correct import is 'from scipy.optimize import basinhopping'.Final Answer:
from scipy.optimize import basinhopping -> Option CQuick Check:
Correct import syntax = from scipy.optimize import basinhopping [OK]
- Using incorrect import order or syntax
- Trying to import basin-hopping directly from scipy
- Using 'import basinhopping from ...' which is invalid
import numpy as np
from scipy.optimize import basinhopping
def func(x):
return (x - 3)**2 + 5
result = basinhopping(func, x0=0, niter=5)
print(round(result.fun, 2))Solution
Step 1: Understand the function and its minimum
The function is (x - 3)^2 + 5, which has its minimum value at x=3, and the minimum value is 5.Step 2: Analyze basin-hopping output
Basin-hopping tries to find the global minimum. Starting at x0=0, after 5 iterations, it should find near x=3, so the function value is near 5.Final Answer:
5.00 -> Option AQuick Check:
Minimum value of (x-3)^2+5 = 5 [OK]
- Confusing minimum value with x-coordinate
- Assuming starting point is the minimum
- Ignoring the constant +5 in the function
from scipy.optimize import basinhopping
def f(x):
return x**2
result = basinhopping(f, x0=[1, 2], niter=10)
print(result.x)Solution
Step 1: Check input types for basin-hopping
basinhopping accepts x0 as a scalar or array-like. A list like [1, 2] is valid and converted to numpy array internally.Step 2: Verify function output
Function f(x) = x**2. For vector x = np.array([1,2]), it returns np.array([1,4]), not a scalar. Optimization requires scalar objective function value.Step 3: Test code behavior
The code raises an error because the objective function returns an array instead of scalar.Final Answer:
Function f must return a scalar, but it returns a list -> Option BQuick Check:
Objective func must return scalar [OK]
- Assuming no error; overlooking non-scalar function return
- Thinking x0 list causes the error
- Believing x0 must be scalar or explicit numpy array
Solution
Step 1: Understand basin-hopping parameters
'stepsize' controls how big the random jumps are between local minimizations. Bigger steps help jump out of local minima.Step 2: Evaluate options
Decreasing 'niter' reduces attempts, lowering success. Setting 'minimizer_kwargs' to None disables local minimization, which is needed. Fixed start without randomization limits exploration.Final Answer:
Increase the 'stepsize' parameter to allow bigger jumps -> Option AQuick Check:
Bigger stepsize = better escape from local minima [OK]
- Reducing iterations thinking it speeds up convergence
- Disabling local minimization by setting minimizer_kwargs to None
- Using fixed start point limits search space
