0
0
SciPydata~20 mins

Simulated annealing (dual_annealing) in SciPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Simulated Annealing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of dual_annealing on a simple quadratic function
What is the output of the following code snippet using scipy.optimize.dual_annealing on a quadratic function?
SciPy
from scipy.optimize import dual_annealing

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

bounds = [(-5, 5), (-5, 5)]
result = dual_annealing(func, bounds)
print((round(result.x[0], 1), round(result.x[1], 1)))
A(-3.0, 1.0)
B(0.0, 0.0)
C(3.0, -1.0)
D(-5.0, -5.0)
Attempts:
2 left
💡 Hint
The function has its minimum where both squared terms are zero.
data_output
intermediate
2:00remaining
Number of iterations in dual_annealing result
After running dual_annealing on a function, what is the value of result.nit representing the number of iterations?
SciPy
from scipy.optimize import dual_annealing
import numpy as np

def func(x):
    return np.sin(x[0]) + np.cos(x[1])

bounds = [(0, 10), (0, 10)]
result = dual_annealing(func, bounds)
print(result.nit)
AA float representing the function minimum value
BAn integer greater than zero representing iterations performed
CA tuple with the best solution coordinates
DA boolean indicating success or failure
Attempts:
2 left
💡 Hint
Check the documentation for result.nit attribute.
🔧 Debug
advanced
2:00remaining
Identify the error in dual_annealing usage
What error will this code raise when running dual_annealing with incorrect bounds?
SciPy
from scipy.optimize import dual_annealing

def func(x):
    return x[0]**2

bounds = [(0, 5), (1, 0)]  # Note the second bound is reversed
result = dual_annealing(func, bounds)
ANo error, runs successfully
BTypeError: func() missing 1 required positional argument
CRuntimeError: Maximum number of iterations exceeded
DValueError: bounds must be in increasing order
Attempts:
2 left
💡 Hint
Bounds must be specified with lower bound less than upper bound.
🧠 Conceptual
advanced
2:00remaining
Why use dual_annealing over other optimizers?
Which reason best explains why dual_annealing is chosen for optimization problems?
AIt combines stochastic and local search to escape local minima
BIt requires no bounds and works on infinite domains
CIt only works for linear functions but is very fast
DIt is guaranteed to find the global minimum for any function
Attempts:
2 left
💡 Hint
Think about how simulated annealing helps avoid local traps.
🚀 Application
expert
3:00remaining
Interpreting dual_annealing output for a noisy function
Given this noisy function and dual_annealing output, what is the best interpretation of the result's fun value?
SciPy
from scipy.optimize import dual_annealing
import numpy as np

def noisy_func(x):
    noise = np.random.normal(0, 0.1)
    return (x[0] - 2)**2 + noise

bounds = [(0, 4)]
result = dual_annealing(noisy_func, bounds)
print(round(result.fun, 2))
AAn approximate minimum value affected by noise
BThe maximum value found during optimization
CThe exact minimum value of the function without noise
DA constant zero because noise averages out
Attempts:
2 left
💡 Hint
Consider how noise affects function evaluations during optimization.