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
scipy_optimize.htm
Advertisements