Bird
Raised Fist0
SciPydata~20 mins

Global optimization (differential_evolution) in SciPy - Practice Problems & Coding Challenges

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

func = lambda x: (x[0] - 3)**2 + (x[1] + 1)**2
bounds = [(-5, 5), (-5, 5)]
result = differential_evolution(func, bounds, seed=42)
print((round(result.x[0], 2), round(result.x[1], 2)))
A(3.0, -1.0)
B(-3.0, 1.0)
C(0.0, 0.0)
D(5.0, -5.0)
Attempts:
2 left
💡 Hint
Recall that differential_evolution tries to find the minimum of the function within the given bounds.
❓ data_output
intermediate
2:00remaining
Number of iterations in differential_evolution result
After running differential_evolution on the Rosenbrock function with bounds [(-2, 2), (-1, 3)], what is the value of result.nit (number of iterations)?
SciPy
from scipy.optimize import differential_evolution

def rosenbrock(x):
    return (1 - x[0])**2 + 100*(x[1] - x[0]**2)**2

bounds = [(-2, 2), (-1, 3)]
result = differential_evolution(rosenbrock, bounds, seed=0)
print(result.nit)
A25
B15
C20
D11
Attempts:
2 left
💡 Hint
Check the nit attribute of the result object.
🔧 Debug
advanced
2:00remaining
Identify the error in differential_evolution usage
What error will the following code raise when trying to run differential_evolution?
SciPy
from scipy.optimize import differential_evolution
import math

def f(x):
    return math.pow(x, 2)

bounds = [(-1, 1)]
result = differential_evolution(f, bounds)
print(result.fun)
ANo error, prints 0.0
BTypeError: only size-1 arrays can be converted to Python scalars
CAttributeError: 'OptimizeResult' object has no attribute 'fun'
DValueError: x must be a 1-D array
Attempts:
2 left
💡 Hint
Check how the function f expects its input and what differential_evolution passes.
🧠 Conceptual
advanced
1:30remaining
Understanding differential_evolution parameters
Which parameter of scipy.optimize.differential_evolution controls the mutation constant that influences the differential weight in the algorithm?
Arecombination
Bpopsize
Cmutation
Dtol
Attempts:
2 left
💡 Hint
Mutation controls the differential weight in the mutation step.
🚀 Application
expert
3:00remaining
Global minimum found by differential_evolution on a noisy function
Given the noisy function f(x) = (x[0]-2)^2 + (x[1]+3)^2 + noise where noise is a small random value, which of the following is the closest output of result.x after running differential_evolution with bounds [(-5,5), (-5,5)] and seed=1?
SciPy
import numpy as np
from scipy.optimize import differential_evolution

np.random.seed(1)
noise = lambda: np.random.normal(0, 0.1)

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

bounds = [(-5, 5), (-5, 5)]
result = differential_evolution(f, bounds, seed=1)
print((round(result.x[0], 1), round(result.x[1], 1)))
A(2.0, -3.0)
B(1.0, -3.0)
C(2.0, -2.0)
D(0.0, 0.0)
Attempts:
2 left
💡 Hint
The noise is small, so the optimizer should find a point close to the true minimum (2, -3).

Practice

(1/5)
1. What is the main purpose of the differential_evolution function in scipy.optimize?
easy
A. To perform linear regression on data
B. To calculate the derivative of a function
C. To find the global minimum of a function within given bounds
D. To generate random numbers for simulations

Solution

  1. Step 1: Understand the purpose of differential evolution

    Differential evolution is a global optimization algorithm designed to find the minimum value of a function over a specified range.
  2. Step 2: Compare options with the purpose

    Only To find the global minimum of a function within given bounds describes finding the global minimum within bounds, which matches the function's purpose.
  3. Final Answer:

    To find the global minimum of a function within given bounds -> Option C
  4. Quick Check:

    Global optimization = find minimum [OK]
Hint: Remember: differential_evolution finds global minimum [OK]
Common Mistakes:
  • Confusing optimization with differentiation
  • Thinking it performs regression
  • Assuming it generates random numbers only
2. Which of the following is the correct way to import the differential_evolution function from scipy.optimize?
easy
A. import differential_evolution from scipy.optimize
B. from scipy.optimize import differential_evolution
C. from scipy import differential_evolution.optimize
D. import scipy.optimize.differential_evolution

Solution

  1. Step 1: Recall Python import syntax

    The correct syntax to import a function from a module is: from module import function.
  2. Step 2: Match syntax with options

    from scipy.optimize import differential_evolution matches the correct syntax exactly. Other options have incorrect order or syntax.
  3. Final Answer:

    from scipy.optimize import differential_evolution -> Option B
  4. Quick Check:

    Correct import syntax = from scipy.optimize import differential_evolution [OK]
