# Bacterial Foraging-Based Algorithm for Optimizing the Power Generation of an Isolated Microgrid
## 3 Two-Swim Modified Bacterial Foraging Optimization Algorithm (TS- MBFOA)  
TS-MBFOA is an algorithm derived from MBFOA proposed to solve CNOPs [16]. In this metaheuristic, a bacterium \(i\) represents a potential solution to the CNOP (i.e., a n-dimensional real-value vector identified as \(\vec{x}\)), and it is defined as \(\theta^{i}(j,G)\), into a population of bacteria (\(S_{b}\)), where \(j\) is the chemotaxis loop (\(N_{c}\)). \(G\) is the generational loop that ends up reaching a maximum number of generations (\(GMAX\)) or using a number of evaluations, defined by the user, calculated as:  
\[GMAX=\frac{Number\ of\ evaluations}{S_{b}\times N_{c}}. \tag{21}\]  
A generation includes the following processes: (1) a chemotaxis process with \(N_{c}\) loops; (2) a swarming towards the best bacterium of the swarm \(\theta^{B}(G)\); (3) a reproduction process, if the frequency parameter \(RepCycle\) (defined by the user) allows it, with the best bacteria of the swarm \(S_{r}\); and finally (4) an elimination-dispersal process that eliminates the worst bacterium of the swarm. **Chemotaxis:** In this process, two swims are interleaved in each generation: either the exploitation swim or exploration swim is performed. The process starts with the exploitation swim (classical swim). Yet, a bacterium will not necessarily interleave exploration and exploitation swims, because if the new position of a given swim \(\theta^{i}(j+1,G)\) has better fitness (based on the feasibility rules) than the original position \(\theta^{i}(j,G)\), another swim at the same direction will take place in the next loop. Otherwise, a new tumble is computed. The process stops after \(N_{c}\) attempts. The exploration swim uses the mutation between bacteria and is calculated by:  
\[\theta^{i}(j+1,G)=\theta^{i}(j,G)+(\sigma)(\theta^{r}_{1}(j,G)-\theta^{r}_{2} (j,G)) \tag{22}\]  
where \(\theta^{r}_{1}(j,G)\) and \(\theta^{r}_{2}(j,G)\) are two different randomly selected bacteria from the population. Additionally, \(\sigma\) is a parameter defined by the user used in the swarming operator, which defines the proximity of the new position of a bacterium with respect to the position of the best bacteria in the population \(\theta^{B}(G)\). In this operator, \(\sigma\) is a positive control parameter for scaling the different vectors in (0,1), i.e., scales of the area where a bacterium can move. The exploitation swim is calculated as:  
\[\theta^{i}(j+1,G)=\theta^{i}(j,G)+C(i,G)\phi(i) \tag{23}\]  
\begin{table}
\begin{tabular}{l c c c c c} \hline \hline \multirow{2}{*}{**Time**} & \(L_{P}\) & \(P_{1}\) & \(P_{2}\) & \(P_{3}\) & \(P_{4}\) \\ \cline{2-6}  & **Watts** & **Watts** & **\%** & **Watts** & **Watts** \\ \hline
08:00 & 2300 & 5000 & 0.480 & 70 & 1 \\
09:00 & 2320 & 5000 & 0.480 & 560 & 1 \\
10:00 & 2350 & 5000 & 0.551 & 700 & 1 \\
11:00 & 2950 & 5000 & 0.656 & 126 & 600 \\
12:00 & 2250 & 5000  
where \(\phi(i)\) is calculated with the original tumble operator of BFOA:  
\[\phi(i)=\frac{\Delta(i)}{\sqrt{\Delta(i)^{T}\Delta(i)}} \tag{24}\]  
where \(\Delta(i)^{T}\) is a random vector with elements within the range \([-1,1]\). \(C(i,G)\) is the random step size of each bacterium updated by:  
\[C(i,G)=R*\Theta(i) \tag{25}\]  
where \(\Theta(i)\) is a randomly generated vector of size \(n\) with elements within the range of each decision variable: \([U_{x},L_{x}]\), \(x=1,\ldots,n\), and \(R\) is a user-defined parameter for scaling the step size (this value must be close to zero, for example 5.00E-04). The initial \(C(i,0)\) is generated using \(\theta(i)\). This random step size allows bacteria to move in different directions within the search space and prevents premature convergence, as suggested in [31]. Step size \(R\) can be randomly, statically, and dynamically adjusted [32].