# A Load Balancing Routing Mechanism Based on SDWSN in Smart City
## 4 SDSNLB Algorithm and Solution
### Clustering and Parameter Adjustment  
The traditional Elman network uses the gradient descent method to train network parameters, but the gradient descent method has the disadvantages of slow convergence and easy to fall into local minimum values. Therefore, we use clustering and parameter adjustment methods to train network data, and achieve the goal of rapid optimization. And the trained model, that is, the improved routing decision is stored in the SDN controller. When there are new requests, the path can be quickly predicted to achieve SDWSN network load balancing.  
Firstly, the training samples are fuzzy clustered, and they are subordinate to each category. These memberships can classify the training samples and construct a neural network classifier. For new unknown samples, it is not necessary to repeatedly calculate the clustering process. The category of the sample can be identified directly by the classifier. The process of clustering is as follows:  
1. Enter link matrices {TM\({}_{i}\), i = 1, 2,..., N\({}_{i}\)}, and select initial cluster centers { TM\({}_{i}\), TM\({}_{2}\),..., TM\({}_{\text{Nc}}\)}, it may not be equal to the number of cluster centers required. The initial value can be arbitrarily selected from the sample.  
2. The N traffic matrix is assigned to the type \(S_{j}\), for the non-cluster center traffic matrix TM in the sample.  
\[D_{i}=min\{\|TM-TM_{i}\|,\ i=1,2,\cdots,N_{c}\}\]  
If \(D_{j}=\|TM-TM_{j}\|\) is the minimum value, then \(TM\in S_{j}\).  
3. If \(S_{j}<\theta_{N}\), cancel the sample subset, then subtract 1 from \(N_{c}\).  
4. Correcting Cluster Centers  
\[Z_{j}=\frac{1}{N_{j}}\sum_{TM\in S_{j}}TMj=1,2,\cdots,N_{c}\]  
5. Calculate the average distance between the traffic matrix and cluster centers in each clustering domain \(S_{j}\)  
\[\overline{D_{j}}=\frac{1}{N_{j}}\sum_{TM\in S_{j}}\bigl{\|}TM-Z_{j}\bigr{\|}j= 1,2,\cdots,N_{c}\]6. Calculating the average of the distances between clusters of all link matrices and the corresponding clustering domain.  
\[\overline{D}=\frac{1}{N}\underset{j=1}{\overset{N}{\sum}}TM_{j}\overline{D_{j}}\]  
7. Splitting, merging and iterative operations  
\(\bullet\) If the number of iterations has reached I, that is, the last iteration, go to 11.  
\(\bullet\) If \(N_{c}\leq\frac{K}{2}\), that is, the number of cluster centers is less than or equal to half of the initial value, go to 8 and split the existing clustering domain.  
\(\bullet\) If the number of iterative operations is an even number, or \(N_{c}\geq 2k\), do not split, go to 11; otherwise, go to 8.  
8. Calculate the Standard Deviation Vector of Sample Distance in Each Cluster.  
\[S_{j}=\left(S_{1j},S_{2j},\cdots,S_{nj}\right)^{T}\]  
The components of the vector are \(S_{ij}=\sqrt{\frac{1}{N_{c}}\underset{k=1}{\overset{N_{j}}{\sum}}\left(TM_{ik} -Z_{ij}\right)^{2}}\) i = 1, 2,..., n; j = 1, 2,..., \(N_{c}\).  
9. Find the maximum component of each standard deviation vector \(\left\{\delta_{j\max},j==1,2,\cdots,N_{c}\right\}\),  
10. In any maximum component set \(\left\{S_{j\max},j==1,2,\cdots,N_{c}\right\}\), if \(S_{j\max}>\theta_{S}\), and satisfies one of the following two conditions:  
\(\bullet\)\(\overline{D_{j}}>\overline{D}\), and \(N_{j}>2(\theta_{N}+1)\)  
\(\bullet\)\(N_{c}\leq\frac{K}{2}\) Then \(Z_{j}\) is split into two new cluster centers, and \(N_{c}\) plus 1.  
11. Calculate the distance of all cluster centers  
\[D_{ij}=\|Z_{i}-Z_{j}\|i=1,2,\cdots N_{c}-1;j=i+1,\cdots,N_{c}\]  
12. Compare the values of \(D_{ij}\) and \(\theta_{c}\), The value of \(D_{ij}<\theta_{c}\) is arranged incrementally by the minimum distance.  
13. Merge two cluster centers \(Z_{ik}\) and \(Z_{jk}\) with distance \(D_{ikjk}\) to get the new center.  
\[Z_{K}^{*}=\frac{1}{N_{ik}+N_{jk}}\Big{[}N_{ik}Z_{ik}+N_{jk}Z_{jk}\Big{]}k=1,2, \cdots,L\]  
14. If the last iterative operation (the L-th time), the algorithm ends.