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: True
Note:

The output values will be changed for every execution of the code.

scipy_optimize.htm
Advertisements