Metaheuristics

Differential Evolution (DE) in MATLAB

Get the code
Comments

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

Illustration for Differential Evolution (DE) in MATLAB
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 NN candidate solutions x1,…,xNx_1, \dots, x_N, each a vector of nn real variables. In every iteration, each member xix_i in turn goes through three steps.

Mutation. Three different members xax_a, xbx_b and xcx_c, all different from xix_i, are chosen at random. A mutant vector is formed by adding a scaled difference of two of them to the third:

y=xa+F(xb−xc)y = x_a + F \left(x_b - x_c\right)

The scaling factor FF is usually between 0.4 and 1.

Crossover. A trial vector zz takes each variable from the mutant with probability CRC_R, and from xix_i otherwise. One random position j0j_0 always comes from the mutant, so at least one variable is taken from the mutant, even if its value happens to equal that of xix_i:

zj={yjif rj≤CR or j=j0xjotherwisez_j = \begin{cases} y_j & \text{if } r_j \le C_R \text{ or } j = j_0 \\ x_j & \text{otherwise} \end{cases}

where rjr_j is a uniform random number in [0,1][0, 1]. This is called binomial crossover.

Selection. The trial vector replaces xix_i only if it has a lower cost. So the population never gets worse.

This scheme is written DE/rand/1/bin: the base vector xax_a 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, f(x)=∑i=1nxi2f(x) = \sum_{i=1}^{n} x_i^2.
  • main.m: a one line entry point that runs de.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 [βmin⁡,βmax⁡][\beta_{\min}, \beta_{\max}], 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 −5-5, 55
MaxIt Maximum number of iterations 1000
nPop Population size 50
beta_min, beta_max Range of the scaling factor FF 0.2, 0.8
pCR Crossover probability CRC_R 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

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, Differential Evolution (DE) in MATLAB (YPEA107), Yarpiz, 2015. https://yarpiz.com/differential-evolution-ypea107
BibTeX
@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}
}

Comments

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