Metaheuristics

Bees Algorithm (BeA) in MATLAB

Get the code
Comments

Compare standard and probabilistic Bees Algorithms in MATLAB, with local recruitment, global scouting, shrinking neighborhoods, and a Sphere example.

Illustration for Bees Algorithm (BeA) in MATLAB
On this page

The Bees Algorithm (BeA) is a metaheuristic optimization algorithm inspired by the food foraging behavior of honey bee colonies. It was proposed by Pham et al. in 2005.

The algorithm uses the waggle dance to simulate communication between bees. Better bees (solutions) have more opportunity to dance, so they can attract more bees to their location and target. This helps the algorithm examine the promising areas of the search space in more detail.

For more information, see the Wikipedia article on the Bees Algorithm (opens in a new tab).

How the Bees Algorithm works

The Bees Algorithm combines a local search around good sites with a global random search. A site is a point in the search space, found by a scout bee.

  1. Scouting. Scout bees are placed at random in the search space, and the cost of each site is evaluated.
  2. Site selection. The sites are sorted by cost. The best mm sites are selected for a local search. Among them, the best ee sites are called elite sites.
  3. Recruitment. Bees are recruited to search the neighborhood of each selected site. Elite sites get more bees than the other selected sites, so the best areas are searched most closely.
  4. Neighborhood search. Each recruited bee visits a random point inside a patch around its site. If the best of these points is better than the site, it becomes the new site.
  5. Global search. The remaining scout bees, those not on selected sites, are sent to new random positions.

These steps repeat until the iteration limit. The size of the patch controls how local the search is.

Implementation in MATLAB

This post shares a structured, open-source implementation of BeA in MATLAB. If you know MATLAB, you should find the code easy to use in your research and projects.

What is in the download

The download has two versions of the algorithm, each in its own folder:

  • 01 Bees Algorithm (Standard): the steps described above, with a fixed number of selected and elite sites.
  • 02 Bees Algorithm (Probabilistic): a version in which the number of recruited bees depends on the quality of each site, and poor sites may be rejected at random.

Each folder holds the same four files:

  • ba.m: the main script, with the problem definition, the parameters, the main loop and the results.
  • PerformBeeDance.m: the neighborhood search. It picks one variable at random and adds a uniform random number from [−r,r][-r, r] to it.
  • 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 ba.m.

To run either version, open its 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 sites with unifrnd, a function of the Statistics and Machine Learning Toolbox, so it probably needs that toolbox.

Parameters of the standard version

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 1000
nScoutBee Number of scout bees (sites) 30
nSelectedSite Number of selected sites, mm round(0.5*nScoutBee), which is 15
nEliteSite Number of elite sites, ee round(0.4*nSelectedSite), which is 6
nSelectedSiteBee Bees recruited for each selected, non elite site round(0.5*nScoutBee), which is 15
nEliteSiteBee Bees recruited for each elite site 2*nSelectedSiteBee, which is 30
r Neighborhood radius (patch size) 0.1 (VarMax−VarMin)0.1\,(\text{VarMax} - \text{VarMin})
rdamp Damping ratio of the radius 0.95

After every iteration, the radius is multiplied by rdamp, so the neighborhood search becomes finer over time.

The probabilistic version

The second version replaces the fixed site counts with one parameter, nBee0 = round(0.3*nScoutBee), which is 9. In every iteration, it computes a fitness Fi=1/ciF_i = 1/c_i from the cost cic_i of each site and the relative fitness di=Fi/Fˉd_i = F_i / \bar{F}, where Fˉ\bar{F} is the mean fitness. Then, for each site:

  • The site is rejected with a probability that depends on did_i: 0.6 if di<0.9d_i < 0.9, 0.2 if 0.9≤di<0.950.9 \le d_i < 0.95, 0.05 if 0.95≤di<1.150.95 \le d_i < 1.15, and 0 if di≥1.15d_i \ge 1.15.
  • A rejected site is replaced with a new random site.
  • An accepted site gets ⌈nBee0 di⌉\lceil n_{\text{Bee0}}\, d_i \rceil recruited bees, so better sites get more bees.

Because the fitness is 1/ci1/c_i, the probabilistic version needs strictly positive costs. Sphere is nonnegative but becomes exactly zero at the optimum, so that case also needs explicit handling.

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 ba.m.

Check the result

For the Sphere example, inspect the best position and its cost against the known optimum at the origin. The probabilistic version divides by each cost and by the mean fitness, so test zero-cost handling before applying it to a new problem. In both versions, shrinking the patch radius concentrates the local search. Compare repeated runs and the number of recruited evaluations, since one iteration can involve many neighborhood trials.

  • Artificial Bee Colony in MATLAB is another metaheuristic based on honey bee foraging.
  • YPEA is a MATLAB toolbox that includes the Bees Algorithm 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, Bees Algorithm (BeA) in MATLAB (YPEA115), Yarpiz, 2015. https://yarpiz.com/bees-algorithm-ypea115
BibTeX
@misc{yarpiz-ypea115,
  author = {{Mostapha Kalami Heris}},
  title = {{Bees Algorithm (BeA) in MATLAB}},
  howpublished = {Yarpiz},
  year = {2015},
  note = {Code YPEA115},
  url = {https://yarpiz.com/bees-algorithm-ypea115}
}

Comments

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