This paper introduces a novel hybrid artificial intelligence technique designed to enhance energy management efficiency within smart grids, with a particular focus on Renewable Energy Communities (RECs). The proposed approach integrates a Time Delay Neural Network (TDNN) to predict upcoming values of key energy variables in the community. These forecasts then feed into a stochastic Model Predictive Control (MPC) framework, which optimizes community operations through effective Battery Energy Storage System (BESS) control strategies. Evaluation on a public dataset for a 24-hour prediction horizon yielded Mean Absolute Errors (MAE) of 1.60 kW for photovoltaic generation, 2.15 kW for total energy consumption, and 0.30 kW for common services. When integrated with stochastic MPC, these predictions yielded higher financial returns than those from alternative methods. Overall, the system increased total income by 18.72% compared to the same management approach without any forecasting component.
The 2015 Paris Agreement established the target of achieving carbon neutrality by 2050. This objective requires substantial reductions in energy use across human activities and in greenhouse gas emissions [1]. Meeting this challenge demands major transformations in existing power systems, including raising the share of Renewable Energy Sources (RES) in electricity supply to 40% by 2040 [2].
In response, the European Commission introduced the concept of Renewable Energy Communities (RECs) through legislation, defining them as specialized forms of smart grids [3]. RECs enable the generation, storage, sale, and internal sharing of renewable energy among members while using the public grid for distribution. Their primary purpose is to deliver economic, environmental, and social benefits to the surrounding community [4]. Members typically include households, small- and medium-sized enterprises, and local public bodies. Under Italian regulations [5], RECs are subject to a 1000 kW peak capacity limit for RES installations, with all participants connected to the same HV/MV substation. Consequently, effective optimization of REC operations directly enhances the community’s overall economic, environmental, and social outcomes.
Driven by these priorities, recent research has concentrated on developing advanced techniques for forecasting and managing electrical energy parameters. In REC contexts, forecasting typically addresses renewable generation [6] and power demand [7]. The objective is to anticipate future values in time series data. For REC applications, optimal control strategies commonly employ sequence-to-sequence forecasting, where the model processes input sequences of one or more variables and produces corresponding output sequences.
Predictive models are generally classified as black-box, white-box, or gray-box [8]. Black-box models rely on Artificial Intelligence (AI) to map input features directly to optimal decisions [9], often using Convolutional Neural Networks (CNNs) or Long Short-Term Memory (LSTM). White-box models depend on physics-based equations [10], while gray-box models combine elements of both [11].
In energy system management, control strategies are applied to either Energy Storage Systems (ESS) or flexible (deferrable) loads. For ESS, such as batteries, the focus is on optimizing economic performance [12] or minimizing emissions [13]. For deferrable loads, the goal is often peak shaving, for instance, through optimized electric vehicle charging schedules [14].
In this study, we propose a Time Delay Neural Network (TDNN) for sequence-to-sequence forecasting of energy-related time series in RECs, covering prediction horizons ranging from 15 minutes to 24 hours. The TDNN demonstrates state-of-the-art accuracy, achieving lower MAE values than the benchmark models we developed and tuned on the identical dataset. Predictions generated by these models are subsequently input into a newly developed stochastic MPC algorithm. This controller maximizes REC income by using the BESS to mitigate forecasting inaccuracies. We show that the stochastic variant outperforms its deterministic counterpart.
We term this a hybrid method because it combines a black-box AI forecasting component with a white-box mathematical optimization model. Application of the proposed hybrid approach raised income by 18.72% relative to a baseline that treats current load and generation measurements as perfect future predictions. The achieved income represents 96.73% of the theoretical maximum possible under ideal (zero-error) forecasts.
The primary contributions of this work are:
· A novel hybrid AI technique for effectively handling uncertainties in user demand and photovoltaic generation within RECs;
· Design and fine-tuning of a TDNN capable of forecasting key electrical energy time series in RECs: common services consumption, aggregated load, and Photovoltaic (PV) generation;
· Comparative performance evaluation of the TDNN against leading contemporary energy forecasting techniques;
· Creation of a stochastic MPC algorithm for optimal REC management that incorporates ESS;
· Examination of the ideal prediction horizon length (PH) for input to the MPC to maximize community income;
· Detailed analysis of the relationship between forecast accuracy and the resulting economic performance of the REC.
A Renewable Energy Community is a legal structure established by European legislation that functions as a distinct type of smart grid [3]. Energy management in smart grids involves integrated strategies to boost performance, efficiency, and resource utilization within the community [8]. In recent years, significant research has focused on sustainable solutions, spanning predictive systems for future energy demand and generation to optimization techniques for managing resources in buildings equipped with RES [8].
Forecasting efforts in electrical energy systems primarily target two variables: energy demand and RES generation. Within RECs, demand refers to the total electricity consumed by participants, while generation denotes the renewable energy produced.
Koprinska et al. [15] developed a CNN model to forecast electricity load and solar PV output using two years of historical data from public Australian, Spanish, and Portuguese datasets [16]. CNNs excel at automatically detecting recurring patterns. The authors tested multiple architectures and hyperparameters, ultimately selecting the top-performing configuration per dataset. The optimal models featured two convolutional layers (sizes 24 and 10), ReLU activation, 0.5 dropout, and 5×1 kernels. Training employed stochastic gradient descent with the Adam optimizer [17], minimizing mean squared error. For a 24-hour horizon, the MAEs were 114.38 kW (solar), 340.64 kW, 1884.86 kW, and 497.62 kW (load datasets), with corresponding RMSEs of 153.91 kW, 476.9 kW, 2392.88 kW, and 642.52 kW.
Barzola-Monteses et al. [18] applied an LSTM network to predict building energy consumption using two days of data from an institutional building in Ecuador. LSTMs are well-suited for capturing both short- and long-term temporal dependencies. The model used an input window of the most recent 8 normalized timestamps, 6 hidden units with tanh activation, and a linear output layer. It achieved an average MAE of 3.714 kW (RMSE 5.085 kW) on a 5-minute prediction horizon.
Kim and Cho [19] combined CNN and LSTM layers to predict household electricity consumption from the University of California repository dataset spanning two years [20]. The CNN extracts spatial-temporal features, which the LSTM then processes for sequential prediction. The architecture included two convolutional layers (64 filters, 2×1 kernels, ReLU), pooling layers, an LSTM with 64 units, and dense layers (32 and 60 neurons). For a 60-minute horizon, it attained an MAE of 0.3493 kW (MAPE 34.84%, RMSE 0.6114 kW).
Regarding REC management, approaches generally fall into ESS control and/or deferrable load management, which may be centralized or decentralized.
Olivella-Rosell et al. [21] examined a community of 100 homes with individual RES and BESS. A centralized controller coordinated BESS operations to minimize bills under both flexible and non-flexible conditions, demonstrating superior savings compared to purely local control.
Barone et al. [22] described a REC with RES-equipped members and electric vehicles. Optimization focused on maximizing self-consumption of renewable energy through smart metering and controlled EV charging, with payments tied to local renewable utilization.
Van Cutsem et al. [23] proposed a REC without a central aggregator, incorporating RES, BESS, and thermal storage. Members reached consensus on a shared cost function using a blockchain-based distributed algorithm for secure information exchange. The process iteratively refined aggregated demand profiles for the next day while optimizing individual contributions.
Several studies have explored hybrid strategies that leverage AI predictions to improve mathematical optimization models. Wen et al. [24] used a deep neural network with two LSTM layers to forecast hourly residential demand and PV output for 40 buildings in Austin, Texas, based on 1 month of publicly available data [25]. Inputs included scheduling, weather, and temporal variables. The resulting MAEs were 2.365 kW (demand) and 4.369 kW (PV). These forecasts informed a Particle Swarm Optimization routine for microgrid dispatch, reducing daily costs by 8.97% through coordinated EV charging and ESS management under uncertainty.
Arkhangelski et al. [26] tested an Optimized Urban Microgrid Energy Management system on one year of public data from New England [27]. Their LSTM model produced MAEs of 16 kW (demand) and 8 kW (PV) for day-ahead forecasts, which were then used within an energy flow management strategy.
As outlined above, existing literature tends to address either forecasting or management in isolation. This study presents an integrated end-to-end solution in which neural network predictions directly drive an MPC optimizer to maximize the cost function.
Although white-box physics-based models offer strong interpretability, empirical evidence shows that black-box deep learning models often deliver equal or superior forecasting accuracy [28]. Importantly, conventional error metrics (such as MAE) do not always correlate directly with economic benefits [28]. Research indicates that even a 1% improvement in short-term load forecast accuracy can yield annual savings of hundreds of thousands to millions of dollars in unit commitment operations [29].
For this reason, the proposed hybrid AI method not only provides accurate energy-feature predictions but also quantifies the resulting monetary gains from optimal BESS management. MAE values are reported in kW for direct assessment of predictor quality, alongside normalized MAE relative to the test set mean to facilitate generalization across different communities. Notably, employing the BESS to offset forecast errors enhances community revenue without imposing additional stress on the external grid.
The proposed hybrid AI-optimized management technique provides a complete pipeline that accepts historical energy data as input and delivers maximized community income as output. It comprises two core modules: the TDNN forecasting engine and the stochastic MPC optimizer. Recorded energy variables undergo preprocessing before being fed into the TDNN to generate future predictions. These predictions then drive the stochastic MPC, which determines optimal BESS operations and computes the resulting maximum income. Figure 1 illustrates the overall architecture of the method. Subsequent sections provide comprehensive details on the dataset, preprocessing steps, TDNN architecture, and the REC optimization framework.

