
- 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.dual_annealing() Function
scipy.optimize.dual_annealing() is a SciPy function for performing global optimization using the Dual Annealing algorithm. This algorithm is particularly useful for optimizing functions that may have many local minima as it combines simulated annealing and local search to efficiently explore the solution space.
This function is designed to solve optimization problems of the form minimize(f(x)) where f(x) is a scalar-valued function. It can handle both bounded and unbounded variables and provides flexibility in specifying constraints and tolerances for the optimization process.
The output of scipy.optimize.dual_annealing() is an OptimizeResult object containing the solution, cost and other diagnostic information.
Syntax
Following is the syntax of the function scipy.optimize.dual_annealing() used for global optimization −
dual_annealing(func, bounds, args=(), maxiter=1000, local_search_options=None, no_local_search=False, seed=None, callback=None, disp=False, init_temp=5230.0, restart_temp_ratio=2e-5, visit=2.62, accept=-5.0, maxfun=None, workers=1)
Parameters
Here are the parameters of the function scipy.optimize.dual_annealing() −
- func(callable): The objective function to minimize. It should take an array x and return a scalar value representing the function's evaluation.
- bounds(array_like): Sequence of tuples indicating the bounds for each parameter in the optimization. Each tuple should specify the lower and upper bounds for a parameter.
- args(tuple, optional): Extra arguments to pass to the objective function func.
- maxiter(int, optional): Maximum number of iterations of the algorithm.
- local_search_options(dict, optional): Options for local search optimizer (if used).
- no_local_search(bool, optional): Whether or not to use local search after global search. Defaults to False.
- seed(int or None, optional): Random seed for reproducibility.
- callback(callable, optional): Function to call at each iteration to track the optimization process.
- disp(bool, optional): Whether to display convergence information at each iteration.
- init_temp(float, optional): The initial temperature for the annealing process (default: 5230.0).
- restart_temp_ratio(float, optional): Ratio for temperature restart, controls how fast the temperature decays.
- visit(float, optional): Parameter to control the search space exploration (larger values explore more widely).
- accept(float, optional): Parameter controlling the acceptance criterion for new candidate solutions.
- maxfun(int, optional): Maximum number of function evaluations (optional).
- workers(int, optional): Number of parallel workers to use for evaluating the function.
Return Value
scipy.optimize.dual_annealing() returns an OptimizeResult object containing the following values −
- x: The solution array (optimized parameters).
- fun: The objective function value at the solution.
- message: Description of the termination condition.
- success: A boolean flag indicating whether the optimization was successful.
- nfev: Number of function evaluations performed during the optimization process.
Minimizing a Simple Objective Function
In this example we minimize the simple quadratic function f(x) = (x - 2)^2 with bounds on the optimization variables −
import numpy as np from scipy.optimize import dual_annealing # Define the objective function def func(x): return (x[0] - 2)**2 # Bounds for the optimization bounds = [(-5, 5)] # Run dual annealing optimization result = dual_annealing(func, bounds) # Display the results print("Optimal parameters:", result.x) print("Objective function value:", result.fun) print("Success:", result.success)
Here is the output of the function scipy.optimize.dual_annealing() used to minimize a simple objective function −
Optimal parameters: [2.] Objective function value: 2.497830176661867e-17 Success: True
Global Optimization with Multiple Variables
This example shows how to use the dual annealing algorithm to minimize a function with two variables −
import numpy as np from scipy.optimize import dual_annealing # Define the objective function def func(x): return x[0]**2 + x[1]**2 # Bounds for the optimization bounds = [(-5, 5), (-5, 5)] # Run dual annealing optimization result = dual_annealing(func, bounds) # Display the results print("Optimal parameters:", result.x) print("Objective function value:", result.fun) print("Success:", result.success)
Here is the output of the function scipy.optimize.dual_annealing() used to perform global optimization with Multiple variables −
Optimal parameters: [-4.98579439e-09 -5.00991030e-09] Objective function value: 4.99573469163112e-17 Success: True
Minimizing a Function with a Complex Landscape
In this example we minimize a function that has a more complex landscape with multiple local minima −
import numpy as np from scipy.optimize import dual_annealing # Define a more complex objective function with multiple local minima def func(x): return np.sin(x[0]) + np.cos(x[1]) # Bounds for the optimization bounds = [(-5, 5), (-5, 5)] # Run dual annealing optimization result = dual_annealing(func, bounds) # Display the results print("Optimal parameters:", result.x) print("Objective function value:", result.fun) print("Success:", result.success)
Here is the output of the function scipy.optimize.dual_annealing() in this case −
Optimal parameters: [-1.57079713 3.14159308] Objective function value: -1.9999999999995897 Success: TrueNote:
The output values will be changed for every execution of the code.