Optimizing energy consumption in smart homes using GA-LSTM

The need to optimize energy consumption arises from the inadequate energy supply many homes face. However, to optimize energy consumption in a home, one must be equipped with the knowledge of the energy consumption rate and energy supply rate in the home. This paper proposed the use of a Long Short-Term Memory (LSTM) model optimized by Genetic Algorithm (GA) to optimize the energy consumption in a smart home. The model was designed using 8 input variables, which were observed weather information of a given region over a span of 350 days. The data set was split into a training data set and a test data set in the ratio 4:1. Both the baseline LSTM model and our proposed GA-LSTM model was used to train and test the data. The results of the baseline LSTM model showed a Mean Average Percentage Error (MAPE) of 0.15 and a Root Mean Square Error (RMSE) of 0.0007, while our proposed GA-LSTM recorded an MAPE of 0.07 and RMSE of 0.0004. The implication of these results is a better performance of our proposed GA-LSTM model over the baseline LSTM model. This paper demonstrated how the Genetic Algorithm was able to optimize the baseline LSTM model by selecting the best weights during the LSTM training process. The findings of this paper are recommended for smart homes having the capability to monitor energy usage of installed electrical appliances and the ability to predict to weather condition of the region the home is sited.


Introduction
As the population of the world continues to grow, it's expected that energy demand will also continue to increase.The United Nations Economic and Social Commission for Asia and the Pacific (ESCAP) noted that countries under the Association of Southeast Asian Nations (ASEAN) recorded a sharp increase in electricity consumption in the region over the past years, and this was mainly due to an increase in the use of cooling systems in residential buildings.(1) The World Bank in one of its reports also noted that at least 13% of the world population, especially people living in South Asian and Sub-Saharan countries have been living with an inadequate supply of energy (2).
In China, the impact of shortage in energy has been affecting the productivity of manufacturing firms and this has led to these firms seeking alternative sources of energy.(3)Similar research on residential buildings in China revealed that inadequate energy supply has led to low life satisfaction in people living in residential buildings irrespective of their age class.(4) As a way of overcoming this energy problem, most of the affected people have resorted to using non-renewable sources of energy such as diesel and kerosene to sustain their homes.(5) The wide use of non-renewable energy is due to its low cost and easy accessibility.However, they are not the best solution.
The European Union in a bid to solve the power problems in countries under the European Union set out a target to provide at least 32% renewable energy by 2030 as a cleaner alternative source of energy.(6) One common source of renewable energy that can meet this target is solar energy.Weschendfelder et al. (7) highlighted two great benefits of solar energy which are its renewability and its easy integration with other forms of renewable energy.
However, despite being an excellent source of renewable energy, the output of solar grids can be affected by so many factors such as humidity, cloud cover, temperature, and dust.(8,9) Most of the factors affecting the output of solar grids are weather conditions, and as humans, we have only little or no control over these factors.
To get the maximum benefit of using a solar grid installation in a home, users must devise a way to optimize the consumption of power in their homes.Optimization of energy consumption requires that users strictly monitor the use of electrical appliances in the home.In this paper, we investigated the power ratings of the various appliances in a smart home and their consumption within a specified time frame.
With the knowledge of the consumption rate of home appliances, we proposed a model that allowed users to predict what the output of their solar grid system would be so that they could make adequate plans on how to use their solar grid to meet the needs of their home appliances.The model we proposed was a Long Short-Term Model (LSTM) optimized by the Genetic Algorithm (GA).
LSTM has been a useful tool in making time series predictions, such as the prediction of the price of natural gas, (10) the nowcasting of macroeconomic trends, (11) and the prediction of the energy output of solar grids.(12) To make better predictions, the parameters of an LSTM model can be optimized using Genetic Algorithm.

Related Works
Scholars in the field of renewable energy have made significant progress in their efforts to optimize power consumption in residential buildings.Some solutions proffered include the use of real-time calculation systems that calculate the energy consumption of buildings,(13) the use of smart control systems to schedule the consumption of energy in the home, (14) and the use of micro-CHP (Combined Heat and Power) generation technologies in buildings.(15) One of the most recent advancements in energy optimization includes the use of machine learning algorithms in making predictions.In this section, we reviewed papers exploring machine learning algorithms in optimizing energy consumption.

