An adaptive real-time energy management system for a renewable energy-based microgrid

This paper proposes an adaptive real-time energy scheduling method (RT-EMS) for a microgrid, using a Lyapunov optimization -based real-time approach. Inaccuracy in day-ahead predictions can result in non-optimal solutions to the energy scheduling problem. Although the real-time optimization method eliminates the need to deal with the prediction uncertainties, it ignores the valuable statistical information used in day-ahead stochastic approaches and provides suboptimal solutions to the problem. The proposed adaptive approach combines the advantages of both the stochastic day-ahead and the RT-EMS and reduces the real-time operational cost of the microgrid. The proposed method moves the RT-EMS solution towards the optimal solution, by adding a penalty term to the objective function. Numerical results are provided to demonstrate the improved performance of the proposed adaptive method.


INTRODUCTION
Energy scheduling of a microgrid, as a small-scale power system that enables integration of distributed and renewable energy sources (RESs) and battery energy storage systems (BESSs), is the most important component in its operation [1]. The energy management system (EMS) of a microgrid determines the optimal operation points of the generation and storage units with various objectives such as operation cost minimization [1,2].
In the EMS of a microgrid, the uncertainty in predictions of load demand and intermittent power generation of RESs is a critical challenge to the optimal operation of the EMS. The main approaches to deal with the uncertainties in the day-ahead EMS (DA-EMS) are the stochastic [3,4] and robust approaches [5][6][7]. Stochastic methods require statistical information about the sources of uncertainty, whereas most robust optimization methods, such as information-gap decision theory and risk management, require a compromise between robustness and higher operation cost.
Unlike day-ahead predictions, real-time measurements eliminate the uncertainty of data because no predictions are In [8], the RT-EMS is designed using a learning algorithm based on the fully connected neuron networks. It is shown that training the learning algorithm-based RT-EMS in [8], reduces the computational burden of optimal power flow in real time. In [9], an RT-EMS is developed for a microgrid using an evolutionary optimization algorithm. In the case that the BESSs were fully charged, the excess energy of the wind turbine (WT) was used as a useful dump load, for example, an auxiliary electric water heater. A two-stage RT-EMS for a multi-microgrid system is introduced in [10]. First, a robust scheduling method is employed 1 day ahead by the DA-EMS. In this case, an unbalance arises between the load and generation due to the uncertainties of loads, RESs, and market prices in real time. Then, the unbalance is compensated in real time, by employing the energy stored in the BESSs or using the conventional generation units (CGUs). In [11], the fluctuation in BESS charging and discharging efficiencies is considered, based on the BESS output current. This variable efficiency is then employed to enhance the accuracy of the EMS. In [11], one goal is to make the final energy level of the BESS equal to the initial value. Therefore, the error in prediction of the RES power generation is compensated in real time by using the RT-EMS in order to achieve the mentioned goal.
Optimal operation of BESS energy is the main challenge of the RT-EMS because demand predictions are not used in RT-EMS. It this case, decision-making about optimal BESS charging and discharging has a high risk of being a non-optimal action. Besides, the limited energy level of BESSs introduces a time-coupled constraint to the RT-EMS problem because the energy level of a BESS at each time instance is a function of its past energy level and the gained power. Recently, the Lyapunov optimization method [12] has been used in energy systems to provide a solution for the RT-EMS problem subject to timecoupled constraints.
A Lyapunov optimization-based two-stage RT-EMS was designed in [13]. In the first stage, the DA-EMS determines the on/off states of the CGUs. Then, the CGUs, BESSs, and loads are scheduled in real time in the second stage. Using Lyapunov optimization, the RT-EMS is designed in [14] with the goals of optimal utilization of BESSs, provision of residential quality of service and the scheduling of the power exchanged with the main grid. However, the operation cost of the BESS is not considered in the introduced RT-EMS. Ignoring the BESS operational cost can result in its fast degradation, which is harmful to the BESS and reduces its useful lifetime. The RT-EMS for a smart microgrid with RES and BESS is investigated in [15], in which the goal is to achieve optimal energy cost with decreasing required battery capacity. A different approach of the Lyapunov optimization-based RT-EMS is designed for a microgrid in [16] using a variable V algorithm and considering the optimal power flow constraints. It is shown that a variable-parameter algorithm improves the performance of the RT-EMS in comparison with the constant V parameter.
Moreover, distributed and finite-time horizon variations of the RT-EMS have also been investigated. Using a distributed optimization scheme, the distributed implementation of the RT-EMS is introduced in [17][18][19] for BESS scheduling with respect to BESSs' size constraints. The finite-time horizon approach is utilized in [20,21] to develop an RT-EMS for BESS management. In addition to BESS scheduling in real time, provision of flexible delay-tolerant loads is studied in [21][22][23]. The RT-EMS is formulated considering the optimal power flow equations in [24].
However, the mentioned RT-EMSs resulted in a non-optimal solution and did not consider the statistical information of the random variables, unlike the DA-EMS. The DA-EMS has the advantage of using the statistical information of the random variables, whereas the RT-EMS provides the advantage of robustness against uncertainties, because of real-time measurements.
In brief, [8][9][10][11] have proposed an RT-EMS but not based on the Lyapunov optimization technique, which does not show acceptable scheduling and cost when used in a power system with energy storage systems. On the other hand, [13][14][15][16][17][18][19][20][21][22][23][24] have used the Lyapunov optimization technique introduced in [12] for the RT-EMS model. In the existing RT-EMS methods, the valuable information from the stochastic day-ahead scheduling has been ignored, except in [13], in which only limited information of the on/off states of the generation units was determined in the day-ahead scheduling.
In the DA-EMS, the predicted values including the RESs' generation, market price and load are some sources of uncertainty. The predictions in the DA-EMS may be inaccurate and erroneous, which may cause large disturbances to the decisions made by the DA-EMS. On the other hand, real-time measurements in the RT-EMS eliminate the need for short-term predictions and thus remove prediction errors and uncertainty. Although the stochastic characteristics of uncertain EMS variables are employed for stochastic DA-EMS, this valuable information is ignored in Lyapunov optimization-based RT-EMS. In order to improve the performance of the above-mentioned Lyapunov optimization-based RT-EMSs and obtain the benefits of employing statistical information of uncertainties, an adaptive RT-EMS (ART-EMS) is proposed here. In the proposed method, the information from the stochastic day-ahead scheduling is adaptively used in a Lyapunov optimization-based RT-EMS to improve its performance. The developed adaptation is implemented by defining a deviation function considering the real-time measurements compared to all the scenarios solved in the day-ahead scheduling problem.
The proposed ART-EMS has two main stages: (i) stochastic DA-EMS and (ii) ART-EMS. In the proposed ART-EMS, the outcomes of the stochastic DA-EMS are adaptively employed to improve the performance of the conventional RT-EMS. The proposed algorithm benefits the advantages of both the stochastic DA-EMS and the robust RT-EMS, by proposing an adaptive scheme based on Lyapunov optimization, stochastic analysis, and short-term predictions. The point estimate method (PEM) is used for scenario generation in the DA-EMS stage and the Lyapunov optimization method is employed in the RT-EMS stage. A deviation function is defined based on the DA-EMS solution and real-time measurements, and added as a penalty term to the objective function of the RT-EMS. Use of RT-EMS with online measurements eliminate the uncertainties caused by predictions and forecasts.
The main contributions of the proposed ART-EMS, based on the above-mentioned discussions, are: • Adaptive integration of the stochastic day-ahead and the Lyapunov optimization-based real-time EMS methods • Improved utilization of BESS in RT-EMS by moving the RT-EMS decisions towards the offline stochastic day-ahead EMS • Enhancement in performance of the Lyapunov optimizationbased RT-EMS by utilizing the information from day-ahead predictions and stochastic analysis • Adaptive reaction to the changes in the predicted information by modifying the objective function of the RT-EMS Definition of the proposed deviation function and its application to integrate the day-ahead and real-time energy scheduling methods (RT-EMS), which is designed based on adaptive scheduling/control, is a significant contribution to enhance the RT-EMS, as they ignored the statistical information of the FIGURE 1 Typical microgrid model available data, whereas this statistical information can play a key role in energy scheduling. The proposed use of statistical information for Lyapunov-based RT-EMS and integration of day-ahead and real-time methods using an adaptive design approach has not been studied in the existing literature, to the best of authors' knowledge. Moreover, the power scheduled in a stochastic day ahead using the forecasted RES and load is employed in real time, which improves the usage of BESSs. In the proposed method, all the energy units are scheduled in real time using the Lyapunov optimization method. Simultaneously, the scheduled output energies of the units are adaptively modified based on the results of stochastic energy scheduling with different scenarios. This design is different from the two-stage stochastic energy scheduling design introduced in papers. The proposed method is considered as an adaptive RT-EMS.
The rest of the paper is organized as follows; The energy components of the microgrid are modelled in Section 2. The proposed adaptive method for ART-EMS is designed in Section 3. Simulation results on the modified IEEE 33 bus test system are shown in Section 4. Section 5 concludes the paper.

