Differential Evolution (DE) in MATLAB
Implement DE/rand/1/bin in MATLAB with differential mutation, binomial crossover, bound handling, and a Sphere example with configurable parameters.

On this page
Differential Evolution (DE) is an evolutionary algorithm that uses the difference of solution vectors to create new candidate solutions. The differences between population members carry information about the spread and the orientation of the population. When the population is spread out, the differences are large and the steps are large. As the population gathers around a minimum, the differences shrink, and the search becomes finer without any extra step size rule.
DE was proposed by Rainer Storn and Kenneth Price in 1997, in this paper (opens in a new tab). The MATLAB package below implements its DE/rand/1/bin variant for bounded continuous variables.
How DE works
DE keeps a population of candidate solutions , each a vector of real variables. In every iteration, each member in turn goes through three steps.
Mutation. Three different members , and , all different from , are chosen at random. A mutant vector is formed by adding a scaled difference of two of them to the third:
The scaling factor is usually between 0.4 and 1.
Crossover. A trial vector takes each variable from the mutant with probability , and from otherwise. One random position always comes from the mutant, so at least one variable is taken from the mutant, even if its value happens to equal that of :
where is a uniform random number in . This is called binomial crossover.
Selection. The trial vector replaces only if it has a lower cost. So the population never gets worse.
This scheme is written DE/rand/1/bin: the base vector is random, one difference vector is used, and the crossover is binomial. Other variants use the best member as the base vector (DE/best/1) or add two difference vectors (DE/rand/2).
MATLAB implementation
This post shares a structured open-source implementation of standard Differential Evolution in MATLAB. If you know MATLAB, you should find it easy to use the code in your projects and research.
The download holds three files in the Differential Evolution folder:
de.m: the algorithm, with the problem definition, the parameters, the main loop and the results.Sphere.m: the sample cost function, .main.m: a one line entry point that runsde.m.
The code implements DE/rand/1/bin. Two details go beyond the basic scheme: the scaling factor is drawn at random for every variable from , and the mutant vector is clipped to the variable bounds.
To run it, open the folder in MATLAB and run main.m. The program prints the best cost in every iteration and plots the best cost against the iteration on a logarithmic scale. The script uses unifrnd, a function of the Statistics and Machine Learning Toolbox, so it probably needs that toolbox.
Parameters in the code
| Parameter | Meaning | Value |
|---|---|---|
nVar |
Number of decision variables | 20 |
VarMin, VarMax |
Bounds of the variables | , |
MaxIt |
Maximum number of iterations | 1000 |
nPop |
Population size | 50 |
beta_min, beta_max |
Range of the scaling factor | 0.2, 0.8 |
pCR |
Crossover probability | 0.2 |
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 de.m.
Check the result
The Sphere example has a known minimum of 0 at the origin. Confirm that each returned variable is within bounds and repeat the run to see how much the outcome varies. The mutation step needs three distinct donors different from the current member, so the population must contain at least four members. Changing the crossover rate affects how many coordinates are inherited, not whether a trial is necessarily numerically different.
Applications
DE works well on continuous problems where the gradient is not available or the cost has many local minima. Typical uses are the tuning of model and controller parameters, engineering design and the training of models. On Yarpiz, DE is also used for evolutionary clustering.
Further reading
- Differential evolution on Wikipedia (opens in a new tab)
- The DE website of Rainer Storn (opens in a new tab), which has many resources about the algorithm
- YPEA, a MATLAB toolbox that includes several DE variants, such as DE/best/2/bin
Downloads
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Differential Evolution (DE) in MATLAB (YPEA107), Yarpiz, 2015. https://yarpiz.com/differential-evolution-ypea107@misc{yarpiz-ypea107,
author = {{Mostapha Kalami Heris}},
title = {{Differential Evolution (DE) in MATLAB}},
howpublished = {Yarpiz},
year = {2015},
note = {Code YPEA107},
url = {https://yarpiz.com/differential-evolution-ypea107}
}Published Updated
Comments