Optimization of the Electrical Demand of an Existing Building with Storage Management through Machine Learning Techniques

Featured Application: The methods and models presented in this paper optimize the management of a building’s storage system by modeling and predicting electrical demand using Machine Learning techniques, obtaining high accuracy results. Abstract: Accurate prediction from electricity demand models is helpful in controlling and optimizing building energy performance. The application of machine learning techniques to adjust the electrical consumption of buildings has been a growing trend in recent years. Battery management systems through the machine learning models allow a control of the supply, adapting the building demand to the possible changes that take place during the day, increasing the users’ comfort, and ensuring greenhouse gas emission reduction and an economic benefit. Thus, an intelligent system that defines whether the storage system should be charged according to the electrical needs of that moment and the prediction of the subsequent periods of time is defined. Favoring consumption in the building in periods when energy prices are cheaper or the renewable origin is preferable. The aim of this study was to obtain a building electrical energy demand model in order to be combined with storage devices with the purpose of reducing electricity expenses. Specifically, multilayer per-ceptron neural network models were applied, and the battery usage optimization is obtained through mathematical modelling. This approach was applied to a public office building located in Bangkok, Thailand.


Introduction
Compliance with the objectives set in the Paris agreement requires a change in the direction of greenhouse gas reduction on each economic sector, with most of the emissions linked to energy consumption, directly or not, as discussed in Regulation 2018/842 [1] and in Directive 2018/844 [2]. Regarding the residential and industrial sectors, buildings are responsible of 40% of final energy consumption and 36% of the emissions in Europe [3].
The key factor in achieving these goals is a dual strategy that encourages the creation and diffusion of renewable energy sources and other zero-emission technologies [4]. In [5] the emissions and the energy demand of three types of residential neighborhoods were analyzed, showing that the increase in renewable energy sources mitigates emissions by 10-25%. In addition, the study shows the importance of controlling electricity production in order to reduce emissions, which can be up to 95% lower. In [6] an analysis of nine neighborhoods was carried out in order to propose strategies to reduce energy consumption in the study region. It is shown that the application of renewable energy sources and electric vehicles would considerably improve energy performance in the analyzed region. [7] is a review of the results of 144 studies on the zero-emission neighborhood topic. Focus areas, research gaps, and future research possibilities are identified.
Controlling the electricity supply is one of the main pillars for the achievement of sustainable development marked by the United Nations [8]. Thus, energy efficiency in the electrical network fluctuates throughout the day according to generation needs, to meet consumption. Generally, periods with renewable energy correspond to periods with lower prices [9].
The European Union recognizes the development of batteries as a strategic value chain to obtain a prosperous and neutral economy from the point of view of global warming [10]. By means of a storage management system, supply control is optimized by adapting demand to possible changes that may appear throughout the day, favoring consumption in the building in periods when energy prices are cheaper or of renewable origin [11] and supplying power in possible unexpected grid outages [12].
The development of a good battery energy control system allows the achievement of a building with a high quality of energy use [13]. The management system can be more complex in case of considering the production of renewable energy in the same building, the charging of electric vehicles, or a combination of factors, reducing the electrical needs on the electrical network and achieving a nearly zero energy building [14]. In [15] an analysis of the impact of different battery parameters, as capacity, charge/discharge rate, power request process, and cost function, on the cost savings using different energy storage algorithms is performed. In [16] an analysis of the savings, efficiency and emissions in three scenarios with storage using a mathematical optimization was performed. It is shown that the storage system can generate great benefits in each of the fields of study. In [17] an intelligent system of a microgrid with photovoltaic, storage, hybrid vehicle and electrical network was proposed. The control algorithm shows a benefit in reducing demand, maximizing user comfort and minimizing energy costs.
There are diverse models of optimization of the battery management system, often consisting of a multiobjective problem. Some of the most used algorithms to solve problems in the field are genetic algorithms [18], linear programming [19], mixed-integer linear programming [20], mixed-integer quadratic programming [21], nonlinear programming [22], mixed-integer nonlinear programming [23], convex programming [24], and reinforcement learning [25].
Accurately recognizing fluctuations in electrical demand, in building, is a key step in optimizing the performance of the storage management system. There are different methods that are developed to know the energy demand. Among them are the white box, through physical modelling; black box, through data driven modelling; and grey box, through the combination of previous models [26].
Among the methods presented, the impact of black box modelling stands out in recent years, mainly because of the development of machine learning techniques, specifically, the deep learning techniques [27]. These replicate how the human brain works, being the key point in offering models the ability to learn. In this way, with deep learning techniques, computers have the ability to identify complex patterns in the data without being explicitly programmed, offering accurate predictions at a low cost [28].
Deep Learning techniques have enormous potential, however, mainly because the dependence on a variety of hyperparameters, the performance of the algorithm can vary significantly, generating erroneous results under certain circumstances, either overfitting or underfitting. These hyperparameters correspond to the number of layers, the number of nodes per layer, the type of activation function in each layer, the selection of a suitable learning rate, considering if it is necessary to consider a decay rate in the problem, the optimizer, or the number of epochs and batches in the model [29]. Nowadays, the selection of the best values or settings and how to adjust each hyperparameter to achieve the best performance is still unclear and is an open question in computer sciences [30]. In this way, the search for the appropriate configuration of these hyperparameters is a challenging problem and the applicability of these techniques on modeling and prediction of electrical energy is in continuous improvement [31].
Deep learning methods are usually divided into three branches: standard neural networks (SNN), convolutional neural networks (CNN), and recurrent neural networks (RNN). The fields of application are diverse, SNNs being habitually linked to areas as building energy [32] or environment (e.g., saline particles classification [33] and botanical origin of honeys prediction [34]); CNN, to image recognition (e.g., object detection [35] and face recognition [36]), or autonomous driving (e.g., road architecture recognition [37] and sign detection [38]); and RNN, to speech recognition [39] or translation [40]. The common characteristic is the structure, from some input data, it is sought to extract a pattern in order to extrapolate the results to new data. However, depending on the type of data used, the results will have greater accuracy. So SNN are often used with tabular data, CNN, with image data and RNN, with sequence data [27].
Similar tasks to those proposed here have been solved with deep learning or related techniques: prediction of the energy consumption of a green building [41], implementation of demand-response programs at a multiple-building scale [42], forecasting of the behavior of a group of residential buildings [43], scheduling of the storage system based on reducing the peak load [44] or forecasting in smart home applications [45].
To carry out this study, the SNN are applied to predict the electricity consumption of an office building. Thus, the fitting of the SNN to problems in the field of study is widely demonstrated; it is possible to obtain a very good accuracy despite the simplicity they present in their structure [46]. In addition, the adjustment of this type of neural network also has great accuracy in the field of photovoltaic production prediction. It is able easy to extend the models [47]. In [48] a RF-XGBoost model was used for the prediction of electrical energy from temperature, humidity, air density and pressure. In [49] different machine learning models (SNN, support vector machine, and RNN) were compared for the prediction of photovoltaic production, heating, cooling, and illumination from outdoor air-dry bulb temperature, outdoor humidity, global solar radiation, cloudiness ratio, schedules of occupants and office equipment, indoor air-dry bulb temperature, and photovoltaic surface temperature. In [50] a SNN model was used for the prediction of electrical energy from outdoor dry-bulb temperature, outdoor relative humidity, precipitation probability, rain indicator, wind speed, sky condition, day indicator, interval stamp, and operational condition. Based on the model obtained with this technique, in this paper the electrical consumption prediction is applied in order to optimize the storage management system, in which, by performing a mathematical analysis, it is sought to minimize costs in the building. The combination of both techniques makes it possible to generate integrated and automated management and control in order to increase energy efficiency and the level of comfort through the flexibility of electricity consumption in the building [51]. The battery management system makes it possible to supply power at necessary moments from the stored energy because the knowledge and prediction of the machine learning models. In this way, the quality of energy consumption can be improved, with a greater and better use of green sources, facing the energy crisis and environmental concerns present nowadays [52]. It can be said that the novelty of this research work lies in the combination of two concepts: modeling the prediction of energy demand using machine learning techniques and minimizing the electricity expenses by optimizing the management of the storage system. Both concepts have their own circumstances: in the first case, parameter adjustment is the key of the process. The latter combines the equations of the storage system, the information related with tariffs and the optimization techniques.
Besides, and in order to justify the use of the above mentioned techniques, focus on the fact that, according to the no free lunch theorem, none of the evolutionary optimization algorithms can outperform the others if they are averaged over all possible problems [53], i.e., the use of the SNN in this type of problem is as valid as any other, taking into account that the solution obtained is acceptable. This paper is organized as follows: the methods and models developed in the study are presented in Section 2; a specific case and the corresponding simulation are shown in Section 3; and the conclusions are outlined in Section 4.

