
- SciPy - Home
- SciPy - Introduction
- SciPy - Environment Setup
- SciPy - Basic Functionality
- SciPy - Relationship with NumPy
- SciPy Clusters
- SciPy - Clusters
- SciPy - Hierarchical Clustering
- SciPy - K-means Clustering
- SciPy - Distance Metrics
- SciPy Constants
- SciPy - Constants
- SciPy - Mathematical Constants
- SciPy - Physical Constants
- SciPy - Unit Conversion
- SciPy - Astronomical Constants
- SciPy - Fourier Transforms
- SciPy - FFTpack
- SciPy - Discrete Fourier Transform (DFT)
- SciPy - Fast Fourier Transform (FFT)
- SciPy Integration Equations
- SciPy - Integrate Module
- SciPy - Single Integration
- SciPy - Double Integration
- SciPy - Triple Integration
- SciPy - Multiple Integration
- SciPy Differential Equations
- SciPy - Differential Equations
- SciPy - Integration of Stochastic Differential Equations
- SciPy - Integration of Ordinary Differential Equations
- SciPy - Discontinuous Functions
- SciPy - Oscillatory Functions
- SciPy - Partial Differential Equations
- SciPy Interpolation
- SciPy - Interpolate
- SciPy - Linear 1-D Interpolation
- SciPy - Polynomial 1-D Interpolation
- SciPy - Spline 1-D Interpolation
- SciPy - Grid Data Multi-Dimensional Interpolation
- SciPy - RBF Multi-Dimensional Interpolation
- SciPy - Polynomial & Spline Interpolation
- SciPy Curve Fitting
- SciPy - Curve Fitting
- SciPy - Linear Curve Fitting
- SciPy - Non-Linear Curve Fitting
- SciPy - Input & Output
- SciPy - Input & Output
- SciPy - Reading & Writing Files
- SciPy - Working with Different File Formats
- SciPy - Efficient Data Storage with HDF5
- SciPy - Data Serialization
- SciPy Linear Algebra
- SciPy - Linalg
- SciPy - Matrix Creation & Basic Operations
- SciPy - Matrix LU Decomposition
- SciPy - Matrix QU Decomposition
- SciPy - Singular Value Decomposition
- SciPy - Cholesky Decomposition
- SciPy - Solving Linear Systems
- SciPy - Eigenvalues & Eigenvectors
- SciPy Image Processing
- SciPy - Ndimage
- SciPy - Reading & Writing Images
- SciPy - Image Transformation
- SciPy - Filtering & Edge Detection
- SciPy - Top Hat Filters
- SciPy - Morphological Filters
- SciPy - Low Pass Filters
- SciPy - High Pass Filters
- SciPy - Bilateral Filter
- SciPy - Median Filter
- SciPy - Non - Linear Filters in Image Processing
- SciPy - High Boost Filter
- SciPy - Laplacian Filter
- SciPy - Morphological Operations
- SciPy - Image Segmentation
- SciPy - Thresholding in Image Segmentation
- SciPy - Region-Based Segmentation
- SciPy - Connected Component Labeling
- SciPy Optimize
- SciPy - Optimize
- SciPy - Special Matrices & Functions
- SciPy - Unconstrained Optimization
- SciPy - Constrained Optimization
- SciPy - Matrix Norms
- SciPy - Sparse Matrix
- SciPy - Frobenius Norm
- SciPy - Spectral Norm
- SciPy Condition Numbers
- SciPy - Condition Numbers
- SciPy - Linear Least Squares
- SciPy - Non-Linear Least Squares
- SciPy - Finding Roots of Scalar Functions
- SciPy - Finding Roots of Multivariate Functions
- SciPy - Signal Processing
- SciPy - Signal Filtering & Smoothing
- SciPy - Short-Time Fourier Transform
- SciPy - Wavelet Transform
- SciPy - Continuous Wavelet Transform
- SciPy - Discrete Wavelet Transform
- SciPy - Wavelet Packet Transform
- SciPy - Multi-Resolution Analysis
- SciPy - Stationary Wavelet Transform
- SciPy - Statistical Functions
- SciPy - Stats
- SciPy - Descriptive Statistics
- SciPy - Continuous Probability Distributions
- SciPy - Discrete Probability Distributions
- SciPy - Statistical Tests & Inference
- SciPy - Generating Random Samples
- SciPy - Kaplan-Meier Estimator Survival Analysis
- SciPy - Cox Proportional Hazards Model Survival Analysis
- SciPy Spatial Data
- SciPy - Spatial
- SciPy - Special Functions
- SciPy - Special Package
- SciPy Advanced Topics
- SciPy - CSGraph
- SciPy - ODR
- SciPy Useful Resources
- SciPy - Reference
- SciPy - Quick Guide
- SciPy - Cheatsheet
- SciPy - Useful Resources
- SciPy - Discussion
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