Artificial hummingbird algorithm for Solving Optimization Problems in C++

Markus Buchholz
7 min readJan 25


In this article, an artificial hummingbird algorithm (AHA), a nature-inspired optimization method, is put forth to address optimization issues. The AHA algorithm mimics the unique flying abilities and intelligent foraging techniques of hummingbirds. Axial, diagonal, and omnidirectional flights are three types of flying abilities used in foraging tactics that are modeled. The presented algorithm mimics the memory function of hummingbirds for food sources, a visiting table of food sources is modeled, along with directed foraging, territorial foraging, and migrant foraging.
The author of this article is under a huge impression over the intelligence of hummingbirds, flying capability, and maneuverability. The hummingbird can fly backward! Additionally, the author found brilliant minds to take to account the hummingbird's behavior and model an extremely powerful optimization algorithm.

In this article, I will display the main features of the algorithm and derive the main updated functions to help with the understanding of hummingbird behavior and how the algorithm mimics nature. Later I will solve the optimization problems with constraints.

The source code for this algorithm in C++ you will find on my Github.

Here and here you can see these amazing birds in action.

Artificial hummingbird algorithm (AHA)

Hummingbirds are the smallest birds in the world. Hummingbirds would be the most intellectual species on earth, including humans if intelligence were determined by the brain-to-body ratio. There are over 360 different hummingbird species in the globe, and the majority of them only have a 7.5 to 13 cm body length. Hummingbirds may beat their wings up to 80 times per second, which is the greatest rate of any bird. Hummingbirds often eat a variety of insects, including mosquitoes. Hummingbirds consume a lot of flower nectar and the delicious liquid found inside flowers each day to provide them the energy they need to soar. During the flight, the hummingbird's heart can beat approx 1200 beats/min! (see here).

Hummingbirds are unique in that they have a remarkable memory of finding food. Each hummingbird can really recall specifics about certain flowers in a given area, such as their position, nectar quality and content, rate of nectar replenishment, and when they last visited the flowers.

The bird can also remember the spatial-temporal information about the food sources and use this information to avoid flowers (food sources), which were visited previously. Episodic memory is the use and preservation of memory regarding specific experiences, and it was formerly frequently used to differentiate between animals and humans.

The capacity to fly is another unique trait of hummingbirds. They are the greatest fliers among bird species due to their small size and high-frequency wingbeats. Hummingbirds fly resemble civil helicopters which can hoover in the air.

Due to harsh weather conditions or food shortages, hummingbirds frequently migrate hundreds of kilometers to far-off locations.

The pioneers of discussed AHA algorithm models three main components of hummingbird life (more information you will find in the article),

Food sources, a hummingbird differentiates characteristics of sources, based o nectar quality and content of individual flowers, nectar-refilling rate, and the most recent time to visit the flowers.
For the sake of simplicity, each food source in AHA is assumed to have the same number and kind of flowers; a food source is a solution vector, and the pace at which a food source replenishes its nectar is represented by its function fitness value.

Hummingbirds, Each hummingbird has a specific food source that it can only eat from, therefore the location of the hummingbird and the food source is always the same. A hummingbird may remember the location and nectar replenishment frequency of this particular food source and pass that knowledge along to other hummingbirds in the community.
Additionally, each hummingbird has the ability to recall how long it has been since it last visited a particular food source.

Visit table, the table keeps track of each hummingbird’s visit level for each food source, which indicates how long it has been since that hummingbird has last visited a particular food source. For a hummingbird, the food source with the highest visit level will receive preferential visits. A hummingbird usually visits the food source with the greatest rate of nectar replenishment among those with the same top visit level in order to get more nectar. The visit table allows each hummingbird to locate the specific food source it is looking for. Every iteration often involves updating the visit table.

The algorithm starts with the random initialization of hummingbirds and visits the table. Bear in mind, in the program we keep the table which includes the position of the birds, the visit table, and the actual value of the fir (objective) function.

Depending on random value [0,1] birds move to the other location according to the presented below foraging behaviors.

foraging behaviors of AHA (by author)

The flight to the next position (guided foraging or territorial foraging) is performed (based on the random number [0,1]) according to light behaviors (axial, diagonal, or omnidirectional flight).

Flights Behaviours of AHA (by author)

The updated function depending on foraging behavior can be presented as follows,

Guided foraging, each hummingbird has a natural desire to seek out the source of nectar with the largest amount, therefore a target supply must have a high rate of nectar replenishment and a lengthy period between visits for that particular hummingbird. Therefore, according to AHA, a hummingbird should select the food source with the highest nectar-refilling rate among those with the highest visit levels for its target food source. This hummingbird can fly toward its intended food source after it has been identified.

where X is the position of the food source, X_target is the position of the target food source that the hummingbird intends to visit. a is a guided factor, random value [0,1].

Territorial foraging, a hummingbird is more likely to hunt for a new food source after visiting its target food source, where the flower nectar has been consumed, rather than going to other nearby food sources. A hummingbird may therefore easily relocate to its surrounding area inside its own territory, where it may find a new food supply as a potential option that may be better than the existing one (see the figure above).

The model for this behavior can be represented as follows,

b — random value[0,1], considered as territorial factor.

Migration foraging, a hummingbird often migrates to a more remote food source to eat when the current area is deficient in food. An estimated migration coefficient is used in the AHA algorithm. The migration in AHA can be expressed as a random reinitialization of the birds' position. We can express the migration using the following formula,


In this article, I used the SCA to solve two optimizations problem. The simple one is the ordinary quadratic function I used for my personal debugging purpose (see Github).

The second problem is connected (benchmark optimization problem) with tension/compression spring where the objective of the problem is to minimize the weight. There are three design variables: wire diameter ( d ), mean coil diameter ( D ), and the number of active coils ( N ). This is a three-dimensional problem (each variable is considered as a dimension).

(a) Schematic of the spring; (b) stress distribution evaluated at the optimum design; and c) displacement distribution evaluated at the optimum design (article I referred to)

The optimization problem can be formulated as follows :

The existing constraints have been included in the objective function as penalties. I modeled penalties as a quadratic loss function (see here and here).
The implemented method solves the problem (computes the minimum weight of the spring). The solution I received was equal (almost) to the article I was inspired by.

Result of the optimization problem. Spring design (by author)

Thank you for reading.