A Comprehensive Model for the Design of a Microgrid under Regulatory Constraints Using Synthetical Data Generation and Stochastic Optimization

Àlex Alonso 1,*, Jordi de la Hoz 1, Helena Martín 1 , Sergio Coronas 1 , Pep Salas 2 and José Matas 1 1 Electric Engineering Department, Escola d’Enginyeria de Barcelona Est, Polytechnic University of Catalonia, 08019 Barcelona, Spain; jordi.de.la.hoz@upc.edu (J.d.l.H.); m.helena.martin@upc.edu (H.M.); sergio.coronas@upc.edu (S.C.); jose.matas@upc.edu (J.M.) 2 km0.Energy, Carrer de Lepant, 43, 08223 Terrassa, Barcelona, Spain; pep.salas@km0.energy * Correspondence: alexandre.alonso.travesset@upc.edu


Context and Definitions
Motivated by the 2030 climate targets, the installation of distributed generation facilities is starting to proliferate in the European Union (EU) [1]. The distributed generation paradigm aims that energy would be produced locally by an aggregation of at most a few hundreds of prosumers. This aggregation, called an energy community (EC) [2], makes use of the resources in their immediate environment to produce the energy that will satisfy their own needs. These resources are eminently renewable, simply because they are the most widespread geographically and the ones that a small group of people is capable of exploiting. The structure that integrates all the elements of production, distribution and consumption in an EC is called a microgrid.
In this study, a grid-connected microgrid is assumed. This structure allows the aggregation of the prosumers while guaranteeing the access to the electricity grid supply in case of not having enough in

Review of the State of the Art, Justification and Objectives of the Paper
Several studies have been of paramount importance in the optimization of the design and management of microgrids under uncertainties. Farsangi et al. present in [6] a two-stage stochastic program aimed to minimize the operational cost in a microgrid with electrical and thermal loads. The uncertainties covered are renewable generation, load and electricity price, and scenarios are generated based on theoretical probability distributions such as Normal or Beta. Li et al. [7] use a combination of mixed integer linear programming and a metaheuristic algorithm to optimize both sizing and operation of a microgrid. A sensitivity analysis is used to account for uncertainties. In [8], Shin et al. elaborate a day-ahead energy management with a detailed model of wind uncertainty using a Markov decision process. Thomas et al. [9] integrate the electric vehicle as a source of uncertainty, alongside photovoltaic (PV) generation. PV measurements are clustered, while synthetical driving profiles are generated to account for randomness in the electric vehicle availability. A different approach is employed by Fazlalipour et al. [10] who investigate the optimal participation of a renewable microgrid in the market, with a non-linear program. A lognormal probability distribution is assumed to model price uncertainties. Nikmehr et al. [11] consider different demand response programs and employ a PSO algorithm to optimize the operation of a microgrid. Normal and Weibull distributions are used for the scenario generation of renewable generation and load. Risk measures originated in economics are applied in the field of energy planning by Narayan and Ponnambalam [12] in a two-stage stochastic program which take into account generation and load variabilities. Correa-Florez et al. [13] present a day-ahead operation of a local energy management system taking into account the battery life cycle and also uncertainties in load and PV generation. This results in a nonlinear program which is decomposed with a metaheuristic algorithm. Wang et al. [14] consider also market price uncertainty by means of a time series. The two stages of the program are devoted to the energy management, being the granularity in the second stage higher than in the first. Lastly, in [15], Mavromatidis   Considering the undertaken search on the state of the art of mathematical programming in microgrids under uncertainty, the present paper is justified because of the following reasons: • Most of the works analyzed consider the effects of uncertainty on renewable generation, some others on the load, but there are few works which take into account the effects of the three principal sources of uncertainty at the same time: generation, load and market price. • Some of the works show the virtues of an innovative metaheuristic algorithm, but more than often this algorithm is complex, leading to a non-reproducible work.

•
Most of the models deal with random variables with a simple model based on theoretical probability distributions, such as the normal distribution. This characterization does not reflect accurately the behavior of these random variables.

•
Most of the results lose accuracy when considering a time horizon of a few days or a daily time step, instead of an hourly one. • Generally, when modelling an EC, an aggregated formulation is employed, meaning that the renewable generation and load of all the prosumers of the community is considered as one.
A disaggregated formulation allows going into detail on how the energy management between different prosumers is done.

•
In general, the models addressed to characterize the EC ignore the regulatory restrictions and its effects on the energy management and economic results.
In order to cover the identified gaps present in the scientific literature, the contributions of the study are aimed to provide:

•
A mathematical model which allows analyzing the viability of the investment in a microgrid for an energy community. This model embeds the regulatory constraints of the EC microgrid. The microgrid is supplied by solar and wind power. This model, besides optimizing the design of the microgrid, allows analyzing how the energy management is done. For this purpose, a disaggregated formulation is applied, the time horizon is set to one year and the time step to one hour. • Two-stage stochastic programming (SP) has been employed to take into account the randomness of the sources of uncertainty, these being: renewable generation, electrical consumption and electricity market price. The first stage corresponds to the design stage, while the second stage corresponds to the energy management during the time horizon. Each source of uncertainty has been characterized with models present in scientific literature, and to train these models, the information contained in databases is used as far as possible.

