Hierarchical distributed framework for optimal dynamic load management of electric vehicles with vehicle-to-grid technology

The tendency towards carbon dioxide reduction greatly stimulates the popularity of electric vehicles against conventional vehicles. However, electric vehicle chargers represent a huge electric burden, which affects the performance and stability of the grid. Various optimization methodologies have been proposed in literature to enhance the performance of the distribution grids. However, existing techniques handle the raised issues from individual perspectives and/or with limited scopes. Therefore, this paper aims to develop a distributed controller-based coordination scheme in both medium and low voltage networks to handle the electric vehicles’ charging impact on the power grid. The scope of this work covers improving the network voltage profile, reducing the total active and reactive power, reducing the load fluctuations and total charging cost, while taking into consideration the random arrivals/departures of electric vehicles and the vehicle owners’ preferred charging time zones with vehicle-to-grid technology. Simulations are carried out to prove the successfulness of the proposed method in improving the performance of IEEE 31-bus 23 kV system with several 415 V residential feeders. Additionally, the proposed method is validated using Controller Hardware-in-the-Loop. The results show that the proposed method can significantly reduce the issues that appear in the electric power grid during charging with minor changes in the existing grid. The results prove the successful implementation of different types of charging, namely, ultra-fast, fast, moderate, normal and vehicle-to-grid charging with minimum charging cost to enhance the owner’s satisfaction level.


I. INTRODUCTION
The evolution of EV batteries with sizable capacities and the development of renewable energy resources promotes the use of EVs over internal combustion vehicles. Moreover, EVs improve urban air quality [1], have 43 percent better fuel economy compared to conventional vehicles, and act as an enabler for renewable power generation by providing storage through the V2G technology [2,3]. Therefore, car companies have begun to invest in the EV market, and distribution grids have evolved through communications infrastructures and smart meters/sensors to support EV charging [4].
Enormous demand for EV charging causes a challenging demand side management problem with stochastic attitudes [5]. Furthermore, it induces detrimental effects on the distribution grids' performance. For instance, frequency oscillations, unacceptable voltage drops, and an increase in total power losses may occur. Therefore, various optimization strategies have been proposed in literature to restrict these impacts. One of the optimization objectives to be achieved during EV charging could be voltage regulation in the distribution grid. For instance, the method in [6] regulates the voltage by controlling the reactive power through the grid. However, it required communication among EVs and the distribution system operator (DSO), the total power losses and battery degradation were not considered in that work. On the other hand, the method in [7] utilizes the reactive power and model predictive control to regulate the voltage, and additionally considers battery degradation. However, the other challenges in the distribution grid, such as, total power losses and frequency fluctuations have not been studied. The method in [8] moves the charging time from peak time to other times for voltage regulation, but benefits from V2G technology and total system losses have not been addressed. Alternatively, the optimization objective may aim to minimize the total active power losses in the distribution grid. For instance, the method in [9] focuses on optimal integration of distributed generators using butterfly optimization to minimize the daily active power losses. However, the EV owner preferred charging time zones have not been handled. Another method introduced in [10] adopts a smart load management methodology for EVs and charging stations with losses minimization, but the presence of renewable energy sources with V2G technology has not been covered Another method introduced in [11] minimizes the real power losses over the distribution feeders but further considers the V2G technology and the charging cost. However, the random arrivals/departures of EVs were not considered.
One more issue that may be addressed during EV charging is the frequency oscillations. This issue is addressed in [12], which provides frequency regulation for the power system by utilizing the V2G technology with no concerns for the total losses in the system and the charging cost. However, the method in [13] regulates the frequency deviations while reducing the charging cost, but, the cost related to the distribution power losses was not considered. The method in [14] and [15] use quadratic programming and bidding constraints, respectively, to reduce the peak load and demand variability in the distribution grids. However, charging stations and battery degradation have not been addressed in these methods.
A different direction of research focused on the minimization of the energy cost. For instance, the methods in [16] and [17] use model predictive control and stochastic mixed integer linear programming approaches, respectively, to find the optimal EV charging strategies for maximizing the aggregator profits. However, these studies have been simulated with no consideration for the distribution system constraints including the peak load and voltage drops. From another perspective, the objective may be to minimize the total charging cost for the owners. The method in [18] uses sequential quadratic programming and genetic algorithms to minimize the power energy cost and load fluctuations. However, this method did not handle the total losses and voltage drops in the distribution grids. In [19], ant colony optimization is used to reduce the waiting time and charging cost without taking into consideration the distribution grid performance.
One key observation from the reviews presented in [20][21][22], is that most of EV charging strategies focuses on one or two of the following power grid issues (total power losses, excessive voltage drops, load/ frequency fluctuations or peak shaving), regardless of the other issues. For instance, authors of [23] tried to minimize the active power losses and voltage regulation specially in the MV networks, in [24] only the peak shaving and valley filling were addressed, while in [25] only the reduction of the energy cost was considered. Thus, it is quite clear that the main shortcoming in the previous methods is that no technique has been designed to address all these power grid issues at medium voltage (MV) and low voltage (LV) levels, taking into consideration the owners' satisfaction factor, battery degradation, charging cost, computational time, and the stochastic behavior of the system. The stochastic behavior stems from: (i) the random arrivals/departures of EVs, (ii) the owner's preferred charging time zones, (iii) batteries with a wide range of capacities and ratings considering ultra-fast and fast charging requirements. The previous research handled the EV system's stochastic behavior, but within a narrow range that is limited only to studying the impacts on the power grid. For example, references [7,13,15,[26][27][28] consider only on one or a maximum of 5 different EVs, which is not a realistic case, as the power grid would normally deal with a large number of different EVs with different capacities and charging rates.
This paper proposes a multi-objective hierarchical formulation of the EV charging problem implemented at the LV/MV distribution grids and charging stations. This formulation aims to reduce the undesirable impacts such as unacceptable voltage drops, total power losses and peak loads. These issues have recently appeared due to EV charging [29]. Besides, the proposed formulation considers minimization of the total energy cost for the EV owners in the charging stations. The cost is addressed through the recent pricing strategies, namely time-of-use price, and realtime price. Furthermore, the formulation takes into account the random arrival/departure pattern for the EVs with predetermined preferred charging time zones based on a priority selection scheme. The V2G technology is applied in the proposed method to motivate the use of renewable energy resources. The contributions of the paper are summarized as follows; • Development of a two-level hierarchical controller-based coordination scheme implemented in both MV and LV networks to handle most (not only one) crucial EV charging impacts on the power grid. • Formulating three improved objective functions for the hierarchical controllers, that are solved by the distributed controllers with rapid convergence speed achieving realtime optimum charging decision making. • The optimal scheduling policy for charging and discharging of EVs is designed to handle practical real-life circumstances, such as all the previously stated random and stochastic behaviors, large penetration of EVs with different capacities and charging rates, bi-directional V2G, distribution grid restrictions and battery degradation. • Finally, the optimization algorithms are validated using Hardware-in-the-Loop via the OPAL-RT real-time simulator and a Digital Signal Processor (DSP).
To prove the enhancement in distribution system performance using the proposed methodology, a standard IEEE test system is simulated and practically validated. This standard system consists of 31 MV feeders (23-kV) with several charging stations and integrated 53 LV (415-V) node residential networks populated with EV chargers of various sizes. This paper is organized as follows. Section II covers the optimization problem formulation. The overall test system structure is described in Section III. Section IV presents the algorithmic details. The results are presented in Section V using MATLAB and Hardware-in-the-Loop via the OPAL-RT real time simulator. Section VI concludes the paper.