Models and Methods
This section presents the mathematical structures that used in the realization of this research work. According to the analysis presented, the first part shows the methodology to estimate the electrical consumption through SNN, and the second, the model developed to maximize the benefits of the storage management system by reducing electricity expenses.

Machine Learning Prediction
The methodology used is based on supervised learning techniques. From a layer with the input data, the input layer, it is sought to predict one or more labeled variables located in the last layer, the output layer. Between both, there are some interconnected, hidden layers. Depending on the complexity of the problem, the hidden layers vary in their number of nodes and layers. The algorithm used to solve the SNN is divided into three steps, forward propagation, backward propagation and weights update. In addition, the Adam optimizer has been used to speed up and improve convergence. The full operation is shown in Figure 1.  Figure 1 shows in detail the classical neural network methodology from the achievement of each of the variables in a specific layer. As can be seen, the method is divided in three stages, forward propagation, backward propagation and update. The steps carried out for the computation of each of the variables are introduced in the following paragraphs.
First, the information provided by the input data, in the input layer, is propagated through the hidden layers by the connections between the nodes until the variable of interest is predicted in the output layer. This is called forward propagation and is divided into two steps. In the first, given by the Equation  When the predicted values correspond to the last layer L (or output layer), the second stage begins, called backward propagation and given by the Equation (3). This commences by determining the error between the predicted variable , and the real variable . Once this deviation is known, the information is propagated to the input layer by determining the derivatives of the layer function [ ] using the chain rule. The bias weight is not considered because its influence with respect to the kernel weight is negligible.
where corresponds to the mean of the output variable, is the total number of values, and [ ] [ ] is the derivative of the activation function considered in layer .
At this stage, the error determined is also propagated through the kernel and bias weights with the purpose of obtain the optimal settings for them, Equations (4) and (5) respectively.
Finally, the kernel and bias weights are updated with the Adam optimizer, Equations (6) and (7) respectively.
where is the learning rate, and are Adam optimizer kernel weights, computed with Equations (8) and (9) respectively, and is a small number to prevent zero division.
where and are Adam optimizer bias weights, computed with Equations (10) and (11) respectively.
where corresponds to the first moment of the exponential decay rate.
where corresponds to the second moment of the exponential decay rate.

Battery Management Optimization
Electricity price varies according to the period of the day, , in which the consumption is established. Usually, these periods have been divided into peak, with high prices, and valley hours, with low prices, however, with the proliferation of the electric vehicle, a new period has appeared, the flat hours, with intermediate prices. The benefits of machine learning techniques allow a model to be obtained with which the electrical demand can be predicted in subsequent periods of time, . The energy storage system benefits from this model, being able to recognize at any time, , the trend in consumption. In this way, it is possible to predict the best option that the storage system can take in order to maximize the electrical energy consumption in periods with lower electricity costs. The Figure 3 shows the storage flowchart and following paragraphs describe the mathematical model performed in order to minimize the electricity bills. The first stage is to apply the first principle of thermodynamics by balancing the power demand in the building, , with the power consumed from the electrical network, . The effects of the battery in both directions are also considered, i.e., the charging and discharging powers, and respectively. Equation (12) shows this principle applied to each period of time .
Subsequently, the battery state of charge at time , ( ), is computed from the previous state, ( − 1), and the charging and discharging needs in the building. In order to optimize the storage according to the needs of the building, in Equation (13) the charging is multiplied by a function that establishes the amount of power consumed from the electrical network needed at each time , ( , , , ). This function is carried out from the predefined neural network model and allows the estimating of whether an overcharging, cover only the demand, a charging defect or a zero charging period is necessary.
where ( ) and ( ) are the battery discharging and charging efficiencies, and is the capacity.
In order to avoid irregularities in battery usage, its boundary conditions are stablished. On the one hand, the energy stored in the battery cannot exceed the capacity, due to physical limits, neither can be completely emptied, to protect and extend its life cycle. Equation (14) shows this restriction using as the minimum allowed state of charge.
On the other hand, the battery cannot be discharged or charged outside of the maximum ranges established by the manufacturer, , and , respectively. Equations (15) and (16) show these conditions.
where ( , ) and ( , ) are the power and energy costs in period and time respectively.

