# Real-Coded Simulated Annealing (SA) in MATLAB

URL: https://yarpiz.com/real-coded-simulated-annealing-ypea106
Author: Mostapha Kalami Heris
Published: 2015-09-22
Updated: 2026-10-30
Topics: Metaheuristics

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

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](https://yarpiz.com/simulated-annealing-ypea105) 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\left(-\frac{\Delta}{T}\right), \qquad \Delta = \frac{f(x') - f(x)}{f(x)}
$$

where $f$ is the cost function, $x$ is the current solution, $x'$ is the neighbor and $T$ 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 $T$ 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 \leftarrow \alpha T$, and the mutation step size is reduced with $\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$ |
| `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 $i$th best neighbor is compared with the $i$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.

## How to cite

Mostapha Kalami Heris, Real-Coded Simulated Annealing (SA) in MATLAB (YPEA106), Yarpiz, 2015. https://yarpiz.com/real-coded-simulated-annealing-ypea106
