Forecast-Based Energy Management for Optimal Energy Dispatch in a Microgrid

: This article describes the development of an optimal and predictive energy management system (EMS) for a microgrid with a high photovoltaic (PV) power contribution. The EMS utilizes a predictive long-short-term memory (LSTM) neural network trained on real PV power and consumption data. Optimal EMS decisions focus on managing the state of charge (SoC) of the battery energy storage system (BESS) within deﬁned limits and determining the optimal power contributions from the microgrid components. The simulation utilizes MATLAB R2023a to solve a mixed-integer optimization problem and HOMER Pro 3.14 to simulate the microgrid. The EMS solves this optimization problem for the current sampling time ( t ) and the immediate sampling time ( t + 1 ) , which implies a prediction of one hour in advance. An upper-layer decision algorithm determines the operating state of the BESS, that is, to charge or discharge the batteries. An economic and technical impact analysis of our approach compared to two EMSs based on a pure economic optimization approach and a peak-shaving algorithm reveals superior BESS integration, achieving 59% in demand satisfaction without compromising the life of the equipment, avoiding inexpedient power delivery, and preventing signiﬁcant increases in operating costs.


Introduction
With the increasing demand for electrical energy in recent years, the use of fossil fuels in conventional power generation plants has increased significantly.As a result, there has been an increase in the amount of greenhouse gases released into the atmosphere and a critical depletion of the Earth's non-renewable natural resources.Consequently, centralized generation systems are making way for small decentralized plants (microgrids) with a high penetration of renewable energy sources (RES), especially solar and wind energy, which globally account for 91% of the power produced from alternative sources [1][2][3].
Microgrids benefit from BESSs and EMSs, thereby enhancing the integration of RES and reducing costs.There are several EMS deployment strategies, including decision-based algorithms and dispatch methods.Consequently, the objective of an EMS when operating microgrids is to maximize the integration of RES, minimize operational costs, and preserve the life of the equipment, especially batteries [4,5].
In the literature, several approaches to deploying EMSs can be found.For example, in [6][7][8], the proposed EMSs characterize the most significant and least demanding hours, defining scenarios by the amount of power available.In this way, the algorithm obtains each element's operating limits, and a multi-objective optimization problem uses this information to minimize voltage fluctuations and energy losses.The authors of [9] discussed the simulation results of various configuration scenarios related to heating and energy storage within the vision of China's 2030 hybrid systems.The analysis focused on determining the optimal combination of these components by studying costs, oil consumption, and CO 2 emissions.The results demonstrate that adding a heated pump and pumped hydro storage with double gates to the individual heating and electricity network is an economically attractive option, reducing fuel consumption and emissions.
The dispatch strategy in [10] involved electric energy tariffs to reduce the operational costs of thermal units, photovoltaic power generation units, and BESSs through an optimization problem that considers two scenarios characterized by energy costs during peak and off-peak hours.Similarly, the strategy in [11] focused on power management through dynamic pricing strategies.
Decision-based algorithms are also quite prevalent in the literature.For instance, in [12][13][14][15], the strategies prioritize battery charging when the available power exceeds demand.However, when renewable resources cannot meet the load, dispatching the available energy from batteries could cover this power deficit.Alternatively, a third auxiliary system, such as a generator set, can be integrated.Reference [16] presented a similar approach, with the difference being the use of a peak-shaving strategy.The authors established the maximum operating power of the BESS based on the consumption peak.
An increasingly popular strategy is optimization through a mixed-integer model, as discussed in [5,17].This type of model incorporates all the costs associated with using each component into the objective function, while the operational limits of each component are set as constraints.Meanwhile, the integer part of the problem determines whether, at the current time instant, it is optimal to charge or discharge the BESS.The authors of [18] presented a derivation of this model, where the objective is to minimize the net present value through flexible energy flow control while also considering parameters such as variation in energy costs and equipment degradation, among other factors.Reference [4] proposed a similar optimization framework but incorporated stochastic models that could address the variability of RES.
Other studies focus on varying the battery SoC limits, as discussed in [19].The authors utilized an artificial neural network to predict the available photovoltaic power, aiming to increase battery integration during times of lower resource availability, including the potential participation of the electrical grid.The strategy discussed in [20] was similar, with the difference being the use of a stochastic model to obtain information on the availability of the photovoltaic resource.
References [1,19] developed prediction algorithms to forecast the meteorological conditions that influence wind and solar generation processes, aiming to provide input to theoretical models that can estimate the available electrical power throughout the day.This information, together with a linear optimization problem, can prevent the BESS from charging or discharging at inconvenient intervals.Reference [21] highlighted that incorporating a microgrid's photovoltaic power and consumption forecast can generate cost savings between 2% and 7%.Additionally, microgrid sizing under the assumption of forecast-based operation can significantly reduce operating costs.On the other hand, [22] proposed a deep learning-based forecasting method for microgrid operation, considering uncertainties in RES, load, and daily price to reduce electricity consumption during periods of high demand.This approach results in a final operating cost that differs by only 2% from the actual value without considering the predictive approach.This literature review outlines three approaches to designing an EMS responsible for power management and optimal energy dispatch in microgrids: control algorithms based on operational limits, mixed-integer optimization algorithms, and a mixed approach involving prediction techniques.The latter approach holds more potential, but there are fewer contributions in the literature.Additionally, one characteristic of a substantial proportion of previous works is the need for a more extensive discussion about the performance evaluation of the algorithm, for instance, on the resulting time series.Consequently, other evaluation metrics, such as the percentage of integration of renewable sources and BESSs, the net present cost, and the operating cost, must be used.These metrics are crucial for selecting between algorithms within a real-world scenario.
In this context, this study proposes an energy dispatch strategy for a microgrid located in Cuenca, Ecuador.The proposal involves an algorithm that predicts photovoltaic power and energy demand over a time interval of a single sample (T = 1 h).A mixed-integer optimization problem processes this information, yielding the optimal power contributions of each component within the microgrid.Simultaneously, the current photovoltaic power and load demand are introduced into the optimization problem, and by comparing the results from the current and one-sample-forward predicted values, the algorithm corrects (if necessary) the operational state of the BESS.In objective terms, the proposed EMS seeks to integrate all microgrid equipment to optimally and synergistically satisfy demand.This approach focuses mainly on greater integration of the BESS to minimize the impact on the useful life of the equipment and avoid significant increases in the operating costs of the system as a whole.
The contributions of this study are as follows: • An optimal energy dispatch strategy based on predictions using LSTM neural networks and mixed-integer optimization.