•
The model allows for reproducibility, modification, and expansion. The mathematical model is in fact a core where different submodels for forecasting the renewable generation, loads and market prices can be coupled to. Besides, the formulation of the model can change to include different types of energy storage, different configurations of the energy community, and the presence or not of incentives and taxes.

Microgrid Topology
The configuration of the microgrid is schematized in Figure 2. The EC consists of a number D of dwellings which are constituted as prosumers. Each dwelling comprises a series of loads, which are described by their energy consumption (C d,h,s ), and a PV facility, which is described by its generated energy (EG d,h,s PV ) and peak power (P peak PV d ). Each dwelling has an available area to install solar panels Smax d . The microgrid on this study is considered to be 3-phase AC. Therefore, PV inverters are required to transform direct current produced by solar panels to alternating current. Each of the dwellings is assumed to have also a 3-phase main breaker, even though the loads can be both single-phase and 3phase. Besides, a centralized generation facility is considered. Centralized generation takes the form of a small-scale wind power plant which consists of a number N_WT of wind turbines. This power plant supplies energy equal to EG h,s WT in form of alternating current. The electricity produced by wind turbines goes through a rectifier, whose output is connected to a centralized battery bank, of capacity Bcap. The battery bank main function is to resolve the intermittency of wind-produced electricity.

Microgrid Topology
The configuration of the microgrid is schematized in Figure 2. The EC consists of a number D of dwellings which are constituted as prosumers. Each dwelling comprises a series of loads, which are described by their energy consumption (C d,h,s ), and a PV facility, which is described by its generated energy (EG PV d,h,s ) and peak power (P PV peak d ). Each dwelling has an available area to install solar panels Smax d . The microgrid on this study is considered to be 3-phase AC. Therefore, PV inverters are required to transform direct current produced by solar panels to alternating current. Each of the dwellings is assumed to have also a 3-phase main breaker, even though the loads can be both single-phase and 3-phase. Besides, a centralized generation facility is considered. Centralized generation takes the form of a small-scale wind power plant which consists of a number N_WT of wind turbines. This power plant supplies energy equal to EG WT h,s in form of alternating current. The electricity produced by wind turbines goes through a rectifier, whose output is connected to a centralized battery bank, of capacity Bcap. The battery bank main function is to resolve the intermittency of wind-produced electricity. Additionally, it can be used as a backup in case of a blackout in the main grid. When the battery is full, the excess energy of the wind power plant is dumped to the grid. The EC considered in this article is created from an existent residential area. In consequence, the connection with the electrical grid is maintained. In order to maintain voltage and frequency within the desired values, PV inverters act as regulators for the solar energy, whereas for the energy that comes from the wind power plant and the battery bank, another regulator is used.
Each line is sized in order to admit a maximum current of I max . It is supposed that regulators act in order to maintain a constant voltage through all the coupling points of the microgrid. In addition, reactive loads are considered negligible compared to active loads. Therefore, the current limitation can be translated to an active power limitation PLineMax.
Energies 2020, 13, x FOR PEER REVIEW  5 of 26 Additionally, it can be used as a backup in case of a blackout in the main grid. When the battery is full, the excess energy of the wind power plant is dumped to the grid. The EC considered in this article is created from an existent residential area. In consequence, the connection with the electrical grid is maintained. In order to maintain voltage and frequency within the desired values, PV inverters act as regulators for the solar energy, whereas for the energy that comes from the wind power plant and the battery bank, another regulator is used. Each line is sized in order to admit a maximum current of I max . It is supposed that regulators act in order to maintain a constant voltage through all the coupling points of the microgrid. In addition, reactive loads are considered negligible compared to active loads. Therefore, the current limitation can be translated to an active power limitation PLineMax According to Figure 2, different nodes by which the electricity flow goes through can be identified. These nodes are: PV generation, wind power generation, storage, consumption, and main grid. The energy flows and their denomination in the mathematical program are depicted in Figure  3.

Model Formulation
As already mentioned in Section 1.2, the merits of the presented model rely on embedding the regulatory constraints into the making decision as well as a comprehensive treatment of the uncertainty.

Physical Constraints
The physical model and energy flow of the microgrid is depicted according to the following equations and Tables 1-5. Firstly, all energy balances must be satisfied according to what is shown in Figure 3.
The variables that figure in the energy balances must be bounded appropriately:

Model Formulation
As already mentioned in Section 1.2, the merits of the presented model rely on embedding the regulatory constraints into the making decision as well as a comprehensive treatment of the uncertainty.

Physical Constraints
The physical model and energy flow of the microgrid is depicted according to the following equations and Tables 1-5. Firstly, all energy balances must be satisfied according to what is shown in Figure 3.
The variables that figure in the energy balances must be bounded appropriately: Energies 2020, 13, 5590 7 of 26 The design variables are constrained to a maximum value: An informative variable is created in order to express the results in terms of peak installed power instead of occupied area.
The flows that go through a line cannot surpass the maximum capacity of that line: The constraints related to the energy storage ensure that the total installed capacity and the charge and discharge power are below a certain limit: It is also ensured that the energy inside the batteries is updated in each time period while not surpassing the installed capacity. An initial charge is set in order for the updating constraint to be consistent for all time periods:   Wind turbine supplied power 0-10 kW

