# Hybrid Energy Routing Approach for Energy Internet
## 4 Subscriber Matching Mechanism
```
Input:\(c,P_{c},S\) Output:\(f0 min,gbest\)  Initialize the PSO parameters (\(k,n,C_{1},C_{2}\), and \(w\)) /* Initialize the random positions of each particle (\(X_{i}\)) */ \(i\gets 1\) while\(i<=k\)do for\(j\gets 1\)to\(n\)do \(X(i,j)\gets Rand(X(i,j)_{min},X(i,j)_{max})\) if\(X(i,m)_{max}<=P_{c}-sum(X(i,n-1))\)then \(X(i,j)\gets P_{c}-sum(X(i,n-1))\) \(i\gets i+1\)  Initialize the particle velocity \(V(i)\) /* Evaluate the fitness function of each particle */ for each particle \(i\)do \(f_{0}(i)\gets OF(X(i,:),S,c)\) \([f0 min,index]\gets min(f_{0})\) \(pbest\gets X\) \(gbest\gets X(index)\) \(ite\gets 1\) while\((ite<=maxit)\)&&! (error criteria)do  Update \(V\) according to Equation (8) for each particle \(i\)do for each producer \(j\) in \(S\)do  Update \(X\) according to Equation (9) \(X(i,j)\gets X(i,j)+V(i,j)\) \(f(i)\leftarrow OF(X(i,:),S,c)\) if\(f(i)<f_{0}(i)\)then \(f_{0}(i)\gets f(i)\) \(pbest(i)\gets X(i,:)\) \([fmin,index]\gets min(f_{0})\) if\(fmin<f0 min\)then \(gbest\gets pbest(index)\) \(f0 min\gets fmin\) \(i\gets i+1\)
```  
**Algorithm 2**Energy Particle Swarm Optimization
* The total amount of power in the particle must be equal to the consumer demand: \[\Sigma_{i=1}^{n}X_{i}=P_{c}\] (10)
* The amount of power from producer \(j\) in particle \(i\) must be within the producer's capacity (the available power): \[0<X_{ij}\leq P_{p_{j}}\] (11)  
Finally, after the determination of the required power amount from each producer in each set with the minimum energy transmission cost, the ER selects the best set using Equation (12):  
\[Fitness_{min}=min_{k=1\ldots m}(Fitness_{S_{k}}) \tag{12}\]  
The ER obtains the best set with all the necessary information (required power, best path to each producer in the best set) and as in case 1, it creates the circuit, does the \begin{table}
\begin{tabular}{l l} \hline \hline
**Symbol** & **Description** \\ \hline \(c\) & The consumer \\ \(P_{c}\) & The amount of consumer demand energy \\ \(S\) & Set producers from the \(C_{n}^{c}\) \\ \(S(i)\) & The \(i\)th producer of set \(S\) \\ \(k\) & The population size (number of particles) \\ \(n\) & The number of consumers in set \(S\) \\ \(C_{1},C_{2}\) & Positive acceleration constants \\ \(r_{1},r_{2}\) & Random values (\(0\leq r_{1},r_{2}\leq 1\)) \\ \(w\) & Inertia factor \\ \(X\) & The position of the particles which represents the amount of energy to took from each producer in the set \(S\), \(X(i)=[P_{1}\ldots P_{m}]\) \\ \(X(i,j)_{min}\) & The minimum amount of energy that could be provided by producer \(j\) in set \(S_{i}\). (in our case 0) \\ \(X(i,j)_{max}\) & The maximum amount of energy that could be provided by producer \(j\) in set \(S_{i}\) (\(P_{j}\)) \\ \(V\) & The velocity \\ \(f_{0}\) & The fitness value of the initial \(X\) (the energy transmission cost of the corresponding energy in \(X\)) \\ \(f\) & The fitness value of \(X\) (the energy transmission cost of the corresponding energy in \(X\)) \\  & The personal best solution of each particle which represents the best amount of energy \\ \(pbest\) & to get from each producer in set \(S\) to reach the consumer demand with minimum \\  & transmission cost \\ \(gbest\) & The global best solution, which represents the best particle (energy amount) with the minimum fitness \\ \(maxit\) & The max iterations number \\ \hline \hline \end{tabular}
\end{table} 
Table 5: Algorithm 2’s parameters. necessary updates and informs the broker, and then starts the energy transmission from selected producers. The consumer-prosumer pairs, produced by the subscriber matching, should satisfy the following constraints:  
* If the prosumer/producer is matched with multiple consumers, the total amount of selling electricity (\(p_{cp}\)) should not exceed the existing power of the prosumer (\(P_{p}\)): \[0\leq\Sigma_{c=1}^{k}p_{cp}\leq P_{p}\] (13)
* If the consumer is matched with multiple prosumers, the total amount of buying electricity (\(p_{cp}\)) should not exceed the demand energy of the consumer (\(P_{c}\)): \[0\leq\Sigma_{p=1}^{k}p_{cp}\leq P_{c}\] (14)