*4.5. Supervisory and Optimization Algorithms*

The management strategy for the EV/PV microgrid (Figure 4) is represented by the flowchart in Figure 5, wherein the acquisition and supervision modules are highlighted.

**Figure 5.** Management algorithm of the microgrid.

The data are acquired by EMS at each time interval, and the power *PEVv* for each vehicle *v* that is connected to the charging stations is determined. In the first step, the measurement variables are acquired, covering all parking lots (*CarSlot*(*v*)). When there is no vehicle connected, the algorithm follows right through the end and distributes the PV available power to loads of the microgrid. Without the EV connected, the EMS may request power from the grid or deliver PV surplus to the grid, as necessary.

When a vehicle is connected, then *NEV* is updated, and EMS determines the connection vector *EVconn* based on Equation (4). At the end of the acquisition process, the car data are saved using the format *EV*.{*struct*}, as defined previously.

After the acquisition process, the supervisory stage is initiated, which is responsible for defining the actions to be taken following the priority order set. This module analyzes the contracted demand limit and takes corrective actions to keep the demand at the appropriate level. The variables Δ*Plim*,*U*, Δ*Plim*,*F*, and Δ*Plim*,*<sup>E</sup>* are related to the difference between the contracted demand limit (*Plim*) and the cumulative sum of each mode. If there are vehicles in ECO or V2G modes, the optimization module is requested.

Both functions for *PU*,*EVU* and *PF*,*EVF* computation are presented in Algorithm 1, which illustrates the processing logic of the ULTRA and FAST modes.

## **Algorithm 1:** ULTRA and FAST modes.

```
1 if Mode == ULTRA then
2 P ← PU,EVU ;
3 end
4 if Mode == FAST then
5 P ← PF,EVF ;
6 end
7 SOCcalc [1] ← SOC(tm);
8 for n = 1 : (tf − tm) − 1 do
9 Calculate P;
10 Calculate SOCcalc [n + 1] ← SOCcalc (P, SOCcalc [n]);
11 if SOCcalc [n + 1]  SOCf then
12 P ← 0;
13 end
14 end
15 if Mode==ULTRA then
16 Calculate PU ← ∑NU
                    EVU =1 P;
17 if NV2G == 0 then
18 if Pnet[tm] + PU[tm]  Plim then
19 Calculate PU(tm);
20 PU,NU [tm] ← PU(tm);
21 end
22 end
23 end
24 if Mode==FAST then
25 Calculate PF ← ∑NF
                    EVF=1 P;
26 if NV2G == 0 then
27 if Pnet[tm] + PU[tm] + PF[tm]  Plim then
28 Calculate PF(tm);
29 PF,NF [tm] ← PF(tm);
30 end
31 end
32 end
```
First, the value of the current SOC is saved, and *SOCcalc* is calculated; *SOCcalc* has a variable size depending on each EV connection time. Then, the size of P from (*tf* − *tm*), as well as *SOCcalc* is established, defining the charging power profile. Assuming that *tm* represents the interval [*ti*, *tf* ] and that *ti* is an updated value in each step, it is noted that P has also a varying size, which decreases each time step. The charging power profile P is recalculated in each time step such that its data may be used as forecast demand in the optimization method.

After the definition stage of the charging power profile, P is calculated according to (19) or (21), based on the specific mode. The SOC value is updated from (1), and the total demand of each mode *PU* and *PF* is computed. During demand limitation, the variables *PU* and *PF* represent the reduced

charging power dispatched to the vehicles, according to the indexes *f <sup>U</sup>* and *f <sup>F</sup>* indicated in (19) or (21), respectively.

When ECO and V2G modes are requested, Algorithm 2 is used to perform the optimization of the ECO and V2G cost functions.

**Algorithm 2:** ECO and V2G modes.

