In the last couple of decades, numerous energy management strategies have been devised to mitigate the effects of greenhouse gas emission, hence introducing the concept of microgrids. In a microgrid, distributed energy generators are used. Microgrid enables a point which ameliorates in exchanging power with the main grid during different times of day. Based on the system constraints, in this work, we aim to efficiently minimize the operating cost of the microgrid and shave the power consumption peaks. For this purpose, we introduce an improved binary bat (iBBat) algorithm which helps to schedule the load demand of smart homes and energy generation from distributed generator of microgrid to the load demand and supply. The proposed energy management algorithm is applied to both grid-connected and islanded modes of the microgrid. The constraints imposed on the algorithm ensure that the load of electricity consumer does not escalate during peak hours. The simulation results are compared with BBat and binary flower pollination algorithm, which validate that the iBBat reflects substantial reduction in operating cost of microgrid. Moreover, results also show a phenomenal reduction in the peak-to-average ratio of load demand from main the main grid. operate in two modes: grid-connected and island modes. In a grid-connected mode it sells the surplus energy to main grid and buys from it when its energy generation is less than the demand. In this mode microgrid is always connected to main grid. On the other hand, an island mode is useful mostly in cases when power supply is interrupted due to the detection of any fault in the grid or in regions such as Russia, where 60% of the territories are not connected to the utility due to their geographical positions [3]. In this case, the connection between microgrid and main grid is terminated.Optimally scheduling the microgrid resources has become a hot research topic from a couple of years. Devising a strategy for managing energy also puts a great impact to optimize the generation pattern of the microgrid in either of its modes. The authors in [4] noticed the large integration of RESs in the microgrid due to which the use of ESS dramatically increased. For this purpose, the authors introduced a bat algorithm to develop corrective strategies to perform least cost dispatches. The authors in [5] employed flower pollination algorithm (FPA) to schedule home appliances to balance the load demand of consumer for demand side management (DSM). Moreover, they put emphasis on the reduction of peak-to-average ratio (PAR) and electricity cost. Zhang et al. [6] introduced a method which helps to schedule the microgrid resources. For this purpose, they proposed a hybrid optimization algorithm. Muhammad et al. [7] introduced an architecture which integrates RESs. A mix-mode energy management strategy is introduced in [8]. It also presents a battery sizing method which helps to operate the microgrid with a minimum operating cost.The power mix generation has a great impact on the cross-country rel...