SciPy optimize.differential_evolution() Function



scipy.optimize.differential_evolution() is a function in SciPy's optimization module used for global optimization of scalar functions. It employs a stochastic population-based optimization technique known as the Differential Evolution algorithm.

This function is particularly effective for solving non-convex, multi-modal problems where traditional optimization methods might fail to find the global minimum.

Syntax

Following is the syntax for the scipy.optimize.differential_evolution() function which is used for global optimization of scalar functions −

scipy.optimize.differential_evolution(func, bounds, args=(), strategy='best1bin', maxiter=1000, popsize=15, tol=0.01, mutation=(0.5, 1), recombination=0.7, seed=None, callback=None, disp=False, polish=True, init='latinhypercube', atol=0)

Parameters

Here are the parameters of the scipy.optimize.differential_evolution() function −

  • func: The objective function to be minimized.
  • bounds: A sequence of (min, max) pairs for each parameter to be optimized.
  • args (optional): Additional arguments to pass to the objective function.
  • strategy (optional): The mutation strategy. Default value is 'best1bin'.
  • maxiter (optional): Maximum number of generations (iterations).
  • popsize (optional): The population size multiplier. Default value is 15.
  • tol (optional): Relative tolerance for convergence. Default value is 0.01.
  • mutation (optional): Mutation constant(s). Default value is (0.5, 1).
  • recombination (optional): Recombination constant. Default value is 0.7.
  • seed (optional): Seed for the random number generator.
  • callback (optional): A function to call after each generation.
  • disp (optional): Display convergence messages if True.
  • polish (optional): Whether to perform a final local optimization step. Default value is True.
  • init (optional): This parameter specifies the initialization method. Default value is 'latinhypercube'.
  • atol (optional): Absolute tolerance for convergence. Default value is 0.

Return Value

The scipy.optimize.differential_evolution() function returns an OptimizeResult object containing the following −

  • x: The solution array representing the parameters that minimize the objective function.
  • fun: The value of the objective function at the solution.
  • success: A Boolean indicating whether the optimization was successful.
  • message: A string describing the termination status of the algorithm.

Minimizing a Quadratic Function

Minimizing a quadratic function is a simple yet effective way to demonstrate the use of optimization techniques. Below is an example of using scipy.optimize.differential_evolution() to minimize a quadratic function −

from scipy.optimize import differential_evolution

# Define the objective function
def objective_function(x):
    return (x[0] - 3)**2

# Define bounds for the variables
bounds = [(0, 5)]

# Perform the optimization
result = differential_evolution(objective_function, bounds)

# Display the result
print("Optimal value of x:", result.x)
print("Minimum value of the function:", result.fun)

Here is the output of the scipy.optimize.differential_evolution() function applied to minimize a quadratic function −

Optimal value of x: [3.0]
Minimum value of the function: 0.0

Optimizing a Multi-Modal Function

The scipy.optimize.differential_evolution() function is particularly effective for solving multi-modal problems. Below is an example where the function is used to optimize a sinusoidal function −

from scipy.optimize import differential_evolution
import numpy as np

# Define the objective function
def objective_function(x):
    return np.sin(10 * x[0]) + (x[0] - 2)**2

# Define bounds for the variables
bounds = [(0, 4)]

# Perform the optimization
result = differential_evolution(objective_function, bounds)

# Display the result
print("Optimal value of x:", result.x)
print("Minimum value of the function:", result.fun)

Below is the output of Optimizing a Multi-Modal function by using the function scipy.optimize.differential_evolution()

Optimal value of x: [1.7332142]
Minimum value of the function: -0.9274008346525426

Optimizing a Nonlinear Function with Constraints

The scipy.optimize.differential_evolution() function supports constrained optimization, making it versatile for solving real-world problems. Here's an example of optimizing a nonlinear function subject to constraints −

from scipy.optimize import differential_evolution

# Define the objective function with the constraint integrated
def objective_function(x):
    # Penalty for constraint violation: if x[0] + x[1] > 3, penalize the objective value
    penalty = 0
    if x[0] + x[1] < 3:
        penalty = 1e6  # Large penalty for violating the constraint
    return x[0]**2 + x[1]**2 + penalty

# Define bounds for the variables
bounds = [(-5, 5), (-5, 5)]

# Perform the optimization
result = differential_evolution(objective_function, bounds)

# Display the result
print("Optimal values of x:", result.x)
print("Minimum value of the function:", result.fun)

The output of the scipy.optimize.differential_evolution() function for optimizing with constraints is as follows −

Optimal values of x: [1.50713362 1.49301869]
Minimum value of the function: 4.500556561098527
scipy_optimize.htm
Advertisements