Regulatory and Economic Constraints
The electrical bill without microgrid is described by Equations (22)- (24), and is based on the Spanish billing structure. The energy cost of each dwelling (CE SA d,s ) covers the costs of energy production plus a percentage of losses, while the access cost (PA SA d,s ) covers the costs of the electrical grid and the renewable energy premiums. Grid charges (Te h and Tp) change depending on the electricity contract, but in this case, it is considered that all housings possess the same contract, with a constant price throughout the day. The total cost (FSE SA d,s ) considers, as well, the taxes (the electricity tax and the VAT, of 5.1% and 21%, respectively): The electrical bill, in the case of using a microgrid under the self-consumption scheme, is described by Equations (25)- (27): The incomes and taxes related to the excess of energy injected into the grid, and the economic cost of using the microgrid, are described by Equations (28)- (30): Regarding the operation and maintenance (O&M) costs, these are eminently flat (€/kW·year) and in the case of the generation elements, are extracted from [16]. Regarding the batteries, the costs are extracted from [17]. The costs are heavily influenced by the operational regime of the batteries: number of cycles and depth of discharge (DoD). These costs are assumed for an operational regime of 1 cycle/day and 80% of DoD.
The new rated power for each dwelling is reduced depending on the average storage installed capacity: where F is a dimensionless parameter which allows modifying the trade-off between battery capacity and reduction of power. It also constrains the installation of a very high battery capacity. In Equation (31), the standardization of the rated power is dismissed for the sake of simplification.
In Tables 6-11 the values and definition of each economic parameter can be consulted. Te h Variable charges over purchased electricity [16] 0.044027 €/kWh Tp Flat charges over purchased electricity [16] 38.04 €/kWh·year Table 9. Parameters related to the sources of uncertainty or stochastic parameters of the economic constraints.

Parameter Definition Value
MarketPrice h,s Purchase price of energy at the electricity market 0-180 €/MWh  Table 11. Parameters related to investment cost.

Description of 2-Stage Stochastic Programming
In order to account for the variability of different future scenarios, 2-stage stochastic programming has been employed. The two stages of the program are divided by the moment of realization of the random variables. The first stage, or design stage takes place before knowing the realization while the second or management stage takes place after that. Defining by x the set of first-stage decisions, by y the set of second-stage decisions and by ξ the set of uncertain parameters, the 2-stage stochastic program can be written as [18]: where: where c and q are the first and second stage costs respectively, and the set of second-stage constraints is characterized by T(ξ)x + W(ξ)y = h(ξ). Assuming that ξ is continuous, its expected value is calculated as: However, computing this integral is practically impossible in most of the cases. This is why the equivalent deterministic form is used. Every uncertain parameter ξ i is discretized as a finite set of scenarios. Each scenario represents a plausible realization of the random variables that occurs with a certain probability. The stochastic program is then reformulated into a set of deterministic programs where: where ω s is a scenario or realization of the random variables. To generate the scenarios, the models described in Section 2 are employed, and it is assumed that each source of uncertainty is independent of each other. Solving the problem with this formulation leads to the optimal solution (x*, y * (ω s )).

Objective Function
The main goal of the program is to ensure that the microgrid is economically feasible by deciding which combination of elements has to be installed and performing the most cost-effective operation for all the energy community. The microgrid would be less likely to be installed in case that its operation was more expensive than in the standard case, meaning purchasing all the electricity from the grid. Therefore, the operation costs of the standard case and the self-consumption case must be compared. These operation costs are given in their majority by the electricity bill that the power company handles to the customer. Installing a renewable energy facility might reduce the energy dependence on the grid, if it is appropriately designed, and in consequence, the price of the bill. Besides, generating energy allows the community to act as a producer and perceive earnings from the sale of electricity. The main drawback is that the maintenance of the facility is severally assumed by the energy community, and not by the system operator. The maintenance costs are here assumed to be a linear function of the installed capacity.
It is thus considered that the facility is economically viable if the investment costs are recovered in less than the lifetime of the facility. This time is called the payback time. The shorter the payback time, the higher the viability of the microgrid. Therefore, the deterministic objective function can be written as: where CInv is the total investment cost, LSpan the lifespan of the facility in years so the ratio of CInv to Lspan represents an undiscounted annualized investment cost. The term inside the sum represents the energy management cost during a year of operation. It consists of the operation and maintenance costs, the savings in the energy purchase respect to the case without self-consumption, and the net income for the sale of electricity. Note that by dividing CInv LSpan it is implied that the time horizon is of one year. Likewise, the effects of inflation and depreciation through the years are neglected.
Considering now the two-stage stochastic formulation, it is clear that the first-stage cost corresponds to the investment cost. The investment is made while the values of solar irradiance, wind speed, electrical consumption and market price remain unknown. In other words, CInv is independent of the scenario s. On the other hand, the energy management, meaning the flux of energy that goes through the different elements of the facility (generators, batteries, loads and grid) changes depending on the values of the aforementioned sources of uncertainty; hence it depends on s. The stochastic objective function can be written as: where s∈S Prob s = 1 and CInv is defined as following, taking into account that the battery lifespan LBat is lower than the one of the facility:

Uncertainty Analysis
In this section, the characterization of the sources of uncertainty is described. The sources of uncertainty under consideration are, as mentioned before, renewable generation (solar and wind power), load and electricity market price. The methodology to implement this characterization is the same for all four sources: models present in scientific literature have been sought, one of them has been chosen on the basis of the good relation effectiveness -simplicity, and subsequently it has been adapted for their use as synthetic data generators.

Solar Generation
Solar irradiance under clear-sky conditions can be modelled deterministically [19]. Nevertheless, it is because of the presence of clouds and other particles that reflect and refract the solar radiation that the irradiance can be considered a random variable [20]. There exists a variety of methods to model solar irradiance under real conditions but there is still no consensus about which models fit better: deterministic [21,22] or stochastic [23][24][25][26][27][28][29][30][31].
The model that has been chosen in this article to model solar irradiance is a seasonal ARIMA or SARIMA [32]. The SARIMA model is chosen because it tackles the seasonality caused by the rotation Energies 2020, 13, 5590 13 of 26 movement of the earth. Solar radiation data has been extracted from the Helioclim database [33], and is modelled indirectly, using a Clear-Sky-Index [19]: where the Clear-Sky Index k is a parameter between 0 and 1 that indicates the proportion of irradiance that reaches the ground level respect the irradiance that would reach if the sky was completely clear Therefore, values of k do not depend on the season of the year.

Wind Power
In order to forecast the wind resource, a wide range of methods have been employed, both deterministic [34,35] and stochastic [36,37].
For the purpose of this article, it is necessary to use a model that is able to generate plausible scenarios from a microscopic (correlation between two consecutive values of wind speed) and a macroscopic (monthly generated energy) point of view. The method that is used is adapted from [38]. The main advantage of this model is that it allows working on daily mean values and having as a result a time series of hourly values. To this end, the following equation is employed: where v h is the wind speed at hour h, v d is the daily mean speed and h p is the hour when maximum speed has been recorded. To the resulting values, an autoregressive model of first order is applied: where a is the autocorrelation coefficient. The series has now consistency from an hourly point of view but it does not lead to the expected monthly generated energy. That is because the resulting series follows a Normal distribution, while wind speed is better approximated by a Weibull distribution. In consequence, a mapping has to be done to let the data of the time series follow a Weibull distribution. The mapping is done by matching the cumulative distribution functions of both Normal and Weibull, which result in the following equation: For this project, daily mean and maximum wind speed data have been extracted from AEMET database [39]. The parameters c and k of Weibull distribution are chosen depending on the location, by consulting wind atlas such as DTU Global Wind Atlas, NASA MERRA-2 or CNER Spanish Atlas [40][41][42]. In practice it is useful to consider different Weibull distributions for each season and obtaining the parameters using this relation: where v is the quarterly mean wind speed and Γ is the gamma function.
Wind power as a function of wind speed is essentially given by the power curve of the wind turbine. This curve can be divided in three zones: A first zone where the wind speed is below the cut-in value and the wind turbine is not working. Then, a second zone where the power follows the following theoretical law: where C p is the coefficient of power of the wind turbine and ρ is the density of the wind. In practice, a polynomial regression is employed to extrapolate the power from the wind turbine power curve. Finally, there is a third zone where the output reaches the rated power value and the power is limited disregarding the speed of the wind. The turbine is stopped when the speed surpasses a cut-out value for safety reasons.

Load
Aggregated demand shows some regular features, and it can be forecasted accurately if factors like season, day of the week and weather conditions are taken into account. It is, nevertheless, the other way around when analyzing individual demands, especially if the analysis is performed on the residential sector. Besides physical and economic conditions, in a dwelling, factors such as the number of appliances, work schedule, holidays and the financial capacity of the dwellers must be taken into account.
There are some works which try to model load in a disaggregated way [43][44][45][46][47]. The approach employed in this paper instead is to generate synthetical data from aggregated values. To do this, the load profiles are created from the probability of use of different electrical appliances. The method is adapted from [48] and the demand profiles are obtained from [49]. For each house and device, a random uniform number R1 d,e between 0 and 1 is generated: Once the devices of each housing have been defined, the demand profiles can be created. A second random number R2 d,e ,t is generated, this time for each dwelling, appliance, and time step: where e' is the set of appliances taking into account the rate of possession (different in each dwelling) and t is the hour. u e,t is the probability that the appliance e is turned on at time t. If R2 d,e ,t > u e,t the appliance is on stand-by. Otherwise, the appliance is switched on and remains on during L e time steps. During this time, the device consumes an energy equal to E d,e ,t . Additionally, when a device is on, it cannot be switched on again and after it is switched off, the probability that it is turned on again becomes lowered by a factor β.
R2 d,e ,t < u t → E d,e ,t..t+L e = P e ·F e ·R e ·∆ T ; u t..t+L e = 0 u t+L e ..T = u t β where P is the nominal power, F is the ratio between nominal power and mean power, R is the ratio between the real time that the device is on and the number of defined time steps L e and ∆ T is the length of the time step. Finally, the consumption of a household is equal to the sum of the consumption of all its appliances: Energies 2020, 13, 5590 15 of 26

