Simulated annealing helps find the best answer when there are many possibilities. It tries different solutions and slowly focuses on the best one.
0
0
Simulated annealing (dual_annealing) in SciPy
Introduction
Finding the lowest cost route for delivery trucks.
Optimizing machine settings to get the best product quality.
Tuning parameters in a model when many options exist.
Scheduling tasks to finish work faster with limited resources.
Syntax
SciPy
from scipy.optimize import dual_annealing result = dual_annealing(func, bounds) # func: function to minimize # bounds: list of (min, max) tuples for each variable
The func should take a list or array and return a number to minimize.
bounds define the search space for each variable.
Examples
Minimize a simple function with two variables between -5 and 5.
SciPy
def f(x): return (x[0] - 1)**2 + (x[1] + 2)**2 bounds = [(-5, 5), (-5, 5)] result = dual_annealing(f, bounds) print(result.x, result.fun)
Minimize the sphere function in 3D space.
SciPy
def sphere(x): return sum(xi**2 for xi in x) bounds = [(-10, 10)] * 3 result = dual_annealing(sphere, bounds) print(result.x, result.fun)
Sample Program
This program finds the minimum of a simple 2D function using simulated annealing. It searches between -10 and 10 for both variables.
SciPy
from scipy.optimize import dual_annealing def objective(x): # Simple function with minimum at (3, -1) return (x[0] - 3)**2 + (x[1] + 1)**2 bounds = [(-10, 10), (-10, 10)] result = dual_annealing(objective, bounds) print(f"Best solution: {result.x}") print(f"Minimum value: {result.fun}")
OutputSuccess
Important Notes
Simulated annealing can find good solutions even if the problem has many local minima.
Results may slightly vary each run because of randomness.
Set bounds carefully to limit the search space and speed up the process.
Summary
Simulated annealing tries many solutions and slowly focuses on the best one.
Use dual_annealing from scipy.optimize to minimize functions with bounds.
It works well for complex problems with many possible answers.