Metaheuristics

Artificial Bee Colony (ABC) in MATLAB

Get the code
Comments

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

Illustration for Artificial Bee Colony (ABC) in MATLAB
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 xix_i has one employed bee. The bee creates a new candidate viv_i near its source, using another source xkx_k chosen at random (k≠ik \neq i):

vi=xi+φ⊙(xi−xk)v_i = x_i + \varphi \odot \left(x_i - x_k\right)

Here φ\varphi holds random numbers drawn uniformly from [−a,a][-a, a]. If viv_i is better than xix_i, it replaces it. Otherwise, the trial counter of source ii 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 LL, 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

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)=∑i=1nxi2f(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, 1010
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 Fi=e−ci/cˉF_i = e^{-c_i / \bar{c}}, where cic_i is the cost of source ii and cˉ\bar{c} is the mean cost of all sources. The original ABC uses Fi=1/(1+ci)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.

  • 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.

Plain text
Mostapha Kalami Heris, Artificial Bee Colony (ABC) in MATLAB (YPEA114), Yarpiz, 2015. https://yarpiz.com/artificial-bee-colony-ypea114
BibTeX
@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}
}

Comments

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