Figure 1. Overview of the proposed hybrid AI approach for optimal energy management.
The analysis is based on a publicly available dataset [30] covering two years of measurements from 15 consumers connected to a shared photovoltaic (PV) generation system supplying a collective electrical load. Individual consumer demands were aggregated to create a single energy demand variable. Likewise, the PV outputs from all sources were combined into a single total PV production variable. The common services load was kept as a separate feature because of its regular daily pattern.
In addition, each energy time series was paired with two auxiliary numerical variables: one indicating the day of the week (1 to 7) and another representing the time of day in 15-minute intervals (1 to 96). This resulted in three time series, each described by three features. Every feature was independently normalized using z-score standardization.
The dataset is essentially composed of three key variables: aggregated load, total PV production, and common services demand. Accurate forecasting of these variables is essential for effective energy management. To achieve this, we employ a Time Delay Neural Network (TDNN) to predict future values of each variable. TDNNs are well-suited for capturing long-term temporal dependencies and require significantly less training time than recurrent architectures such as LSTMs [31].
We adopt a sequence-to-sequence strategy in which the model receives a sequence of the past N values and outputs a sequence of PH future values, where PH is the chosen prediction horizon in time steps. The day of the week and time of day are also included as inputs.
Formally, after fixing the input window size N, the predicted sequence Fˆ for a feature F at time t on day D can be expressed as a function of the recent history of F, the current day D, and the current timestamp T:
(1)
Here, the input vector F contains the past values [F(t-N+1),…, F(t)], while D and T are single scalar values. In the TDNN architecture, delays are applied only at the input layer (corresponding to the window size N), with no delays in the hidden layer. The output vector 𝐅̂ is then compared against the actual observed values [F(t+1),…, F(t+PH)] to assess performance.
A dedicated model predicts each of the three variables. The TDNN consists of an input layer that accepts a window of size N, a hidden layer with a fixed number of neurons, and an output layer with 96 neurons (one for each 15-minute interval up to 24 hours ahead).
Hyperparameters were first tuned through preliminary experiments: learning rate = 0.01, hyperbolic-tangent-sigmoid activation for all neurons, regularization parameter = 0.3, Scaled Conjugate Gradient backpropagation [32], and a maximum of 5000 epochs. A grid search was then performed over combinations of input window size N and the number of hidden neurons, testing values from {4, 12, 24, 48, 96, 144, 180, 200, 220, 240, 260}. These values were selected based on initial trials and existing literature. The grid search was conducted independently for load, production, and common services.
The two-year dataset was split into a discovery set (the first 50%) and a held-out test set (the remaining 50%). The discovery set was further split into training (80%) and validation (20%) subsets, with shuffling at each epoch. Z-score normalization parameters (mean and standard deviation) were computed exclusively from the training data and applied to all sets.
Models were trained on the training data while monitoring validation performance to enable early stopping and prevent overfitting. The best hyperparameter combination for each model was identified on the validation set. Final performance was evaluated on the test set after retraining on the entire discovery set.
The structure of the considered Renewable Energy Community (REC) is illustrated in Figure 2. The community includes a shared PV power plant and a Battery Energy Storage System (BESS) that serves both residential buildings (community members) and a common load. The connections shown in Figure 2 do not necessarily represent a physical electrical grid; according to the European REDII directive [33], energy sharing within a REC can be performed virtually as long as the renewable sources and consumers are located in the same defined network area. The exact definition of this area remains an open regulatory question.
Under the Italian implementation of the Internal Electricity Market directive [34], all REC resources and participants must be connected to the same HV/MV substation. The configuration in Figure 2 is representative of various real-world cases, such as a condominium with rooftop PV and shared building loads (e.g., lighting), or a neighborhood where a large prosumer (e.g., a shopping center, school, or municipal building) shares its renewable generation with smaller consumers.

