# Escaping Local Minima in Path Planning Using a Robust Bacterial Foraging Algorithm
## Implementation Details
### The Robot's Trajectory Process  
With the bacterial foraging algorithm, the process is initialized by generating \(N_{s}\) random particles (bacterium) around the robot within a circle of radius \(R\). This defines the step size of the movement in the trajectory as:  
\[\theta^{i}(t+dt)=\theta^{i}(t)+R\left(\frac{\Delta(t)}{\|\Delta(t)\|}\right) \tag{9}\]  
where \(\Delta(t)\) is a random vector of a unit length to determine the direction of each particle, and \(\|\Delta(t)\|\) defines the magnitude of the vector. Each bacterial group that is around the robot keeps searching for the optimal way to the target, and it can change direction at any time. The robot updates its position to that of one of these bacteria based on the selection of the best. This process continues until every obstacle is avoided and the goal is reached. The determination of the strongest bacterium is based upon two considerations. At every step, the distance between each bacterium, and the distance between the current position of the robot and the nearest obstacle, are calculated. The application of these two variables results in an attractive-repellent pattern, which guides the robot to the target point (global minimum).  
During the movement of the robot, the cost function \(J_{o}\) is zero when no obstacle is encountered. Otherwise, it will assume the repulsive Gaussian cost function for an obstacle:\[J_{o}=\begin{cases}H_{o}exp^{-W_{o}\left\|\theta^{i}(t)-q_{o}(t)\right\|^{2}}, \text{ if }\left\|q_{o}(t)-q\right\|\leq\beta\\ 0,\text{ otherwise}\end{cases} \tag{10}\]  
\(H_{o}\) and \(W_{o}\) are constants that refer to the repellent's height and width, respectively. With sensors placed at the front of the robot, it can detect any obstacle's location, \(q_{o}(t)\). The value of the \(J_{o}\) always remains zero until the robot senses the obstacle in its range, whereas the range of the sensor can be adjusted by \(\beta\). The Gaussian cost function of the goal point can be represented as:  
\[J_{g}=-H_{g}exp^{-W_{g}\left\|\theta^{i}(t)-q_{g}(t)\right\|^{2}} \tag{11}\]  
\(H_{g}\) and \(W_{g}\) refer to the goal's height and width. The direct distance between the goal and the \(ith\) bacterium can be calculated by the Euclidean distance, \(\left\|\theta^{i}(t)-q_{g}(t)\right\|\). Obstacle avoidance and goal search are the two most important concerns during mobile robots' PP. For the purpose, a combined cost function is defined as:  
\[J=J_{o}+J_{g} \tag{12}\]  
The above equation is used to find the total cost of each bacterium. Based upon their cost values, all particles are sorted from highly desirable to undesirable. Then, the robot's current location, \(q(t)\), is updated according to the highly desirable \(ith\) bacterium that ensures the minimum possible cost. These steps will guide the robot to the target while avoiding the obstacles. The strategy is based on the distance error, which can be written as follows:  
\[e_{d}^{i}=d^{i}(t+dt)-d^{i}(t) \tag{13}\]  
where \(d^{i}(t)\) and \(d^{i}(t+dt)\) are the Euclidean distances from the \(ith\) bacterium's position \(\theta^{i}\) to the goal \(q_{g}\) at times \(t\) and \(t+dt\), respectively. Equation (14) is the cost function error used for sorting all bacteria in an ascending order. The particle with the lowest distance error will be at the top of the priority list.  
\[e_{f}^{i}(t)=J(\theta^{i}(t+dt))-J(\theta^{i}(t)) \tag{14}\]  
The robot keeps on saving the trajectory in its memory; this information is later used for avoiding the local minima and rerouting.