Metaheuristics

Real-Coded Simulated Annealing (SA) in MATLAB

Get the code
Comments

Apply simulated annealing to continuous variables in MATLAB, with Gaussian neighbors, bound handling, cooling, and a Sphere test problem.

Illustration for Real-Coded Simulated Annealing (SA) in MATLAB
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

P=exp⁡(−ΔT),Δ=f(x′)−f(x)f(x)P = \exp\left(-\frac{\Delta}{T}\right), \qquad \Delta = \frac{f(x') - f(x)}{f(x)}

where ff is the cost function, xx is the current solution, x′x' is the neighbor and TT is the temperature. The code measures Δ\Delta relative to the current cost. A high temperature lets the search accept worse solutions and leave local minima. As TT 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 T←αTT \leftarrow \alpha T, and the mutation step size is reduced with σ←0.98 σ\sigma \leftarrow 0.98\,\sigma.

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: runs sa.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 −10-10, 1010
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 iith best neighbor is compared with the iith 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.

Plain text
Mostapha Kalami Heris, Real-Coded Simulated Annealing (SA) in MATLAB (YPEA106), Yarpiz, 2015. https://yarpiz.com/real-coded-simulated-annealing-ypea106
BibTeX
@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}
}

Comments

Sign in or create a free account to join the discussion. No password needed.