**2. Problem Formulation**

As mentioned before, the EMS is the information system responsible for the economical optimization and the steady operation of the MG. Its main function at a single MG is to ensure the dynamic economic dispatch (DED) between generators of the system. The DED problem consists of allocating the total demand among generating units so that the production cost is minimized [27]. However, it is important to stress that the DED in MG context is more straightforward than the DED in a conventional power system. Indeed, the MG has much fewer units than the whole system. Also, the DED in MG does not have a wide variety of conventional fuel generators and at the same time, tries to integrate more renewable energy RE resources. The hypotheses considered in this work are as follows:


The objective function of the DED optimization problem is defined as:

$$\min \sum\_{t=1}^{m} \left( F\_t^{pv} + F\_t^w + F\_t^{ES-} + F\_t^{G-} - F\_t^{G+} - F\_t^{ES+} - F\_t^l - F\_t^l \right) \cdot \Delta t \tag{1}$$

*m* represents the optimization period. *F pv <sup>t</sup>* and *<sup>F</sup><sup>w</sup> <sup>t</sup>* represent the cost function of the PV panels and the wind turbine respectively; *<sup>F</sup>ES*<sup>−</sup> *<sup>t</sup>* and *<sup>F</sup>ES*<sup>+</sup> *<sup>t</sup>* represent the cost function of the battery storage in discharging and charging mode respectively; *FG*<sup>−</sup> *<sup>t</sup>* and *<sup>F</sup>G*<sup>+</sup> *<sup>t</sup>* represent the cost function of the power imported from the grid and exported to the grid respectively; *F<sup>l</sup> <sup>t</sup>* and *Fcl <sup>t</sup>* represent the cost function of the of non-controllable loads and controllable load of the EWH respectively. The generation cost function of each unit is determined by:

$$F\_t^{pv} = \sum\_{k=1}^{n\_{pv}} \pi\_t^{k,pv} P\_t^{k,pv} \tag{2}$$

$$F\_t^w = \sum\_{k=1}^{n\_{tr}} \pi\_t^{k,w} P\_t^{k,w} \tag{3}$$

$$F\_t^{ES-} = \sum\_{k=1}^{n\_{ES}} \pi\_t^{k,ES-} \cdot (1 - \theta\_{ES}) \cdot P\_t^{k,ES-} \tag{4}$$

$$F\_t^{ES+} = \sum\_{k=1}^{n\_{ES}} \pi\_t^{k.ES+} \cdot \theta\_{ES} \cdot P\_t^{k.ES+} \tag{5}$$

$$\boldsymbol{F}\_t^{\rm G-} = \boldsymbol{\pi}\_t^{\rm G-} \cdot (1 - \theta\_{\rm G}) \cdot \boldsymbol{P}\_t^{\rm G-} \tag{6}$$

$$\, \, \, F\_t^{G+} = \pi\_t^{G+} \cdot \theta\_G \cdot P\_t^{G+} \tag{7}$$

$$F\_t^l = \sum\_{k=1}^{n\_l} \pi\_t^{kJ} P\_t^{kJ} \tag{8}$$

$$F\_t^{cl} = \sum\_{k=1}^{n\_{cl}} \pi\_t^{kcl} P\_t^{kcl} \tag{9}$$

