In this work, an enhanced slime mould algorithm (ESMA) based on neighborhood dimension learning (NDL) search strategy is proposed for solving the optimal power flow (OPF) problem. Before using the proposed ESMA for solving the OPF problem, its validity is verified by an experiment using 23 benchmark functions and compared with the original SMA, and three other recent optimization algorithms. Consequently, the ESMA is used to solve a modified power flow model including both conventional energy, represented by thermal power generators (TPGs), and renewable energy represented by wind power generators (WPGs) and solar photovoltaic generators (SPGs). Despite the important role of WPGs and SPGs in reducing CO2 emissions, they represent a big challenge for the OPF problem due to their intermittent output powers. To forecast the intermittent output powers from SPGs and WPGs, Lognormal and Weibull probability density functions (PDFs) are used, respectively. The objective function of the OPF has two extra costs, penalty cost and reserve cost. The penalty cost is added to formulate the underestimation of the produced power from the WPGs and SPGs, while the reserve cost is added to formulate the case of overestimation. Moreover, to decrease CO2 emissions from TPGs, a direct carbon tax is added to the objective function in some cases. The uncertainty of load demand represents also another challenge for the OPF that must be taken into consideration while solving it. In this study, the uncertainty of load demand is represented by the normal PDF. Simulation results of ESMA for solving the OPF are compared with the results of the conventional SMA and two further optimization methods. The simulation results obtained in this research show that ESMA is more effective in finding the optimal solution of the OPF problem with regard to minimizing the total power cost and the convergence of solution.