# FireFly Algorithm for Robot Path Planner in C++

FireFly algorithm (FA) is a stochastic optimization algorithm based on how fireflies behave after they ignite. The luminosity and phototaxis of fireflies are idealized in order to guarantee the effectiveness, practicability, and simplicity of the method. The firefly is relocated to the brightest firefly in the area and its location is constantly updated during the operation of the algorithm, which solely searches for the partner of the firefly’s illuminance and phototaxis.

The FA is based on the following rules which can be summarized as follows,

The FA is very simple and effective however there are certain constant parameters which has to be evaluated and deployed.
For this article, I followed the article which can be read here. The authors specified the cost function which I deployed in C++.

The source code for this article in C++ can be found on my GitHub.

Considering the robot path planner, we are looking at the shortest obstacle-free path (here in 2D spaces) between the start and the goal. The waypoints on the path (the position of fireflies) have to be chosen in the way that the cost function has to achieve a global minimum (optimization problem).
The proposed algorithm runs in the loop and updates the positions of the fireflies (the position of the robot on the path we are trying to find) according to the formula,

where,

x_i or x_j is the position of the firefly, 𝛽0 is the attractiveness of ﬁreﬂy, rij — the euclidean distance between i and j firefly, 𝛾 — light absorption coefﬁcient, 𝛼0 and 𝛿 randomization parameters (see article), scale is a range of robot working space (here 100).

The cost function that the algorithm is trying to optimize (compute minimum) can be defined as follows,

where,

distance firefly — obstacle,

distance firefly — goal,

K1 and K2 are coefficients taken from the article.

The optimal values of all constants depicted in the above formulas are taken from the article I refer to.

For the purpose of this algorithm, I used 100 fireflies. the initial positions were chosen randomly. The intuition of the working algorithm can be depicted as follows,

Now we can specify pseudo-code for the FA algorithm, which has its implementation in my C++ program.

The performance of the algorithm can be depicted as follows;

`//compileg++ my_prog.cpp -o my_prog -I/usr/include/python3.8 -lpython3.8// //run./my_prog//folder tree├── my_prog├── my_prog.cpp├── matplotlibcpp.h`