MICROGRID SYSTEM MODEL
For modelling the power units in a microgrid, a typical gridconnected microgrid consisting of RESs, BESSs, CGUs, and electrical loads is shown in Figure 1. Two-way information flow is considered to implement energy management algorithms. The operational costs of the generation units (i.e. RESs and CGUs), the BESSs, flexible loads and the cost of exchanging energy with the main grid are modelled in the following.

CGUs
The operational cost of a CGU is modelled by a quadratic function of its output power (i.e. P g ) as [24]: where t is the number of each time slot, and Δt is the time duration of each time slot. The operating conditions of the CGU is limited by the operational constraints (2) and (3). The CGU output power is limited by the lower and upper bounds in (2), whereas the rate of power change is limited by ramp limitations in (3) [13].

BESS
The operational cost function of the BESS is modelled, using a quadratic function of its output power (i.e. P b (b, t )), as [24] Fast transitions between charging and discharging modes in a BESS can cause battery degradation and reduce the lifetime of the BESS [25]. Equation (4) penalizes fast transitions in BESS operating modes to reduce degradation. Consequently, the constraints (5) and (6) are used to satisfy the limitation on output power of the BESS, and constraint (6) is considered to limit the stored energy in a BESS [17].
The BESS stored energy is calculated based on the charged/discharged power at each time slot, as

