0
0
SciPydata~20 mins

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

Choose your learning style9 modes available
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.