1 Introduction

Global solar radiation (GSR) is an essential parameter for the design and operation of solar PV energy systems. Nowadays, many tools and approaches are developed to predict different solar radiation components (global, diffuse and direct) [1] and also to simulate the produced energy from PV systems [2]. The combination of photovoltaic (PV) systems with a diesel generator and a storage system is a feasible and key solution for countries willing to install a PV project for power generation. The share of PV power and the use of a diesel generator and/or a battery depend on the selection of the operating modes. Indeed, if the proportion of PV systems is high, the dependency on diesel fuels will decrease as well as the effect of diesel price on the levelized cost of energy (LCOE) [3]. Moreover, the combination with the wind turbine as another renewable power source is more advantageous. For grid or off-grid modes, the design of renewable energy hybrid systems (REHSs) affects considerably the economic and financial parts of the project also defines the electricity price. Some optimization problems can be occurred due to a lack of information regarding load demand. In the case of an oversized system, high payback times, high operating costs and low overall efficiency will result. Furthermore, the operational mode does not allow any space for PV-power feed in and leads to unnecessary energy losses. Otherwise, an undersized system leads to reduce the service quality and to an unreliable supply. Also, the imprecise sizing, potentially carry on higher operation and maintenance costs of the system.

A detailed and efficient system sizing is crucial since either oversized and undersized systems lead to incorrect operation of the hybrid system and lower quality of power supply with higher costs. The optimization of stand-alone hybrid renewable energy systems has begun earlier, using different optimization software tools [4, 5]. Yimen et al. [6] simulated a stand-alone hybrid system using a genetic algorithm in MATLAB. The proposed system comprises photovoltaic, wind, battery and diesel. Techno-economic and emissions analyses are also performed in their study by optimizing the annualized cost (TAC), cost of energy (COE) and carbon dioxide (CO2) emissions. They concluded that the solar-PV-based hybrid system plays a crucial role in the sustainable power supply in rural areas. Recently, Fathi et al. [7] optimized the size of standalone microgrid systems using particle swarm optimization (PSO) and three other algorithms. The optimized systems contain different combinations of diesel generators, solar photovoltaic, wind turbines, fuel tanks and battery energy storage. The net present value of electricity (NPV) was the chosen objective function over a 20 years lifetime. They found that the system including the diesel generator, solar photovoltaic, wind turbine, and the battery is the most economic standalone microgrid, besides PSO as a reliable algorithm. Another hybrid microgrid optimization of PV, wind turbine, battery and diesel is done by Ishraque et al. [8]. In their work, reducing NPC, COE and CO2 emissions were the purpose of the simulation study. They used HomerPro and MATLAB tools in order to analyze proposed strategies and to reach the feasibility analysis, respectively. Resk et al. [9] presented a techno-economic analysis and optimal design of an economical hybrid energy system that contains PV, biomass-CHP, diesel generator, gas boiler, battery and thermal energy storage. The levelized cost of energy consumption and energy production were found to be 0.355 $/kWh and 0.275 $/kWh, respectively. Moreover, Faccio et al. [10] provided an inclusive review presenting the various optimization objectives, optimization methods and tools and the investigated technology (hybrid system configuration). Different comparative studies applying many methods for sizing a stand-alone PV-wind-fuel cell hybrid system found that PSO is the most promising method [11, 12]. The PSO could be used for many optimization objectives, notably, cost reduction [13], sizing design [14] and multi-objective that groups sizing and cost Minimization [15].

Safari et al. [16] optimized a PV-Wind Turbine—Fuel cell (FC) hybrid power system using a developed PSO algorithm. Following their study, the optimal sizing of their system led to an 18% reduction in the investment cost. Baghaee et al. [17] designed a microgrid system composed of wind solar as power generation and hydrogen power as storage. Also, PSO is used in this study to achieve two objective functions, namely, minimize the annualized cost of the system and maximize reliability. Further research has been done regarding sizing optimization of PV-WT hybrid system including hydrogen power as storage [18, 19]. A techno-economic investigation of a standalone hybrid system had been performed by Homayouni et al. [20]. The purpose of their study was to find out the optimal system that could provide cooling, heating and power using the PSO approach. For the grid-connected, Singh et al. [21] modeled a PV-FC hybrid system to feed the electrical load demand of 135 MWh/years. To carry out this investigation, three meta-heuristic algorithms are used including the PSO method and the LCOE found to be 0.104 $/kWh. In Egypt, Barakat et al. [22] investigated a grid-connected PV–wind hybrid system to supply the load of a remote village. Also, the PSO algorithm was used in this study with three objective functions, namely minimizing the cost of energy (COE), loss of power supply probability (LPSP) and maximizing the renewable energy fraction (REF). Sultan et al. [23] developed an artificial ecosystem optimization (AEO) model to optimize grid-connected and off-grid PV/WT/FC hybrid systems with the objective function of minimizing the COE and the LPSP. They found that for an off-grid system, FC has the highest cost, while PV presents the highest capital cost and FC has the highest operation and maintenance (O&M) and replacement cost for the grid-connected configuration. Many other researchers conducted the optimization study of PV–WIND–FC hybrid renewable energy systems for either standalone or grid-connected configurations [24,25,26,27,28,29].

