# Bees Algorithm (BeA) in MATLAB

URL: https://yarpiz.com/bees-algorithm-ypea115
Author: Mostapha Kalami Heris
Published: 2015-09-11
Updated: 2026-10-30
Topics: Metaheuristics

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

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](https://en.wikipedia.org/wiki/Bees_algorithm).

## 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 $m$ sites are selected for a local search. Among them, the best $e$ 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]$ to it.
- `Sphere.m`: the sample cost function, $f(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$ |
| `MaxIt` | Maximum number of iterations | 1000 |
| `nScoutBee` | Number of scout bees (sites) | 30 |
| `nSelectedSite` | Number of selected sites, $m$ | `round(0.5*nScoutBee)`, which is 15 |
| `nEliteSite` | Number of elite sites, $e$ | `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\,(\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 $F_i = 1/c_i$ from the cost $c_i$ of each site and the relative fitness $d_i = F_i / \bar{F}$, where $\bar{F}$ is the mean fitness. Then, for each site:

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

Because the fitness is $1/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.

## Related posts

- [Artificial Bee Colony in MATLAB](https://yarpiz.com/artificial-bee-colony-ypea114) is another metaheuristic based on honey bee foraging.
- [YPEA](https://yarpiz.com/yarpiz-evolutionary-algorithms-ypea) is a MATLAB toolbox that includes the Bees Algorithm and 14 other algorithms that share one problem definition.

## How to cite

Mostapha Kalami Heris, Bees Algorithm (BeA) in MATLAB (YPEA115), Yarpiz, 2015. https://yarpiz.com/bees-algorithm-ypea115
