**3. The Implementation of the Optimal Reactive Compensation for Loss Minimization Problem**

The five metaheuristic algorithms presented in the previous chapter were run in an implementation of the Optimal Capacitor Banks Allocation (OCBA) problem for active energy loss minimization. The approach used in this paper is stated as follows: Find the optimal buses in an EDN where capacitor banks (CB) should be installed and the amount of reactive load compensation in each bus, with the objective of operating the EDN with minimal active power and energy losses for the interval of a typical day. For an EDN with *NN* buses (nodes) and *NB* branches, the mathematical expression of the objective function of the OCBA problem was defined as:

$$
\Delta P[\%] = \sum\_{h=1}^{24} \Delta P\_h / \left( \sum\_{h=1}^{24} \sum\_{h:s=1}^{NN} P\_{b,h} + \sum\_{h=1}^{24} \Delta P\_h \right) \* 100 = \min \tag{19}
$$

The mathematical model of the fitness function considered the following constraints: Cr1. The available CB stock cannot be exceeded:

$$\sum\_{b \le s = 1}^{NN} NCB\_{\text{bus}} \le stock\_{CB} \tag{20}$$

Cr2. The compensation level in each bus cannot exceed the reactive bus load (avoid reversed reactive power flows):

$$N\!CB\_{bus} \cdot \eta\_{\subseteq \mathcal{B}} \le Q\_{bus} \quad \text{bus} = 1 \ldots \text{NN} \tag{21}$$

Cr3. Branch current flows after compensation cannot exceed the branch rated current:

$$I\_{br} \mathrel{\mathop{:}} = I\_{\text{max},br}, \quad br = 1 \dots NB \tag{22}$$

Cr4. Bus voltages after compensation cannot exceed the maximum allowed value:

$$\text{lL}\_{\text{bus}} << \text{lL}\_{\text{max}, \text{bus}} \quad \text{bus} = 1 \dots \text{NN} \tag{23}$$

In Equations (19) to (22), Δ*P*[%] is the percent active power loss in the EDN over 24 h, Δ*Ph* is the active power loss in the EDN at hour *h*, *NCBbus* is the number of CBs installed in a generic bus; *qCB* is the reactive power rating of a CB, *Qbus* is the reactive load of a generic bus, s*tockCB* is the CB stock; *Ibr* is the current flow on a generic branch *br*, *Imax,br* is the rated current of branch *br*, *Ubus* is the voltage of a generic bus after compensation, and *Umax,bus* is the maximum bus voltage allowed in a generic bus.

All the algorithms tested in the case study used the same solution encoding for their population members. They were generated as vectors of the type described by Equation (1), with integer numbers and length equal to the number of buses in which compensation was possible in the network. The significance of the value of a generic element represented the number of CBs placed in the bus to which it was designated. All the algorithms started in the first iteration with the same population, generated randomly but considering constraint Cr2 of maximum allowed number of CBs in each bus and Cr1, the maximum CB stock (Figure 9).

**Figure 9.** The encoding of the solutions used by the OCBA problem.

The fitness of the optimal solutions was assessed in all the algorithms using the objective function (19), which also considered the constraints from Equations (20) to (23). The methodology employed for calculating the fitness of each solution is described in Figure 10.

**Figure 10.** Fitness computation and solution validation for the OCBA problem.

By applying any of the equations (2) to (18) or by genetic crossover and mutation, the changes undergone by population members can result in their invalidation because of


Thus, every newly generated population member, for each algorithm, must pass through a validation procedure before being allowed in the population created for the next iteration.

If constraint Cr1 is not satisfied, the solution is always discarded as unfeasible. If the constraint Cr2 is not fulfilled, the solution is modified by setting the unfeasible values to the nearest allowed value, using for each element *xj* from Figures 1 and 9 the following correction:

$$\mathbf{x}\_{j} = \begin{cases} \begin{array}{c} 0, \quad \text{if } \mathbf{x}\_{j} = \mathbf{0} \\ \text{NCB}\_{j}, \quad \text{otherwise} \end{array} \tag{24}$$

For each population member, the active power losses used in equation (19) are computed with the Newton–Raphson load flow (LF) algorithm, which is slower, but generally considered more accurate than the branch-and-bound methods preferred in the analysis of distribution networks. The LF algorithm also provides the results required for checking the constraints Cr3 and Cr4, bus voltage and branch current flow limits. Prior to computing the LF, the compensation solution is simulated subtracting from the bus reactive loads the CB injection for each compensated bus, according to the population member/solution being tested.