Furthermore, various optimization studies have been achieved using simulation tools with available libraries including different types of renewable energy systems, financial models and projects. Hence, hybrid optimization of multiple energy resources (HOMER) is one of the most used software. Türkay and Telli [30] used HOMER to simulate technical and economic analysis of standalone and grid-connected hybrid energy systems. The results showed that grid-connected configurations present a higher probability of adaptation than the standalone systems with a COE of 0.307 $/kWh. As well, HOMER software could be a simulation and modeling tool to optimize electric and thermal load demand of stand-alone hybrid systems according to the excess energy and waste heat [31]. Also, It is used to test new technology development needs in remote areas with 100% renewable energy hybrid systems [32] and to provide the cost of electricity to satisfy the electric need from PV–WT and Bio-diesel generators hybrid systems [33].

The main goal of designing hybrid renewable energy systems is a reliable supply of the load, under varying weather conditions, with minimum cost. In this study, a hybrid system is designed for 20 years of operation. Moreover, an optimal combination of the number of wind turbines and PV panels, days of autonomy for battery capacity and the number of houses in a remote area in which renewable hybrid energy systems may share for them is achieved by applying PSO. The optimization problem is subject to the maximum allowable reliability index as well as the minimum price of electricity. Furthermore, the configuration with the lowest cost of generating energy (COE) is taken as the optimal one from the configurations that can guarantee the required reliability of the power supply. In this paper, number of houses to be supplied was considered as a parameter to be optimized, then, it is fixed in chosen numbers. Also, generated powers from the hybrid system components are simulated and hydrogen amount from generated energy is calculated. The achieved results were analyzed and compared for four different cities from Morocco, Spain and Algeria.

2 Methodology

2.1 Target regions and weather data

In this work, investigated cities are chosen from Morocco, Spain and Algeria, with approximately the same climate. Table 1 reports the geological and climatic coordinates of selected cities for this optimization study.

Table 1 Geological and climatic coordinates of studied cities

Figure 1 includes the monthly average of the most important parameters used in this optimization study. For solar irradiance, Ouarzazate and Granada receive the highest amount, followed by Bechar and Oujda in the hottest months (Fig. 1, a).

Fig. 1
figure 1

Monthly average of a solar irradiation, b sunshine hours, c temperature and d wind speed

Overall, the highest temperature is recorded in Bechar, with an average of 35 °C in July, followed by Ouarzazate, Oujda and Granada (Fig. 1c). Wind speed reaches an average of 4.85 m/s in May and April in Bechar city, followed by Oujda, Ouarzazate and Granada (Fig. 1d).

2.2 Hybrid system configuration and modeling

2.2.1 System configuration

Figure 2 presents a block representation of the standalone hybrid solar PV-wind turbine (WT)—diesel generator (DG)-battery system. The proposed simulated hybrid system includes PV panels and wind turbines as renewable energy resources connected to a direct current (DC), battery storage, diesel generator, and load profile.

Fig. 2
figure 2

Proposed configuration for the optimized hybrid system

Solar PV modules provide DC power, which is converted to alternating current (AC), and then is connected to the AC bus bar. For WT, AC power is directly generated and it is connected to the DC bus bar using AC/DC converter. Thus, an inverter is used to allow PV modules, WT, and battery storage to be connected to the AC bus bar, which can feed the AC loads.

2.2.2 System component modeling

2.3 Photovoltaic (PV) system

The power generated from a PV panel can be expressed as follows [34]:

$$ P_{{{\text{PV}}}} \left( t \right) = N_{{{\text{PV}}}} \times A_{{{\text{PV}}}} \times \eta_{{{\text{PV}}}} \times G(t) $$
(1)

where, \({\text{N}}_{{{\text{PV}}}}\) presents the number of photovoltaic modules, \({\text{A}}_{{{\text{PV}}}}\) is the area of the PV module, \(\eta_{{{\text{PV}}}}\) is the PV system efficiency and \(G(t)\) is the solar insolation (kW/m2). The efficiency is written as:

$$ \eta_{{{\text{PV}}}} = \eta_{r} \eta_{{{\text{pc}}}} \left[ {1 - N_{T} \left( {\left( {T_{{{\text{air}}}} + \left[ {\frac{{T_{{{\text{nom}}}} - 20}}{800}} \right]R_{t} } \right) - T_{{{\text{ref}}}} } \right)} \right] $$
(2)

where, \( \eta_{r}\) and \(\eta_{pc}\) are the reference and power conditioning module efficiencies, respectively, while \(T_{{{\text{air}}}}\) and \(T_{{{\text{ref}}}}\) denote the temperatures (°C) of the ambient air and cell at the reference conditions. \(T_{{{\text{nom}}}}\) is the nominal cell operating temperature, and \(N_{T}\) is the PV collector efficiency temperature coefficient.

2.4 Wind power system

As known, the delivered power by wind generator is highly proportional to wind speed. Theoretically, the generated power by wind turbine can be expressed as [35]:

$$ P_{{{\text{WT}}}} \left( t \right) = \left\{ {\begin{array}{*{20}c} {0 } \\ {P_{r} } \\ {P_{r} } \\ \end{array} } \right. \times \frac{{v^{3} \left( t \right) - V_{{{\text{ci}}}}^{3} }}{{V_{r}^{3} - V_{{{\text{ci}}}}^{3} }} \begin{array}{*{20}c} {v\left( t \right) \le V_{{{\text{ci}}}}\, \, or \, \, v\left( t \right) \ge V_{{{\text{co}}}} } \\ {V_{{{\text{ci}}}} < v\left( t \right) < V_{r} } \\ {V_{r} < v\left( t \right) < V_{{{\text{co}}}} } \\ \end{array} $$
(3)

where, \(P_{r}\) is the rated power, \(v\left( t \right)\), \(V_{r}\), \(V_{{{\text{ci}}}}\) and \(V_{{{\text{co}}}} { }\) present wind speed, rated velocity, cut-out and cut-in values, respectively.

2.5 Battery

In this study, a bank battery is used to store the excess of the produced power and monitor the power to load. In this context, the consideration of the state of storage (SOC) of the battery bank is important. Indeed, there are two modes [36, 37]:

  • Charging process:

The expression of charging process is written as:

$$ P_{ch} \left( t \right) = \left( {P_{WT} \left( t \right) + P_{PV} \left( t \right)} \right) - \left( {P_{1} \left( t \right)/\mu_{inv} } \right) $$
(4)

where, \(P_{{{\text{WT}}}} \left( t \right)\), \( P_{{{\text{PV}}}} \left( t \right)\) and \(P_{{1}} \left( t \right)\) are the power generated by wind turbine, PV system and the load power. While, \(\mu_{inv}\) denote the efficiency of the converter. Further, \(P_{{{\text{ch}}}} \left( t \right)\) could be replaced by \(E_{{{\text{ch}}}} \left( t \right)\) since hourly iteration time is used.

The expression of charging mode can be written as:

$$ E_{b} \left( t \right) = E_{b} \left( {t - 1} \right) + E_{ch} \left( t \right) $$
(5)

This expression is conditioned by the following condition:

$$ E_{{{\text{ch}}}} \left( t \right) \le E_{b\max } - E_{b} \left( t \right) $$
(6)

where, \( E_{b} \left( {t - 1} \right)\) is the state of the battery at the previous moment and \(E_{b\max }\) is the maximum state of the battery.

Otherwise, in a wasted energy case is appeared due to an excessive [22], load then another expression will be considered:

$$ \left\{ {\begin{array}{*{20}c} {E_{{{\text{dump}}}} \left( t \right) = E_{{{\text{ch}}}} \left( t \right) - \left( {E_{b\max } - E_{b} \left( t \right)} \right)} \\ {E_{{{\text{dump}}}} \left( t \right) = 0} \\ \end{array} if \left\{ {\begin{array}{*{20}c} {E_{b} \left( t \right) = E_{b\max } } \\ {{\text{else}}} \\ \end{array} } \right.} \right. $$
(7)

where, \(E_{{{\text{dump}}}} \left( t \right)\) presents the excess energy at time t.

  • Discharge process:

The expression of discharging process is written as:

$$ P_{dch} \left( t \right) = \left( {P_{1} \left( t \right)/\mu_{inv} } \right) - \left( {P_{WT} \left( t \right) + P_{PV} \left( t \right)} \right) $$
(8)

Also, we can replace \(P_{{{\text{dch}}}} \left( t \right)\) by \(E_{{{\text{dch}}}} \left( t \right)\) since hourly iteration time is used. The expression of discharging mode can be written as:

$$ E_{b} \left( t \right) = E_{b} \left( {t - 1} \right) - E_{{{\text{dch}}}} \left( t \right) $$
(9)

where, \(E_{b} \left( t \right)\) is the state of charge for a time (t) and this expression is conditioned by the following condition:

$$ E_{{{\text{dch}}}} \left( t \right) \le E_{b} \left( {t - 1} \right) - E_{b\min } $$
(10)

where, \(E_{{b{\text{min}}}}\) is the minimum state of the battery.

2.6 Diesel generator

Diesel generator (DG) presents a good alternative for backup power supplies and particularly for non-sunshine hours, which is directly connected to the AC load [38]. The Hourly fuel consumption of the DG is calculated as follows:

$$ F_{g} = A_{g} \times \,P_{ng} + B_{g} \, \times \,P_{g} $$
(11)

where, \(P_{{{\text{ng}}}}\) is the output power of diesel generator (kW), and \(P_{g}\) is nominal power (kW). \(A_{g}\) = 0.246 L/kWh and \(B_{g}\) = 0.0845 L/kWh are the fuel curve slope and intercept coefficients, respectively.

2.6.1 Optimization formulation

2.7 Objective function

The main purpose of this study is to analyze and optimize the proposed hybrid system by reaching the optimum results that present low costs and high energy reliability. Minimizing the cost of generating energy (COE) is a purpose of the optimum design problem, which is an economic assessment of the cost of the energy-generating system, and it could be formulated as follow [24, 30, 39, 40]:

$$ {\text{COE}} = \left( {\frac{{{\text{NPC}}}}{{E_{{{\text{served}}}} }}} \right) \times \left( {\frac{{i\left( {1 + \left( {\frac{i + f}{{1 + f}}} \right)} \right)^{{N_{{{\text{proj}}}} }} }}{{\left( {1 + \left( {\frac{i + f}{{1 + f}}} \right)} \right)^{{N_{{{\text{proj}}}} }} - 1}}} \right) $$
(12)

where, \(E_{{{\text{served}}}}\) presents the primary load served (kWh/year), \({\text{N}}_{{{\text{proj}}}}\) is denotes the project lifetime (20 years), \(i\) and \(f\) present the real interest (9%) and annual inflation (3%) rates, respectively.

NPC is the net present cost, which is expressed as follow:

$$ {\text{NPC}} = \sum\limits_{X} {C^{\text{cap}} + C^{{\text{O}}\& {\text{M}}} + C^{\text{rep}} } $$
(13)
$$ {\text{NPC}} = \sum\limits_{n = 0}^{N_{\text{proj}} } {\left( {C^{\text{cap}} \left( n \right) + \;C^{{\text{O}}\& {\text{M}}} \left( n \right) + C^{\text{rep}} \left( n \right)} \right) \times \frac{1}{{(1 + I_{r} )^{n} }}} $$
(14)
$$ I_{r} = \frac{{(i^{ } {-}f)}}{(1 + f)} $$
(15)

where, \(n\) is the number of the years, \({\text{C}}_{ }^{{{\text{cap}}}} \) is the annual capital cost of X element, \({\text{C}}_{{\text{X - cap}}}\) and \( {\text{N}}_{{\text{X}}}\) are the unit cost and the number of the element X, respectively.

$$ {\text{C}}_{ }^{{{\text{cap}}}} { = }\sum {\text{N}}_{{\text{X}}} {\text{C}}_{{\text{X - cap}}} $$
(16)

\({\text{C}}_{ }^{{\text{O}}\&{\text{M}}}\) is the annual maintenance and operation cost and \({\text{C}}_{{{\rm X}} - {{\rm O}}\& {{\rm M}}}\) denotes the maintenance cost of the X component.

$$ C_{ }^{{\text{O}}\&{\text{M}}} = \sum N_{X} C_{{X - {\text{O}}}\& {\text{M}}} $$
(17)
$$ C_{ }^{{{\text{rep}}}} = \sum N_{X} C_{{X - {\text{rep}}}} $$
(18)

\(C_{ }^{{{\text{rep}}}}\) and \(C_{{X{\text{ - rep}}}}\) denote the annual replacement cost and the replacement cost of the X component, respectively.

To obtain a reliable hybrid renewable energy system (HRES), the system should have sufficient power to feed the load demand during a certain period or even a small loss of power supply probability (LPSP). For this purpose, power reliability analysis is an important step in the system design process. It is an essential parameter to measure the system performance for assumed load distribution. The value of LPSP is limited in the range of 0 and 1 [41].

LPSP of 0 indicates that the load will always be satisfied, and the system is very reliable. While an LPSP of 1 means that the load will never be fulfilled. The expression of LPSP for one year (T = 8760 h) can be written as follows [42]:

$$ {\text{LPSP}} = \frac{{\mathop \sum \nolimits_{1}^{8760} P_{{{\text{deficit}}}} (t)}}{{\mathop \sum \nolimits_{1}^{8760} P_{L} (t)}} = \frac{{\mathop \sum \nolimits_{1}^{8760} \left( {P_{L} (t) - P_{{{\text{generated}}}} (t)} \right)}}{{\mathop \sum \nolimits_{1}^{8760} P_{L} (t)}} $$
(19)

where, \(P_{{{\text{deficit}}}}\), \(P_{L}\), \(P_{{{\text{generated}}}}\) present the power deficit, the total consumer load and the total energy generated, respectively.

Therefore, the objective function could be expressed as:

$$ {\text{MinCOE }}\left( {{\text{N}}_{{{\text{PV}}}} {\text{, N}}_{{{\text{WT}}}} {\text{,N}}_{{\text{DG }}} {\text{, N}}_{{{\text{batt}}}} } \right){\text{ = C}}_{{\text{a}}}^{{{\text{cap}}}} {\text{ + C}}_{{\text{a}}}^{{{\text{OM}}}} {\text{ + C}}_{{\text{a}}}^{{{\text{rep}}}} $$
(20)

where, \({\text{N}}_{{{\text{PV}}}} ,{\text{N}}_{{{\text{WT}}}} ,{\text{ N}}_{{\text{DG }}} ,{\text{ N}}_{{{\text{batt}}}}\) denote the number of PV panels, wind turbine, diesel generator and battery, respectively.

2.8 Constraint

The constraints related to minimum and maximum size of the components are:

$$ {0 } \le {\text{N}}_{{{\text{PV}}}} { } \le {\text{N}}_{{{\text{PV}}}}^{{{\text{max}}}} $$
$$ {0 } \le {\text{ N}}_{{{\text{WT}}}} { } \le {\text{ N}}_{{{\text{WT}}}}^{{{\text{max}}}} $$
$$ {0 } \le {\text{ N}}_{{{\text{DG}}}} { } \le {\text{ N}}_{{{\text{DG}}}}^{{{\text{max}}}} $$
$$ {0 } \le {\text{ N}}_{{{\text{BATT}}}} { } \le {\text{ N}}_{{{\text{BATT}}}}^{{{\text{max}}}} $$
$$ {0 } \le {\text{ LPSP }} \le {\text{LPSP}}^{{{\text{max}}}} $$

2.8.1 Optimization algorithm

The particle swarm optimization (PSO) is an efficient population-based optimization algorithm inspired by animal social behavior, such as fish bird flocking and schooling [43]. Originally, it was developed by Kenedy and Eberhart and was applied for multi-objective and non-linear complex problems [44]. PSO is one of the most popular optimization algorithms because it has a simple concept and can promptly find a reasonably good solution. It is widely used in the size optimization of hybrid systems [45].

The population that formed the PSO is composed of particles and each one represents the possible solution. Within this algorithm, each particle moves and updates its position \(X_{i}\) depending on its previous experience in the searching space with a velocity \( V_{i}\). The position and velocity of the particle in the searching space are presented as vectors, \(X_{i} = \left( {X_{i1} , X_{i2} , \ldots ,X_{id} } \right)\) and \( V_{i} = \left( {V_{i1} ,V_{i2} , \ldots ,V_{id} } \right)\), respectively. Also, the previous best position is denoted as \({\text{pbets}}_{{\text{i}}}\) and it is presented as \(p{\text{betst}}_{i} = \left( {p{\text{best}}_{i1} ,p{\text{best}}_{i2} , \ldots ,p{\text{best}}_{id} } \right)\) [46].

The modified position of the best particle is represented by \(g{\text{best}}_{id}\) and calculated using the following expression [47]:

$$ V_{id}^{k + 1} = w_{k} V_{id}^{k} + c_{1} r_{1} \left( {p{\text{best}}_{id} - X_{id}^{k} } \right) + c_{2} r_{2} \left( {g{\text{best}}_{id} - X_{id}^{k} } \right) $$
(21)
$$ i = 1,2, \ldots N_{p} , d = 1,2, \ldots , N_{g} $$

where, \(N_{p}\) is the number of particles, \(N_{g}\) number of dimensions in a particle, \(V_{id}^{k}\) is the velocity of the i-th particle in k-th iteration, \({\text{w}}\) is weight parameter, \(c_{1}\) and \(c_{2}\) are acceleration constants, \(r_{1}\) and \(r_{2}\) are random numbers in the range [0, 1] and \(X_{id}^{k}\) is the position of the i-th particle in k-th iteration. For each iteration, the particle position is updated, and it is found by adding the velocity vector to the position vector:

$$ X_{id}^{k + 1} = X_{id}^{k} + V_{id}^{k + 1} $$
(22)

To carry out the optimization, different steps [48] are followed and resumed in the flowchart presented in the Fig. 3.

Fig. 3
figure 3

Flowchart to perform the optimization study

Technical, economical and cost information [49,50,51] related to components including PV panel, WT, battery, and DG and other components are presented in Table 2.

Table 2 Technical characteristics of the simulated hybrid system components

3 Results

In this section, the achieved results of each city will be presented and discussed. As mentioned above, the purpose of this work is to find out the optimum design of our proposed system. As the first study, two different months (January and July) are chosen to compare generated powers of PV panels (\({\text{P}}_{\text{PV}}\)), wind turbine (\({\text{P}}_{\text{WT}}\)), and the state of charge of the battery (EB), besides demanded load (PL) and dump load (\({\text{E}}_{\text{dump}}\)) (see Figs. 4, 5, 6, 7).

Fig. 4
figure 4

Generated powers from different components for 24 h in a January and b July for Oujda city

Fig. 5
figure 5

Generated powers from different components for 24 h in a January and b July for Ouarzazate city

Fig. 6
figure 6

Generated powers from different components for 24 h in a January and b July for Granada city

Fig. 7
figure 7

Generated powers from different components for 24 h in a January and b July for Bechar city

Overall, the demanded load records higher values in July than in January. Regarding generated PV, WT, battery and diesel powers, a slight difference between both months is shown. For all cities, PV power is generated in a time interval ranging between 9 and 17 h. Wind energy (PWT) could be generated at any time of the day for the case of Oujda (Fig. 4), Granada (Fig. 6), and Bechar (Fig. 7). Otherwise, from Fig. 5, EWT in Ouarzazate is produced from 9 to 17 h for both January and July.

3.1 Sizing optimization results

In this paper, a standalone hybrid system considering four components (PV, WT, Battery, and DG) is optimally assessed to supply electrical demand to a remote district located in four different cities from Morocco, Spain and Algeria.

The optimal PV/Wind/diesel configurations obtained from the PSO optimization algorithm are summarized in Table 3. The table presents the optimum results for the optimized variables: \({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\), \({\text{N}}_{\text{DG}}\), Nhouse, battery autonomy (ad), COE, RF and LPSP. From the financial aspect, Ouarzazate presents the lowest COE of 0.568 $/kWh presented by configuration 2, followed by Bechar, Granada and Oujda. Although, an optimal selection of a hybrid system configuration depends on a minimum energy cost. Therefore, from Table 3, the optimal structures achieved by the PSO algorithm are (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\), \({\text{N}}_{\text{DG}}\), ad) = (37, 9, 1, 6), (34, 7, 1, 6), (35, 5, 2, 6) and (44, 5, 2, 4) for Oujda, Ouarzazate, Granada and Bechar, respectively.

Table 3 Summary of the obtained results of sizing and cost optimization by PSO algorithm

Produced energy/electricity from hybrid PV/wind/battery/diesel generator system for each studied configuration is presented in Table 4. As indicated in the table, the developed PSO algorithm could provide the generated power from different system components. From Tables 3 and 4, The generated power from different optimized configurations for all investigated cities does not depend only on the number of technologies (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\) and \({\text{N}}_{\text{DG}}\)), but highly depends on the meteorological conditions of the city, namely, the amount of received irradiation, temperature and wind speed.

Table 4 Summary of the achieved output

Figures 811 show the hourly PV power (Ppv), electrical power from wind turbine (Pwt) and diesel generator power (Pdg), besides the state of charge of the batteries (Eb), Load power (Pload) and Dump energy (Edump). The evolutions of the obtained results were presented for one year of study from the configurations 1 and 2 for each city. In the following section, the number of houses is fixed in 5, 10 and 15. Therefore, three configurations for each city will be discussed and then the performance of the best one will be presented.

Fig. 8
figure 8

Generated powers in kW from different components by a conf.1 and b conf.2 for Oujda city

Fig. 9
figure 9

Generated powers in kW from different components by a conf.1 and b conf.2 for Ouarzazate city

Fig. 10
figure 10

Generated powers in kW from different components by a conf.1 and b conf.2 for Granada city

Fig. 11
figure 11

Generated powers in kW from different components by a conf.1 and b conf.2 for Bechar city

3.2 Nhouse is fixed in 5

The obtained solution outcomes from the proposed optimization algorithm for the PV–Wind–Diesel–Battery-based hybrid system are presented in Table 5. It can be seen that the COE of configuration 4 is the lowest for Oujda, Ouarzazate and Bechar compared to the rest of the configurations, while for Granada, configuration 5 provides the lowest COE. The PSO algorithm reached the final point (optimal solution) with (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\),\({\text{N}}_{\text{DG}}\), ad) = (22, 4, 1, 3), (32, 4, 1, 1), (32, 9, 3, 3) and (44, 9, 1, 2) for Oujda, Ouarzazate, Granada and Bechar, respectively as presented in Table 5.

Table 5 Summary of the obtained results of sizing and cost optimization for a fixed house number of 5 by PSO algorithm

Scatter plots presented in Fig. 12 are obtained from the developed PSO algorithm. The circle spot indicated in cyan in each graph represents the optimal result for LPSP, COE, RF and \({\text{E}}_{\text{dump}}\). While the green ones indicate the path and the procedure carried out, varied and iterated from 1 to 5 in order to reach the optimal solution. Thus, the optimal results of (LPSP, COE, RF, \({\text{E}}_{\text{dump}}\)) were (9%, 0.73 $/kWh, 0.29, 15,000 kWh), (10%, 0.81 $/kWh, 0.17, 49,000 kWh), (7%, 1.68 $/kWh, 0.33, 20,000 kWh), (8%, 0.96 $/kWh, 0.09, 79,000 kWh) for Oujda (Fig. 12a), Ouarzazate (Fig. 12b), Granada (Fig. 12c), Bechar (Fig. 12d), respectively.

Fig. 12
figure 12

The performance of the best found configuration for 5 houses in a Oujda, b Ouarzazate, c Granada and d Bechar

3.3 Nhouse is fixed in 10

In the case of supplying the demand of 10 houses, Table 6 reports the results of sizing and cost optimization by PSO algorithm. As mentioned in the Table, COE is varied according to each city, also to the obtained configurations. The lowest COE is found to be 0.99 $/kW which is provided by the configuration 7 in for Ouarzazate, followed by 1.08 $/kW, 1.09 $/kW and 1.68 $/kW for Granada, Oujda and Bechar, respectively. As shown in the Table 6, the best combinations of the optimal solutions are (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\),\({\text{N}}_{\text{DG}}\), ad) = (28, 5, 2, 3), (28, 9, 2, 1), (36, 9, 2, 3) and (31, 5, 3, 1) for Oujda, Ouarzazate, Granada and Bechar, respectively.

Table 6 Summary of the obtained results of sizing and cost optimization for a fixed house number of 10 by PSO algorithm

The best performance of the optimal configuration achieved by the PSO algorithm to supply the load demand of 10 houses for the studied cities is summarized in Fig. 13. For each study case, the best combination (LPSP, COE, RF, \({\text{E}}_{\text{dump}}\)) of solutions is presented by the cyan color and by varying weather conditions, the optimal solutions also vary. Hence, the optimization results were, (10%, 1.09 $/kWh, 0.57, 5000 kWh), (9%, 0.99 $/kWh, 0.37, 27,000 kWh), (10%, 1.08 $/kWh, 0.42, 16,000 kWh), (4%, 1.68 $/kWh, 0.54, 21,000 kWh) for Oujda (Fig. 13a), Ouarzazate (Fig. 13b), Granada (Fig. 13c), Bechar (Fig. 13d), respectively.

Fig. 13
figure 13

The performance of the best-found configuration for 10 houses in a Oujda, b Ouarzazate, c Granada and d Bechar

3.4 Nhouse is fixed in 15

The optimization results of fixing the house number in 15 are presented in Table 7. Therefore, achieved solutions depend on the investigated city and its climatic conditions. The lowest COE was 1.38 $/kW which is belongs to Bechar (Conf.10), followed by Ouarzazate, Oujda, and Granada with COE of 1.62 $/kW, 1.64 $/kW, and 1.89 $/kW, respectively. The optimal solutions found to be (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\), \({\text{N}}_{\text{DG}}\), ad) = (33, 6, 3, 2), (31, 2, 3, 3), (25, 9, 3, 3) and (42, 4, 3, 3) for Oujda, Ouarzazate, Granada and Bechar, respectively.

