Real-Coded Simulated Annealing (SA) in MATLAB
Apply simulated annealing to continuous variables in MATLAB, with Gaussian neighbors, bound handling, cooling, and a Sphere test problem.

On this page
This package is an implementation of Real-Coded Simulated Annealing (SA) in MATLAB. It searches for the minimum of a cost function with real-valued decision variables.
Simulated Annealing was proposed by Kirkpatrick, Gelatt and Vecchi in 1983. The Simulated Annealing post explains the method and applies it to the Traveling Salesman Problem. This package uses the same idea for continuous problems: the neighbors are created by small random changes of the real-valued variables.
The acceptance rule
A neighbor that is not worse than the current solution is always accepted. A worse neighbor is accepted with the probability
where is the cost function, is the current solution, is the neighbor and is the temperature. The code measures relative to the current cost. A high temperature lets the search accept worse solutions and leave local minima. As falls, the search accepts fewer worse moves and settles near a good solution. The best solution found so far is always kept.
After each iteration, the temperature is reduced with , and the mutation step size is reduced with .
What is in the download
sa.m: the algorithm. It keeps a population of solutions and creates several neighbors for each one. A worse neighbor is accepted with a probability that depends on the current temperature. The temperature and the mutation step size are reduced at every iteration.Mutate.m: creates a neighbor by adding Gaussian noise to some of the variables, then clips the result to the variable bounds.Sphere.m: the example cost function.main.m: runssa.m. The script shows the best cost at each iteration and plots the best cost curve at the end.
The main parameters (MaxIt, MaxSubIt, T0, alpha, nPop, nMove, mu and sigma) are set at the top of sa.m.
Parameters in the code
| Parameter | Meaning | Value |
|---|---|---|
nVar |
Number of decision variables | 5 |
VarMin, VarMax |
Bounds of the variables | , |
MaxIt |
Maximum number of iterations | 1000 |
MaxSubIt |
Sub-iterations at each temperature | 20 |
T0 |
Initial temperature | 0.1 |
alpha |
Temperature reduction rate | 0.99 |
nPop |
Population size | 10 |
nMove |
Neighbors per individual | 5 |
mu |
Probability that a variable is mutated | 0.5 |
sigma |
Initial mutation step size (standard deviation) | 0.1*(VarMax-VarMin) |
In every sub-iteration, all the neighbors of the population are sorted by cost, and the th best neighbor is compared with the th member of the population.
To run the code, open the folder in MATLAB and run main.m. The best cost curve is plotted on a logarithmic scale. To solve your own problem, write a cost function that takes a row vector and returns a number, and set CostFunction, nVar, VarMin and VarMax at the top of sa.m. The script creates the first population with unifrnd, a function of the Statistics and Machine Learning Toolbox, so it probably needs that toolbox.
Check the result
For the Sphere example, inspect the distance to the origin, the cost, and the variable bounds together. The relative acceptance calculation divides by the current cost, so an exact zero cost needs explicit handling before adapting this code. Objectives that can be negative also need a reviewed acceptance rule. Repeat runs when comparing temperatures or mutation settings, and compare function evaluations as well as iterations.
Downloads
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Real-Coded Simulated Annealing (SA) in MATLAB (YPEA106), Yarpiz, 2015. https://yarpiz.com/real-coded-simulated-annealing-ypea106@misc{yarpiz-ypea106,
author = {{Mostapha Kalami Heris}},
title = {{Real-Coded Simulated Annealing (SA) in MATLAB}},
howpublished = {Yarpiz},
year = {2015},
note = {Code YPEA106},
url = {https://yarpiz.com/real-coded-simulated-annealing-ypea106}
}Published Updated
Comments