The Flower Pollination Algorithm (FPA) is a nature-inspired metaheuristic for continuous optimization, proposed by Xin-She Yang in 2012. Global pollination moves a solution toward the best one found so far using a Lévy flight, and local pollination mixes two random solutions from the population. In C++ you implement it with std::vector, the <random> distributions and a switch probability that picks between the two moves.
Because FPA needs no gradient, it suits noisy or non-differentiable problems. It is a heuristic, so it finds good solutions but cannot prove optimality.
Each flower is a candidate solution and its pollen is the position vector. The algorithm rests on these rules:
p decides, for every move, whether it is global or local.Global pollination moves x_i toward the current best g*:
Local pollination uses two other randomly chosen flowers x_j and x_k:
L is a Lévy-distributed step and eps is uniform in the range 0 to 1. A candidate replaces the current flower only if its cost is lower.
Draw u from a normal distribution with standard deviation sigma and v from a standard normal distribution, then use step = u / |v|^(1/beta). With beta = 1.5, sigma is:
std::tgamma from <cmath> computes the gamma function, and C++20 provides std::numbers::pi in <numbers>.
The program minimises the Rastrigin function in 5 dimensions, a standard multimodal benchmark whose minimum is 0 at the origin.
Build and run it with:
Rastrigin has many local minima, so the final cost can be a small positive number rather than exactly zero. Re-running with different seeds and more iterations improves the odds of reaching the global minimum.
Wrapping the state in a class keeps the random engine, population and best solution together and makes the optimizer reusable with another objective. Greedy selection never makes a flower worse, which stabilises the search. std::move avoids copying the accepted candidate.
| Parameter | Typical value | Effect |
|---|---|---|
| Population | 20 to 50 | Larger populations explore more but cost more per iteration |
Switch probability p | 0.8 | Higher values favour global exploration |
Lévy exponent beta | 1.5 | Controls how heavy the tails of the step distribution are |
| Step scale | 0.01 here | Match it to the size of the search range |
Global pollination takes long Lévy jumps toward the best solution, which explores. Local pollination mixes two random flowers, which refines the search near existing solutions.
Rastrigin has many local minima, so it shows whether the Lévy flights help the search escape them.
Evaluate candidates in parallel with std::for_each(std::execution::par, ...) only if your objective is thread-safe and you give each thread its own random engine. Otherwise reduce population size or iterations.
No. It is a stochastic heuristic, so test several seeds and compare with other optimizers on your problem.
FPA combines Lévy-flight moves toward the best solution with local mixing of random solutions, switched by a probability p. In modern C++ it needs std::vector, <random>, std::tgamma and greedy replacement. Seed the engine, clamp positions to the bounds, and run several seeds to get reliable results.