SciPy optimize.brute() Function



scipy.optimize.brute() is a powerful function in the SciPy library's optimization module. It performs a brute-force grid search to find the global minimum of a given function over a specified range. This method systematically evaluates the objective function on a grid of points, making it particularly useful for problems with multiple local minima where global optimization is desired.

Using this function, users can define the search space as a tuple of slices and set various parameters to control the granularity and efficiency of the search. While it is computationally intensive for high-dimensional problems, it guarantees finding the global minimum if the function is evaluated finely enough.

Syntax

The syntax for the scipy.optimize.brute() function is as follows −

scipy.optimize.brute(func, ranges, args=(), Ns=20, full_output=False, finish=None, workers=1, disp=False)

Parameters

Below are the parameters for the scipy.optimize.brute() function −

  • func: The objective function to be minimized.
  • ranges: A tuple of slices specifying the range for each variable. Each slice represents the search bounds.
  • args: Additional arguments passed to the objective function.
  • Ns: The number of grid points along each dimension and the default value is 20.
  • full_output: If True then returns additional information like the function value at the minimum.
  • finish: A minimization function to refine the result e.g., scipy.optimize.minimize().
  • workers: The number of workers for parallel computation in which -1 uses all available processors.
  • disp: If True then displays progress and convergence messages.

Return Value

The scipy.optimize.brute() function returns:

  • x0: The coordinates of the global minimum.
  • fval: The function value at the global minimum if full_output=True.
  • grid: The evaluated grid points if full_output=True.
  • Jout: The objective function values evaluated on the grid if full_output=True.

Minimizing a Quadratic Function

Here is an example of using scipy.optimize.brute() to minimize a quadratic function −

import numpy as np
from scipy.optimize import brute

# Define the objective function
def objective(x):
    return x**2 + 3*x + 2

# Perform brute-force optimization
result = brute(objective, ranges=[slice(-10, 10, 0.5)], full_output=True)

# Output the result
print("Global minimum:", result[0])
print("Function value at minimum:", result[1])

Following is the output of the function scipy.optimize.brute() which is used to minimize a quadratic function −

Global minimum: [-1.5]
Function value at minimum: -0.25

Optimizing a Multi-Variable Function

In this example we optimize a two-variable function by using the function scipy.optimize.brute()

from scipy.optimize import brute

# Define a two-variable function
def func(x):
    return (x[0] - 3)**2 + (x[1] + 2)**2

# Perform brute-force optimization
result = brute(func, ranges=[slice(-5, 5, 0.5), slice(-5, 5, 0.5)], full_output=True)

# Output the result
print("Global minimum:", result[0])
print("Function value at minimum:", result[1])

Below is the output of optimizing a Multi-Variable Function −

Global minimum: [3. -2.]
Function value at minimum: 0.0

Using a Refinement Step

Here in this example we will optimize the function f(x)=sin(x)+0.1x2 by using scipy.optimize.brute(). After finding an approximate global minimum with brute-force search, we will refine it using a local optimizer like scipy.optimize.minimize()

from scipy.optimize import brute, minimize

# Define the function
def func(x):
    return np.sin(x) + 0.1*x**2

# Perform brute-force optimization with refinement
result = brute(func, ranges=[slice(-10, 10, 0.5)], full_output=True, finish=minimize)

# Output the result
print("Global minimum:", result[0])
print("Function value at minimum:", result[1])

Here is the output of Refinement step performed with the help of scipy.optimize.brute() function −

Global minimum: [-1.30644259]
Function value at minimum: -0.7945823375576344
scipy_optimize.htm
Advertisements