Table 7 Summary of the obtained results of sizing and cost optimization for a fixed house number of 15 by PSO algorithm

Results of the best designing PV–wind–battery–diesel hybrid system optimized by PSO algorithm to feed 15 houses in the selected remote areas are presented in Fig. 14. The obtained optimal results (LPSP, COE, RF, \({\text{E}}_{\text{dump}}\)) with regards to load feed of 15 houses, which are (12%, 1.65 $/kWh, 0.89, 16,000 kWh), (12%, 1.62 $/kWh, 0.86, 16,000 kWh), (14%, 1.89 $/kWh, 1.3, 19,900 kWh) and (11%, 1.52 $/kWh, 0.71, 14,500 kWh) for Oujda (Fig. 14a), Ouarzazate (Fig. 14b), Granada (Fig. 14c), Bechar (Fig. 14d), respectively.

Fig. 14
figure 14

The performance of the best-found configuration for 15 houses in a Oujda, b Ouarzazate, c Granada and d Bechar

Figure 15 summarizes obtained COE from all mentioned configurations in this study. The COE found for the different cities can be clearly compared and therefore find out the lowest and the optimal solution for the optimized standalone system. As presented above and indicated in Fig. 15, the lowest optimized energy cost was 0.57 $/kWh from conf.2 for Ouarzazate. Then, simulations with conf.4 provides the lowest COE in Oujda of 0.73 $/kWh, followed by 0.89 $/kWh and 1.08 $/kWh for Bechar and Granada, respectively.

Fig. 15
figure 15

The cost of generating energy according to the resulting configurations for each city

The outcomes from Table 8 show the generated energy from different components of the optimized configurations according to the sizing optimization results presented in Tables 5, 6, 7 for Nhouse = 5, 10 and 15. In the case of fixing the number of houses in 5, configuration 4 was the best optimized structure for Oujda and Ouarzazate with a produced diesel power of 13,218.56 kVA and 13,229.6 kVA, respectively. For Granada and Bechar, the optimum configurations provided 16,880.16 kVA and 14,315.2 kVA, respectively. Furthermore, the optimized configurations 6 were the best in the case of Oujda and Granada with 33,701.44 kVA and 31,530.24 kVA of power produced by the diesel generator, respectively. Configurations 7 were the optimum solutions for Ouarzazate and Bechar with 30,330.56 kVA and 39,953.76 kVA of diesel power production, respectively. In the optimization study of 15 houses, it was found that configurations 10 presented the optimum results for Oujda and Bechar, and the obtained \({\text{P}}_{\text{dg}}\) were 60,112.8 kVA and 47,703.84 kVA, while for Ouarzazate and Granada, 59,604.96 kVA and 70,512.5 kVA were achieved from the configurations 11, respectively.

Table 8 Summary of achieved outputs for the case of varied house number

Table 9 shows a comparison with earlier publications in the optimization of PV–wind–diesel–battery hybrid system.

Table 9 Comparison between our work and some earlier publications

Figure 16 shows the total electricity generated by the optimized hybrid system for all the configurations discussed above. The amount of electricity differs within the implemented configuration and the target climates. To overhaul the sizing optimization of the configurations, Tables (3, 4, 5, 6, 7, 8) include whole information and details.

Fig. 16
figure 16

The total electricity output generated according to the resulting configurations for each city

The simulated amount of electrolytic H2 to be produced is carried out in two steps: first, the electrical power needed for the electrolysis process is estimated from the PV and wind systems by PSO approach. Then, in the second step, the amount of H2 is calculated by using the following equation [55]:

$$ M_{{{\text{H}}_{2} }} = \eta_{{{\text{elec}}}} \times \frac{{E_{T} }}{{{\text{HHV}}_{{{\text{H}}_{2} }} }} $$
(23)

where, \({\text{M}}_{{{\text{H}}_{{2}} }}\) [kg] is the mass of hydrogen produced, \({\upeta }_{{{\text{elec}}}}\) is the electrolyzer system efficiency (79%), \({\text{HHV}}_{{{\text{H}}_{{2}} }}\) presents the hydrogen higher heating value (39.4 kWh/kg), and \({\text{E}}_{{\text{T}}}\) [kWh] denotes the simulated total electricity output.

Figure 17 presents the produced hydrogen from the total electricity generated by the optimized hybrid system according to each obtained configuration. The expression described in Eq. (1) is used to calculate the amount (kg) of hydrogen likely produced from the electricity generated from the stand-alone PV–WIND hybrid system.

Fig. 17
figure 17

The produced hydrogen from the total electricity according to the resulting configurations for each city

4 Conclusion

In this paper, the principal objective of designing a hybrid renewable energy system is a reliable supply of load with a minimum cost of energy by varying weather conditions and the number of houses in the chosen remote areas. In this work, the PSO method is used to optimize a hybrid system for 20 years of operation in four cities located in Morocco, Spain and Algeria. Therefore, several aspects of the optimization and simulation with MATLAB are represented in this study. The proposed system comprises PV panels, wind turbines, diesel generators, and batteries. The objective function of this optimization study was the optimal combination-solutions of number of PV panels (\({\text{N}}_{\text{PV}}\)), number of wind turbines (\({\text{N}}_{\text{WT}})\), number of diesel generators (\({\text{N}}_{\text{DG}})\), days of autonomy for battery capacity (ad) and the number of houses (\({\text{N}}_{\text{house}}\)) in the first part of the study. Afterward, the optimization study was subjected to a fixed number of houses (5, 10 and 15) under the different selected areas. In addition to the obtained optimal configurations for the sizing optimization (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\), \({\text{N}}_{\text{DG}}\), ad, \({\text{N}}_{\text{house}}\)), other significant parameters are provided by the developed PSO algorithm. Namely, the cost of energy (COE), renewable factor (RF), loss of power supply probability (LPSP), the excess of energy (\({\text{E}}_{\text{dump}})\), and the energy generated by the components of the simulated hybrid system. Based on the achieved simulation and optimization results of this study, and which are presented above the main found outcomes were:

  • (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\), \({\text{N}}_{\text{DG}}\), ad, \({\text{N}}_{\text{house}}\)) = (37, 9, 1, 6, 3), (34, 7, 1, 6, 8), (35, 5, 2, 6, 4) and (44, 5, 2, 4, 11) for Oujda, Ouarzazate, Granada and Bechar.

  • To feed the load demand for 5 houses, the carried out simulation results of (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\),\({\text{N}}_{\text{DG}}\), ad, LPSP, COE, RF, \({\text{E}}_{\text{dump}}\)) were (22, 4, 1, 3, 9%, 0.73 $/kWh, 0.29, 15,000 kWh), (32, 4, 1, 1, 10%, 0.81 $/kWh, 0.17, 49,000 kWh), (32, 9, 3, 3, 7%, 1.68 $/kWh, 0.33, 20,000 kWh) and (44, 9, 1, 2, 8%, 0.96 $/kWh, 0.09, 79,000 kWh) corresponding to Oujda, Ouarzazate, Granada and Bechar, respectively.

  • To supply the load demand for 10 houses, the optimal combination solutions (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\),\({\text{N}}_{\text{DG}}\), ad, LPSP, COE, RF, \({\text{E}}_{\text{dump}}\)) found to be (22, 4, 1, 3, 10%, 1.09 $/kWh, 0.57, 5000 kWh), (32, 4, 1, 1, 9%, 0.99 $/kWh, 0.37, 27,000 kWh), (32, 9, 3, 3, 10%, 1.08 $/kWh, 0.42, 16,000 kWh) and (44, 9, 1, 2, 4%, 1.68 $/kWh, 0.54, 21,000 kWh) for Oujda, Ouarzazate, Granada and Bechar, respectively.

  • To feed the load demand for 15 houses, the best obtained configurations were (\({\text{N}}_{\text{PV}}\), \({\text{N}}_{\text{WT}}\),\({\text{N}}_{{\text{D}}{\text{G}}}\), ad, LPSP, COE, RF, \({\text{E}}_{\text{dump}}\)) of (33, 6, 3, 2, 12%, 1.65 $/kWh, 0.89, 16,000 kWh), (31, 2, 3, 3, 12%, 1.62 $/kWh, 0.86, 16,000 kWh), (25, 9, 3, 3, 14%, 1.89 $/kWh, 1.3, 19,900 kWh) and (42, 4, 3, 3, 11%, 1.52 $/kWh, 0.71, 14,500 kWh) for Oujda, Ouarzazate, Granada and Bechar, respectively.

  • The produced energy is highly dependent on the weather condition of the investigated region, for example, the amount of solar irradiation received by the area.

  • The amount of hydrogen was also calculated from the produced energy by the hybrid renewable energy system optimized in the present investigation.

Furthermore, future investigations will focus on developing an algorithm for the optimization of a hybrid system with solely renewable recourses using one and multi-objectives in order to maximize the benefits of this system from different aspects.