II. PROBLEM FORMULATION
This section introduces the formulation of the optimization problem associated with the different controllers that can be implemented in the distribution grid. Moreover, it includes the required constraints that balance between the highperformance levels for grids and EV owner's satisfaction levels. Fig. 1 demonstrates the different locations for the controllers in the distribution gird. The objective of each optimization problem will differ according to its location in the distribution grid. For instance, the controller implemented in the MV feeders (high-level control), has an objective that aims to reduce the total power losses, enforce load shaving and voltage regulation through the MV network. On the other hand, the controller located in the LV feeders (low-level control), is responsible for charging the EVs on the residential charging points in a specific period, considering minimum losses and acceptable voltage drops in the LV network. Moreover, minimizing the total energy cost for EV owners or maximizing the aggregator profits are the objectives for the controller installed in large charging stations (low-level control).
On a large scale, these three controllers (MV feeders, LV feeder and charging station controllers) improve the performance of the distribution grids through the integration and coordination among them. Fig. 1 illustrates the coordination between the low-level controllers and the MV feeder controller. This coordination can be described as follows: (i) The low-level controllers calculate the required energy to charge every EV connected to it. For every EV, the low-level controller optimally allocates different power levels in different time slots (based on its optimization objective) to achieve this required energy. The low-level controller sums up the powers delivered to EVs in each time slot and generates the charging profile as in Fig. 2(a). (ii) The MV controller receives this charging profile from each low-level controller. The MV controller runs load flow analysis over the medium voltage buses. If the power exceeds the permissible level for generation or causes an excessive voltage drop in the MV network, then the MV controller sets a limit to the low-level controller and suggests a shift for the portion of power that causes this issue to a different time slot, as shown in Fig. 2(b) by the red portion that is shifted from slot 5 to slot 6. Moreover, the MV feeder controller suggests a shift in portions of power to a different time slot to minimize extra losses over the MV networks, as shown by the green portion in Fig. 2(b), that is shifted from slot 4 to slot 6. (iii) The lowlevel controller receives the modified charging profile limits, and coordinates between the EVs based on the modified profile to achieve better owner satisfaction level. The proposed model handles more than one objective, namely, the active, reactive power losses, load fluctuations, energy cost, and excessive voltage drop. Using one controller to handle all these objectives in LV and MV together is computationally complex. Therefore, a two-level hierarchical controller-based coordination scheme is proposed, which is shown in Fig. 1, to reduce the computational time and maintain system reliability as the failure of any controller will not entirely affect the whole system.

A. Charging stations (low-level control)
The coordination problem of EVs in charging stations is targeted to determine the allocated power for each EV in different time slots, considering the random arrival of EVs, owners selected charging priorities (with extra fees) and grid topology.

1) OBJECTIVE FUNCTION FOR MINIMIZING THE LOAD FLUCTUATIONS AND TOTAL ENERGY COST
Charging stations are commonly integrated with MV buses and aim to minimize the load fluctuations and the total energy cost for EV owners. Sudden load increase causes undesirable frequency oscillations and can be addressed by minimizing the following objective function: where 1 is the first objective function for the station that describes the load variance over the day, is the number of time intervals, is the set of the time intervals, ( ) is the total power delivered to the EVs in the time interval , ( ) describes the daily average load [18]. The daily average load can be calculated as, In addition, the charging station aims to minimize the total energy cost for the EV owners using the following cost function, where is the total cost required to charge EVs, and ( ) is a linear model for the tariff in time interval . This linear model is determined based on the two common pricing strategies, namely, the time of use (TOU) and dynamic real-time price. TOU is designed to encourage customers to use more energy  at off-peak periods. On the other hand, the dynamic price helps to set fair tariffs for different time slots based on the time and load demand and can be expressed as,

LV feeder
where and are the slope and the intercept, respectively, for the linear price model. The TOU pricing can be achieved by assigning different values to over the day. The second objective function for the station can be formulated as, where ,2 describes the total charging cost. Due to the quadratic nature of the problem, the proposed method merges these two objective functions into a single objective function, so that it can be easily addressed using quadratic programming. Quadratic programming rapidly finds the optimal solution that helps significantly during the communication with other controllers. Such a merging can be achieved through the following steps. Equation (1) can be reformulated as, where ( 1 ) is a diagonal matrix with diagonal elements equal to 1 and of size ( × ), t is a vector containing the total power consumed by EVs in each time interval and has a size of ( × 1). t can be written in terms of the decision variables by utilizing the matrix transformation, where is the total number of EVs in the charging station, is the decision variables vector which determines the allocated power for a specific EV in a specific time slot and has a size of ( × 1), and is the transformation matrix, which sums up all allocated EVs power in each time slot with a size of ( × ), as shown in Appendix A. Therefore, (6) can be reformulated as follows, The Hessian matrix for the first objective function can be defined as follows, The second objective function in (5) can be reformulated as follows, where is a vector which consists of the constant terms for the linear model of tariff and has a size of (1 × ) . The Hessian matrix 2 and gradient vector for the second objective function can be defined as follows, Consequently, the final objective function for the station can be described by merging (8) and (10) as follows, This objective function together with the linear constraints described below can be addressed by utilizing the methods of quadratic programming, namely, interior point method, active set, sequential quadratic, or any other relevant method.

2) CHARGING STATION CONSTRAINTS
The optimization problem considers sufficient constraints to achieve the best level of owner satisfaction while abiding by the restrictions dictated by the distribution grid. Moreover, the stations are designed to deal with sizable batteries. Each EV is treated by nominal charging rate used during normal charging periods and maximum charging rate under fast and ultrafast charging conditions. This can be achieved using the following constraint, which describes the charging range that should not be exceeded during charging to protect the battery, where ( ) is the maximum charging rate for EV while − ( ) is considered for the sake of V2G purposes. Furthermore, to prolong the lifetime of EV batteries, the state of charging (SOC) should be limited between the recommended bands settled by the manufacturers [30] as follows, where , is the state of charging at the end of the charging process.
, are the minimum and maximum depth of discharging settled by manufacturers. In lithium-ion batteries currently used in some EVs, optimum DoD typically ranges from 45% to 90%, which is a tradeoff between battery life and sufficient range for the required journeys [10].
is used when the state of the EV is charging, while is used during discharging states for V2G purposes.
One more constraint is that the total charging power for EVs should not exceed the maximum capacity dictated by the grid. This constraint ensures stable and normal operation for the power grid, .
where ( ) is the maximum allowable capacity for charging during period . This dynamic value is obtained from the upper-level controller which ensures stable MV operation and is studied in detail in the next section.
To ensure owner satisfaction and fulfill the journey requirements, the final state of the battery , should reach the required value within the owners' selected charging priorities such that, where , is the state of charging before the charging process, is the nominal capacity of EV , start and end are the owners selected charging periods, and , is the expectation level for 0 value of EV .

B. LV feeder controller (Residential compounds -lowlevel control)
The optimization problem of EVs in residential compounds determines the scheduling of individual residential charging points in the LV distribution system, in such a way that minimizes the LV network power losses, where peak demand shaving is achieved and voltages at all LV nodes are regulated within allowable tolerances. Furthermore, the optimization algorithm considers the residential load variations over a 24-hour period, EV owner selected charging priorities, the grid topology, and the random arrival for the EVs. This subsection describes the problem formulation for the residential compound controller considering the necessary constraints.

1) OBJECTIVE FUNCTION FOR THE RESIDENTIAL COMPOUND
This objective function seeks to minimize the total power losses and to regulate the voltage in low distribution grids and can be described as follows, where is the total number of LV nodes, ( , , +1) , ( , , +1) are the active power and reactive power losses in period , respectively, and are calculated for the cable connecting between nodes and + 1, ( , ) is the voltage at node in period , and ( ( , ) ) is a barrier function to restrict the voltage within permissible range and is defined as follows, where is the minimum voltage limit and set to ±10% ( = .9 = 1.1 ), which is the case in many distribution systems [10]. The previously stated objective function requires accurate information about the power losses and voltage at each LV bus; therefore, a modified Newton-based load flow analysis is implemented to access the necessary information required for the computation of the objective function. Newton-based load flow has a quadratic and rapid convergence sufficient for real-time analysis [31]. The residential controller deals with 53 buses in the power flow analysis step, which represents a computational burden to the controller. Therefore, the proposed model suggests an improved objective function (17) using the logarithmic-barrier method for the voltage constraints to reduce the computational time. The idea behind using the logarithmic-barrier method for computational time reduction has been illustrated in [32].

2) RESIDENTIAL COMPOUNDS CONSTRAINTS
Similarly, the residential compound constraints seek to compromise between the high-performance levels for grids and owner's satisfaction levels. These constraints are similar to other constraints stated in the previous subsection. However, an exception exists due to the limitations of the wiring in residential charging points. According to [10], there are 15 and 20 A outlets (single-phase and three-phase), that can supply a maximum power of 4 and 14.4 kW, respectively. Therefore, a maximum charging rate of 5 kW with unity power factor is considered in the following analysis, which covers the normal residential infrastructure without having to reinforce wiring. Accordingly, the constraint in (13) will be modified as follows,

C. MV controller (high-level control)
The optimization problem for the MV feeders is to decide the amount of the allocated power for each charging station and for each feeder supplying the residential compounds, such that the total power losses in the MV network are minimized, peak load shaving is achieved and voltage magnitudes at MV nodes are regulated. Therefore, this controller continuously receives the energy required from the charging stations and the MV feeders that feed the residential compounds. It then reallocates this energy over the day if the permissible MV drops and the total power losses exceed the permissible limits. This subsection describes the problem formulation for the MV controllers identifying the necessary constraints.

1) OBJECTIVE FUNCTION FOR MV CONTROLLER.
This objective function seeks to minimize the total MV network losses considering the permissible voltage drop tolerances and load shaving, and can be described as follows, where is the total number of MV nodes, and ( ) represents the allocated power for medium feeder at period . The optimum value for ( ) continuously updates the constraint in (15), related to the charging station and for others related to the residential compound controllers. VOLUME XX, 2021 6

2) MV CONTROLLER CONSTRAINTS
The MV controller operation is constrained by the following voltage limits, where and are the MV limits and are set to ±5% which is typical for many medium distribution systems ( = .95 and = 1.05 ).
The controller is constrained by a maximum demand level ( ), which shaves the load if it exceeds the generation levels.
Also, the objective function in (20) requires power flow analysis. The MV and residential compound optimization problems in (17) and (20) can be addressed easily using any of the heuristic optimization algorithms due to nonlinearity of these problems. For instance, a genetic algorithm, particle swarm optimization or pattern search methods can be used.

III. THE OVERALL TEST SYSTEM STRUCTURE
The performance of the previously stated controllers with different objective functions is validated using the IEEE test system described in Fig. 3. The system consists of 23 kV 31 buses with 415 V residential compound feeders. This section describes the grid topology, EVs/charging points specifications and load assumptions required for the analysis.   As shown in Fig. 3, the smart grid distribution test system consists of 23 kV 31 buses with line data described in [33], and is associated with a 415 V residential network. There are also 22 residential compound controllers arranged from feeder 10 to feeder 31, and 4 charging station controllers on feeders 2, 4, 7 and 9. Furthermore, there is a one MV controller allocated at the substation which communicates with the previously stated controllers. The detailed diagram for the 415 V residential network is shown in Fig. 4, where each lowvoltage residential network has 53 nodes representing customer households and populated with EV charging points. These residential networks are powered by 23 kV/415V 300 kVA distribution transformers. These networks are based on real system data of an Australian distribution network with line impedance given in [10].

B. EV specifications
This study involves a wide range of recently manufactured EVs (plug-in hybrid or all-electric vehicles) [34]. Each EV battery has a specific capacity and nominal/maximum charging rates. Validation handles the random arrival, departure and SOC of EVs. The arrival time is modeled as a Gaussian distribution with ( = 6 , = 2 ℎ ). The departure time in the normal operation is modeled as a Gaussian distribution with ( = 6 , = 2 ℎ ); these Gaussian distributions are limited by 4 hr around the mean. However, the departure time may depend on the owner's selected charging priorities, whether the owner prefers ultra-fast (30 minutes), fast charging (1 hour) or moderate charging (2 hours). The initial SOC of EVs is modeled as a Gaussian distribution based on previously stated DoD in (14), namely ( = 50%, = 5%) for the vehicles that will be charged and ( = 85%, = 5%) for the vehicles that will be discharged for V2G purposes. The EV specifications are listed in TABLE A1 in Appendix A.

C. Charging station specifications
Charging stations are located at the MV feeders (2, 4, 7 and 9) and are considered as a proper option for long distance traveling like conventional fuel-based vehicles. Each station has a maximum capacity of 50 vehicles and is supplied by reinforced wiring to support ultra-fast charging (30 minutes) and DC charging. The charging demand in stations commonly increases from 7 a.m. to 10 a.m. for morning journeys and from 3 p.m. to 6 p.m. for evening journeys. The optimization technique for the charging station handles the random arrival/departure and the SOC of EVs.

D. Charging point specifications and residential load
The proposed method assumes a variation in the residential load over the day, which is based on actual data recorded form the distribution transformer in Australia [10] with an average loading of 1.5 kW at 0.95 power factor and the peak value occurs in the evening 6 p.m., as shown in Fig. 5. Moreover, the dynamic real time and TOU prices over the day are set according to the delivered power. The system has 3 different categories for the tariffs, namely, the high tariff zone (red colored), the moderate tariff zone (blue colored) and low tariff zone (green colored) in Fig. 5. Charging points are located on the LV buses or in the householder's garage. The case study handles two levels of penetration (31% and 62%) which cover the scenarios in the near and far future. The penetration level refers to the portion of the LV nodes that has charging points. As previously mentioned, the charging points are fitted with 15 and 20 A outlets (single-phase and three-phase) that can supply approximately 4 and 14.4 kW, respectively [10]. However, as the single-phase is commonly used on the residential scale, the maximum charging power supplied by the charging points is assumed to be 5 kW and that ensures no reinforced wires. The charging points' penetration scenarios in the LV network are listed in TABLE A2 in Appendix A. The study assumes that 3 vehicles out of 32 (in 62% penetration) and 2 out of 16 (in 31% penetration) require fast charging (red colored in TABLE A2) while those that require moderate charging are marked in blue and those that require discharging for V2G purposes are marked in yellow.

E. Controllers' flowcharts and their coordination
This subsection describes the coordination between the controllers aided by flowcharts. Initially, as shown in Fig. 6 (residential controller flowchart), the residential controller gathers data about the EVs connected to it, for instance, (battery capacity), (maximum charging rate), ( ) (state of charge), (arrival time), (departure time), and ( ) , that describes the charging direction V2G or G2V. V2G technology is used to improve the grid performance in peak load hours as EVs feed electricity back to grid and the users earn a high revenue based on the energy supplied to the grid during peak load hours. This is advantageous for both the grid performance and the user due to the dynamic and TOU pricing. Therefore, the users can charge during the off-peak hours and sell stored energy during peak load. Every user is provided with a day ahead hourly load forecast and real-time charging prices, shown in Fig. 5. In the implemented code, the arrival time ( ) controls the preferred discharging time and the arrival time can be controlled by the user. Moreover, the departure time in the algorithms controls the charging rate (as the departure time decreases the charging rates increases to recharge the battery in the required time, therefore, the ultra-fast and fast charging rates are achieved). The residential controller produces an initial charging pattern for every EV, this charging pattern describes the specific power that will charge the EV at each time slot, then the controller solves the optimization problem in (17) (using the pattern search algorithm) which minimizes the total losses in the low voltage network; therefore, it inherently runs a load flow function in the low voltage network based on information about the low voltage line and bus data and the daily load curve for the linear loads connected to the LV nodes. The daily load curve can be formed based on real time date thanks to the recent smart meters used in modern smart grids [35]. If the changes in loads over the same session are significant, the daily load curve can be formed using historical data. The tolerance. Function tolerance is a lower bound on the change in the value of the objective function from one iteration to the next (| ( ) − +1 ( )|< function tolerance (10 −5 )). Time tolerance is calculated as one third of the time slot in the algorithm, for instance, the time tolerance is 10 minutes in 30 minutes time slot. Then the controller sums up the optimum achieved powers for all EVs for each time slot and sends it as an initial charging pattern to the main controller.
Simultaneously, as shown in Fig. 6 (charging station controller flowchart), the charging station controller collects the EVs information then uses interior-point method from the quadratic programming techniques to solve (12) with its constrains, until one of two stopping conditions achieved; function tolerance or time tolerance as in case of residential controller. Also, this controller sums up the optimum achieved powers for each time slot and sends the initial charging pattern to the main controller.
As shown in Fig. 6 (main controller flowchart), the main controller receives the initial charging patterns from the residential and charging station controllers and now it knows the required power profile at each medium voltage node, therefore, it runs a medium voltage load flow for the 24 hours and checks the voltage and power limits. If it is exceeded at any time slot, the main controller movies the portion of power that causes the issue to another time slot in order to keep the voltage level within limit and increases the system stability. The initial charging patterns after modification are called the restricted charging patterns as the residential and station controllers must follow these patterns in charging for grid stability. These restricted patterns are used as initial

Residential Controller Flowchart
Send the restricted pattern and the suggested pattern to the residential and station controllers Data flow populations for the minimization problem in (20) which reduces the power losses in the medium voltage network while satisfying the constrains. This process iterates until one of two stopping criteria (function tolerance or time tolerance) are achieved. The charging patterns that conserve the constrains and reduce the losses in the medium voltage are called the suggested charging patterns. The main controller sends the restricted and suggested versions for the charging patters to the residential and charging station controllers, which await this command, then residential and station controllers check if the suggested patterns will produce less power losses or not, by calculating the net savings between the suggested and restricted patterns and using the pattern that minimizes the losses. If the suggested pattern is much better than the restricted pattern, then the low-level controllers check if it will negatively affect the satisfaction factor. If the satisfaction factor is not affected, then the suggested pattern is used. Otherwise, the restricted pattern is selected

IV. HIERARCHICAL CONTROLLER ALGORITHMS
This section presents the optimization algorithms used by the hierarchical controllers to achieve their objectives.
The optimization problem at the charging stations' controllers is solved using the interior point method for quadratic programming due to its quadratic form as shown in (12). The residential compounds and MV associated optimization problems are solved by applying the pattern search algorithm due to the nonlinearity in their formulations as shown in (17) and (20). The pattern search is preferred over the genetic algorithm, as it finds the minimum direction with fewer steps especially in a small search space [36]. The pattern search algorithm uses the mesh adaptive direct search algorithm [37]. At each step, this algorithm searches a set of points, called a mesh, around the current point (the point computed at the previous step of the algorithm). The mesh is formed by adding the current point to a scalar multiple of a set of points (vectors) called a pattern. If the algorithm finds a point in the mesh that improves the objective function more than the current point, then this step is considered as a successful poll and the new point (best mesh point) becomes the current point in the next step of the algorithm. Moreover, if the poll is successful, the algorithm increases the search space by increasing the mesh size around the new current point. Therefore, the current mesh size is multiplied by an expansion factor and the process is repeated. If none of the mesh points yields a better objective function value, then this step is considered as an unsuccessful poll and the current point does not change but the algorithm reduces the search space by multiplying the current mesh size by a contraction factor and the search process is continued. These steps are repeated until the convergence is achieved. In the following simulations, the expansion and contraction factors are 2 and 0.5 respectively.
As the constraints for the charging stations and residential compound controllers are linear, an initial feasible solution to each of those control problems can be obtained using linear programming. With a feasible initial solution, the pattern search and quadratic programming algorithms takes fewer iterations to converge.

