The considerable amount of energy utilized by buildings has led to various environmental challenges that adversely impact human existence. Predicting buildings' energy usage is commonly acknowledged as encouraging energy efficiency and enabling well-informed decision-making, ultimately leading to decreased energy consumption. Implementing eco-friendly architectural designs is paramount in mitigating energy consumption, particularly in recently constructed structures. This study utilizes clustering analysis on the original dataset to capture complex consumption patterns over various periods. The analysis yields two distinct subsets that represent low and high consumption patterns and an additional subset that exclusively encompasses weekends, attributed to the specific behavior of occupants. Ensemble models have become increasingly popular due to advancements in machine learning techniques. This research utilizes three discrete algorithms, namely Artificial Neural Network (ANN), K-nearest neighbors (KNN), and Decision Trees (DT). In addition, the application employs three more machine learning algorithms bagging and boosting: Random Forest (RF), Extreme Gradient Boosting (XGB), and Gradient Boosting Trees (GBT). To augment the accuracy of predictions, a stacking ensemble methodology is employed, wherein the forecasts generated by many algorithms are combined. Given the obtained outcomes, a thorough examination is undertaken, encompassing the techniques of stacking, bagging, and boosting, to conduct a comprehensive comparative study. It is pertinent to highlight that the stacking technique consistently exhibits superior performance relative to alternative ensemble methodologies across a spectrum of heterogeneous datasets. Furthermore, using a genetic algorithm enables the optimization of the combination of base learners, resulting in a notable enhancement in prediction accuracy. After implementing this optimization technique, GA-Stacking demonstrated remarkable performance in Mean Absolute Percentage Error (MAPE) scores. The improvement observed was substantial, surpassing 90 percent for all datasets. In addition, in subset-1, subset-2, and subset-3, the achieved R2 scores were 0.983, 0.985, and 0.999, respectively. This represents a substantial advancement in forecasting the energy consumption of residential buildings. Such progress underscores the potential advantages of integrating this framework into the practices of building designers, thereby fostering informed decision-making, design management, and optimization prior to construction.