Market Price
The electricity market price fits into the field of financial time series, and for that reason, many forecasting methods can be applied to it [50][51][52][53]. The particular model employed in this paper is based on [54]. It takes into account the strong relation between the price of energy and the demand, and allows to capture the typical features that are present in this financial series such as seasonality, price spikes, and rapid reversion to the mean. The equation of the model, simplified from the aforementioned article, is the following: where MarketPrice h is the energy price at the hour h, L h is the energy demand, v h is the availability of power, meaning the ratio between available power and installed capacity, and X h is a stochastic process which describes the short-term variability of the energy price. v is assumed to be deterministic and the load is assumed to have two components: whereL h is the deterministic forecast of the demand, which can explain most of the variability, and L h is the error, which can be modelled as a SARIMA time series with a frequency of 24 h. Function f relates the load with the logarithm of energy prices. To determine the coefficients of the function, a polynomial regression is employed. The inclusion of the availability term v t is showed to improve the performance of the regression. Note that the function also depends on t, meaning that the relationship between market price and demand also changes depending on the day of the week. In practice in this work it is considered the difference between weekdays and weekends and between winter time and summer time. For each one of the four groups, the price-load regression is performed.
Process X describes the fluctuations of the market due to its own nature and to eventualities such as outages and blackouts. It is assumed that both processes X and L are independent. The seasonality of X is much lower than the one that can be seen in L, anyway, it can be modelled as well with a SARIMA model with a frequency of 24 h.
For this work, data of Spanish market prices has been extracted from the CNMC [55] and OMIE [56] databases. The availability and aggregated demand have been extracted from the transmission system operator, REE [57].

Scenario Reduction
In order to increase the accuracy of the simulation it is convenient to generate the highest possible number of scenarios. However, this must be done without greatly increasing the simulation time. To this aim, a set of representative scenarios has been employed, using the backward algorithm [58].

Case Study Definition
A particular location has been analyzed in order to test whether it is suitable or not for transforming it into an energy community. More specifically, the location corresponds to the residential area located at the geographic coordinates 41.65 N, 2.16 E in Spain. This zone was considered adequate because the PV potential is high and the orography is suitable for the installation of wind facilities.
For practical purposes, it is considered that all solar panels are installed on-roof, facing south and with a constant tilt α = 15 • . In order to transform the global horizontal irradiance into the irradiance over a fixed plane, a linear regression is done on the quotient between horizontal and tilted irradiance data in Helioclim database [33].
For the solar panel model, generic characteristics have been employed. It is considered that the solar panel allows generating 180 W/m 2 under an irradiance of 1000 W/m 2 . The temperature effects on the module efficiency are neglected. Each panel has an area of 1.5 m 2 and they are connected to maximum point power tracking (MPPT) inverters, which ensures that the power of the PV string is always the highest possible. These inverters are simplistically modelled, assuming that they start to work with constant efficiency from 2% of nominal power onwards. The batteries considered in this case are of li-ion technology. Self-discharge is neglected. Figure 4 depicts the solar production and the consumption of a dwelling for a single week. maximum point power tracking (MPPT) inverters, which ensures that the power of the PV string is always the highest possible. These inverters are simplistically modelled, assuming that they start to work with constant efficiency from 2% of nominal power onwards. The batteries considered in this case are of li-ion technology. Self-discharge is neglected. Figure 4 depicts the solar production and the consumption of a dwelling for a single week.
The program has been computed using the AIMMS interface, release 4.73, using the CPLEX solver, on a laptop with an Intel i5-6300HQ CPU and 8 GB RAM. To improve the clarity of the results, a model with D = 10 dwellings and S = 10 representative scenarios is employed, as Table 12 shows.  The program has been computed using the AIMMS interface, release 4.73, using the CPLEX solver, on a laptop with an Intel i5-6300HQ CPU and 8 GB RAM. To improve the clarity of the results, a model with D = 10 dwellings and S = 10 representative scenarios is employed, as Table 12 shows.

First Stage Variables: Sizing Results
The first stage variables lead to determine the total amount to be invested in the microgrid. In this regard, as depicted in Table 13, the optimal investment is 230,210 € (23,021 €/dwelling) and the money must be spent in installing the maximum number of PV panels and wind turbines, limited in this model at 10 kWp/dwelling and one wind turbine for every two houses. Additionally, the battery bank capacity is 19.0 kWh (21.1% of the upper limit, 90 kWh in this case). The contracted power can be reduced to 2.64 kW. It is noteworthy that the optimal solution includes the installation of the maximum solar and wind power, while not wholly relying on batteries, as it indicates that both sources are profitable in this location. The objective function result, which consists of the undiscounted annualized investment costs minus the savings achieved during a year of operation, is negative, which indicates that the microgrid is viable. According to the results, each year (neglecting the effects of depreciation and inflation) 2799 € extra are saved. On Figure 5 the breakdown of the objective function management costs and savings can be observed, and Table 13 shows the values that the first stage variables take after the optimization is performed.

