# Coordinated Dispatch of Multi-Energy Microgrids and Distribution Network with a Flexible Structure
## 4 Solving Method
### First Level  
The problem can be solved by a particle swarm optimization (PSO) algorithm. PSO originates from the study of the predation behavior of birds. The basic idea of the particle swarm optimization algorithm is to find the optimal solution through cooperation and information sharing between individuals in the group. The variables are abstracted as particles (points) without mass and volume and extend to N-dimensional space. The position of particle k in N-dimensional space at ith iteration is represented as vector \(x_{i,k}\), the flight speed at ith iteration is represented as \(v_{i,k}\). Each particle has a fitness value, determined by the objective function. \(\textit{Pbest}=(\textit{pbest}_{1},\textit{pbest}_{2}\ \ldots,\textit{pbest}_{M})\) is the set of the best position of each particle. \(M\) is the number of particles. The best position the particles have ever found and the current positions can be seen as the particle's own flight experience. In addition, each particle also knows that the best position \(\textit{gbest}_{i}\) (the best value in _Pbest_). This can be regarded as the experience of particle companions. Particles update their own flight speed and position by comparing the fitness value fit(\(x_{i,k}\)) with the corresponding personal best fitness value in _Pbest_ and _gbest_. The detailed procedures are shown as follows:  
* **Step 1:** Initialization, acquire the system data, including line and bus parameters, generation information and states of switches. Generate a particle swarm with a sample number of M. Initialize the position \(x_{0,k}\) (including the vectors \(SW\) and \(\Phi\) in Equation (21)) and speed \(v_{0,k}\) of each particle. Set \(\textit{pbest}_{k}=x_{0,k}\), evaluate the fitness value fit(\(x_{0,k}\)) of each particle using the function in Equation (20) by power flow calculation. Find the particles with the minimum fitness value of all particles and set its position to \(\textit{gbest}_{0}\).
* **Step 2:** For ith iteration. Update the position and speed of each particle using Equations (32) and (33), evaluate the fitness value fit(\(x_{i,k}\)) of each particle using the function in Equation (20). \[v_{i,k}=\left\{\begin{array}{l}wv_{i,k}+c_{1}\times rand_{1}()\times(\textit {pbest}_{k}-x_{i,k})+\ldots\\ \ldots c_{2}\times rand_{2}()\times(\textit{gbest}-x_{i,k})\\ v_{\max}\ v_{i,k}>v_{\max}\\ v_{\min}\ v_{i,k}<v_{\min}\end{array}\right.\] (32) \[x_{i,k}=\left\{\begin{array}{l}x_{i,k}+v_{i,k}\ x_{\min}\leq x_{i,k}\leq x _{\max}\\ x_{\max},v_{i,k}=-v_{i,k}\ x_{i,k}>x_{\max}\\ x_{\min},v_{i,k}=-v_{i,k}\ x_{i,k}<x_{\min}\end{array}\right.\] (33) where \(rand_{1}()\), \(rand_{2}()\) represents random numbers between 0 and 1. \(c_{1}\), \(c_{2}\) are learning factors, \(w\) is the inertia weight. If the particles are discrete variables like the state of the ith switch \(s_{i,t}\) in the vector \(SW\), the following rules are required when performing speed updates: \[x_{i,k}=\left\{\begin{array}{l}1\ \ rand_{i,k}()<S(v_{i,k})\\ 0\ \ rand_{i,k}()\geq S(v_{i,k}))\end{array}\right.\] (34) \[S(v_{i,k})=\frac{1}{1+e^{-v_{i,k}}}\] (35) where \(S(V_{k})\) is a Sigmoid function, which is used to select the probability of discrete variable positions. Generate a random number \(rand_{i,k}()\) between 0 and 1 and compare it with \(S(v_{i,k})\) to determine the value of the binary variable \(x_{i,k}\).  
* **Step 3:** For each particle, compare the fitness value fit(\(x_{i,k}\)) with fit(\(pbest_{k}\)), update \(pbest_{k}\) to the position with lower fitness value.
* **Step 4:** For all particles, select the particle who has the lowest fitness value. Update \(gbest_{i}\) to its position.
* **Step 5:** Judge whether the result meets the end condition. If the iteration number has reached the upper limit or the fitness value convergence, the calculation stops. Otherwise, return to Step 2.