Case Study and Results
The aim of this study is to obtain a model to predict, with high accuracy, the electrical energy demand, by using SNN to manage the energy storage system in order to reduce the electricity expenses of a building. The validation of the methods proposed in the previous section is implemented in an office building divided into seven floors and located in Bangkok, Thailand. The monitored data have a period of 1 min and are collected in different zones on each floor. These data correspond to indoor temperature, ambient illumination and lighting and loads consumption [54]. Furthermore, the external monitored data have a period of 30 min and correspond to outdoor temperature.
The study is performed in zone 2 of the second floor due to data quality. The monitoring period goes from 1 July at 12 a.m. to 12 September at 4 p.m., both in 2018, and from 3 March 2019, at 2 p.m. to 1 January 2020, at 12 a.m. The percentage of errors of each variable monitored in the collection period are shown in Table 1. As can be seen, the errors of the building's interior sensors are 1.56%, the exterior ones, 0.66%, and those of the energy meters, 0.15%. The tariff considered has hourly discrimination with three periods, peak hours, flat hours, and valley hours. On working days, peak hours go from 10 a.m. to 2 p.m. and from 6 p.m. to 10 p.m., flat hours, from 8 a.m. to 10 a.m. and from 10 p.m. to 12 a.m., and valley hours, from 12 a.m. to 8 a.m. On the other hand, nonworking days are always considered as valley hours. The prices of energy and power prices in each period are presented in Table 2. The energy storage system must have values according to the demand in the selected zone. The capacity must be adjusted to the consumption, avoiding oversizing or undersizing. The charging and discharging powers must also be high enough in order to be able to supply the energy in each instant. Therefore, from the study of the electricity demand of the building zone, the data used to simulate the battery are shown in Table 3. Table 3. Battery characteristics considered.

Machine Learning
Data with a period of 1 min are converted to 30 min by adding or averaging their values over the periods, due to differences in the monitoring period. Thus, as NaN values are allowed in the neural network, there are 17,956 values. In addition to the data presented above, the neural network also considers the hour of the day, day of the week and day of the year, with the final objective being to recognize the electrical consumption of the building (lighting and loads) from the rest of the variables.
The configuration shown in Figure 4 has been obtained from the selection of the best configuration with root mean square error (RMSE), modeled in Python. I.e., the following hyperparameters are properly adjusted: number of layers, the number of nodes per layer, the type of activation function in each layer, the selection of a suitable learning rate, the optimizer, or the number of epochs and batches in the model. The testing was carried out for neuron values corresponding to the range 0-100 with step 25 and with the possibility of up to six hidden layers. All possible configurations of the activations shown in Figure  2 were also evaluated. The methodology used consisted of a batch learning of 64 units in each epoch, with a maximum of 500. The proposed model was carried out with a 90% Train/Dev and 10% test sample partition, i.e., 90% modelling and 10% predicting, and a 10 division k-fold method. The mean error for the different divisions of the sample and the global sample is shown in Table 4. As can be seen in Table 4, the errors of the SNN model with k-fold reduced. On the one hand, the mean modeling errors are 5.44% for the training set, and 5.59% for the validation set (Dev). On the other hand, the mean errors of the prediction set (Test) are 5.28%. In this way, it has been possible to obtain a model that can model and predict the data with mean errors of 5.47%. The fitting of the electricity demand of this SNN model with k-fold presented with respect to the real data is shown in Figure 5. As can be seen in Figure 5, the SNN k-fold results were accurate. The model adjusted to the variations in electrical consumption that occur through the day in the building, both in the Train/Dev and Test partitions. Therefore, it can be determined that the model is capable of modeling and predicting electricity consumption in the selected office building. From Figure 5, it is also possible to determine the use established in the building, setting up the working hours through the day and closing at night. In addition, the figure shown corresponds to a weekday, since, as expected, no activity is established on weekends.