Prediction of energy consumption in campus buildings using long short-term memory
Faiq et al. (2033) (16) in their paper proposed an LSTM model that was capable of predicting the next day's energy consumption of a residential building on a campus.The input features used were some weather variables and they were relative humidity, wind velocity, rainfall duration, rainfall amount, pressure, and temperature.The output variable used was the maximum energy consumption of electrical appliances in the building.The authors were able to demonstrate the relationship between the input features and output variables using Support Vector Regression (SVR) and Gaussian Process Regression (GPR).The results obtained from these models were then compared with the results obtained from using LSTM.To strengthen the performance of their model, the authors employed some techniques such as feature importance, normalization and dropout techniques The performance of the three models under review was then evaluated using Mean Absolute Error (MAE) and the Root Mean Square Error (RMSE).The results of the experiment revealed that LSTM had the least RMSE and MAE.The authors also concluded that wind speed, rainfall duration, rainfall amount, and temperature had a strong effect on the performance of the model.

Machine Learning Techniques for Predicting the Energy Consumption/Production and Its Uncertainties Driven by Meteorological Observations and Forecasts
Bogner et al. (17) based their research on the optimization of hydro-power plants and water bodies used in providing energy.The authors posited that meteorological conditions can highly influence the production of energy as opposed to the demand for energy by consumers.They opined that if managers of energy facilities can predict the production of energy, they can be able to optimize the distribution of energy among consumers.The authors used various models such as the Multivariate Adaptive Regression Splines (MARS), Quantile Random Forest (QRF) and Gradient Boosting Machines (GBM).The novelty in their research was the use of Machine Learning (ML) methods and monthly weather forecasts in predicting energy consumption or production.The result of the research revealed that the Continuous Ranked Probability Score (CRPS) of QFR was the lowest indicating a better performance over other models used in their research.

Influencing Factors Evaluation of Machine Learning-Based Energy Consumption Prediction
Khan et al. (2021) (18) took an in-depth approach to establishing a relationship between weather conditions and energy consumption.The authors evaluated various factors that could affect the precision of their model.The predicting features used by the authors were humidity, temperature, and rainfall.The data for the experiment was collected on a South Korean Island while the prediction output was collected from an energy smart grid.To make a better model, the authors considered special days like weekends, weekdays, and special days and how these days were able to affect the model's performance.A hybrid ML model consisting of Xgboost, Catboost, and Multi-layer perceptron was used in making the predictions.In the results obtained, the authors realized that special days recorded the highest mean error which was 4.28% while weekdays and weekends recorded 2.78% and 2.79% respectively.The authors also analyzed the effect of fluctuations in temperature and typhoons on energy consumption.

Model Structure
This paper used the LSTM model as its baseline model.We went further to optimize the training process of the LSTM model by using the Genetic Algorithm to choose the best weights for the model.This section gives a detailed note on the operation of our proposed model.