Power exchange with the main grid
The microgrid imports power from the grid, when the generation units are not sufficient to supply the demand. Furthermore, the surplus energy can be exported to the main grid to gain profits from selling energy to the grid. Based on the electric market price, the cost of exchanging power with the main grid is modelled as: It is noted that P grid (t ) is assumed to be positive (negative) when the microgrid imports (exports) power from (to) the main grid. In practice, the exchanged power with the main grid has a limited capacity, which is modelled as:

Renewable power generation
Here, WT is considered as the RES unit. The output power of the WT is modelled as (11), regarding the technical characteristics of the WT and the wind speed [26].
where v(t ) is the wind speed, and v r , v ci , and v co are the rated, cut-in and cut-out wind speeds of the WT, respectively; and P r w is the rated output power. As shown in (11), the output power of the WT is 0 when the wind speed is lower than the cut-in speed or higher than the cut-out speed. The output power is linearly proportional to the rated power, if wind speed is between v ci and v r . If wind speed is between the rated and the cut-out wind speeds, the output power of the WT is equal to its rated power (i.e. P r w ).

PROBLEM FORMULATION AND THE PROPOSED ART-EMS METHODOLOGY
The total operational cost of the microgrid is the sum of the operational costs of CGUs (i.e. (1)), BESSs (i.e. (4)), and power exchange with the main grid (i.e. (9)), defined as: Energy management of a microgrid is a stochastic problem in the presence of these uncertainties. Monte Carlo simulation is the fundamental probabilistic approach to deal with the uncertainties in the DA-EMS. The stochastic characteristics of the predicted variables such as the intermittent RESs and loads are employed in probabilistic DA-EMS. Although Monte Carlo simulation is fundamental to probabilistic analysis of uncertainties, the requirement to generate a great number of scenarios makes it computationally difficult. Approximate methods reduce the number of scenarios, while keeping accurate results.
Here, the PEM is utilized to produce scenarios in the DA-EMS. In the following, first the day-ahead energy management problem is introduced. Then, the PEM is presented for scenario generation in stochastic DA-EMS. Subsequently, the RT-EMS problem is modelled and the Lyapunov optimization method is used to solve it. Next, a deviation function is defined based on the differences between the decisions made in DA-EMS and RT-EMS problems. Finally, the proposed ART-EMS is defined as a constrained optimization problem, in which the objective function is the combination of the RT-EMS cost and deviation functions. The proposed ART-EMS is a combination of stochastic DA-EMS and RT-EMS problems. The stochastic DA-EMS stage of the proposed method is solved employing a scenario-based approach.

Stochastic DA-EMS using PEM for scenario generation
The day-ahead energy scheduling in DA-EMS is defined as a constrained optimization problem as: (10) and where the subscription scen is the index of the scenarios and {.} is the expectation function.
In order to utilize the information from stochastic analysis, it is assumed that a DA-EMS problem is already solved stochastically considering different scenarios from the predicted data of the variables. Although predictions are available from the historical data and stochastic characteristics of the EMS variables (i.e. renewable generation, load, and market price), predictions are inaccurate due to intermittent nature of these EMS variables. Therefore, there might be a significant variation in the EMS variables in real time. Here, the 3−PEM is used to generate limited number of scenarios for load and WT power (WTP). First and second points of each random variable (i.e. load and WTP) are created as [27]: where l is the index of the variable for which the scenarios are generated; M is the number of random variables; l ,3 is the third moment (i.e. skewness) of the variable; l ,4 is the forth moment (i.e. kurtosis) of the random variable l distribution; and k is the index of points. Here, M = 2, and l = 1 and l = 2 represent the load and the RES generation of the microgrid, respectively. Using (15), three points for each input random variable are created as: Each scenario is obtained using the points of random variables as: Weight for each decision variable is calculated as: Here, load and wind power are uncertain parameters. As these uncertainties are independent, their probabilities in each point is calculated as (19) [28]. Also, load demand and wind generation amounts are independent at each hour. So, we consider three points for each uncertain variable at each time. Three points for each load and WT output power are considered. Taking into account time independencies nine points are calculated at each time.
Using (17), the scenarios are generated for the load demand and WTP. The weights of the points for load and RES generation are calculated by (18). The scenarios of load and RES generation are generated by (17). The weight (i.e. t point ) of each scenario point is defined based on the weights of each variable, as: where w 1,k and w 2,k ′ are for load demand and RES power generation, respectively.

Real-time energy management system
The RT-EMS is modelled as the following optimization problem [14]. (10), and where C (t ) is the total operation cost defined in (12), and {.} is the expected value function. From (20), it can be seen that Problem P1 tries to minimize the time average of the expected value of the cost function C (t ). The Lyapunov optimization algorithm is the proper method to solve an optimization problem in the form of (20) [23]. The flowchart of the RT-EMS using the Lyapunov optimization method is shown in Figure 2 and described in the following. First, certain queues are defined for the time-coupled constraint, that is, the stored energy limitation in a BESS. Next, a Lyapunov drift-plus-penalty function is defined and its upper bound is calculated. An optimization problem is developed to minimize the upper bound of the drift-plus-penalty function.

3.2.1
BESS virtual queue limiting the energy stored in the BESS results in a time-coupled constraint shown in (7). By applying (22), the average charging/discharging of the BESS is forced to be zero, in order to satisfy (7) [13].
The virtual queue for the BESS stored energy is defined as where B b (b, 0) is bounded.
The constraints (7) is satisfied, if the defined virtual queue for the BESS is mean-rate stable [18]. Consequently, the BESS energy will be kept in the practical pre-defined range. According to the Definition 1, B b Summing the virtual queue (23) over the time ∈ {0, … , t − 1}, t → ∞, [12] and applying the expectation function, we have: It is noted that since B b (b, 0) is bounded then According to Definition 1, it is also noted that if the queue (23) is mean rate stable, then From (28) and (26), it is seen that the right-hand side of (26) is zero. Consequently, the constraint (22) is satisfied.
In order to implement real-time optimization, the timecoupled constraint (7) must be avoided [24]. Therefore, (7) is replaced with (23), and thus Problem P1 is updated as: Problem P2. Nonetheless, constraint (7) is still kept in addition to (23), to ensure that BESS energy is limited to the desired operational range at each time slot. The optimization Problem P2 can be solved using the Lyapunov optimization method.

Drift-plus-penalty function
In order to calculate the drift-plus-penalty function, first we define the Lyapunov function for the BESS virtual queue Based on (30), the one-slot conditional Lyapunov drift is calculated as B(t ) is also called backlogs and can be defined as the amount of work that needs to be done [12]. Minimizing Lyapunov drift minimizes queue backlogs that makes queues mean rate stable and satisfies described inequality constraints. Although minimizing only the Lyapunov drift can decrease the backlogs in the BESS virtual queue and stabilize it, the operation cost will be elevated.
To solve it, the operational cost is penalized by a penalty factor V and added to the cost function. The weighted sum of drift and cost is defined in (32) as the drift-plus-penalty function [24].
Minimizing drift-plus-penalty minimizes the backlogs in the virtual queue as well as the cost function.
where Δ(B(t )) is the one-slot conditional Lyapunov drift defined as: In order to employ Lyapunov optimization method, the upper bound of (32) is calculated in the following. It is noted that the conditional terms in (33) can be eliminated, because the BESS queue has a known value at each time slot.
Squaring both sides of (23), the BESS virtual queue is upper bounded by: Applying (34) into (32), and taking its conditional expectation gives where In Lyapunov optimization method, the objective function (32) is not minimized directly. Instead, its upper bound calculated by (35) is minimized as the objective function of the optimization problem. At the beginning of each time step, the variables including the RES generated power, load demand, and the market price are observed and the virtual queue of the BESS is updated. Then, the decision variables (P b (b, t ), P g (g, t ), P grid (t )) are determined by solving the following optimization problem.

Formulation of the proposed ART-EMS
In order to adaptively account for the variations, while benefiting from real-time optimization and stochastic day-ahead energy scheduling, a deviation function DF (t ) is proposed based on the differences between the predicted values and the online measurements at real time, as: where >> 1 is a real positive penalty factor; 0 ≤ ≤ 1 are real positive design gains; P DA b (b, t ) is the power of the BESS b, scheduled by the stochastic DA-EMS. The gain denotes the weight of the differences between the predicted value at the DA-EMS and the real-time measured value at the RT-EMS problem. The variation of the scheduled output power of the BESS at RT-EMS compared to the DA-EMS is penalized by the penalty factor >> 1. When the total WTP in real time is higher than the predicted values in the DA-EMS, the deviation function (38) helps to make the decision of increasing the charg- ing power of the BESS in real time. The same happens when the total load at real time is less than predicted load demand at real time. Conversely, when the load in real time is higher than the predicted values in DA-EMS or the WTP in real time is less than the predicted power, the deviation function (38) helps to make the decision of decreasing the charging power or increasing the discharging power of the BESS in real time.
In the proposed ART-EMS, the deviation function (38) is added to the objective function in Problem P3 as a penalty term. Minimizing the weighted sum of the deviation function (38) and the objective function in (37), helps the proposed ART-EMS to account for the deviations in the predictions. It moves the real-time scheduling towards the solutions of the offline energy management scheme, which is the global optimum solution of the energy management problem.
By adding the deviation function (38) to the objective function of Problem P3, the proposed ART-EMS problem is defined as: It is noted that this algorithm could be applied to any number of BESSs and CGUs regarding the indices b and g. The detailed schematic flowchart diagram of the proposed ART-EMS is shown in Figure 2.