Figure 2. REC system architecture.
This subsection outlines the mathematical representations adopted for every element of the system. Throughout the equations below, 𝑡 stands for the discrete time instant, with a fixed sampling interval of 𝛥 = 15 minutes. All power quantities refer to average values measured across each sampling window.
RES. Total renewable energy source output is represented by 𝑃𝑡 𝑟𝑒𝑠, while 𝑃𝑡 𝑐 denotes the curtailed power, which is subject to specific limits.
(2)
Connection with the main grid. In every 15 minutes 𝑡, the REC collective facility may either export power 𝑃𝑡𝑒 to or import power 𝑃𝑡𝑖 from the external network. The relation captures this:
(3)
(4)
(5)
Where 𝑃𝑡𝑔 indicates the net power flow between the REC and the grid, 𝑃𝑚𝑎𝑥𝑔 defines the highest permissible exchange level, and 𝛿𝑡𝑔 is a binary decision variable that signals import (𝛿𝑡𝑔 = 1) or export (𝛿𝑡𝑔 = 0) mode.
BESS. The battery storage unit can draw power to raise its state of charge or supply power to reduce it. The SoC progression is modeled by:
(6)
(7)
In which SoC t represents the instantaneous state of charge, η ch and η dsc denote the respective charging and discharging efficiency factors, and E is the battery’s rated energy capacity. Operational boundaries are enforced through these constraints:
(8)
(9)
Where 𝑃𝑚𝑎𝑥𝑐ℎ ≥ 0 is the upper charging power limit, 𝑃𝑚𝑖𝑛 𝑑𝑠𝑐 ≤ 0 is the highest allowable discharge magnitude, and 𝛿𝑡𝑐ℎ, 𝛿𝑡𝑑𝑠𝑐 serve as binary indicators. To avoid excessive cycling, the model includes:
(10)
(11)
With 𝛼 being a predefined scalar that sets the maximum permitted charge/discharge cycles within the prediction horizon (PH).
REC. Following the Italian adoption of the European IEM [34] and RED II [33] directives, renewable energy communities earn standard market revenue for energy fed into the grid plus additional compensation for internally shared renewable energy. As illustrated in Figure 2, shared energy refers to the fraction of exported renewable output (𝛥 ⋅ 𝑃𝑡𝑒) that is simultaneously absorbed by community participants. Formally, the shared energy 𝛥 ⋅ 𝑃𝑡𝑠ℎ at interval 𝑡 equals the lesser of the exported energy and the members’ consumption. This leads to:
(12)
Where 𝑃𝑡𝑠ℎ is the shared power, and 𝑃𝑡𝑙 is the anticipated total load of the residential units at moment 𝑡. Because the economic performance metric [18] encourages maximization of both shared and exported energy, Eq. 12 can be equivalently expressed via the inequalities:
(13)
(14)
(15)
Moreover, battery charging within the REC must rely exclusively on renewable generation, imposing the requirement:
(16)
With 𝑃𝑡𝑟𝑒𝑠 as the predicted renewable generation in that quarter-hour.
Power balance, operational costs, and available data.
System operation must continuously satisfy the power balance:
(17)
Where 𝑃𝑡𝑐𝑙 stands for the forecasted common load demand during interval 𝑡, the overall REC economic benefit is given by:
(18)
Here, 𝑐𝑡𝑒 is the remuneration price for sold energy, 𝑐𝑐 penalizes wasted renewable production, 𝑐𝑚 is the support tariff granted by the Italian Ministry of Economic Development (MISE), and 𝑐𝑟 reimburses network charges avoided by shared consumption [5]. The primary aim of the study is to maximize this economic return. At every instant 𝑡, over a rolling prediction window of length 𝑃𝐻, the following inputs are considered known:
• Predicted RES generation trajectory {𝑃𝑡𝑟𝑒𝑠+𝑘} from 𝑘=0 to 𝑃𝐻−1 along with the corresponding forecast error variance vector {𝜓𝑡𝑟𝑒𝑠+𝑘};
• Predicted profiles for common load {𝑃𝑡𝑐𝑙+𝑘} and member load {𝑃𝑡𝑙+𝑘}, each paired with their forecast error variance vectors;
• Present battery SoC value 𝑆𝑜𝐶𝑡;
• Energy price forecasts covering the interval from 𝑡 to 𝑡 + 𝑃𝐻 − 1.
This section describes two distinct strategies for optimal REC operation. Both approaches rely on the data outlined previously. The control sequence executed at time 𝑡 is depicted in Figure 3: a neural network first ingests recent sensor readings from the REC assets and produces refreshed forecasts. These forecasts, the current SoC, and the price schedule are then fed into an MPC optimizer that calculates the required BESS power references 𝑃𝑡𝑐ℎ and 𝑃𝑡𝑖.
The initial strategy applies deterministic model predictive control, which ignores forecast uncertainty within the optimization. Any real-time mismatches are corrected on the fly by modulating the exchange with the main grid. The second strategy employs stochastic model predictive control that explicitly incorporates the likelihood of prediction inaccuracies during planning. In this variant, deviations during execution are counterbalanced primarily through adjustments in battery operation.