V. RESULTS AND DISCUSSION
This section describes the effects of the proposed method on the distribution grid performance using MATLAB. Moreover, it tests the validity for implementing the proposed algorithms on the available hardware in the lab based on control Hardware-in-the-Loop (CHiL) using OPAL-RT and DSP.

A. Simulation Results
To validate the proposed controllers, the hybrid smart distribution grid given in Figs. 3 and 4, is simulated using MATLAB at two levels of penetration, namely 31% and 62%. Furthermore, the validation considers the stochastic arrival/departure of EVs, ultrafast/fast charging with extra fees V2G technology. The results describe the difference between coordinated charging using the proposed method and random charging on the distribution grid performance.

CASE STUDY 1: 31% PENETRATION LEVEL
This case covers the near future, which assumes that 16 out of 53 nodes have charging points and each charging station has a total parking capacity of 25 EVs. A random scenario assumes that each vehicle continues to charge without any constraint as long as it is plugged in. Moreover, EVs arrive randomly following a normal distribution over three uncoordinated charging, the system peak dramatically rises over the maximum demand level limit and electricity shortage may occur in power plants with limited reserving capacity and generators with limited spinning reserve as shown in Fig. 7(a). In contrast, while using coordinated charging in Fig. 7(b), the power demand is smoothly distributed over the day in a manner that does not surpass the maximum demand limit. Furthermore, coordinated charging increases the system reliability taking into consideration the ultrafast/fast charging and EV owners' preferred time zones. The power demand is further reduced when using the V2G technology as indicated by the shaded green area in Fig. 7(b).
The shaded area describes a total reduction of 12% in the supplied energy from the grid while is compensated by discharging 2 EVs in V2G or V2V mode. Fig. 8 describes the total system losses in the distribution networks over the day. As shown in Fig. 8(a), a considerable extra amount of energy losses -estimated by 302.12 kWh per day -can be noticed in case of the uncoordinated charging (over the system without EVs).
However, it sharply decreases by 37% and 48% in case of coordinated charging without/with V2G technology, respectively as shown in Fig. 8(b). On the other hand, coordinated charging results in a significant decrease in the transformer loading and saves an annual cost estimated by 10097 $ and 13072 $ when utilizing the V2G technology (assuming the electricity price in Australia for household is 0.247 $/kWh). Fig. 9 describes the voltage profile per day for the 53 LV nodes connected to Feeder-15, which is the worst affected MV feeder. Fig. 9(a) shows the system voltage profile without introducing EVs and for a maximum acceptable voltage drop up to 10% in the LV networks.
For every bus, there are 48 circles, which represent the voltage over the day in case of a 30-minute time slot. As illustrated in Fig. 9(b), extreme voltage deviations up to 16% may occur in case of random uncoordinated charging, which may be addressed using transformers tapping or placing capacitor banks at the faulted buses with extra cost. However, the voltage deviations are within the acceptable region for our coordinated charging scenario, with a maximum value of 0.91 pu occurring at node-14. This scenario covers the long-term future, which assumes 32 nodes out of 53 have charging points and each charging station has a total parking capacity of 50 EVs. Fig. 10 illustrates the total system power demand level over the day for 62% penetration level. The peak demand level is sharply doubled during uncoordinated charging, which causes undesirable effects on the distribution grid stability as shown in Fig. 10(a). However, this peak is shaved using our coordinated charging and the loads are smoothly shifted to fill the valley as shown in Fig. 10(b). Furthermore, the green area describes a total reduction of 10% in the supplied energy from the grid and compensated by 3 EVs out of 30 discharging in V2G or V2V mode. Additionally, the peak at 6 p.m. is slightly raised to allow charging with extra fees according to the owner's selected charging priorities. Fig. 11 describes the total system losses in the distribution network over the day. As shown in Fig. 11(a), the extra amount of energy losses is 1170 kWh per day during uncoordinated charging (over the system without EVs). However, the losses are significantly decreased by 43% and 50% during coordinated charging without/with V2G technology, respectively as shown in Fig. 11(b). Furthermore, the annual cost is reduced by 45258$ and by 53390$ when utilizing the V2G technology. Fig. 12 describes the voltage profile for the residential compound connected to Feeder-22. Severe voltage deviations can be observed during uncoordinated charging with a maximum voltage drop at bus-10 as shown in Fig. 12(a). However, the voltage deviations are within the acceptable limits during coordinated charging as shown in Fig. 12(b).

