# A Framework for the Joint Placement of Edge Service Infrastructure and User Plane Functions for 5G
## 4 Framework Proposal
### UPF Placement Stages
#### 4.2.2 Near-Optimal UPF Placement
This kind of area is formed by the unassigned access nodes near the candidate (\(N_{nr_{c}}[n_{c}]\)) that satisfy the latency requirement (lines: 7-8). At this point, Algorithm 2 is executed, which is in charge of setting the UPF service area. Its first step is to ensure that restriction (11) of the problem formulation is satisfied (lines: 1-3). To achieve this,the algorithm checks whether the candidate is a main UPF co-located with an access node (line: 1). If this is the case, the (R)AN node is assigned to the service area and the UPF available capacity (\(C_{u_{c}}\)), the unassigned access nodes near the candidate (\(N_{nr_{c}}[n_{c}]\)) and the UPF service area (\(S_{sa_{c}}\)) are updated (lines: 2-3). Afterwards, the unassigned (R)AN nodes near the candidate (\(N_{nr_{c}}[n_{c}]\)) are sorted by their proximity to the candidate (line: 4) and an assignment process takes place (lines: 5-21). This process is repeated while the UPF candidate has available capacity and there are unassigned access nodes near it. ```
1 if\(n_{c}\) is a main UPF and\(s\) co-located with an access nodethen
2 Assign the access node to \(n_{c}\)
3 Update \(S_{sa_{c}}\), \(N_{nr}[n_{c}]\), \(C_{u_{c}}\)
4 Sort unassigned access nodes (\(N_{nr_{c}}[n_{c}]\)) by proximity to \(n_{c}\)
5 while\(N_{nr_{c}}[n_{c}]\) is no empty and\(C_{u_{c}}<C_{u_{max}}\)do
6 Search for critic access nodes (\(N_{r_{c}}\))
7 ifthere are critic access nodesthen
8 Select a critic access node from \(N_{r_{c}}\)
9 else
10 if\(n_{c}\) is a main UPF and\(n\)=1 then
11 Select the access node in \(N_{nr_{c}}[n_{c}]\) with the highest frequency of handover respect to the candidate service area
12 else
13 Select the access node in \(N_{nr_{c}}[n_{c}]\) that is closest to \(n_{c}\)
14 ifthe selected access node can be served by\(n_{c}\)then
15 Search for affected access nodes
16 ifthere are not affected access nodesthen
17 Assign the selected access node to \(n_{c}\) service area
18 Update \(S_{sa_{c}}\), \(C_{u_{c}}\)
19 elseifthe selected access node is a critic access nodethen
20 break
21 Update \(N_{nr_{c}}[n_{c}]\)
```  
**Algorithm 2**FormingServiceArea  
For the assignment, those access nodes that have the selected \(n_{c}\) as unique candidate, known as critic access nodes (\(n_{r_{c}}\)), are prioritized (lines: 7-8). If there are no critic access nodes the selection of an access node for its assignment to the candidate is made according to the UPF type and mobility requirements (lines: 9-13). Namely, if the service area belongs to a main UPF with mobility requirements (m = 1), the unassigned access node with the highest frequency of handover, with respect to the access nodes in the UPF service area, is selected (lines: 10-11). Otherwise, the chosen access node is the closest one to the candidate (lines: 10-13). Once an access node is selected, it is necessary to check that it can be served by the UPF (line: 14). If the UPF has enough available capacity to serve the selected access node, the next step is to verify that no access node is affected by the assignment (line: 15). An assignment may affect other access nodes when their corresponding main UPF cannot be placed without violating constraint (11). If a critic access node cannot be served by the available capacity in the candidate, an error indicator is activated and Algorithm 2 is interrupted (lines 20). Otherwise, the set \(N_{nr_{c}}[n_{c}]\) is updated by removing the selected access node (line: 21), regardless of whether it was assigned or not. After Algorithm 2 is executed, the next step in Algorithm 1 is to verify whether the candidate service area was successfully created. If the execution of Algorithm 2 was interrupted due to the existence of an unassigned critic access node, a sub-process to find a candidate for the critic access node is launched (line: 10).