Bird
Raised Fist0
SciPydata~20 mins

Basin-hopping for global minima in SciPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
🎖️
Basin-Hopping Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of basin-hopping on a simple function
What is the output of the following code that uses basin-hopping to find the minimum of a quadratic function?
SciPy
import numpy as np
from scipy.optimize import basinhopping

def func(x):
    return (x[0] - 3)**2 + (x[1] + 1)**2

initial_guess = [0, 0]
result = basinhopping(func, initial_guess, niter=10)
print(result.x.round(2))
A[nan, nan]
B[0.0, 0.0]
C[3.0, -1.0]
D[-3.0, 1.0]
Attempts:
2 left
💡 Hint
Think about where the quadratic function reaches its lowest value.
data_output
intermediate
1:30remaining
Number of iterations performed
After running basin-hopping with niter=15 on the function below, how many iterations does the result report?
SciPy
import numpy as np
from scipy.optimize import basinhopping

def func(x):
    return np.sin(x[0]) + (x[1] - 2)**2

result = basinhopping(func, [0, 0], niter=15)
print(result.nit)
A15
B14
C16
D0
Attempts:
2 left
💡 Hint
The nit attribute shows the number of iterations performed.
🧠 Conceptual
advanced
1:30remaining
Understanding the role of the 'stepsize' parameter
What effect does increasing the 'stepsize' parameter have in the basin-hopping algorithm?
AIt decreases the number of iterations performed.
BIt increases the size of random jumps, helping escape local minima more easily.
CIt changes the function to be minimized.
DIt sets the tolerance for convergence.
Attempts:
2 left
💡 Hint
Think about how the algorithm explores the search space.
🔧 Debug
advanced
1:30remaining
Identify the error in basin-hopping usage
What error will this code raise when executed?
SciPy
from scipy.optimize import basinhopping

def f(x):
    return x**2

result = basinhopping(f, 5, niter='10')
ANo error, runs successfully
BValueError: initial guess must be an array
CSyntaxError: invalid syntax
DTypeError: 'str' object cannot be interpreted as an integer
Attempts:
2 left
💡 Hint
Check the type of the niter parameter.
🚀 Application
expert
2:30remaining
Choosing the best initial guess for basin-hopping
You want to find the global minimum of a function with many local minima. Which initial guess strategy is best to improve basin-hopping results?
AUse multiple random initial guesses and run basin-hopping separately for each.
BUse a fixed initial guess at zero always.
CUse the derivative of the function as the initial guess.
DUse the maximum value of the function as the initial guess.
Attempts:
2 left
💡 Hint
Think about how to avoid getting stuck in local minima.

Practice

(1/5)
1. What is the main purpose of the basin-hopping algorithm in optimization?
easy
A. To calculate the derivative of a function
B. To perform a simple linear regression
C. To sort a list of numbers efficiently
D. To find the global minimum of a function with many local minima

Solution

  1. 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).
  2. 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.
  3. Final Answer:

    To find the global minimum of a function with many local minima -> Option D
  4. Quick Check:

    Basin-hopping = global minimum search [OK]
Hint: Basin-hopping = global minimum finder in tricky functions [OK]
Common Mistakes:
  • Confusing basin-hopping with simple optimization methods
  • Thinking it sorts or differentiates functions
  • Assuming it only finds local minima
2. Which of the following is the correct way to import the basin-hopping function from scipy?
easy
A. from scipy import basinhopping
B. import scipy.basinhopping
C. from scipy.optimize import basinhopping
D. import basinhopping from scipy.optimize

Solution

  1. Step 1: Recall correct import syntax in Python

    To import a specific function from a module, use 'from module import function'.
  2. 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'.
  3. Final Answer:

    from scipy.optimize import basinhopping -> Option C
  4. Quick Check:

    Correct import syntax = from scipy.optimize import basinhopping [OK]
Hint: Use 'from scipy.optimize import basinhopping' to import [OK]
Common Mistakes:
  • Using incorrect import order or syntax
  • Trying to import basin-hopping directly from scipy
  • Using 'import basinhopping from ...' which is invalid
3. What will be the output of the following code snippet?
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))
medium
A. 5.00
B. 0.00
C. 9.00
D. 3.00

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    5.00 -> Option A
  4. Quick Check:

    Minimum value of (x-3)^2+5 = 5 [OK]
Hint: Minimum of (x-3)^2+5 is 5 at x=3 [OK]
Common Mistakes:
  • Confusing minimum value with x-coordinate
  • Assuming starting point is the minimum
  • Ignoring the constant +5 in the function
4. Identify the error in the following code using basin-hopping:
from scipy.optimize import basinhopping

def f(x):
    return x**2

result = basinhopping(f, x0=[1, 2], niter=10)
print(result.x)
medium
A. No error; code runs correctly
B. Function f must return a scalar, but it returns a list
C. x0 should be a numpy array, not a list
D. x0 should be a scalar, not a list

Solution

  1. 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.
  2. 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.
  3. Step 3: Test code behavior

    The code raises an error because the objective function returns an array instead of scalar.
  4. Final Answer:

    Function f must return a scalar, but it returns a list -> Option B
  5. Quick Check:

    Objective func must return scalar [OK]
Hint: basinhopping objective must return scalar for vector x0 [OK]
Common Mistakes:
  • Assuming no error; overlooking non-scalar function return
  • Thinking x0 list causes the error
  • Believing x0 must be scalar or explicit numpy array
5. You want to find the global minimum of a function with many local minima using basin-hopping. Which parameter should you adjust to increase the chance of escaping local minima?
hard
A. Increase the 'stepsize' parameter to allow bigger jumps
B. Decrease the 'niter' parameter to reduce iterations
C. Set 'minimizer_kwargs' to None
D. Use a fixed starting point without randomization

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Increase the 'stepsize' parameter to allow bigger jumps -> Option A
  4. Quick Check:

    Bigger stepsize = better escape from local minima [OK]
Hint: Bigger stepsize helps jump out of local minima [OK]
Common Mistakes:
  • Reducing iterations thinking it speeds up convergence
  • Disabling local minimization by setting minimizer_kwargs to None
  • Using fixed start point limits search space