NUMERICAL STUDIES
For evaluation of the proposed ART-EMS, the modified IEEE 33-bus distribution system is adopted [29]. The schematic diagram of the modified test system is depicted in Figure 3. A CGU, four BESSs and three WTs are added to the IEEE 33bus distribution system. 24-h load profiles are considered for the load busses, and a market price is assumed to buy/sell electricity from/to the main grid. The characteristics of the CGU and the BESSs are presented in Tables 1 and 2, respectively. The capacity of each WT and the main grid are assumed as 2 and ±5 MW,  respectively. The cost of load shedding is set to 500$∕MW 2 , and the parameter V in the RT-EMS is selected as 150. The time slot duration is set to 10 min and thus Δt = 10∕60 = 0.167 h. For DA-EMS, the scenarios of load demand and wind power generation are generated by the PEM. For simulating a practical case study, the scenarios in the RT-EMS are selected completely different than the day-ahead predicted values. For the DA-EMS, the PEM points for the scenarios of the load demand at bus 32 are shown in Figure 4a. The load demand scenario selected for RT-EMS is shown in Figure 4b. The scenarios in Figure 4 are only the data of load at bus 32. The loads at other busses are selected proportional to the load at bus 32, where the proportions are listed in Table 3 [30]. Similarly, the PEM points for the scenarios of wind power generation are shown in Figure 5a for DA-EMS, and the scenario for the RT-EMS is shown in Figure 5b. The wind power shown in Figure 5 is divided equally among the WTs. The market price is shown in Figure 6.
Performance of the proposed ART-EMS is compared to the energy management methods of stochastic DA-EMS, RT-EMS,

4.1
Case study I: Performance of the proposed ART-EMS with = 0, for ±20% deviations in load and WTP As shown in Figure 2, the stochastic DA-EMS is the first stage of the proposed ART-EMS. In this case, the impact of the DA-EMS solution on the proposed ART-EMS is studied. The load and WTP scenarios shown in Figures 4b and 5b are used in case study I. First, similar scenarios are solved in DA-EMS and RT-EMS. Next, ±20% deviation in load demand and WTP are considered for the RT-EMS from the scenario solved in the DA-EMS, to evaluate the proposed RT-EMS. The WTP and load demand are deviated ±20% and the studied scenarios are shown in Table 4. The gain in the DF (38) are selected as = 0 to investigate only the impact of the BESS scheduling in DA-EMS on the proposed ART-EMS. The penalty factor in the DF (38) is set to = 150.
In this case study, only 20% deviation in load demand and WTP is considered to simplify the analysis and the stochastic DA-EMS with random change in these two variables is investigated in next case studies in Ssections 4.2 and 4.3.
The operational costs of the microgrid with different energy scheduling methods are given in Table 4 and compared to the performance of the proposed ART-EMS. The negative (positive) values in this table show the benefits (costs) of operating the microgrid. As mentioned earlier, only the first term of the proposed adaptive law is considered in this case and thus = 0 is set in (38). The cost of the deterministic DA-EMS is −248, which is attained by solving the DA-EMS for the predicted values of load demand and WTP. In order to assess the proposed adaptive law, it is assumed that the real-time values of load demand and WTP are deviated by ±20% from the day-ahead predicted values. The results show that the proposed ART-EMS improves the performance of the RT-EMS method for all deviations. As shown in Table 4, the proposed ART-EMS provides a better solution compared to RT-EMS and increases the benefits, when there is no deviation. Using the proposed ART-EMS, the costs are reduced for 20% decrease in WTP as well as for 20% increase in load demand. Moreover, the proposed ART-EMS increases the benefits compared to the RT-EMS, when there is ±20% change in WTP and load, as well as 20% decrease in WTP or load demand. As mentioned before, it is almost impossible to obtain the global optimal results of the offline optimization in practice because the uncertainties of the WTP and load are inevitable.
The energy level of BESS1 is depicted in Figure 7, where the WTP and load are deviated 20% from the DA-EMS data. It is shown that the proposed ART-EMS converged the energy level of BESS1 to the global optimal solution (i.e. the solution of offline optimization). For improved deployment of BESSs, it is desirable to make the final energy level of the BESSs equal to the initial values. Otherwise, the BESS energy level might be used up which reduces the functionality of the BESSs and degrades the stability of the microgrid system. The results in Figure 7 shows that the whole energy in BESS1 is not depleted and the energy level is restored to its initial value, except for greedy optimization method. With greedy optimization, the energy of the BESS1 is depleted at the first several hours of the day, because BESS has a lower cost and the greedy optimization does not consider the future and the past in its decision-making algorithm. The energy levels of BESSs 2-4 are similar to BESS1 and thus are not included for brevity. In addition to the improved costs in Table 4, it is shown in Figure 7 that the proposed ART-EMS advances the deployment of BESS units compared to RT-EMS and greedy optimization methods, even when the gain is selected as = 0. The proposed ART-EMS moves the energy trajectory of the BESS close to the solution of the offline optimization, by employing the decisions made by the DA-EMS and the adaptive

