## 3 Controller Design
### PSO-Fuzzy PID Controller
#### 3.3.1 Particle Swarm Optimization Algorithm
```
Step 1: Initialize random positions and velocities for each particle  For each particle in particles  particle.position = RandomPosition()  particle.velocity = RandomVelocity() end Step 2: Evaluate the fitness of each particle  For each particle in particles  particle.fitness = EvaluateFitness(particle.position) end Step 3: bestParticle = GetBestParticle(particles) %Obtain the best particle's position  Step 4:globalBestPosition = bestParticle.position %Update the global best position over all previous bests Step 5: WHILE number_of_iterations < max_iterations  Evaluate the new position for each particle  For each particle in particles  particle.position = UpdatePosition(particle, globalBestPosition)  particle.fitness = EvaluateFitness(particle.position)  Update the best particle if necessary  If particle.fitness < bestParticle.fitness  bestParticle = particle  end end number_of_iterations \(\leftarrow\) 1 end
```  
**Algorithm 1**Strategy for the PSO algorithm  
Additionally, the parameters of the PSO algorithm, including the number of agents (Np), iterations (Ni), cognitive, social coefficient, and inertia weight, are summarized in Table 3. The PSO algorithm parameters listed in Table 3 were obtained from extensive experimentation with the mathematical model in Simulink (MATLAB). The decision for the number of agents (Np) and iterations (Ni) was made after testing various values to find the optimal configuration that balances computational efficiency with fast convergence. These chosen values were based on the performance consistency observed in various experimental runs, where these values led to optimal controller parameters (_Kp_, _Ki_, _Kd_) and exhibited improved response of the system. These hyperparameters were not arbitrary but were determined after a thorough process of experimentation to ensure the best results. These values ensure optimal control performance, with improved system stability and response time, as shown in Figure 16.  
Figure 17, which includes a block diagram of the process, illustrates the optimization process for the PSO-Fuzzy PID system. The MATLAB program was used to find the optimal values of Fuzzy PID parameters. To search the domain of position and velocity of particles, the program uses a minimization algorithm. After 100 iterations, the optimal values of Fuzzy PID controller parameters are obtained. As shown in the dashed box of Figure 17, the update and iteration of particles were separated into two sections in MATLAB. In the first section (A), the control system model of the Simulink platform was used, and the performance index was obtained for each sampling  
Figure 16: System response over time with optimized PSO-Fuzzy PID controller. \begin{table}
\begin{tabular}{c c c c c c} \hline
**Parameter** & **No. Iteration** & **No. Particles** & \begin{tabular}{c} **Social** \\ **Coefficient** \\ \end{tabular} & \
\begin{tabular}{c} **Cognitive** \\ **Coefficient** \\ \end{tabular} &
\begin{tabular}{c} **Inertia** \\ **Weight** \\ \end{tabular} \\ \hline Value & 100 & 50 & 2 & 2 & 0.7 \\ \hline \end{tabular}
\end{table} 
Table 3: The parameters of the PSO algorithm. Figure 17: The block diagram of the optimized Fuzzy PID controller. period. The second section (B) involved programming the PSO optimization in MATLAB executable code file (.m file). In this section, the particle position and velocity were updated based on the fitness value. The gBest, which represents the historical best position of the swarm, corresponds to the values for the three proportional and quantitative factors.