Hint: Use 'from module import function' for importing functions [OK]
Common Mistakes:
  • Using 'import function from module' which is invalid
  • Trying to import submodules incorrectly
  • Using dot notation in import statements wrongly
3. What will be the output of the following code snippet?
from scipy.optimize import differential_evolution

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

result = differential_evolution(func, bounds=[(-5, 5), (-5, 5)])
print(round(result.fun, 2))
medium
A. 0.00
B. 5.00
C. 13.00
D. Error due to wrong bounds

Solution

  1. Step 1: Understand the function and bounds

    The function calculates the sum of squares of (x0-2) and (x1+3). The minimum is at x0=2, x1=-3, which is inside the bounds (-5,5).
  2. Step 2: Evaluate the minimum function value

    At x0=2 and x1=-3, func returns (2-2)^2 + (-3+3)^2 = 0 + 0 = 0. So the minimum function value is 0.
  3. Final Answer:

    0.00 -> Option A
  4. Quick Check:

    Minimum value at (2,-3) = 0 [OK]
Hint: Minimum of squared terms is zero at exact target points [OK]
Common Mistakes:
  • Misunderstanding bounds causing error
  • Calculating function value at wrong points
  • Ignoring rounding in output
4. Identify the error in the following code using differential_evolution and how to fix it:
from scipy.optimize import differential_evolution

def f(x):
    return x**2

bounds = [(-3, 3)]
result = differential_evolution(f, bounds)
print(result.x)
medium
A. No error; code runs correctly
B. Bounds should be a tuple, not a list; fix by changing bounds to a tuple
C. Missing import of numpy; fix by adding import numpy as np
D. Function f expects a scalar but receives an array; fix by defining f(x) to accept array input

Solution

  1. Step 1: Analyze function input requirements

    The differential_evolution function passes an array to f, but f expects a scalar and tries to square it directly, which causes an error.
  2. Step 2: Fix function to accept array input

    Modify f to accept an array input and return a scalar, for example: def f(x): return x[0]**2.
  3. Final Answer:

    Function f expects a scalar but receives an array; fix by defining f(x) to accept array input -> Option D
  4. Quick Check:

    Function input shape mismatch = Function f expects a scalar but receives an array; fix by defining f(x) to accept array input [OK]
Hint: Functions must accept array input for differential_evolution [OK]
Common Mistakes:
  • Assuming function gets scalar input
  • Incorrect bounds format
  • Ignoring error messages about input types
5. You want to minimize the function f(x) = sin(x[0]) + cos(x[1]) over the bounds x[0] in [0, 6] and x[1] in [-3, 3]. Which of the following code snippets correctly uses differential_evolution to find the minimum and print the best input values?
hard
A. from scipy.optimize import differential_evolution import numpy as np def f(x): return np.sin(x[0]) + np.cos(x[1]) bounds = [(0, 6), (-3, 3)] result = differential_evolution(f, bounds) print(result.x)
B. from scipy.optimize import differential_evolution import numpy as np def f(x): return np.sin(x) + np.cos(x) bounds = [(0, 6), (-3, 3)] result = differential_evolution(f, bounds) print(result.x)
C. from scipy.optimize import differential_evolution import numpy as np def f(x): return np.sin(x[0]) + np.cos(x[1]) bounds = [0, 6, -3, 3] result = differential_evolution(f, bounds) print(result.x)
D. from scipy.optimize import differential_evolution import numpy as np def f(x): return np.sin(x[0]) + np.cos(x[1]) bounds = [(0, 6), (-3, 3)] result = differential_evolution(f) print(result.x)

Solution

  1. Step 1: Check function definition

    The function f correctly uses array indexing for x[0] and x[1] and applies numpy sine and cosine functions.
  2. Step 2: Verify bounds and function call

    Bounds are correctly given as a list of tuples for each variable. The differential_evolution call includes both the function and bounds.
  3. Final Answer:

    Code snippet C correctly defines the function, bounds, and calls differential_evolution -> Option A
  4. Quick Check:

    Correct function and bounds usage = from scipy.optimize import differential_evolution import numpy as np def f(x): return np.sin(x[0]) + np.cos(x[1]) bounds = [(0, 6), (-3, 3)] result = differential_evolution(f, bounds) print(result.x) [OK]
Hint: Use list of tuples for bounds and array input function [OK]
Common Mistakes:
  • Passing bounds as flat list instead of list of tuples
  • Defining function without indexing array input
  • Calling differential_evolution without bounds argument
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard