# 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,

- Fireflies do not distinguish genders, and their attraction to one another is solely based on how bright they are.
- The attraction of two fireflies is proportional correlated to the brightness and inversely correlated to the distance between them.

The position of the firefly affects the brightness. If the position is better the brightness illuminated by the firefly is more intense. The more brightness firefly is the more fireflies are attracted. Where they are positioned affects how brilliant they are. - The value of the desired objective function determines how brilliant fireflies are.

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;

Plotting requires incorporating the header file which has to be in the same folder as your cpp (a file you can clone from my repository).

Your program can be compiled as follows,

`//compile`

g++ 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

Thank you for reading.