Case study II: Evaluation of the proposed ART-EMS for different ≠ 0 values and variations in WTP and load
In addition to the difference in energy level of the BESS in the DA-EMS and RT-EMS, the DF also consists of the differences in WTP scenarios as well as the load demand, as shown in (38). The impact of only the energy level of BESSs in DA-EMS on the proposed ART-EMS was investigated in Ssection 4.1 by setting = 0. In the following, the impact of BESS energy level, WTP and load scenarios in the DF (38) on the proposed method is studied. The gains = 0 determine the weights of WTP and load with respect to the BESS energy level in the DF (38). As for case I, the RT-EMS scenarios for WTP and load are generated by deviating the DA-EMS scenarios by 20%. Similarly, the load and WTP scenarios shown in Figures 4b and 5b are used in case study II.
In order to design the penalty factor and the gain , different values are examined and the best parameters are chosen, based on the resultant costs. In this regard, two different values for are considered and their impact on the operation costs and energy level of the BESSs is evaluated in the following. The results show that a large gain causes more changes in charging and discharging modes of the BESSs, as is expected from the DF (38). The energy level of BESS1 is illustrated in Figures 8 and 9. Figure 8 depicts the energy level of BESS1 using the proposed ART-EMS with = 1500 and = 0.05, whereas Figure 9 shows the results for = 1. As shown, the BESSs are charged when load is decreased or WTP is increased in the ART-EMS solution, compared to the DA-EMS solution. As well, the BESSs are discharged when load is increased or WTP is decreased.
Comparing the energy levels shown in Figure 9 with Figure 8, it is concluded that a large gain can cause saturation or depletion of BESSs energy levels. Although saturation of BESSs can be helpful to improve the flexibility of the microgrid, depletion of BESS energy since large gains can reduce its reliability and stability. Therefore, a small gain is desirable to have a compromise between flexibility and reliability of the microgrid. Consequently, = 1500 and are chosen for further analysis of the performance of the proposed ART-EMS. The operation costs are shown in Table 5, for 20% deviation in real-time scenarios from the day-ahead scenarios. Comparing them with the results of RT-EMS in Table 4, it is shown that the proposed ART-EMS with ≠ 0 in the DF (38) provides a better solution with higher benefits or lower operational costs. In the proposed ART-EMS, it is shown in Table 5 that the operation cost is further improved with ≠ 0, compared to = 0 in