•
A technical and financial analysis of the proposed dispatch algorithm.

•
A comparison of the proposed technique with the three management approaches reported in [5,14,15].
This document is structured as follows.Section 2 presents the methodology for the development of an optimal and predictive EMS.Section 3 describes the results of the time series obtained, the technical and financial analysis, and the comparison with the other three EMSs.Section 4 presents the conclusions of this work.

Methodology
Figure 1 shows the operation of the proposed EMS.Three crucial components are discernible: the predictor, the optimization problem, and the decision algorithm.The following sections discuss each of these components.The case study for this work corresponds to the microgrid of the University of Cuenca, Ecuador, located at geographical coordinates (−2.891, −79.036).This system is interconnected with the electrical distribution grid and incorporates clean generation technologies, including photovoltaic, wind, hydro, and hydrogen cells.The microgrid operates in gridconnected mode, using the power obtained from the primary grid and photovoltaic panels, which have an installed capacity of 35 kW [23].Regarding the storage system, various types of batteries with different operating principles are present, including vanadium flow, leadacid, supercapacitors, and lithium-ion.Lithium-ion batteries with a capacity of 44 kWh are used for this study.Figure 2 describes the configuration of this microgrid, illustrating the feasible power flows within the system with arrows.Specifically, solar energy captured by the PV array is converted into photovoltaic power through power converters.Conversely, the maximum power point tracking (MPPT) algorithm governs the power converter to identify the optimal power point of the PV system.The point of common coupling (PCC) facilitates power exchange between the microgrid and the primary grid.

Description of the Simulation Scenario
The proposed EMS is implemented and validated using MATLAB and HOMER Pro to obtain the microgrid time series over one year, with a time step of 1 h, equivalent to 8760 samples.The economic and technical impact of using the proposed EMS is also analyzed.Finally, for the simulation, the consumption profile recorded over the 31 days of March 2022 is used, with an average power consumption of 8.33 kW and a peak power of 26.63 kW.This profile includes the energy used to charge electric vehicles, the energy supply to a data center, tests with programmable loads, and the building lighting system.
The simulation scenario developed for this work involves interconnecting MATLAB and HOMER Pro. Figure 3 shows the information flow between the two simulation platforms.
It is essential to identify the functions through which the exchange of information between the two simulation platforms is carried out.In this context, three functions stand out that run in different MATLAB m-files:

Forecast of the Photovoltaic Power and Load Consumption
The data related to the time series of photovoltaic power and consumption load were collected with a sampling period of 1 min over the 31 days of March 2022, resulting in a total of 44,640 observations.However, as is common in any time series, there are missing data associated with instrument failure or disconnections.Therefore, these missing data are imputed through the mean values in similar time intervals, as indicated in [24].
Since the microgrid simulation in HOMER Pro is performed with a sampling time of 1 h, the entire design of the EMS in this work is restricted to this time interval.Consequently, to maintain consistency with this restriction and the time horizon associated with the predictor system, the data set is reduced so that the value for a specific hour is the average of the 60 min comprising that hour.In this way, the predictor is trained with data separated by one-hour time intervals, allowing predictions with this step size.
Two LSTM neural networks are used for the prediction system to forecast load and photovoltaic power over a time horizon of one hour (1 sample).Reference [24] discusses the network structure in detail, which consists of an input layer followed by an LSTM layer with 200 hidden units.To prevent network overtraining, a dropout layer with a dropout rate of 0.5 is connected to the output, which connects to a fully connected layer.Finally, a layer regression performs the prediction and calculates the root-mean-square error (RMSE).The advantage of this network lies in its continual learning ability, which means that each time it makes a prediction, it updates the states of the memory and forgetfulness cells.
For the training of the LSTM networks, real photovoltaic power and load data are used, extracted from the microgrid under study.Thus, 70% of the total data set (44,640 samples) is used for the network training process, whereas the remaining 30% is used as a validation set to determine the quality of the forecast.In this context, the training phase is carried out with the help of the MATLAB Deep Learning Toolbox, using the training options shown in Table 1.The LSTM network yielded an RMSE of 0.199 with a training time of 950 s for the photovoltaic power adjustment process, whereas for the load prediction, an RMSE of 0.588 kW and a time of 975 s were obtained.The validation process evaluates the quality of the predictions using a closed-loop system.In other words, the network states are initialized with 1440 data from the validation set (equivalent to 1 full day), and the remaining data in the set are predicted from this information.Consequently, the validation predictions reached an RMSE of 0.3189 kW for the PV power and 0.6170 kW for the demand.Figure 4 depicts the data adjustment of the predictor, and although there are slight differences between the actual data and the forecast, the trend is similar.Furthermore, although the predictor operates within the proposed EMS, the LSTM network will constantly update its memory states for each prediction.When evaluating the forecast quality through the simulation in HOMER Pro, the RMSE for the prediction signals was 1.7251 kW for the PV and 3.111 kW for the load, which are good results considering the high stochastic behavior of these variables.As shown in Figure 5, the predictions made on a 1 h prediction horizon reasonably follow the trend of the time series.Consequently, they can provide a good approximation of the total available power at the upcoming sampling time.In particular, the increase in the RMSE of these predictions compared to the validation is more pronounced because the prediction covers an entire year, as opposed to the two days used to validate the model; in addition, it should be considered that HOMER generates synthetic simulation data, which can influence the forecasts of the LSTM network.

Mixed-Integer Linear Optimization Problem Formulation
To ensure an optimal power contribution from each element of the microgrid, based on the approach proposed by [5], we formulate a mixed-integer linear optimization problem.It involves guaranteeing that the demand will be met while selecting the operational state of the batteries (integer decision variable) with the lowest possible cost while adhering to constraints that ensure operation within allowed limits.The following is an explanation of each equation in the model.

Objective Function
The objective function models the total dispatch cost at a specific time.In this context, it comprises several terms associated with the cost of the power generated by a given equipment.For our microgrid, the objective function must consider the cost of purchasing power from the public grid (C grid ) and the cost of using the battery system (C BESS ch ,C BESS dch ).However, the objective function does not consider PV power because it is a variable that is available in one way or another at all times.In this context, the function to be minimized is as follows: where P s , P BESS ch , and P BESS disch are the power purchased from the grid, the charging power of the batteries, and the discharging power of the batteries, respectively.The parameter ∆t represents the time step between two sampling times, which, for this work, is 1 h.

Active Power Balance Constraint
An active power balance ensures that the supplied power precisely matches the demand.In this scenario, the power of the generation units within the microgrid that contribute to meeting demand must be equal to the load.Additionally, power that cannot be stored or used to satisfy demand at a given time must be sold to the public grid and can be treated as consumed power.The energy balance constraint is as follows: incorporating three additional variables into the problem: photovoltaic power (P PV ), load power (P L ), and power sold to the grid (P V ).