where π*k*,*pv <sup>t</sup>* , <sup>π</sup>*k*,*<sup>w</sup> <sup>t</sup>* depict the offer prices by the *k*th PV panel and the wind turbine respectively; *npv*, *nw*, *nES* indicate the number of PV panel, wind turbines and ES installed in the MG; π*k*,*ES*<sup>−</sup> *<sup>t</sup>* , <sup>π</sup>*k*,*ES*<sup>+</sup> *t* represent the electricity cost of the *k*th storage unit during discharging and charging mode respectively; θ*ES* is a binary number equal to 1 if the battery is charging and 0 otherwise; π*G*<sup>−</sup> *<sup>t</sup>* , <sup>π</sup>*G*<sup>+</sup> *<sup>t</sup>* depict the power price of the power imported from the grid and exported to the grid respectively; θ*<sup>G</sup>* is a binary number equal to 1 if the MG is exporting to the grid and 0 otherwise; π*k*,*<sup>l</sup> <sup>t</sup>* , <sup>π</sup>*k*,*cl <sup>t</sup>* represent the power cost of the of non-controllable loads and controllable load of the EWH respectively. *nl*, *ncl* indicate the number of the of non-controllable loads and controllable load; *Pk*,*pv <sup>t</sup>* is the power generated by the *k*th PV panel during time period *t*; *Pk*,*<sup>w</sup> <sup>t</sup>* is the power generated by the *k*th wind turbine during time period *t*; *Pk*,*ES*<sup>−</sup> *<sup>t</sup>* , *Pk*,*ES*<sup>+</sup> *<sup>t</sup>* are the power generated and consumed of the *k*th storage unit during discharging and charging mode respectively; *Pk*,*<sup>l</sup> <sup>t</sup>* and *Pk*,*cl <sup>t</sup>* are the power generated and consumed by non-controllable loads and controllable load of the EWH respectively.

The objective function must be minimized subject to the following constraints:

*Energies* **2019**, *12*, 3004

• Power balance

$$\begin{array}{ll} \sum\_{k=1}^{n\_{\mathcal{V}}} P\_{t}^{k, \mathcal{V}} + \sum\_{k=1}^{n\_{\mathcal{U}}} P\_{t}^{k, \mathcal{U}} + \sum\_{k=1}^{n\_{\mathcal{ES}}} \theta\_{k, \mathcal{ES}} \cdot P\_{t}^{k, \mathcal{ES}-} + \theta\_{G} \cdot P\_{t}^{G-} \\ \sum\_{\begin{subarray}{c} n\_{\mathcal{E}} \\ k=1 \\ k=1 \\ k=1 \end{subarray}}^{n\_{\mathcal{E}}} \left( 1 - \theta\_{k, \mathcal{ES}} \right) \cdot P\_{t}^{k, \mathcal{ES}+} + \left. \left( 1 - \theta\_{G} \right) \cdot P\_{t}^{G+} + \sum\_{k=1}^{n\_{\mathcal{I}}} P\_{t}^{k, \mathcal{I}} + \sum\_{k=1}^{n\_{\mathcal{U}}} \left( 1 - \theta\_{k, \mathcal{E}} \right) \cdot P\_{t}^{k, \mathcal{E}+} \\ - \sum\_{k=1}^{n\_{\mathcal{E}}} \theta\_{k, \mathcal{E}} \cdot P\_{t}^{k, \mathcal{E}-} \\ \end{array} \tag{10}$$

• The renewable energy resources

$$0 \le \sum\_{k=1}^{n\_{pv}} P\_t^{k,pv} \le P\_t^{\max,pv} \tag{11}$$

$$0 \le \sum\_{k=1}^{n\_{\text{IF}}} P\_t^{k,w} \le P\_t^{\text{max},w} \tag{12}$$

where *Pmax*,*pv <sup>t</sup>* , *<sup>P</sup>max*,*<sup>w</sup> <sup>t</sup>* are the maximum available power from PV panels and wind generators during the period *t*.

• ES unit [28]:

Maximum discharge limit:

$$
\theta\_{ES} \cdot P\_t^{ES-} \le P\_{max}^{ES-} \cdot P\_t^{ES-} \ge 0 \tag{13}
$$

Maximum charge limit:

$$(1 - \theta\_{ES}) \cdot P\_t^{ES+} \le P\_{\text{max}}^{ES+} \cdot P\_t^{ES+} \ge 0 \tag{14}$$

When the battery is in discharging mode θ*ES* = 1, the power discharged cannot go beyond the maximum discharging power of the battery *PES*<sup>−</sup> *max* ; similarly, the power charging the battery cannot go beyond the maximum power that could be consumed by the battery *PES*<sup>+</sup> *max* .

Maximum discharge limit considering the total stored energy:

$$(\theta\_{ES} \cdot P\_t^{ES-} \cdot \Delta t) \le E\_{t-1}^{ES} \tag{15}$$

