## 4 Problem Formulation
### Algorithm for Determining Optimal Settings for APF  
The flowchart presented in Figure 2 illustrates the algorithm for determining the optimal APF settings in a test network. The algorithm's execution consists of the following steps: load flow calculation, harmonic flow calculation, determination of the line (bus), APF selection, determining nominal filtering current, and optimization process. Figure 2: Flowchart of the algorithm for determining optimal settings of APF. The load flow calculation is carried out to determine the network's state, analyzing the status of the entire test network. Based on the results obtained from the load flow calculation regarding the test network state, the algorithm decides whether integrating the APF into the test network is possible. Suppose there is no sufficient capacity for the APF integration; in that case, the process of determining the setting of APF is canceled, and the algorithm outputs a message indicating that adding the APF is not possible. This approach limits the maximum number of APFs connected to the test network in terms of its technical limits. Suppose it is possible to integrate APFs into the test network; i.e., in that case, if there is enough space in the network concerning loads and generation, the algorithm moves to the harmonic calculation of power flows as the next step. Harmonic flow calculation is performed after the load flow status of the entire test network. This calculation comprehensively analyzes the whole harmonic spectrum (from the 1 st to the 40 th order) to determine harmonic content across all lines and cables within the test network. Based on the results obtained, the analysis defines the relative contributions of individual harmonics. Determination of the line (bus) for APF placement is the next step of the algorithm. Analyzing the results of the harmonic flow calculation identifies the line or cable with the highest share of the third harmonic. The position of the highest value of the third harmonic is set for the initial point for placing the APF. The negative impact of the triplen harmonics in three-phase systems with neutral conductors is the main reason for setting this condition for this step [49]. APF, as a current source, cannot be connected directly to the line; it is necessary to identify the node where the APF should be added. From the point of view of the substation, the algorithm will define the node (bus) located at the end of the line based on the specified physical limitation. The reason for choosing the node at the end of the line is due to the principle of operation of APF; i.e., APF, as a current source, generates currents that cancel the injected harmonic components of nonlinear loads coming from the network, and it is necessary to install it at the end of the lines. APF selection is performed after the algorithm determines the location (bus) to which the APF is connected. Based on the obtained technical capabilities of the test network, the algorithm determines the maximum current that the APF can generate provided that the physical constraints of the network are met. The obtained value represents the maximum filtering current and enables the algorithm to define the nominal filter current from the base of commercially available devices. The APF model used in the algorithm, as well as commercially available devices, supports the generation of different currents per phase with corresponding phase angles. Also, the APF model is configured to allow the generation of harmonic currents per phase with a specific spectrum based on each harmonic current's relative amplitude and angle. The nominal filtering current is selected from the database of commercially available devices by selecting the device with the nominal current closest to but not exceeding the calculated maximum current. Most commercially available APFs have the physical constraint that limits them to generating no more than 15 higher-frequency harmonic currents simultaneously.