First Stage Variables: Sizing Results
The first stage variables lead to determine the total amount to be invested in the microgrid. In this regard, as depicted in Table 13, the optimal investment is 230,210 € (23,021 €/dwelling) and the money must be spent in installing the maximum number of PV panels and wind turbines, limited in this model at 10 kWp/dwelling and one wind turbine for every two houses. Additionally, the battery bank capacity is 19.0 kWh (21.1% of the upper limit, 90 kWh in this case). The contracted power can be reduced to 2.64 kW. It is noteworthy that the optimal solution includes the installation of the maximum solar and wind power, while not wholly relying on batteries, as it indicates that both sources are profitable in this location.
The objective function result, which consists of the undiscounted annualized investment costs minus the savings achieved during a year of operation, is negative, which indicates that the microgrid is viable. According to the results, each year (neglecting the effects of depreciation and inflation) 2799 € extra are saved. On Figure 5 the breakdown of the objective function management costs and savings can be observed, and Table 13 shows the values that the first stage variables take after the optimization is performed.

Second Stage Variables: Energy Management Results
The second stage variables are the ones which show how the optimal management of the microgrid must be done. According to the results depicted in Figures 6 and 7, PV generation and batteries are the main sources by which loads are satisfied. In Figure 7, it is depicted how the batteries For a lifespan of 20 years, the payback time is equal to 16.09 years.

Second Stage Variables: Energy Management Results
The second stage variables are the ones which show how the optimal management of the microgrid must be done. According to the results depicted in Figures 6 and 7, PV generation and batteries are the main sources by which loads are satisfied. In Figure 7, it is depicted how the batteries supply the consumption of a single dwelling. The energy storage is working intensively to supply the demand. Only when the solar irradiance is at its peak, the batteries stop running. Besides, the optimal solution leads to injecting all the surplus energy to the grid (see Figure 8), which is possible because the installed peak power is higher than the peak of consumption. Regarding the contribution of PV power to the battery charge, results have shown (see Figure 9) that it is limited, because the batteries are mostly charged by the energy generated by the wind turbines (see Figure 10).
supply the consumption of a single dwelling. The energy storage is working intensively to supply the demand. Only when the solar irradiance is at its peak, the batteries stop running. Besides, the optimal solution leads to injecting all the surplus energy to the grid (see Figure 8), which is possible because the installed peak power is higher than the peak of consumption. Regarding the contribution of PV power to the battery charge, results have shown (see Figure 9) that it is limited, because the batteries are mostly charged by the energy generated by the wind turbines (see Figure 10).   the demand. Only when the solar irradiance is at its peak, the batteries stop running. Besides, the optimal solution leads to injecting all the surplus energy to the grid (see Figure 8), which is possible because the installed peak power is higher than the peak of consumption. Regarding the contribution of PV power to the battery charge, results have shown (see Figure 9) that it is limited, because the batteries are mostly charged by the energy generated by the wind turbines (see Figure 10).   optimal solution leads to injecting all the surplus energy to the grid (see Figure 8), which is possible because the installed peak power is higher than the peak of consumption. Regarding the contribution of PV power to the battery charge, results have shown (see Figure 9) that it is limited, because the batteries are mostly charged by the energy generated by the wind turbines (see Figure 10).     The sizing results lead the rated power of the wind systems to reach its upper limit; as a result, the wind system is able not only to charge the energy system but to sell the rest of the energy to the grid. As it is depicted in Figure 11, the energy management system forces the wind system to inject the surplus of energy to the grid in the night, when the lowest consumption happens while the wind speed is the highest. However, as PV and batteries have priority at supplying the loads, wind turbines' contribution remains little, as Figure 12 shows. The sizing results lead the rated power of the wind systems to reach its upper limit; as a result, the wind system is able not only to charge the energy system but to sell the rest of the energy to the grid. As it is depicted in Figure 11, the energy management system forces the wind system to inject the surplus of energy to the grid in the night, when the lowest consumption happens while the wind speed is the highest. However, as PV and batteries have priority at supplying the loads, wind turbines' contribution remains little, as Figure 12 shows. Figure 9. Energy transmission from PV panels to the battery bank, for a single dwelling. Comparison between the most and least probable scenarios (blue and orange) for one week of April. Source: selfelaboration. The sizing results lead the rated power of the wind systems to reach its upper limit; as a result, the wind system is able not only to charge the energy system but to sell the rest of the energy to the grid. As it is depicted in Figure 11, the energy management system forces the wind system to inject the surplus of energy to the grid in the night, when the lowest consumption happens while the wind speed is the highest. However, as PV and batteries have priority at supplying the loads, wind turbines' contribution remains little, as Figure 12 shows. On the other hand, the supply of the grid is minimal (see Figure 13). One of the main goals when installing a microgrid is to reduce dependence on the grid, and so it has been. Only a few times throughout the year, and particularly during the winter months, it is necessary to import energy from the grid. The new rated power is determined in fact, by the peak power of supplied energy in the most adverse scenario. On the other hand, the supply of the grid is minimal (see Figure 13). One of the main goals when installing a microgrid is to reduce dependence on the grid, and so it has been. Only a few times throughout the year, and particularly during the winter months, it is necessary to import energy from the grid. The new rated power is determined in fact, by the peak power of supplied energy in the most adverse scenario.
On the other hand, the supply of the grid is minimal (see Figure 13). One of the main goals when installing a microgrid is to reduce dependence on the grid, and so it has been. Only a few times throughout the year, and particularly during the winter months, it is necessary to import energy from the grid. The new rated power is determined in fact, by the peak power of supplied energy in the most adverse scenario.