Management Storage
The storage management system was implemented to minimize the global energy cost. The models used established that the battery must not be charged during peak hours. It should not be charged during flat hours, and there are no restrictions for valley hours. To consider the prediction of future consumption from the inputs presented, a lag 2 was considered, i.e., 1 h. In addition, an error of 5% of the inputs was considered in the lagged periods to simulate the imprecision of the prediction system. Furthermore, with the purpose of protecting the battery and maximizing its life cycle, a depth of discharge of 90% was considered. Figure 6 shows the evolution of the state of charge of the battery from the management storage system proposed with the data from the SNN model with k-fold. This figure shows both the Train/Dev and the Test divisions. The results for both partitions are similar, not showing a priori irregularities in the prediction model. As can be seen in Figure 6, the battery was charged in the valley hours and discharges in the flat and peak hours. The greatest discharge occurred in the hours in which the demand is greatest, corresponding mainly to the central hours of the day, establishing both peak hours and a small period of flat hours. Battery discharge began at 8 a.m., when the building workday begins. From 9 p.m. the battery maintained its state of charge until the valley hours are reached, since the flat hours were established and there was no consumption in the building.
Once the evolution of the battery's state of charge was determined, Figure 7 shows the variation that the storage management system produced in the instantaneous energy costs. In the same way as before, both the Train/Dev and the Test divisions are shown, not existing strange results in the predictions. The period of electrical consumption that was established in the building generated costs in the hours with high energy cost, i.e., peak hours and flat hours. For this reason, as can be seen in Figure 7, the building's habitual consumption costs were established in these hours. On the contrary, with the energy storage system, the building's electrical consumption was covered by the discharge of the battery. Thus, the costs in the battery system were set at the lowest energy cost, i.e., valley hours, the costs in peak hours and flat hours being null. In this way, an economic benefit was generated. The differences between both situations are presented in Table 5. Table 5. Comparative of the electricity bills in the study period in the selected zone of the building considering or not the installation of the battery.

Without Battery
With Battery Savings Electricity expenses (€) 97. 26 39.54 57.72 As previously determined, the battery allowed us to alleviate the demand of the electrical network in the periods in which the price of electricity was higher and to consume in periods with low price. As can be seen in Table 5, the total electricity price in the study period, twelve months divided into two periods, of the building's usual consumption was higher than that generated with the energy monitoring system proposed.

Conclusions
The aim of this paper was to obtain an integrated and automated management and control in buildings. Based on the data of an office building located in Bangkok, the possibility of carrying out a control system using machine learning techniques for the prediction of electrical consumption and mathematical models for the storage management system has been demonstrated.
The fitting of the SNN model obtained, i.e., the adjustment of the corresponding parameters, was accurate and allowed us to predict instant and subsequent electricity consumption. The results obtained present clear evidence that the combination of the techniques presented establish a model that is more beneficial to the environment and with consequent economic savings.
Specifically, the SNN with k-fold model established sought to characterize the electricity consumption in the building from the interior and exterior temperatures, the interior lighting and the temporal variables of time of day, day of year and day of week. The determined dataset samples, either modelling or predicting, had errors over 5% and were made up of two hidden layers with 100 and 75 units and with linear and sigmoid activations respectively, being the activations of the output layer sigmoid. In case of considering the management storage system based on the machine learning model presented, the savings in the study zone were considerable, amounting to a reduction of 52.72 € in the electricity bills in the period analyzed.