Artificial Bee Colony (ABC) in MATLAB
Implement Artificial Bee Colony in MATLAB with employed, onlooker, and scout phases, fitness-based selection, and a Sphere example.

On this page
Artificial Bee Colony (ABC) is a metaheuristic algorithm inspired by the foraging behavior of honey bee swarms. It was proposed by Derviş Karaboğa in 2005. It is a simple algorithm with few parameters, and it has been used on a wide range of practical optimization problems.
In ABC, each food source is a candidate solution, and the amount of nectar at a source stands for the quality of that solution. The colony has three groups of bees: employed bees, onlooker bees and scout bees. They share the work of searching around good food sources and finding new ones.
How ABC works
Each iteration has three phases.
Employed bee phase
Each food source has one employed bee. The bee creates a new candidate near its source, using another source chosen at random ():
Here holds random numbers drawn uniformly from . If is better than , it replaces it. Otherwise, the trial counter of source goes up by one.
Onlooker bee phase
Onlooker bees wait in the hive and watch the dances of the employed bees. Each onlooker picks a food source with a probability that grows with its quality, so good sources get more visits. It then searches near that source with the same formula and the same greedy rule. In this way the colony spends more effort around the best solutions.
Scout bee phase
A source that has not improved for a number of trials is probably exhausted. When the trial counter of a source reaches the limit , its bee becomes a scout. The scout leaves the source and picks a new random position in the search space. This step introduces new search locations when a source stops improving.
Check the result
The Sphere example has its minimum at the origin with cost 0. The onlooker fitness uses the population mean cost as a divisor, so a zero mean requires explicit handling before adapting the code. Review the fitness mapping for objectives that can be negative. Changing the population also changes the default abandonment limit, so compare the full settings and evaluation budget across runs.
Further reading
- Artificial bee colony algorithm on Wikipedia (opens in a new tab)
- The official ABC website (opens in a new tab), with resources, references, news and software
MATLAB implementation
This post shares a structured open-source implementation of Artificial Bee Colony in MATLAB. If you know MATLAB, you should find it easy to use the code in your research and projects.
What is in the download
The Artificial Bee Colony folder holds four files:
abc.m: the ABC script, with the problem definition, the settings, the main loop and the results.RouletteWheelSelection.m: picks a food source for each onlooker bee.Sphere.m: the sample cost function, .main.m: a one line entry point that runsabc.m.
To run the code, open the Artificial Bee Colony folder in MATLAB and run main.m. The program prints the best cost of every iteration and then plots the best cost against the iteration number on a logarithmic scale.
The script creates the first food sources with 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 | 5 |
VarMin, VarMax |
Lower and upper bounds of the variables | , |
MaxIt |
Maximum number of iterations | 200 |
nPop |
Colony size (number of food sources) | 100 |
nOnlooker |
Number of onlooker bees | nPop |
L |
Abandonment limit (trial limit) | round(0.6*nVar*nPop), which is 300 |
a |
Upper bound of the acceleration coefficient | 1 |
Details of this implementation
- In the code, the employed bee phase is labeled “Recruited Bees”.
- The new candidate changes every variable of the source at once, with one random for each variable. The original ABC changes only one variable chosen at random.
- The onlookers choose sources by roulette wheel selection. The code turns each cost into a fitness value , where is the cost of source and is the mean cost of all sources. The original ABC uses for nonnegative costs.
- Positions outside the bounds are set back to the nearest bound.
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 in the problem definition section of abc.m.
Related posts
- Bees Algorithm in MATLAB is another metaheuristic based on honey bee foraging.
- YPEA is a MATLAB toolbox that includes ABC and 14 other algorithms that share one problem definition.
Downloads
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Artificial Bee Colony (ABC) in MATLAB (YPEA114), Yarpiz, 2015. https://yarpiz.com/artificial-bee-colony-ypea114@misc{yarpiz-ypea114,
author = {{Mostapha Kalami Heris}},
title = {{Artificial Bee Colony (ABC) in MATLAB}},
howpublished = {Yarpiz},
year = {2015},
note = {Code YPEA114},
url = {https://yarpiz.com/artificial-bee-colony-ypea114}
}Published Updated
Comments