Battery System Model
This model accounts for the SoC and prevents operation near limits that could affect the equipment's lifespan.It is an energy balance equation where the charging energy equals the discharging energy, and an optional self-discharge term can be included if necessary.This operational constraint is expressed by Equation ( 3): where E AE t−1 is the battery SoC at the previous time step, and E AE t is the SoC at the current moment, resulting from the charging or discharging process.The coefficients α and β correspond to the battery's storage efficiency and self-discharge rate, respectively.

Operational Limits Constraints
To ensure that the supply or storage power of the batteries does not exceed the permitted limits of the equipment, the constraints in Equations ( 4) and ( 5) are incorporated.These equations include an additional term, e AE , a binary variable that selects the operational state of the BESS, where 1 represents discharging and 0 represents charging.Furthermore, P BESS dischmax and P BESS chmax represent the maximum discharging and charging powers that the BESS can handle.
e AE ∈ {0, 1}∀t A similar situation occurs with the battery SoC.Due to usage recommendations, the operational range cannot fall below 10% or exceed 90% of the maximum capacity of the BESS.Therefore, Constraint (7) is used.Here, E AE and E AE denote the operational limits of the SoC.
Lastly, to reduce consumption of the primary grid, Equation ( 8) is introduced to constrain the purchase of energy from the interconnected system while maintaining the capacity to supply demand during high consumption hours and when there is low availability of renewable resources.This set point is established based on the approach presented in [14], which defines the maximum penetration of the backup unit considering the maximum power it can provide and a critical operating point.
In this case study, the maximum power extractable from the grid is 400 kW, whereas the maximum load is 26.63 kW.Assuming that this consumption occurs at a critical moment when renewable resources are unavailable and the battery does not allow discharging, the grid must meet this demand, representing only 7% of its capacity.In this context, the limit γ is set at 0.07 of the maximum power extractable from the grid (P grid ).

Decision Algorithm
Directly using the optimization problem leads to at least two fundamental issues.Since this approach only considers current time instances, selling excess power becomes inevitable.However, this surplus power could be used in future instances when a deficit occurs.Another occurrence arises when the grid tariffs are flat and there is no peak-hour consumption penalty.This results in the problem of satisfying the demand only from the grid and selling renewable power in all instances (this is the current operational scenario of the microgrid studied due to the regulatory framework of Ecuador).
In light of the issues discussed, Figure 6 introduces the algorithm that integrates prediction, optimization, and BESS state correction components.Based on the previous ideas, this algorithm is a significant advancement in addressing the challenges in microgrid operation.The proposed EMS initially samples the charging, photovoltaic, and available energy in the BESS.The first two data inputs are fed into the LSTM networks, generating forecasts for these variables over a 1 h prediction horizon.The optimization problem solves two scenarios: one related to the current time (t) and the other to the next time instance (t + 1).Furthermore, the discharging cost is negative, to encourage battery integration for meeting demand.The overall approach to solving these two problems is to prevent power sales when they are detrimental to the system's health or cause unnecessary discharging of the BESS at the current time.Therefore, the algorithm integrates two redundant conditions to ensure that energy delivery at time t is not unnecessary at t + 1.In this context, Condition 1 verifies whether the power purchased from the grid at t + 1 is more significant than that at t if the distribution at t occurs during hours of sunlight.If this resource is greater than the load, it is because if at t, the resource is not sufficient to meet the demand, it is not convenient to store it for the next instance.The second condition checks whether the battery's SoC allows storage.If both of these conditions are met, the battery's SoC is enforced by setting its cost as unfavorable and propagating this solution into the system.Otherwise, the solution calculated at the beginning of the algorithm is retained.

