# Machine Learning-Based Methods for Enhancement of UAV-NOMA and D2D Cooperative Networks
## 3 UFBS Placement Procedure
### 2D UFBS Placement  
#### 3.1.1 k-Means Analysis and Setup  
This sub-subsection describes the UFBS 2D placement procedure utilizing the k-means algorithm. In more detail, the k-means algorithm is fed with the coordinates \(u_{l}\) (\(1\leq l\leq N\)) of all GMTs located within the region of interest \(A\). Subsequently, the algorithm groups the users into a cluster and returns as output the centroid point \(p_{1}^{c}\in A\) where \(p_{1}^{c}=(x_{1}^{p_{1}^{c}},y_{1}^{p_{1}^{c}},z_{1}^{p_{1}^{c}})\). The goal of the k-means method is to minimize the centroid-point to group distances metric, expressed as \(\sum_{u_{l}\in U}\|u_{l}-p_{1}^{c}\|^{2}\). In particular, this expression represents the objective function of the following minimization problem:  
\[\operatorname*{arg\,min}_{p_{1}^{c}\in A}\sum_{u_{l}\in U}\|u_{l}-p_{1}^{c}\| ^{2}. \tag{28}\]  
Therefore, the UFBS should be placed in \(p_{1}^{c}\) to achieve improved communication quality. The operation of the 2D UFBS placement process using the k-means algorithm is summarized in Algorithm 1.  
```
1:input: The set of coordinates of all GMTs \(U=\{u_{1},u_{2},\ldots,u_{N}\}\), and the number of UFBSs Y
2:\(\epsilon=10^{-6}\)
3:\(t=0\)
4: Initialize Y centroid points \(C^{t}=\left\{p_{1}^{c},p_{2}^{c},...,p_{Y}^{c}\right\}\subseteq U\), randomly
5:repeat
6:\(S_{k}=\emptyset,\forall_{k}=1,2,\cdots,Y\)
7:for\(i\gets 1\) to \(N\)do
8:\(k^{*}=\operatorname*{arg\,min}_{k=1\ldots Y}\|u_{i}-C_{k}^{t}\|\)
9:\(S_{k^{*}}=S_{k^{*}}\cup\{u_{i}\}\)
10:endfor
11:for\(k\gets 1\) to Y do
12:\(C_{k}^{t}=\frac{1}{|S_{k}|}\sum_{u_{l}\in S_{k}}u_{l}\).
13:endfor
14:\(t=t+1\)
15:until\(C^{t}-C^{t-1}\leq\epsilon\)
16:output: A set of centroid points that the Y UFBSs will be deployed \(C^{t}\).
```  
**Algorithm 1** 2D UFBS placement process through the k-means algorithm  
For simplicity, it is assumed that the number of UFBS \(Y=1\). However, as can be shown in Algorithm 1, the k-means algorithm can be straightforwardly applied to scenarios with \(Y>1\). Hence, in our case, the centroid \(p_{1}^{c}\) is given by the following three steps:  
**Step 1:**: Determine the coordinate \(Y_{u}\) of the UFBS as follows: \(Y_{u}=\frac{\sum_{i=1}^{N}y_{i}^{u}}{N}\)
**Step 2:**: Determine the coordinate \(X_{u}\) of the UFBS as follows: \(X_{u}=\frac{\sum_{i=1}^{N}x_{i}^{u}}{N}\)
**Step 3:**: Configure the point \(p_{1}^{c}\) that the UFBS should be placed as follows: \(p_{1}^{c}=(X_{u},Y_{u},h)\), where \(h\) is the initial height of the UFBS before the 3D UFBS placement procedure.  
Finally, it is essential to acknowledge that the choice of the optimal number of clusters for a clustering problem is not straightforward and may be influenced by a range of factors, including the specific requirements and objectives of the analysis, as well as the inherent properties of the data. Within the context of our system model, the user locations are randomly distributed within a circular region of interest, forming a single cluster. This characteristic of the data renders the choice of Y equal to 1 in k-means clustering a sensible and appropriate decision, as it adequately captures the underlying structure of the data. The resulting cluster is representative of the overall distribution of users and adequately reflects the inherent properties of the dataset. In this particular scenario, using a single cluster is sufficient to accurately and effectively represent the nature of the user distribution and therefore is a suitable approach to analyze the data [32].