Figure 3. Decision framework.
At every 15-minute slot 𝑡, the upcoming time steps are labeled as 𝑘 = 0, 1, …, 𝑃𝐻 − 1, representing the interval from 𝑡 to 𝑡 + 𝑃𝐻 − 1.
Deterministic algorithm.
The control strategy employs model predictive control (MPC) based on the receding horizon concept. For this application, the procedure repeated at each time step 𝑡 (with 𝑘 ranging from 0 to 𝑃𝐻−1) consists of the following:
• Solve the Mixed Integer Linear Programming (MILP) problem.
(19)
Subject to constraints Eqs. 2–11 and Eqs. 13–17 across the finite horizon (0, 1, …, 𝑃𝐻 − 1), which produces the full optimal control sequence {𝑈𝑘∗} for 𝑘 = 0 to 𝑃𝐻−1.
• Implement only the very first element of that optimal sequence.
This process ensures that every control decision at time 𝑡 is based on the latest available measurements and refreshed forecasts. The receding-horizon mechanism provides strong robustness against inaccuracies in models and predictions. Because new optimizations are performed using current system feedback, MPC operates as a closed-loop system. Moreover, applying the first control action solely allows correction of any forecast mismatches from the prior interval 𝑡−1 before execution. These corrections are realized by modifying the power traded with the external grid.
Let 𝑃̂𝑡𝑟𝑒𝑠, 𝑃̂𝑡𝑐𝑙, and 𝑃̂𝑡𝑙 denote the measured renewable generation, actual common demand, and real total member demand, respectively. Given that the power balance Eq. 17 must hold at all times, the following relation applies:
(20)
(21)
Where 𝑃̂𝑡𝑖 and 𝑃̂𝑡𝑒 represent the actual import and export powers, as a result, the realized shared power at time 𝑡 is:
(22)
And the actual economic return achieved by the REC at that instant is:
(23)
Stochastic algorithm.
This part describes the stochastic MPC method used for real-time optimal operation of the REC at each timestamp 𝑡, based on the available data.
To address forecast uncertainties in renewable output and load profiles, the community operator leverages the BESS to keep sufficient energy buffers. The potential flexibility of the battery at interval 𝑡 is quantified through the maximum upward and downward adjustments in charging and discharging power, defined as ≥0, ≥0, ≤0, and ≤0. These adjustments generate two modified battery power trajectories 𝑃𝑘𝑏 = 𝑃𝑘𝑐ℎ + 𝑃𝑘𝑑𝑠𝑐 (one positive and one negative):
(24)
(25)
This helps absorb the effects of prediction errors. From these power deviations, the resulting maximum upward and downward deviations in the SoC evolution, indicated as 𝑆𝑜𝐶𝑘 and 𝑆𝑜𝐶𝑘, are obtained for every 𝑘 in [0, 𝑃𝐻 − 1] via:
(26)
(27)
The deviation profiles defined in Eqs. 26 and 27 further satisfy these constraints and starting conditions:
(28)
(29)
(30)
(31)
(32)
(33)
(34)
(35)
(36)
(37)
(38)
Where 𝛿𝑘 and 𝛿𝑏𝑘 are binary flags for charge and discharge states along the two SoC paths, the terms 𝜃 𝑘 and 𝜎𝑘 𝑏 are given by:

With 𝛽𝑘 denoting the confidence level for the chance constraints. Cycle limits are enforced as:
(39)
(40)
At each time 𝑡, the following optimization is solved: (Eq. 41)
(41)
Subject to (2)–(4), (8), (9), (13)–(17), (24)–(40) over the horizon (0 to 𝑃𝐻 − 1). This yields the optimal sequence{𝑈𝑘∗}𝑡𝑘+=𝑃𝐻𝑡 −1. Only its first element is executed, while any forecast deviations are compensated by altering BESS power flows and drawing on the pre-allocated reserves. Consequently, the power balance equation becomes:
(42)
(43)
Where 𝑃̂𝑡𝑐ℎ and 𝑃̂𝑡𝑑𝑠𝑐 are the realized charging and discharging powers. The actual SoC dynamics then follow:
This section first reports the optimization outcomes and prediction accuracy of the proposed TDNN model compared with alternative methods across multiple horizons. It then analyzes how these forecasting performances affect the economic gains achieved with stochastic MPC.
A comprehensive grid search was carried out to select the most suitable structural hyperparameters for the TDNN. Training focused on minimizing the mean absolute error (MAE). Since the task involves sequence-to-sequence forecasting, no single metric fully captures performance across horizons. Therefore, top configurations were chosen based on results from the shortest horizon (15 min), a short horizon (1 h), two medium horizons (12 h and 18 h), the longest horizon (24 h), and the overall average across all 96 horizons. This yielded strong candidate sets for PV output, aggregated demand, and common services. Some configurations performed best for particular data streams or horizons. All selected configurations were subsequently retrained from scratch on the respective datasets and assessed within the complete REC management context.
Table 1 displays the test-set outcomes for the best hyperparameter sets identified for generation, demand, and common services. Values are provided for key horizons (15 min, 1 h, 12 h, 18 h, 24 h), the average MAE over all horizons, and the average Normalized MAE (NMAE). The NMAE is computed as 𝑁𝑀𝐴𝐸 = 𝑀𝐴𝐸 / 𝑥̄, where 𝑥̄ is the mean of the actual sequence. This normalization facilitates direct comparison of error scales across different variables and with external studies. Forecasts from these configurations were fed into the REC optimization.
The highest accuracy was observed for the common services load in both raw and normalized forms, thanks to its stable, periodic nature and minimal short-term fluctuations. Larger networks with more hidden neurons and extended input windows consistently yielded better results, with statistically significant differences between configurations (paired T-test, p-value ≤ 10-4).
Aggregated demand proved the hardest to predict accurately, according to NMAE, mainly because of irregular, sudden changes that disrupt patterns. No configuration stood out clearly, and statistical tests indicated no significant differences among them.
PV generation predictions improved markedly with a large input window (240 timestamps) and 200 hidden neurons. Configuration differences were statistically significant (paired T-test, p-value ≤ 10-4). The 240-timestamp/200-neuron setup also yielded near-optimal results for the other variables.
Table 1. Performance of the best TDNN setups using different input window lengths (Input dim.) and hidden neuron counts (Nr. hidden). Test results include MAE [kW] at 15 min, 1 h, 12 h, 18 h, and 24 h horizons, plus average MAE and average NMAE across all horizons.
Input dim. | Nr. hidden | 15 min | 1 h | 12 h | 18 h | 24 h | Mean MAE | Mean NMAE |
PV generation | ||||||||
4 | 96 | 1.650 | 2.450 | 5.450 | 5.200 | 3.350 | 4.700 | 0.612 |
180 | 200 | 1.550 | 2.000 | 3.600 | 2.950 | 3.600 | 2.850 | 0.376 |
200 | 200 | 1.600 | 2.000 | 2.950 | 3.600 | 2.950 | 2.850 | 0.369 |
240 | 260 | 1.550 | 1.450 | 1.400 | 1.500 | 1.600 | 1.500 | 0.066 |
Aggregated demand | ||||||||
4 | 96 | 1.743 | 2.039 | 2.113 | 2.113 | 2.150 | 2.113 | 0.178 |
180 | 200 | 1.743 | 2.076 | 2.113 | 2.150 | 2.187 | 2.150 | 0.181 |
200 | 200 | 1.743 | 2.039 | 2.113 | 2.187 | 2.150 | 2.150 | 0.180 |
240 | 260 | 1.743 | 2.039 | 2.113 | 2.113 | 2.150 | 2.113 | 0.178 |
Common services | ||||||||
4 | 96 | 0.240 | 0.480 | 0.530 | 0.490 | 0.430 | 0.540 | 0.119 |
180 | 200 | 0.260 | 0.270 | 0.290 | 0.300 | 0.290 | 0.290 | 0.065 |
200 | 200 | 0.270 | 0.280 | 0.280 | 0.280 | 0.300 | 0.290 | 0.063 |
240 | 260 | 0.310 | 0.290 | 0.290 | 0.300 | 0.320 | 0.300 | 0.065 |
To provide a fair comparison with leading neural network approaches in time series analysis and energy forecasting, multiple alternative models were developed and tuned. Each competing architecture underwent the identical grid search process used for the TDNN, including the same data normalization procedure. The same division into training, validation, and test sets was maintained for all models. A regularization strength of 0.3, combined with early stopping (guided by validation performance), helped prevent overfitting. The same hyperparameter ranges were explored wherever feasible. Optimal parameter combinations were determined for each model, evaluated on unseen test data, and the resulting forecasts were supplied to the REC management system. The models examined, along with their best configurations, are listed below:
· Single-layer LSTM (LSTM1). Input window dimensions and node counts were identical to those evaluated for the TDNN. One setup outperformed all others across tests: an input window spanning 96 time steps (equivalent to the previous 24 hours) paired with 180 hidden units.
· Double-layer LSTM (LSTM2). The initial hidden layer contains twice as many nodes as the second layer. Ranges for the input window length and second-layer nodes matched those of the TDNN experiments. This design is based on Wang et al. [35]. The superior configuration in every test used a 96-timestamp input window and 200 hidden units.
· Bidirectional single-layer LSTM (biLSTM). Input window sizes and node quantities followed the same ranges as the TDNN. A 96-timestamp input window proved optimal across all cases, with 200 hidden units yielding the best results.
· CNN–LSTM hybrid network, built with two 2D convolutional layers, one LSTM layer, and a final dense layer of 32 neurons. Input window sizes and LSTM node counts were consistent with TDNN testing; convolutional filter counts for both layers were drawn from the set [2, 5, 10, 32, 64, 128]. The strongest performer across tests featured a 96-timestamp input window, 2×1 filters with 32 channels, and 48 LSTM cells. This architecture was first introduced in Kim and Cho [19] and is recognized for excellent short-horizon (one-hour) accuracy. The original setup from that paper — 64 filters and 64 LSTM units — was also tested for direct comparison.
All optimized competitor configurations are evaluated in Table 2. When assessed via NMAE, the pattern aligns with the TDNN: common services predictions are the most accurate, followed by aggregated demand, with PV generation proving the most difficult. Among the alternatives, the LSTM2 model using the 96–200 setup achieves the highest overall average performance. Nevertheless, none of the competitors match the accuracy of the proposed TDNN. The difference is particularly pronounced for PV generation forecasts, where the top competitor reaches only 4.65 kW MAE compared to 1.5 kW for the TDNN. Interestingly, the pure LSTM variants (LSTM1, LSTM2, and biLSTM) consistently surpass the CNN–LSTM models. This outcome is likely explained by the extended prediction horizons examined here; the CNN–LSTM approach tends to excel primarily with horizons of 60 minutes or less [19].
Table 2. Performance metrics for the best configurations of all competitor models. Test-set outcomes are presented as MAE [kW] at horizons of 15 min, 1 h, 12 h, 18 h, and 24 h, the overall average MAE, and the corresponding average NMAE.
Model and configuration | 15 min | 1 h | 12 h | 18 h | 24 h | Mean MAE | Mean NMAE |
PV generation | |||||||
LSTM1 (96-180) | 4.650 | 4.650 | 4.650 | 4.650 | 4.650 | 4.650 | 0.604 |
LSTM2 (96-200) | 4.800 | 4.800 | 4.750 | 4.800 | 4.800 | 4.800 | 0.612 |
biLSTM (96-200) | 4.800 | 4.750 | 4.800 | 4.750 | 4.800 | 4.750 | 0.618 |
CNN–LSTM (96-2 × 1-32-48) | 6.300 | 6.200 | 6.300 | 6.250 | 6.250 | 6.350 | 0.921 |
CNN–LSTM (96-2 × 1-64-64) | 5.645 | 6.060 | 6.450 | 8.745 | 5.815 | 7.115 | 0.927 |
Aggregated demand | |||||||
LSTM1 (96-180) | 2.410 | 2.410 | 2.410 | 2.410 | 2.410 | 2.410 | 0.204 |
LSTM2 (96-200) | 2.336 | 2.336 | 2.299 | 2.299 | 2.336 | 2.336 | 0.196 |
biLSTM (96-200) | 2.410 | 2.410 | 2.410 | 2.410 | 2.410 | 2.410 | 0.203 |
CNN–LSTM (96-2 × 1-32-48) | 3.077 | 3.077 | 3.077 | 3.077 | 3.077 | 3.151 | 1.020 |
CNN–LSTM (96-2 × 1-64-64) | 3.559 | 3.726 | 3.659 | 3.548 | 3.467 | 3.926 | 0.331 |
Common Services | |||||||
LSTM1 (96-180) | 0.510 | 0.560 | 0.580 | 0.620 | 0.570 | 0.550 | 0.121 |
LSTM2 (96-200) | 0.440 | 0.450 | 0.450 | 0.440 | 0.440 | 0.450 | 0.098 |
biLSTM (96-200) | 0.450 | 0.450 | 0.440 | 0.450 | 0.440 | 0.450 | 0.098 |
CNN–LSTM (96-2 × 1-32-48) | 1.300 | 4.530 | 4.530 | 1.580 | 1.300 | 1.610 | 0.357 |
CNN–LSTM (96-2 × 1-64-64) | 1.342 | 1.145 | 1.465 | 1.537 | 1.411 | 1.649 | 0.364 |
The examined scenario involves fifteen residential users connected to a shared photovoltaic (PV) installation that also feeds a collective electrical load. This configuration is designed as a renewable energy community (REC) in Italy, governed by the applicable national rules [5]. The main technical specifications of this REC are listed in Table 3. Real historical profiles for common load demand 𝑃𝑐𝑙, total aggregated member demand 𝑃𝑙, and PV output 𝑃𝑟𝑒𝑠 were sourced from Ramos et al. [30]. The original measured demand curves for both 𝑃𝑐𝑙 and 𝑃𝑙 were retained without modification, whereas the PV generation profile was scaled upward in nominal capacity relative to the source data.
All developed forecasting techniques were assessed in combination with the two control strategies. These were further evaluated against two baseline scenarios: one using perfectly accurate forecasts (“ORACLE” case) and another relying on simplistic, non-predictive inputs (“NOPRED” case). In the NOPRED approach, the forecast values for PV generation, common demand, and aggregated demand at time 𝑡 are copied from the actual measurements at the previous time step 𝑡 − 1. Due to the resulting extremely high forecast error variances (which would lead to impractically large 𝜎 𝑘 𝑏 values), only the deterministic MPC variant could be applied for this naive method.
Specifically, results reported in Table 4 for the “NO-PRED” method were obtained using the standard deterministic MPC formulation. On the other hand, results in Table 5 correspond to the same deterministic MPC logic, but with real-time forecast corrections handled by the BESS instead of grid adjustments — achieved by substituting Eqs. 20 and 21 with Eqs. 42 and 43.
Table 3. REC study case parameters.
Parameter | Symbol | Value |
Main grid connection nominal power | Pmaxg | 200 kW |
BESSNominal charge power | Pmaxch | 120 kW |
BESSNominal discharge power | Pmindsc | -120 kW |
BESS capacity | E | 120 kW h |
BESS charge efficiency | ηch | 95 % |
BESS discharge efficiency | ηdsc | 95 % |
BESS maximum SoC | SoCmax | 1 |
BESS minimum SoC | SoCmin | 0 |
Maximum number of complete charge and discharge cycles | α | 2 |
Aggregated nominal power demand | Pnoml | 37.076 kW |
Common nominal power demand | Pnoml | 10.037 kW |
PV Plant nominal power | Pnomres | 50 kW |
MISE incentive | cm | 0.11 €/kWh |
Restitution of grid charges | cr | 0.009 €/kWh |
The performance of the algorithms was tested on 30 days of simulation, from midnight on July 4th, 2019, to midnight on August 3rd 2019.
The reliability parameter 𝛽𝑘 varies over time rather than remaining fixed. In this study, it was set to increase gradually as the prediction horizon extends. Lower values of 𝛽𝑘 lead to higher 𝜃𝑘, which reduces the influence of uncertainties in longer-term forecasts. The chosen values are:
• 0.025 for 𝑘 ∈ [0,3];
• 0.05 for 𝑘 ∈ [4,7];
• 0.075 for 𝑘 ∈ [8,11];
• 0.1 for 𝑘 ∈ [12,15];
• 0.15 for 𝑘 ∈ [16,19];
• 0.25 for 𝑘 ∈ [20, 𝑃𝐻 − 1].
A detailed sensitivity analysis was conducted by changing the prediction horizon (PH) length. The control strategies were judged primarily on the resulting REC income. For the stochastic MPC, the optimizer’s failure rate was also recorded — the percentage of simulation time steps during which a feasible optimal solution could not be found.
Table 4 presents the outcomes from the deterministic MPC, while Table 5 presents the outcomes from the stochastic MPC. Note that the figures in both tables represent only the additional revenue earned by the REC itself. The true financial benefit for the community members is this REC income combined with the bill savings on the shared common load, amounting to €889 over the 30 days (calculated at price 𝑐𝑒).
Both tables demonstrate that feeding the control algorithms with TDNN-based forecasts leads to noticeably higher earnings than using forecasts from any of the competing neural networks. This superiority is consistent across both deterministic and stochastic MPC, and for every prediction horizon length examined.
Additionally, the stochastic MPC consistently outperforms the deterministic MPC, confirming that keeping the planned grid exchange profile as stable as possible is the most profitable strategy. The highest economic return achieved was €3600.75, reached by the stochastic MPC with a prediction horizon of 𝑃𝐻 = 18 h (Table 5).
This peak performance does not align with the best results from the TDNN models. For the TDNN configurations, the maximum income was obtained using the TDNN-200-200 network with a prediction horizon of 𝑃𝐻 = 12 h. This horizon length is precisely where the TDNN-200-200 delivers the lowest mean absolute error (MAE) across all forecasted energy variables, as detailed in Table 1.
Table 4. Deterministic MPC results.
Prediction method | REC income [€] | % of ORACLE income | ||||
Empty Cell | Prediction horizon | Prediction horizon | ||||
Empty Cell | 12 h | 18 h | 24 h | 12 h | 18 h | 24 h |
LSTM1 | 2977 | 2967 | 2973 | 84.76 | 82.24 | 82.88 |
LSTM2 | 2976 | 2967 | 2961 | 84.71 | 82.26 | 82.55 |
biLSTM | 3005 | 2974 | 2963 | 85.54 | 82.45 | 82.58 |
CNN–LSTM (96-2 × 1-32-48) | 2913 | 2868 | 2856 | 82.93 | 79.51 | 79.62 |
CNN–LSTM (96-2 × 1-64-64) | 2744 | 2798 | 2770 | 78.11 | 77.57 | 77.21 |
NO-PRED | 2841 | 2959 | 2922 | 80.87 | 82.02 | 81.45 |
ORACLE | 3513 | 3607 | 3588 | 100 | 100 | 100 |
TDNN-4–96 | 3178 | 3195 | 3153 | 90.47 | 88.59 | 87.89 |
TDNN-180–200 | 3219 | 3298 | 3272 | 91.64 | 91.44 | 91.20 |
TDNN-200–200 | 3238 | 3297 | 3266 | 92.18 | 91.41 | 91.05 |
TDNN-240–260 | 3245 | 3297 | 3265 | 92.36 | 91.40 | 91.02 |
Table 5. Stochastic MPC results.
Prediction Method | RECIncome [€] | % of ORACLE Income | Failure Rate [%] | ||||||
Empty Cell | Prediction Horizon | Prediction Horizon | Prediction Horizon | ||||||
Empty Cell | 12 h | 18 h | 24 h | 12 h | 18 h | 24 h | 12 h | 18 h | 24 h |
LSTM1 | 3233 | 3169 | 3138 | 92.06 | 88.02 | 87.39 | 2.26 | 0.56 | 0.56 |
LSTM2 | 3252 | 3177 | 3118 | 92.58 | 88.23 | 86.85 | 0.28 | 0.45 | 0.45 |
biLSTM | 3229 | 3197 | 3147 | 91.93 | 88.79 | 87.64 | 2.22 | 2.22 | 2.22 |
CNN–LSTM (96-2 × 1-32-48) | 3180 | 3138 | 3082 | 90.55 | 87.15 | 85.84 | 0.56 | 0.03 | 0.10 |
CNN–LSTM (96-2 × 1-64-64) | 3085 | 3116 | 3010 | 87.83 | 86.55 | 83.83 | 2.40 | 1.77 | 1.81 |
NO-PRED | 2862 | 2991 | 2955 | 81.48 | 83.07 | 82.30 | – | – | – |
ORACLE | 3512 | 3601 | 3591 | 100 | 100 | 100 | – | – | – |
TDNN-4–96 | 3347 | 3341 | 3206 | 95.29 | 92.80 | 89.28 | – | – | 0.31 |
TDNN-180–200 | 3378 | 3309 | 3268 | 96.18 | 91.91 | 91.01 | – | – | 2.43 |
TDNN-200–200 | 3398 | 3330 | 3295 | 96.73 | 92.47 | 91.76 | – | – | 0.45 |
TDNN-240–260 | 3395 | 3082 | 3268 | 96.66 | 85.60 | 91.02 | – | 0.17 | 0.38 |
Among all the Prediction Method and Prediction Horizon combinations reported in Tables 4 and 5, the one that delivers the highest economic return is examined in detail in this section. The corresponding control variable profiles are presented for the representative week running from midnight of July 4th, 2019, to July 11th 2019.
This top-performing case uses the TDNN-200-200 forecasting model with a prediction horizon of PH = 12 h. It achieves 96.73% of the theoretical maximum income (i.e., the income obtained with the perfect “ORACLE” forecasts) and provides an 18.72% improvement compared to the “NO-PRED” approach.
The time-varying energy sell-back price 𝑐𝑡𝑒, at which renewable energy is remunerated, is illustrated in Figure 4. This profile corresponds to the real Italian energy market clearing prices. It is important to note that the prices used in this study are based on actual market data recorded from July 4th, 2021, to July 11th 2021.