Comparison with a System without Energy Storage
One of the common quandaries when designing an EC is whether to include an energy storage system or not. The main advantage of storage is that it provides more autonomy and independence of the main grid, whereas the main drawback is that it increases the investment cost. To quantify the importance of installing energy storage, a simulation was performed setting the maximum battery capacity to zero. The results are shown in Table 14 and Figure 14. G.to.C (kWh) Day Figure 13. Energy transmission from grid to loads, for a single dwelling. Comparison between the most and least probable scenarios (blue and orange) for one month (April). Source: self-elaboration.

Comparison with a System without Energy Storage
One of the common quandaries when designing an EC is whether to include an energy storage system or not. The main advantage of storage is that it provides more autonomy and independence of the main grid, whereas the main drawback is that it increases the investment cost. To quantify the importance of installing energy storage, a simulation was performed setting the maximum battery capacity to zero. The results are shown in Table 14 and Figure 14.  It is worth noticing, as Figure 14 shows, the difference in the second stage costs between the models with and without energy storage. The absence of a battery saves some O&M costs, although the optimal capacity is low enough for them not to be greatly decreased. Earnings due to the sale of energy increase a little bit due to the reduction in the level of self-consumption, which obligates the management system to dump energy into the grid. This in turn increases the generation taxes. However, what is most noticeable is the increase on the energy bill, which increases by more than two times compared to the model with energy storage. This is due to the reduction of autonomy that supposes not having an energy storage system available.
It is also worth observing how the dependency from the main grid increases drastically without It is worth noticing, as Figure 14 shows, the difference in the second stage costs between the models with and without energy storage. The absence of a battery saves some O&M costs, although the optimal capacity is low enough for them not to be greatly decreased. Earnings due to the sale of energy increase a little bit due to the reduction in the level of self-consumption, which obligates the management system to dump energy into the grid. This in turn increases the generation taxes. However, what is most noticeable is the increase on the energy bill, which increases by more than two times compared to the model with energy storage. This is due to the reduction of autonomy that supposes not having an energy storage system available.
It is also worth observing how the dependency from the main grid increases drastically without an energy storage system, as Figure 15 shows. energy community without energy storage. In parenthesis, the second stage costs for the model with energy storage. Source: self-elaboration.
It is worth noticing, as Figure 14 shows, the difference in the second stage costs between the models with and without energy storage. The absence of a battery saves some O&M costs, although the optimal capacity is low enough for them not to be greatly decreased. Earnings due to the sale of energy increase a little bit due to the reduction in the level of self-consumption, which obligates the management system to dump energy into the grid. This in turn increases the generation taxes. However, what is most noticeable is the increase on the energy bill, which increases by more than two times compared to the model with energy storage. This is due to the reduction of autonomy that supposes not having an energy storage system available.
It is also worth observing how the dependency from the main grid increases drastically without an energy storage system, as Figure 15 shows. Figure 15. Energy transmission from grid to loads in a system without energy storage, for a single dwelling. Comparison between the most and least probable scenarios (blue and orange) for one month (April). Source: self-elaboration.

Performance of the Optimization
In order to evaluate the suitability of the approach employed in this study, two quality metrics are employed. The first one is the performance of the optimization (PO). This metric compares the solution that would be chosen in the case that the facility installers would not use any optimization algorithm, with the configuration resulting from the stochastic optimization approach.
One of the most common approaches that EC designers use when sizing a microgrid is to maximize the self-consumption rate, or equivalently, reduce the dependence to the grid as much as possible. To this end, distributed generation facilities are oversized to their maximum capacity, which is just limited by the roof or land area and the amount of investment [59]. Therefore, the solution that,

Performance of the Optimization
In order to evaluate the suitability of the approach employed in this study, two quality metrics are employed. The first one is the performance of the optimization (PO). This metric compares the solution that would be chosen in the case that the facility installers would not use any optimization algorithm, with the configuration resulting from the stochastic optimization approach.
One of the most common approaches that EC designers use when sizing a microgrid is to maximize the self-consumption rate, or equivalently, reduce the dependence to the grid as much as possible. To this end, distributed generation facilities are oversized to their maximum capacity, which is just limited by the roof or land area and the amount of investment [59]. Therefore, the solution that, intuitively, distributed generation facilities would take, is to set all design or first-stage variables to the upper limit.
The PO metric is then measured as the difference between the values of the objective function under this intuitive approach and the objective function under the stochastic programming approach: where z Int is the value of the objective function on the original program after the design variables are taken to their maximum limits.

