# Artificial Bee Colony (ABC) in MATLAB

URL: https://yarpiz.com/artificial-bee-colony-ypea114
Author: Mostapha Kalami Heris
Published: 2015-09-11
Updated: 2026-10-30
Topics: Metaheuristics

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

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 $x_i$ has one employed bee. The bee creates a new candidate $v_i$ near its source, using another source $x_k$ chosen at random ($k \neq i$):

$$
v_i = x_i + \varphi \odot \left(x_i - x_k\right)
$$

Here $\varphi$ holds random numbers drawn uniformly from $[-a, a]$. If $v_i$ is better than $x_i$, it replaces it. Otherwise, the trial counter of source $i$ 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** $L$, 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](https://en.wikipedia.org/wiki/Artificial_bee_colony_algorithm)
- [The official ABC website](https://abc.erciyes.edu.tr/), 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, $f(x) = \sum_{i=1}^{n} x_i^2$.
- `main.m`: a one line entry point that runs `abc.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 | $-10$, $10$ |
| `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 $\varphi$ | 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 $\varphi$ 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 $F_i = e^{-c_i / \bar{c}}$, where $c_i$ is the cost of source $i$ and $\bar{c}$ is the mean cost of all sources. The original ABC uses $F_i = 1/(1 + c_i)$ 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](https://yarpiz.com/bees-algorithm-ypea115) is another metaheuristic based on honey bee foraging.
- [YPEA](https://yarpiz.com/yarpiz-evolutionary-algorithms-ypea) is a MATLAB toolbox that includes ABC and 14 other algorithms that share one problem definition.

## How to cite

Mostapha Kalami Heris, Artificial Bee Colony (ABC) in MATLAB (YPEA114), Yarpiz, 2015. https://yarpiz.com/artificial-bee-colony-ypea114