Case study III: Evaluation of the proposed ART-EMS, using PEM-based stochastic DA-EMS
In case studies I and II, only one scenario was used in the DA-EMS stage of the proposed method, in order to provide detailed analysis and parameter selection. However, as described in Section 3, a stochastic day-ahead scheduling is employed in the proposed ART-EMS. The scenarios for the stochastic DA-EMS are generated using PEM as shown in Figures 4a and  5a. Besides, the scenario in real-time analysis is selected randomly and completely different from the day-ahead scenarios, as shown in Figures 4b and 5b. The WTP and load scenarios are chosen randomly considering the Rayleigh and the normal probability distribution functions, respectively. Performance of the proposed ART-EMS with PEM-based stochastic DA-EMS and the parameters chosen as = 1500 and = 0.05 are evaluated in the following.
Energy levels of BESSs 1 and 2 in Figures 10 and 11 depict that the proposed ART-EMS improves the utilization of BESSs compared to the RT-EMS. As shown, the proposed ART-EMS moves the solutions towards the offline EMS solutions. Moreover, the final energy level of the BESSs are restored close to the initial values, using the proposed ART-EMS. The operation cost of the proposed ART-EMS with PEM-based day-ahead scheduling is compared to different energy scheduling methods in Table 6. The performance of the proposed ART-EMS is compared to the energy management methods of stochastic DA-EMS, RT-EMS, offline optimization, greedy optimization, and a two-stage robust EMS. In the two-stage EMS [31], a scenario-based robust optimization is used in the first stage and an online optimization is employed in the second stage.
In the first stage, the Taguchi orthogonal array (TOA) method [32][33][34] is used for robust optimization. The scenarios of load and renewable generation for TOA testing are designed using the method described in [32,33] considering different levels of uncertainty (i.e. 5%, 10%, 15% increase and decrease in the expected load and renewable generation). In the second stage, six time slots are used for online optimization. Only the first operation on the time horizon of the sequence is implemented [31]. Comparing the results illustrates that the proposed ART-EMS decreases the operational costs in comparison with the conventional Lyapunov optimization-based RT-EMS by 42%, as well as the greedy optimization method. This improvement is obtained by the proposed ART-EMS because of adaptively modifying the solutions of the RT-EMS based on the stochasticbased DA-EMS. This modification is based on the energy levels of the BESSs as well as the deviations in WTP and load, as modelled in the DF (38). As shown in Table 6, we get different costs for different uncertainty levels, that is, the robust optimization problem. For low/high uncertainties, the two-stage robust optimization results in a lower/higher cost compared to the proposed ART-EMS.
In the proposed ART-EMS, the advantages of the stochastic DA-EMS in employing the prior knowledge about BESSs and prediction of loads and WTPs, is adaptively combined with the advantages of the RT-EMS in robustness against uncertainties due to using real-time online measurements. It should be noted that the global optimal solution obtained by the offline optimization method is not achievable in practice, because the exact prediction of WTP and load are impossible due to their intermittent nature and uncertainties. Although there are certain methods such as the RT-EMS to deal with these uncertainties, it is shown in Table 6 and Figures 10 and 11 that the proposed ART-EMS provides a better solution with a lower operation cost.
The proposed method improves the performance of the Lyapunov optimization-based RT-EMS by moving the solutions towards the offline optimization solutions. Moreover, the proposed ART-EMS uses real-time online measurement, similar to the RT-EMS methods, which intrinsically eliminates the presence of uncertainty in WTP and load.

CONCLUSIONS AND FUTURE WORKS
This paper has proposed an adaptive real-time EMS for a microgrid to improve the performance of the Lyapunov optimizationbased RT-EMS. Although the RT-EMS uses online measurements and thus avoids the uncertainties arising from the forecasted data of the WT generation and load demand, it ignores the valuable statistical information from their stochastic characteristics. The proposed algorithm combines the advantages and features of both the DA-EMS and RT-EMS. In this regard, a deviation function is defined as a penalty term and added to the objective function. The deviation function is the sum of differences between day-ahead and real-time scenarios of WTP, load, and the BESSs' energy level. In the proposed ART-EMS, PEM is used to generate scenarios for the stochastic DA-EMS, and Lyapunov optimization method with the objective function of Lyapunov drift-plus-penalty function is used for the RT-EMS. The proposed ART-EMS adaptively uses the optimal solution from the stochastic day-ahead analysis to improve the solution of the RT-EMS by moving the real-time decisions towards the global optimal solution. In order to evaluate the effectiveness of the proposed method, the modified IEEE 33-bus distribution system was employed for simulation. The simulation case studies demonstrate that the proposed ART-EMS obtains a better performance and significantly reduces the operation costs in comparison with the Lyapunov optimization-based RT-EMS.
The next steps to improve the proposed method might be considering the instantaneous peak charging rate of BESS and the rebound effect in off-peak hours in modelling and operation of the RT-EMS to guarantee the security and reliability of the power network. Moreover, the high penetration of EVs and EV aggregators in RT-EMS could be addressed. The proposed adaptive EMS should be improved considering EVs, flexible loads, and delay-tolerant loads.
Regarding the existing literature on the Lyapunov-based RT-EMSs, it is concluded that the performance of Lyapunov-based RT-EMS is not affected for different levels of renewable penetration, as long as the load is less than the generation capacity or effective load-shedding methods are in place. Nonetheless, a dedicated study is worthy of understanding the maximum achievable renewable penetration under an RT-EMS.