The use of renewable energy resources in the production of electrical energy is becoming prevalent due to the decreasing installation costs of these resources and increasing environmental concerns. A Hybrid Renewable Energy System (HRES) is beneficial for meeting load demands, but optimal sizing is the main problem in the process of obtaining a cost-efficient system based on certain load demands and technoeconomic parameters. In this article, the Harmony Search (HS) algorithm was used for the optimal sizing of components and compared to other methods. The HRESs consisted of photovoltaic (PV), wind turbine, battery, diesel generator and inverter components. A powerful rule-based energy management scheme was introduced to manage the power flow between system parts which constitute the microgrid that will minimize the annual system cost and reliably meet the energy demand. The decision variables for this optimization were the PV panel power, wind power, and the number of batteries. Simulation results revealed that HS provided the optimum sizing among the other methods including HOMER, Ant Colony Optimizer and Jaya. The time performances of the algorithms were also examined, and the HS algorithm had better performance and convergence properties. The optimization process was programmed using the MATLAB simulation package.(c) 2022 Elsevier Ltd. All rights reserved.