Peak-Shaving Algorithm (Complementary)
Although the peak-shaving algorithm is not part of the proposal in this work, it is used as a contrasting case to assess the effectiveness of the proposed algorithm.Therefore, a brief description of this approach is provided to enhance the reader's understanding.In the following, the peak-shaving approach is presented, as described in [14,15].
The algorithm proposed by [14,15] consists of a series of nested conditionals that define different operational modes based on the availability of the photovoltaic resource.Consequently, the first condition verifies the presence of photovoltaic power.Two macroscenarios are defined: one where the sun is absent and the photovoltaic energy is zero, and another where the system operates during sunlight hours.
In this context, each scenario has its own control algorithm.Thus, the nighttime operation of the system is regulated by the five operating modes described below.
• Mode 1: Operation of the system when the load power is greater than the optimal microgrid generation (P L > γP grid ) and the state of charge of the battery is above its minimum value (Soc > SoC min ).Thus, the demand is met through the discharge of the BESS and the main grid, and it is governed by the active power balance given by Equation ( 9): • Mode 2: This mode operates when the load is greater than the optimal operating point of the microgrid but less than the sum of the extractable power from the grid and the BESS (γP grid ≥ P L > P grid + P BESS dischmax ).In this context, the grid fully satisfies the demand while also charging the batteries, thereby governing the power balance through Equation ( 10): • Mode 3: This mode is activated when the load power exceeds the combined power of the grid and the battery bank but is less than the extractable capacity from the grid (P grid + P BESS dischmax ≥ P L > P grid ), and the state of charge of the BESS is above its minimum level.The demand power is solely covered through the discharge of the storage system, and dispatch is carried out using Equation (11): • Mode 4: This scenario occurs when the sum of the consumption power and the power needed to charge the battery is less than the extractable power from the grid (P BESS chmax + P L < P grid ).Thus, if the state of charge of the battery is below its maximum limit, the power balance equation is given by ( 12): • Mode 5: This operating mode defines a state of inactivity for the BESS equipment.It is activated when the sum of the consumption power plus the battery charging power exceeds the maximum extractable power from the main distribution grid.Additionally, the BESS does not allow discharge, as it is below its minimum operating level.Thus, the microgrid solely meets the demand, and the battery remains resting.The equation for the active power balance is reduced to (13): Under the assumption that the renewable resource is available, the algorithm undergoes a complete change and is governed by three distinct operating modes that consider the power of the photovoltaic renewable resource.Here is a brief description of the operating modes: • Mode 1: In this operating mode, if the load (P L ) is greater than the optimal operating point of the microgrid plus the available photovoltaic power (γP grid + P PV ), it is assumed that the renewable resource is not sufficient to meet demand.Therefore, the battery power is integrated, and it is verified whether this can cover demand.If possible, demand is fulfilled using these two components, as indicated by the energy balance in Equation ( 14): On the other hand, if these two resources are not sufficient, demand is met through the integration of the main distribution system, and it is governed by Equation ( 15): • Mode 2: In this scenario, it is verified whether it is possible to meet demand using renewable resources.Otherwise, if the SoC of the battery allows discharge, the battery power is integrated into the dispatch.If demand can still not be fully met, the remaining part is supplied through the integration of the main distribution system.
The following three conditions govern this mode: 1. P PV ≥ P L , with the power balance given by Equation ( 16): P PV < P L && SoC > SoC min && P PV + P BESS dischmax ≥ P L .In this scenario, energy dispatch occurs through the active power balance described by Equation ( 17): P PV + P BESS dischmax ≤ P L .This case is satisfied by the active power balance given by Equation ( 18): • Mode 3: This mode is activated when the photovoltaic solar resource exceeds the load (P PV ≥ P L ) and the battery SoC is below its maximum.In this scenario, the surplus photovoltaic power charges the BESS.Thus, the power balance adheres to Equation ( 19): Power dispatch is executed to fulfill demand by verifying each condition defining the operational modes.Dispatch revolves around a straightforward power balance.Consequently, the comparison algorithm utilized in our work combines these operating modes, optimizing resources via a mixed-integer optimization model.This model considers the energy balance of the batteries, their operational constraints, and the costs associated with purchasing energy from the main distribution grid.