Value of Stochastic Solution
The Value of Stochastic Solution (VSS) [60] is a parameter that quantifies the benefits of using a stochastic program instead of a deterministic one. The uncertainties of the deterministic program are modelled using their expected value: where EPV is the expected value problem. The solution of this problem leads to the set of first stage variables. If these decisions are taken, the solution would be suboptimal because it is hardly likely that the second stage variables would take their expected value in the future. When fixing the first stage variables of the original problem with the values obtained in the expected value problem, a suboptimal solution is obtained. This is the so-called expected solution of the problem (ESP): ESP : min z ESP = c T x * EVP +E[Q(y, ξ)] s.t. T(ξ)x + W(ξ)y = h(ξ) y ≥ 0 Thus, the VSS is equal to: VSS = z ESP −z (55) Table 15 shows the values of the two quality metrics for this case study: The VSS indicates that using a statistical approach in front of a deterministic one is worth a mean of 428 €/year, whereas the performance of optimization shows that using a statistical approach in front of an intuitive one is worth a mean of 2439 €/year.

Final Remarks
The optimal solution of the problem indicates that, at present, given the costs of investment, operation and maintenance of renewable energies, the installation of a microgrid in an energy community is economically feasible.
These results show that, even with detailed characterization of the sources of uncertainty, renewable energy supplied microgrids are a safe option of energy supply. The combination wind-solar is essential, in order not to depend on a single source because they are highly weather-dependent. The stochastic program, unlike deterministic formulations, envisages the possibility that there might be several consecutive hours or days in which there is low or no availability of the renewable energy sources. In consequence, the stochastic solution is more battery dependent, leading to a more conservative energy management than in the deterministic formulation.
It has been shown that even though the inclusion of energy storage increases the investment costs, it reduces the value of the objective function. This means that the energy storage system makes the facility more profitable by reducing dependence on the main grid and increasing autonomy. In the absence of it, however, renewable energy cannot guarantee a reduction of the dependence. Moreover, it is more reliable to supply loads with batteries, as the stochastic program solution demonstrates, than directly from renewable sources. The reason is that the intermittency of renewable energies, especially wind power, needs to be properly addressed by a control system, whereas batteries can supply a constant flow of energy.
However, it is worth noting that, in this case, the intuitive approach has performed worse than the model with no energy storage system. Therefore, oversizing storage can be even more detrimental for the finances of the EC users than not considering it. This strengthens the arguments in favor of using optimization techniques in distributed generation facility sizing problems.
To quantify how valuable is the use of stochastic optimization in front of deterministic optimization, VSS has been calculated. The positive result of the VSS shows that considering different representative scenarios gives a more valuable solution than by just considering a single expected value scenario.

Conclusions
A mathematical model that analyses the viability of a microgrid in an energy community has been implemented with satisfactory results. For the case study analyzed in this project, the installation of the facility supposes an average saving of 280 €/dwelling·year in contrast with the original grid-dependent configuration. The model has also proved the suitability of the renewable energy sources and set the storage capacity to 1.9 kWh/dwelling, a value that does not surpass the typical commercial specifications for li-ion batteries for households.
The characterization of the sources of uncertainty has been thorough, adapting methods of forecasting present in the scientific literature. An hourly step of temporal resolution has been chosen in order to consider diverse scenarios that test the resilience of the microgrid, meaning the ability to adapt to improbable future scenarios. This has led to a more battery-intensive management compared with a deterministic approach, which also results in more flexibility at handling different future situations. Each source of uncertainty has been characterized by different methods due to their different idiosyncrasy.
When modelling the solar irradiance, the tendency has been deleted by the use of the clear-sky index, and the seasonal component has been taken into account with a SARIMA model. Regarding the wind speed model, an autoregressive series of first-order has been applied to the data, and the result has been mapped into a Weibull distribution. In the market price model, the strong correlation between it and the demand has been exploited. When characterizing the consumption, a free-data model is used, which is based on the empirical probability of use of the different appliances.
Simulations based on these models have allowed obtaining synthetical data that, while not being a forecast of any particular time, can well represent any realistic year. A high number of scenarios has been generated and then reduced to a set of representative ones by using the backward algorithm.
The methodology developed is expected to be a useful tool for future investors and policy-makers, who will be able to conceive or design effectively energy communities supplied exclusively by renewable energy sources. The uncertainty inherent to generation and consumption is no longer a barrier which compromises the security of supply because it now can be analyzed thoroughly and effectively with models like the ones employed in this work. It is expected that the development of statistical forecasting techniques and optimization algorithms leads to an increase of this type of projects, thus contributing to higher energy independence and a reduction of the pollution caused by the centralized energy generation.