Long Short-Term Memory Model
LSTM is a modified version of the Recurrent Neural Network (RNN) created to handle some of the limitations of RNN such as long-term dependency issues and vanishing gradient problems.(19) In RNN, the flow of information from either the root to child nodes or the child node to the root gets extinct at a point along the way.
The architecture of LSTM is a set of sub-networks, also called memory blocks, that occur in a recurring manner.(20) Houdt et al. (20) explained the idea of a memory block as cells that keep the state of the memory constant and also prevent the loss of information.A simplified version of the LSTM block memory is shown in Figure 1.  : the current time step which is an update of  − after the processing of the input,   , and the processing of the previous hidden state,  − .
: the hidden state of the current time step which is derived from the current time step.It is also the input of the next time step.
− : the hidden state from the previous step.It is also the output from the previous time step.
: the input at the current time step.
The mathematical equations are given as: =   (    +    − +  ) ( 5) Where σ is the differentiable function, W is the weight, and b is the bias.
Due to the improvement in the learning capability of LSTM, companies have begun adapting the model to their services.For example, Amazon uses the LSTM model for its Alexa product, (22) Facebook uses it for some of its prediction problems, (23) and Google uses it for speech recognition and translation tasks.GA establishes a population and continually produces a new population by the iterative use of the algorithm on every member of the recent population.(33) Some important elements of GA are crossover, mutation, selection, and fitness function.A brief description of these elements is given below: Crossover: Crossover is carried out by picking a random point on the chromosome where there's an exchange of the parents' part.(34) Mutation: The mutation operation is used to produce new offspring from parents.(35) Mutation happens by applying random changes to one or more genes, generating new offspring.This process helps to overcome the local optima problem.
Selection: The process of selecting the chromosomes that will be responsible for mating, as well as the number of offspring each chromosome will reproduce, is called selection.(34) Fitness Function: The fitness function is used during the selection phase to determine which chromosome to keep or discard.(36) The quality of the result produced by GA is determined by the hyperparameters.The hyperparameters are usually set by experimenting with various values.The four basic parameters are given as follows: Number of Generations: This parameter determines the number of iterations the loop will undergo before the process ends.
Population Size: This is the number of chromosomes that are present in a given population.A small population size implies that there is little search space, and it will be possible for the algorithm to reach a local optimum, while a large search space implies a high computational load during the iterations.(37) Mutation rate: This is the probability that a chromosome will be selected during a generation.The value is between 0 and 1.
Crossover rate: This is the probability that two chromosomes will exchange some of their parts.A probability of 1 means that every new offspring was made by a crossover process, while a crossover of 0 means that the new offspring is an exact copy of the previous generation.(34) Katoch et al. (33) broadly classified GA into five categories namely parallel, multi-objective, real and binary coded, chaotic, and hybrid GAs.In their research, they also gave the pseudocode of a classical GA as shown in Table 1.

Evaluation Metric
In this paper, we employed the use of Mean Absolute Performance Error (MAPE).According to Myttenaere et al., (38) MAPE is recommended when it is more important to measure the relative variations of error than the absolute variation.
The mathematical equation of MAPE is given as: Where   denotes the actual value and   denotes the predicted value of the   instance, and m is the total number of the testing number.The closer the MAPE is to 0, the better the prediction.

Data Collection and Pre-processing
The data set used for this research was retrieved from an online data science community, Kaggle.It was downloaded as a CSV file containing 503, 911 rows and 32 columns.The columns were a mix of weather information and power consumption data of various electrical appliances in the home indexed by a time span of one second.Amongst the columns, we selected the 'gen' column which gave information about the energy supplied by the solar grid as the output column while the weather information columns were used as the input variables Pre-processing of the data set, and every other subsequent activity, was done using Python programming language and Visual Studio Code (VS Code) as the Integrated Development Environment (IDE).A quick look at the features of the data set revealed that every column had complete records and appropriate data types, except for the 'cloudCover' column which had a mix of object and integer data types, and the last row across the data set which was empty.The object data in the 'cloudCover' column was replaced with the median of the column, the empty row in the data set was removed, and the time index was converted from a Unix time stamp to a date-time format.To successfully carry out predictions, all non-integer data type columns were removed.The rows of the data set were also reduced by selecting only the first instance of each day's record.This brought down the total number of rows to 351.Also, the number of weather information columns was reduced from 10 to 8 by selecting features that are more correlated with the output column.Information on the input variables can be seen in Table 2.

Data Visualizations
To optimize the consumption of energy in a home, there has to be a proper analysis of the amount of energy consumed by each appliance or section of the home.Figure 3 shows the average consumption of power in the home daily.From the figure, it is evident that 'Furnace 2', 'Furnace 1', and 'Home office' consumed most of the energy supplied to the home.Also, the energy consumed by 'Kitchen 12', 'Kitchen 14', 'Kitchen 38', and 'Well' was relatively small.

Figure 3 Average daily consumption of home appliances and sections
Figure 4 shows the average power generated by the solar grid each day.From the figure, it was evident that power generation peaked on Tuesdays and dropped on Sundays.

