Using hybrid renewable energy systems is a smart choice for reducing the carbon emitted by power plants. It also helps climate change mitigation and global warming, leading to universal health for humans and the environment. The efficiency of these systems depends on choosing the right combination of renewable sources, their sizes, and proper scheduling of the generating units. This paper suggests an optimisation method for sizing a geothermal/PV/wind/diesel system, both in off-grid and grid-connected configurations. In the grid-connected mode, the possibility of selling surplus energy generated by renewable resources to the network is considered. Investigations reveal that without accurate control of geothermal reservoirs, they may become depleted. In this study, a strategy is suggested for unit commitment; moreover, the harmony search algorithm is used to find the optimal size of the hybrid system in both configurations. The effectiveness of the proposed approach is represented by simulating an HRES for Ferdows/Iran. [ABSTRACT FROM AUTHOR]