```
1 if Mode == ECO then
2 J ← JE,EVE ;
3 P ← PE,EVE ;
4 end
5 if Mode == V2G then
6 J ← JV2G,EVV2G ;
7 P ← PV2G,EVV2G ;
8 end
9 for s = 1 : SOCvalues do
10 for k = 1 : Pevvalues do
11 SOCnext ← SOCpossible + ηEV
                                 PevpossibleΔt
                                   EbatE,V2G
                                            ;
12 if (SOCnext -
                    1) ∧ (SOCnext  0) then
13 Calculate J (tf);
14 Pevcost[tf − tm,s] ← J (tf);
15 Pevcontrol [tf − tm,s] ← Pevpossible[k];
16 end
17 end
18 end
19 for n = (tf − tm) − 1 : −1:2 do
20 for s = 1 : SOCvalues do
21 for k = 1 : Pevvalues do
22 Calculate SOCnext;
23 Calculate I(Pevcost, Pevcontrol, SOCpossible);
24 Calculate J (n, I);
25 if (SOCnext  40%) ∨ (Pevpossible  0) then
26 Pevcost[n,s] ← J (n, I);
27 Pevcontrol [n − 1,s] ← Pevpossible[k];
28 end
29 end
30 end
31 end
32 SOCcalc [1] ← SOC(tm);
33 for n = 1 : (tf − tm) − 1 do
34 Calculate I(Pevcontrol, SOCpossible);
35 Calculate P(n, I);
36 Calculate SOCcalc [n + 1] ← SOCcalc (P, SOCcalc [n]);
37 end
38 if Mode==ECO then
39 Calculate PE ← ∑NE
                    EVE=1 P;
40 if Pnet[tm] + PU[tm] + PF[tm] + PE[tm]  Plim then
41 Calculate PE(tm);
42 PE,NE [tm] ← PE(tm);
43 end
44 end
45 if Mode==V2G then
46 Calculate PV2G ← ∑NV2G
                      EVV2G=1 P;
47 if Pnet[tm] + PU[tm] + PF[tm]  Plim then
48 Calculate PV2G(tm);
49 PV2G,NV2G [tm] ← PV2G(tm);
50 Recalculate PU,NU [tm];
51 Recalculate PF,NF [tm];
52 end
53 end
```
The optimization related variables are quantized considering the value range and the level number of SOC (*SOCvalues*) and charging power (*Pevvalues*). The higher is the number of quantization levels, the more accurate will be the control, but more processing will be required. The range of allowed values is represented by *SOCpossible* and *Pevpossible*.

In Algorithm 2, initially, all operation possibilities are examined for the last sample time (*tf*) of the EV charging horizon. The costs of the optimal powers (*J*(*P*∗ *EV*(*tf*))) are saved in *Pevcost* for each state *SOCpossible*. The power that implies the lower cost of J is saved in matrix *Pevcontrol*, corresponding to *P*∗ *EV*(*tf*). Then, the regression analysis is performed by checking each value of *SOCpossible* and *Pevpossible* for all sample times in the interval [*tm*, *tf* [. From *SOCcalc* calculation, values not quantized in *SOCpossible* are usually obtained; therefore, the cost is linearly interpolated (I) according to the obtained SOC value. The control law is also interpolated. After, the cost matrix *Pevcost* is assembled based on lower calculated cost (*J*(*P*∗ *EV*(*tm* − *tf*))). The optimal power matrix *Pevcontrol* receives the control law, which provides the lower cost (*P*∗ *EV*(*tm* − *tf*)).

The progressive stage (Line 32) follows the same strategy adopted for ULTRA and FAST; however, the matrix *Pevcontrol* calculated in the previous stage is used, which contains the optimal power profile for the determined initial SOC. This optimization process is repeated throughout all the EV connection period, including the charging profiles to the *PE* and *PV*<sup>2</sup>*<sup>G</sup>* vectors.

The demand limitation is verified for both ECO and V2G modes, and when V2G mode is requested, the charging dispatch for ULTRA (*PU*) and FAST (*PF*) modes is recalculated.
