
- 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.shgo() Function
scipy.optimize.shgo() is a function in SciPy's optimization module used for solving global optimization problems. It employs the Sobol sequences to sample the parameter space and uses a local optimization algorithm to refine the solution.
This function makes it suitable for finding the global minimum of complex, multi-model functions where traditional optimization methods might get stuck in local minima.
This method ensures reliable and efficient optimization by combining stochastic sampling with deterministic refinement by making it robust for challenging optimization tasks.
Syntax
The syntax for the scipy.optimize.shgo() function is as follows −
scipy.optimize.shgo(func, bounds, args=(), constraints=None, n=100, iters=None, callback=None, minimizer_kwargs=None, options=None, sampling_method='sobol')
Parameters
Here are the parameters of the scipy.optimize.shgo() function which is used to solve global optimization −
- func: The objective function to be minimized.
- bounds: A sequence of tuples specifying the bounds for each variable.
- args(optional): Additional arguments to pass to the objective function.
- constraints(optional): A dictionary or sequence of dictionaries specifying constraints.
- n: Number of sampling points to use in each iteration and default value is 100.
- iters(optional): Number of iterations for refinement. If None then the algorithm continues until convergence.
- callback(optional): A function called at each iteration with the current solution.
- minimizer_kwargs(optional): Additional keyword arguments for the local minimizer.
- options(optional): Additional options for the SHGO algorithm.
- sampling_method(optional): This parameter specifies the sampling method. Default value is 'sobol'.
Return Value
The scipy.optimize.shgo() function returns an OptimizeResult object containing the following −
- x: The solution array representing the global minimum.
- fun: The value of the objective function at the global minimum.
- message: A string describing the termination status of the algorithm.
- success: A Boolean indicating whether the optimization was successful.
Minimizing a Multi-model Function
Multi-model functions are those that have multiple local minima by making optimization challenging for traditional methods that may get trapped in a local minimum. The scipy.optimize.shgo() function is particularly effective for solving such problems as it systematically explores the parameter space to identify the global minimum.
In this example we show how to use scipy.optimize.shgo() to find the global minimum of a sinusoidal function with multiple peaks and valleys −
import numpy as np from scipy.optimize import shgo # Define a multi-model function def objective_function(x): return np.sin(5 * x[0]) * (1 - np.tanh(x[0]**2)) # Define bounds for the variable bounds = [(-2, 2)] # Perform the global optimization result = shgo(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.shgo() function which is used to minimize a multi model function −
Optimal value of x: [1.99684822] Minimum value of the function: -0.00036504063175867756
Optimizing with Constraints
Optimization problems often involve constraints that restrict the feasible region of the solution space. These constraints can be equality, inequality or boundary conditions. The scipy.optimize.shgo() function supports constrained optimization by making it a versatile tool for solving real-world problems with complex requirements.
Here is the example in which we minimize a quadratic function subject to a linear inequality constraint −
from scipy.optimize import shgo # Define the objective function def objective_function(x): return x[0]**2 + x[1]**2 # Define bounds for the variables bounds = [(-1, 1), (-1, 1)] # Define a constraint: x[0] + x[1] 1 constraints = {'type': 'ineq', 'fun': lambda x: 1 - (x[0] + x[1])} # Perform the optimization result = shgo(objective_function, bounds, constraints=[constraints]) # Display the result print("Optimal values of x:", result.x) print("Minimum value of the function:", result.fun)
Following is the output of Optimizing with constraints with the usage of the function scipy.optimize.shgo() −
Optimal values of x: [0. 0.] Minimum value of the function: 0.0
Minimizing a Nonlinear Oscillatory Function
Here is the example of using the scipy.optimize.shgo() function to minimize a nonlinear Oscillatory function −
Here is the example in which we minimize a quadratic function subject to a linear inequality constraint −
from scipy.optimize import shgo import numpy as np # Define a multi-modal function def objective_function(x): return np.sin(10 * x[0]) + np.cos(5 * x[1]) # Define bounds for the variables bounds = [(0, 2 * np.pi), (0, 2 * np.pi)] # Perform the optimization result = shgo(objective_function, bounds) # Display the result print("Optimal values of x:", result.x) print("Minimum value of the function:", result.fun)
Below is the output of minimizing a Nonlinear Oscillatory Function −
Optimal values of x: [2.35619449 3.14159265] Minimum value of the function: -1.9999999999999996