Results and Discussion
This section presents the simulation results related to the proposed dispatch technique, along with a comparison with the techniques reported in [5,14].Initially, the proposed algorithm is evaluated through an analysis of time series.For a clear visualization, a small portion of this series is presented, demonstrating various scenarios regarding resource availability.Two specific days were selected: 3 July, where photovoltaic power significantly exceeds the load, and 4 July, where the opposite occurs.These distinctions are visible in Figure 7. Figure 7a illustrates how the dispatch algorithm schedules battery charging during the early hours when consumption and rates are typically low.It is noteworthy that on 3 July, battery charging occurs during high renewable resource availability periods, almost obviating the need for purchasing electrical energy.Consequently, the surplus can be injected into the distribution system.On the other hand, when solar power decreases toward the end of the day, the integration of the BESS is evident.It discharges without dropping below its lower limit, providing the system with a continuous autonomy of 7 h, during which integration with the primary distribution system is practically negligible.Finally, the grid system covers the supply when the battery reaches its minimum charge, at around 21:00 h.
On the other hand, in more critical scenarios, where the load exceeds the renewable resource, such as on July 4th, the predictive effect of the algorithm becomes evident.From 11:00 AM to 1:00 PM, the battery can meet demand or sell surplus renewable resources.However, its discharge is extended until after 2:00 PM, when solar power is significantly lower than in earlier periods.Unlike the previous day, the penetration of the BESS is much lower.However, it is essential to consider the insufficient photovoltaic power available for storage or extend the use of BESS beyond four consecutive hours of microgrid autonomy.
Figure 7b shows the results of the technique based on solving a mixed-integer optimization problem for each time instance.The primary characteristic of this dispatch is the repetitive battery charging and discharging processes, with or without the presence of renewable resources.This cyclical operation has two critical consequences: a severe impact on the useful life of the BESS due to cyclic stress and significant power wastage through energy purchases, even when solar resource availability could fully meet the system's requirements during specific time intervals.It is also noticeable that during the period with the highest availability of renewable resources, the amount of energy stored is significantly lower in the optimization-based approach than in our proposed algorithm.This discrepancy arises because the optimization approach prioritizes the sale of power over storage for subsequent instances.The power-sold diagrams reflect this distinction: in the proposed algorithm, the sale occurs only when there is an excess of solar resources, whereas in the optimization approach, all surplus power, both from batteries and photovoltaic panels, is sold.
Figure 7c shows the time series using the approach presented in [14].Observing how the BESS loses integration within the microgrid, where the photovoltaic power and the primary distribution system cover demand, is intriguing.This decision is linked to the inherent nature of the algorithm, as it is a dispatch solely based on predefined scenarios to cover demand using the optimal combination of elements (PV, BESS, or the grid).As a result, the relevant options tend to be grid or photovoltaic power, except in particular situations where the load exceeds the capacity, leading to a marginal involvement of the BESS.In conclusion, this algorithm must precisely configure its set points to achieve higher penetration of renewable resources and fully utilize the BESS.However, defining specific operational points becomes challenging in the studied scenario, where the load exhibits significant variability.The many possible scenarios constitute a combinatorial problem that could overlook certain situations, leading to ill-timed dispatch decisions.
Lastly, Figure 7d illustrates the fourth comparative case, the original development of the authors.It combines the optimization-based algorithm with the condition-based one.The distinction lies in that once the current scenario is determined along with the state of the BESS, the set points for each device are calculated by solving the optimization problem.The notable feature of this algorithm is the charging of the BESS during periods of scarcity of renewable resources, resulting in increased energy purchases from the primary distribution system.
Figure 8 shows the time series related to the SoC of the energy storage system and the sales made to the grid system.In the case of the EMS solely based on an optimization algorithm, it becomes apparent that the energy stored at a given time is immediately sold in the subsequent period.This action has a pronounced impact on the SoC because of the constant cycling of the battery.On the other hand, the peak-shaving algorithm has a lesser impact on the SoC, as it only utilizes the available energy in the BESS in specific consumption circumstances.In contrast, optimal algorithms that focus on peak shaving and prediction (proposed in this work) have a lower impact on the SoC.They perform a controlled charging and discharging process that aligns with periods of high solar availability (charging) and resource scarcity (discharging).Furthermore, the sale of energy only occurs when charging the battery or meeting the demand is not feasible.However, performance over only two days is not representative of behavior over an entire year.Therefore, Figure 9 shows heat maps illustrating the frequency with which the battery engages in dispatch decisions, considering the demand and available solar resources throughout the year (8760 h).Thus, the proposed algorithm uses the battery approximately 59% of the time to meet demand when the renewable resource is scarce.On the other hand, about 24% of the time is allocated to storage, specifically when there is a surplus of renewable energy compared to consumption.About 6% of the time, the battery engages in storage processes even when the solar resource is lower than the load.This action is directly related to the predictive component of the algorithm.In such cases, the algorithm predicts that the power purchased in time t + 1 would be higher than that purchased in time t, prompting the choice of battery charging.Finally, 11% of the time, the BESS goes into idle mode because it reaches the maximum SoC limits.Therefore, it does not allow charging or discharging under any consumption condition.When these metrics are compared with those of other algorithms, it becomes evident that the proposed algorithm achieves a higher effective integration of the batteries.For example, in algorithms with a focus on peak shaving, batteries spend more time in a resting state than contributing to demand coverage, accounting for only 18% of the time.
Regarding the pure optimization approach, batteries achieve 78% integration in energy management, which might initially seem significant.However, upon closer examination, charging and discharging processes are prioritized when renewable resources are scarce.This management results in additional power purchases from the grid, which is more impractical, as the same power is immediately dispatched in the subsequent instance.
Another result of interest is the amount of CO 2 emissions generated by each EMS when operating the microgrid.Figure 10 shows that the proposed technique produces 25,766 kg/year of CO 2 , demonstrating a reduction of at least 3. 58% compared to the peakshaving approaches and an impressive 33% compared to the pure optimization approach.These results highlight the importance and effectiveness of the proposed technique in minimizing greenhouse gas emissions.This achievement can be directly attributed to the planned storage of excess photovoltaic energy, which was made possible by the accurate forecast provided by the LSTM network.The ability to anticipate and efficiently manage surplus energy contributes to the reduction in CO 2 emissions, thus consolidating the environmental and sustainable relevance of the proposed approach.In a different context, the economic aspect is also crucial for evaluating the performance of an algorithm.Therefore, Figures 11 and 12 depict the amount of energy purchased and sold to the primary grid compared to the penetration level of renewable sources in each algorithm.In this context, the proposed algorithm achieves 52% integration of photovoltaic energy, with a purchase of 40,770 kWh/year and sales of 11,435 kWh/year.Therefore, it is the algorithm with the second-highest integration of renewable energy, surpassed only by the peak-shaving approach, which achieves 62% renewable integration.However, it is essential to note that the integration percentage is measured on the basis of the load covered solely by renewable energy, without considering the BESS.As a result, since photovoltaic power is surplus most of the time it is available, the load is covered by this energy, and the remaining power is sold to the grid.However, according to the time-series data, it is evident that these two metrics favor the algorithm with low battery usage.Despite this, with greater integration of the BESS, the proposed algorithm only results in about a 20% increase in purchases from the grid.Furthermore, there is no dispatching of excess solar energy, as most of it is stored to meet demand in other instances, consequently leading to a considerable reduction in energy sold to the grid.In Figure 13, the annual operating cost is compared to each algorithm's renewable penetration level.In this aspect, it becomes evident that the operating cost decreases as the percentage of renewable penetration increases.Consequently, the proposed algorithm has the third-lowest operating cost at USD 17,149, which is 22% higher than that of the peak-shaving algorithms.It is essential to note that this cost difference is insignificant when considering two crucial factors: the significantly higher integration of batteries in the proposed algorithm compared to the other three techniques, and due to this higher integration, the use, maintenance, and replacement of the BESS contribute to the operating cost of the microgrid.Additionally, the peak-shaving algorithm does not consider the self-discharge effect of the battery.According to the heat map presented in Figure 9, the BESS can retain stored energy for several days without loss, which is unrealistic and could lead to significant power losses.Consequently, the benefit of this EMS for the microgrid in this case study translates into a greater integration of the storage system into energy management without exerting an aggressive impact on its charging cycle, which reduces the costs associated with the operation, maintenance, and replacement of the BESS.On the other hand, it is crucial to consider the poor economic performance of the approach based on pure optimization.This performance can be explained by the formulation that seeks to keep the SoC within its operating limits while supplying demand using all possible energy sources.However, this strategy charges the battery at any time, even when the renewable resource is unavailable.As a result, in addition to meeting demand, the grid must satisfy the battery requirement, generating unnecessary additional costs.Then, when the battery has sufficient energy, it is discharged to meet demand or delivered to the grid as surplus power.This constant loop of unnecessary charging and discharging results in an increase in the operating cost of the system, as it implies a continuous and inopportune use of the batteries at all times.
Figure 14 summarizes the costs associated with the capital, replacement, operation, and benefits of the microgrid components, considering a project duration of 25 years.The capital cost is identical in all four cases since the microgrid uses the same architecture.The BESS represents a significant investment at a cost of USD 13,200, together with the photovoltaic panels costing USD 87,500.However, there is a significant expenditure in terms of replacement costs, mainly generated by the BESS.As observed, with the optimization algorithm, the continuous and cyclical action of the state of charge results in significant battery degradation.Consequently, its replacement occurs more frequently compared to the other algorithms.There is also the opposite scenario where the battery has minimal influence on the system, as seen in the case of the peak-shaving algorithm, leading to a relatively low replacement cost for the equipment.The optimal peak-shaving algorithm and the proposed EMS maintain average and similar costs.However, it should be remembered that the proposed algorithm integrates the BESS to a greater extent than the peak-shaving algorithm.The proposed algorithm involves more battery usage to meet demand while preserving the useful life of the equipment.
Regarding operating costs, it is evident that the grid plays a significant role within the system in all four scenarios.This behavior is expected due to the nature of the resource.The grid comes into play when the renewable resource is at its minimum and the energy stored in the BESS is insufficient to meet the demand.
The global summary in Table 2 presents an exhaustive comparison of all metrics evaluated in the four algorithms presented.The proposed algorithm stands out for solidly integrating the operation of the BESS, reaching 59% efficiency, with an increase of only 22% in operating and replacement costs compared to other available techniques.It is crucial to note that given the growing concern about the decarbonization of the planet, the predictive optimization approach achieves a notable reduction of 4% in CO 2 emissions.This balance between efficiency, operability, and sustainability positions the proposal as a valuable option in the search for advanced energy solutions.The proposed EMS is easily scalable and adaptable to different microgrid topologies.For example, when considering a possible expansion plan that involves integrating loads, storage systems, and RES, the formulation of the optimization problem can be adjusted.In this process, the costs associated with using the new equipment can be incorporated, and the new energy contributions can be added to the active power balance, considering the operating limits of the BESS.Regarding prediction systems, it is possible to implement a single neural network with as many inputs as there are variables to be predicted.This approach allows for the use of a deep learning network to identify relationships between different variables and improve the quality of forecasts.
However, in the decision-making algorithm, the total power of all renewable energy sources is considered, as well as the SoCs of all storage systems.When integrating new components into the algorithm, a systematic approach should be taken to ensure computational efficiency, involving adjusting the optimization problem and training the neural network with the new information available while maintaining the capacity to efficiently solve the problem.Furthermore, to obtain a quality forecast, it is essential to have a sufficiently extensive data history specifically for the geographical area of the microgrid.