• Maximum charge limit considering the total stored energy:

$$((1 - \theta\_{ES}) \cdot P\_t^{ES+} \cdot \Delta t) + |E\_{t-1}^{ES} \le E\_{\text{max}}^{ES} \tag{16}$$

• Energy balance in ESS:

$$E\_t^{ES} = E\_{t-1}^{ES} + (P\_t^{ES+} - P\_t^{ES-}) \cdot \Delta t \tag{17}$$

The inequality (16) formulates the energy constraint for the battery in a way that the discharged should not exceed the available power. Similarly, the energy charging the battery should not exceed the upper battery limit. During the battery operation, the available energy *EES <sup>t</sup>* should always be between the upper and lower limits *EES min*, *EES max* respectively as showed in Equation (18) [29].

$$E\_{\rm min}^{ES} \le E\_t^{ES} \le E\_{\rm max}^{ES} \tag{18}$$

• State of charge of the battery:

$$\text{SOC}\_{t} = \frac{E\_{t}^{ES}}{E\_{tot}^{ES}} \tag{19}$$

• The interconnection with the grid [30]:

$$\mathbb{P}(1-\theta\_G)\cdot P\_t^{G+} \le P\_{\text{max}}^{G+} \cdot P\_t^{G+} \ge 0 \tag{20}$$

$$
\theta\_G \colon P\_t^{G-} \le P\_{\text{max}}^{G-} \colon P\_t^{G-} \ge 0 \tag{21}
$$

*PG*<sup>−</sup> *max* is the maximum power that could be injected in the grid; *PG*<sup>+</sup> *max* is the maximum power that could be imported from the grid.

#### **3. Proposed Algorithm for the Real-Time Optimization**

Among classical techniques to solve the DED are the dynamic programming [31,32] or the Lagrangian relaxation [4,33,34]. However, most deterministic optimization techniques are not suited for real-time optimization since they need more time to reach the optimum. Heuristic techniques, on the other hand, may reach a sub-optimum solution, but their computational time is very competitive, which make them adequate for real-time application. Much progress was made in heuristic techniques that have proven their efficiency as competitive optimization methods in power system [3,5,7,35–37]. The artificial immune algorithm is inspired from the biological process of immune cells defense mechanism. The search process of this optimization approach is fast and robust, which, not like the particle swarm and the genetic algorithm, search for potential solutions in the overall shape space and don't fall into the trap of sub-optimality convergence [38]. This work proposes the T-Cell algorithm as a variant of immune system algorithm to solve the DED in real time MG operation.

The T-Cell algorithm is inspired by the mediated immune cells in the human body. These cells are called lymphocytes and develop in the Thymus as group of white blood cells [14]. They play a central role in cell-mediated immunity. They have receptors called T-Cell Receptors (TCR) that interact with proteins in their environment. Through this interaction, the T-Cells suffer two stages to get activated: proliferation then differentiation. In the first process, T-Cells proliferate by generating other clones of themselves; then, in the second process, each clone cell acquires new propriety; this is the so-called activation process [15]. This algorithm implements this process:

Each cell has nine characteristic values:

• *Pt* represents the decision variables of the dispatch problem;

$$P\_t = \begin{bmatrix} P\_t^{1, \text{pv}} & \dots & P\_t^{\text{pv}, \text{pv}} \\ P\_t^{1, \text{pv}} & \dots & P\_t^{\text{pv}, \text{pv}} \\ P\_t^{1, ES-} & \dots & P\_t^{\text{pv}, \dots, ES-} \\ P\_t^{1, ES+} & \dots & P\_t^{\text{pv}, ES+} \\ P\_t^{G-} \\ P\_t^{G+} \\ P\_t^{1, cl-} & \dots & P\_t^{\text{vl}, cl-} \end{bmatrix} \tag{22}$$


The required parameters for the T-Cell algorithm are: Number of objective function evaluations, population size and probability of the redistribution operators. The appropriate values of each parameter were chosen from the analysis result of this study [15].

Figure 1 describes the T-Cell algorithm execution steps.

**Figure 1.** T-Cell algorithm process.