B. Hardware-in-the-Loop Validation
The system is validated based on DSP and control Hardwarein-the-Loop (CHiL) using OPAL-RT, as shown in Fig. 13. The OPAL-RT platform is operating on 4 cores based on Intel Core Xeon processor at 3 GHz and RAM 2 × 8 GB. The system controller is uploaded on a 150 MHz DSP labeled as (TMS320F28335ZJZA). The validation process involves two cases.

1) CASES 1: CHARGING STATION CONTROLLER
The optimization algorithm of the charging station is to be validated; therefore, the charging station algorithm is set up on the DSP and the OPAL-RT platform is used to implement the constrained signals from the main controller. First, the main controller constrained signals are sent from the OPAL-RT to the station algorithm on the DSP, then the OPAL-RT receives the optimal station charging schedules generated There are 5 different scenarios: (1) EV arrives at 6:30 PM with initial SOC 44% and is totally charged to 90% within 30 minutes (ultra-fast charging), (2) EV arrives at 4:30 PM with initial SOC 46% and totally charged to 90% within 1 hour (fast charging), (3) EV arrives at 7:00 PM with initial SOC 47% and totally charged to 90% within 2 hours (moderate charging), (4) EV arrives at 4:00 PM with initial SOC 45% and totally charged to 90% without any prioritized periods (normal charging), (5) EV arrives at 5:30 PM with initial SOC 98% and totally discharged to 45% for the purposes of supporting the grid during peak loads and purposes of V2G.  . 15 describes the cost analysis for the charging station, ensuring that the implemented controller successfully reduced the cost. As previously stated, the pricing is addressed using dynamic real time pricing based on the linear model stated in (4). The parameters for the linear model are = 2 × 10 −4 $/ (kWh) 2 and = 0.247 $/kWh [18]. TOU pricing can be shaped by changing over the day. Fig. 15(a) shows the change of with loading, during the peak and mid-peak hours. The price increases by 40% and 20%, respectively. Fig. 15(b) shows the total savings in charging station while using different pricing techniques. When the price is constant, there are no savings. However, the savings are approximately 10$ over the day while using dynamic real time pricing without TOU and savings jump to 55.41$ while using dynamic real time pricing with TOU.

2) CASE 2: RESIDENTIAL CONTROLLER
In the second case, the optimization algorithm for the residential controller is validated. The OPAL-RT platform is set up as in case 1, but with the residential algorithm as the DSP code. Fig. 16 describes the charging scenarios for the residential compound connected to Feeder-15 to clarify that the implemented controller succeeded in meeting the constraints. There are 4 different scenarios: (1) EV arrives at 4:00 PM with initial SOC 50% and totally charged to 90% within 1-hour (fast charging), (2) EV arrives at 5:00 PM with initial SOC 40% and totally charged to 90% within 2 hours (moderate charging), (3) EV arrives at 3:00 PM with initial SOC 49% and totally charged to 90% without any prioritized periods (normal charging), (4) EV arrives at 3:30 PM with initial SOC 98% and totally discharged to 45%.

3) EXECUTION TIME
Due to the heuristic nature of the optimization algorithm, the execution time cannot be precisely determined. However, the average execution time for the three controllers can be estimated by measuring the average execution time for the residential controller, because the residential controller execution time includes that of the main controller, since the residential controller waits for the main controller to finish its task and send back the initial pattern. Therefore, the execution time for 1000 samples from different residential controllers for different time slots have been measured. As shown in Fig. 17, which shows the histogram generated from 1000 samples, the average execution time for the residential controller in the case of the 62% penetration level is approximately 15 minutes for a 30-minute time slot, as shown in Fig. 17(a), and the average execution time in the case of the 31% penetration level is approximately 10 minutes for a 30-minute time slot, as shown in Fig. 17(b).

VI. CONCLUSION
This paper addressed the impact of random charging of EVs on the distribution grid performance and proposed an integrated optimization problem formulation implemented at LV and MV networks to enhance the distribution grid performance from various perspectives including minimization of losses and maximization of customer satisfaction level. Additionally, the proposed formulation seeks shaving the peak demand while keeping the voltage drops within the acceptable region. The proposed method handles the charging of EVs in stations with different priorities and minimizes the charging cost for EV owners. The method is validated using a hybrid smart distribution grid of IEEE with 23 kV 31 buses with residential 415 V 53 LV nodes at two EV penetration levels, namely 31% and 62%. The results from