Conclusions
In this study, an EMS was successfully developed for a microgrid using an optimization approach based on the prediction of load and photovoltaic power within a time horizon of one hour.An analysis of the time-series data demonstrates that the proposed algorithm achieves greater integration of the BESS, which accounts for 59% of the demand coverage.It effectively utilizes surplus renewable resources by storing energy for dispatch during more opportune times (when solar resources are scarce).Despite the continuous charge and discharge cycles of the algorithm, its impact on the storage system's lifespan is minimal, resulting in reasonable replacement costs compared to those of a pure optimization approach.

Figure 1 .
Figure 1. Outline of proposed EMS.2.1.Description of the Simulation Scenario with a Focus on the Case Study 2.1.1.Description of the Microgrid

Figure 4 .
Figure 4. Comparison of real data and predictions through an LSTM neural network with the validation data set.

Figure 5 .
Figure 5.Comparison of real data and predictions through an LSTM neural network in the HOMER simulation.

Figure 6 .
Figure 6.Flowchart of the proposed EMS algorithm.

Figure 7 .
Figure 7.A segment of the time series: (a) Dispatch based on the proposed optimization and forecasting algorithm, (b) dispatch algorithm with a pure optimization approach, (c) dispatch algorithm with the peak-shaving technique, and (d) dispatch algorithm based on a combination of the peak-shaving technique and the optimization scenario.

Figure 8 .
Figure 8.Time series related to SoC and energy selling to the primary distribution grid.

Figure 10 .
Figure 10.Amount of CO 2 emissions from each EMS.

Figure 12 .
Figure 12.Total energy supplied to the main grid for each dispatch technique.

Figure 13 .
Figure 13.Operating cost of each dispatch technique.

Figure 14 .
Figure 14.Summary of costs generated by the microgrid components.
Total purchased energy from the main grid for each dispatch technique.

Table 2 .
Comparison of the evaluation metrics obtained in the four dispatch algorithms.
C BESS disch Rate per Kw/h for discharging the battery P s Power supplied from the grid P grid Maximum extractable power from the main distribution grid P BESS ch Battery state of charge at the current time step P BESS dischmax Battery's maximum discharging power P BESS chmax Battery's maximum charging power e AE Binary variable that selects the operational state of the BESS E AE Battery's minimum state of charge E AE Battery's maximum state of charge γ Coefficient limiting the maximum extractable power from the main distribution grid