Figure 4. Simulation data: energy price profile.
The power profiles for PV generation, common load demand, and the aggregated demand of the residential buildings are displayed in Figure 5.

Figure 5. Simulation data: PV generation, common power demand, and building aggregated power demand profiles.
Figure 6 presents the optimal operating strategy implemented by the REC. The figure reveals that the community manager actively works to minimize exported power 𝑃𝑒 throughout the daytime by using surplus energy to charge the BESS. This strategy reduces 𝑃𝑡𝑒 (Eq. 17), aligning it more closely with 𝑃𝑡𝑙 to maximize the internally shared power 𝑃𝑡𝑠ℎ. Consequently, the battery SoC is built up in advance of sunset, as illustrated in Figure 7.
During nighttime hours, the BESS operates exclusively in discharge mode, as per constraint Eq. 16, supplying stored energy to the system. Nevertheless, the battery output is often unable to fully meet the common load requirements, forcing the REC to import power from the external grid. The negative values visible in Figure 6 represent these grid imports, denoted as 𝑃𝑡𝑖.

Figure 6. REC power profiles.
In Figure 7, the power flow of the BESS is depicted, where positive values indicate 𝑃𝑐ℎ and negative values correspond to 𝑃𝑑𝑠𝑐, alongside the BESS state of charge (SoC), which is consistently constrained between ¯(SoC) (upper boundary) and 𝑆𝑜𝐶 (lower boundary). Due to the restrictions on the number of charge and discharge cycles, the BESS exhibits a relatively uniform and steady pattern during both charging and discharging, with the charging phase showing particularly smooth behavior.

Figure 7. The BESS power exchange profiles, where positive values represent charging power and negative values indicate discharging power (top), along with the corresponding SoC profile of the BESS (bottom).
This work introduced a hybrid AI framework for optimal operation of RECs, offering a complete end-to-end approach: neural network predictions of aggregated load, PV output, and common REC services are fed into an MPC optimization algorithm that maximizes revenue. The approach was tested using publicly available datasets and benchmarked against several alternative methods, with results showing it achieves the highest REC income. Nevertheless, this study has certain limitations. Validation was conducted on data from a single REC, so future research should extend testing to multiple public and private datasets. Additionally, incorporating further inputs—such as weather forecasts to improve predictions of future energy behavior—could enhance overall performance.
All authors approved the version of the manuscript to be published.
None
None
None
Open Access The author(s) retain copyright. This article is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. It may be shared and adapted for non-commercial purposes with appropriate attribution, an indication of changes, and distribution of adaptations under the same license. Third-party material may be subject to separate terms identified in its credit line. View the license at https://creativecommons.org/licenses/by-nc-sa/4.0/.