Figure 4
The average power generated by solar in a week

Data Splitting and Normalization
The data set was split into training and test data in the ratio of 4:1.The test data allowed us to validate the accuracy of the model's prediction after the model had been trained using the training data.After splitting, the data was then normalized.Normalization helped all the input features in the data set to contribute equally to the learning process of the model.This paper employed the use of a MinMax scalar from Scikit's preprocessing library to normalize the data to a range of values between 0 and 1.

Modelling
For our baseline model, we set the hyperparameters to the arguments given in Table 3.The model was fed with 80% of the data set and thereafter validated with the remaining 20%.The hyperparameters were continuously adjusted by trying various values and arguments until we got optimal values.After making predictions using only the baseline LSTM model, we further optimized the baseline's weight using the GA algorithm whose hyperparameters are given in Table 4.

Result and Discussion
The result of our research showed that there was a correlation between the weather information and the amount of energy produced by a solar grid in that region.This implied that if the weather forecast of that region could be obtained, then the energy generated by a solar grid sited in that same region could also be predicted, and this would in turn help users optimize energy consumption in their home.
From the data visualization shown in Figure 1, it was possible to identify the high and low-energy-consuming appliances or areas in the home.Although the energy consumption rate of the majority of appliances and areas in the home outweighed the energy production of the solar grid, users can utilize the information when considering expanding their solar systems or when buying new appliances.Information obtained from Figure 2 will help users plan the days that will be suitable for using heavy appliances like the furnace and Fridge.
The MAPE of the baseline LSTM model was 0.15 while our proposed GA-LSTM model recorded an MAPE of 0.07.In a like manner, the RMSE of our baseline LSTM model was 0.0007 while our proposed GA-LSTM recorded an RMSE of 0.0004.This results showed that our proposed GA-LSTM model performed almost two times better than the baseline LSTM model.Table 5 shows the performance evaluation in a tabular form.Although our proposed GA-LSTM required more computational power and time, the reduction in error is quite significant and is worth the extra resources needed.
The high accuracy of our GA-LSTM model could be attributed to the selection of optimal weights by the GA during the training process.Using the principle of natural selection, the GA was able to select a matrix of weights that would give the LSTM model the least error.

Conclusion
The proposed GA-LSTM model was able to make better predictions of solar generation which can lead to the optimization of power in a smart home.GA can optimize the performance of the neural network by tuning the hyperparameters, selecting optimal weight, optimizing learning rate, and selecting features.In this research, GA was used to select optimal weights for the baseline LSTM model.
The data set used for making the prediction contained 8 columns and 351 rows, one row for each day.The input features were temperature, humidity, visibility, apparent temperature, pressure, cloud cover, dew point, and precipitation probability.Energy generated by the solar grid was used as our output variable.The input features proved to be useful in making energy predictions for the energy output of the solar grid.We prescribe the application of this research to smart homes where the energy consumption of electrical appliances can be measured and where the weather forecast of that region is always available.

Disclosure of conflict of interest
No conflict of interest to be disclosed.

Figure 1 A
Figure 1 A schematic diagram of LSTM block memory.(21)A brief description of the components in an LSTM block memory is given below (21) : f (forget gate): decides what should be removed from the cell state.g (activation layer): creates a new vector of values which can be added to the state.i (input gate): selects which value from g to update in the cell state.o (output gate): decides the next hidden state.

( 24 )
LSTM can ignore minimal time lags, up to 1000 steps, by ensuring a constant error flow throughout the data.(25)If the data contains noise, LSTM ignores the spikes.The architecture of LSTM consists of four layers consisting of input, output, forget, and information gates.(21)The LSTM layer architecture is shown in Figure2.

Figure 2
Figure 2 LSTM architecture before water droplets begin to condense precipProbabilityThe probability of precipitation occurring

Table 1
Pseudocode of a GA

Table 3
Hyperparameters for baseline LSTM model

Table 4
Hyperparameter for GA

Table 5
Performance evaluation of models