## 2 Methodological Proposal for Community-Based Urban Microgrid Planning
### Two-Stage Stochastic Planning Model  
Since the deterministic models described in the previous section do not consider the uncertainty represented by moments of the year in which a fault could occur (any hour of the analysis horizon), the stochastic dimension was included in order to optimally plan microgrids capable of facing different main grid fault scenarios. The literature describes stochastic programming as a two-stage problem [40]: the first stage associates investment decisions on a project, whilst the second stage involves decisions on the operation and/or maintenance of project assets. In this sense, the variables of the models from the previous section were adjusted to include a random vector represented by scenarios (s) for the moments of the year in which a fault could occur, and as a consequence solve a planning problem with a stochastic approach. Taking into account that the equations of the first stage are the same as those presented in the deterministic models of Section 2.1, only the variables and constraints that affect the second stage of the stochastic planning problem are presented below:  
Battery power constraints:  
\[E(t,s)\geq 0,2.E_{bess} \tag{24}\]  
\[E(t,s)\leq 0,9.E_{bess} \tag{25}\]  
\[E(t,s)=E(t-1,s)+P_{ch}(t,s).\eta_{c}.\Delta t-P_{dch}(t,s)/\eta_{d}.\Delta t \tag{26}\]  
where \(s\) represents the operational scenarios of the microgrid, for hours of the year in which a fault occurs. For this work, \(s\) takes values of 1, 2 and 3 (three scenarios). Power injected into the grid:  
\[P_{out}(t,s)\leq P_{pv}(t) \tag{27}\]  
Battery charging and discharging power:  
\[P_{ch}(t,s)\leq M.x_{c}(t,s) \tag{28}\]  
\[P_{ch}(t,s)\leq P_{invc} \tag{29}\]  
\[P_{ch}(t,s)\leq P_{insth} \tag{30}\]  
\[P_{dch}(t,s)\leq M.x_{d}(t,s) \tag{31}\]  
\[P_{dch}(t,s)\leq P_{invc} \tag{32}\]  
\[P_{dch}(t,s)\leq P_{insth} \tag{33}\]  
\[E(t,s)\leq 0,9.E_{bess} \tag{25}\]0  
Minimum power requirement of the inverter charger:  
\[E(t,s)\leq 0,9.E_{bess} \tag{25}\]1  
Finally, the objective function of the stochastic planning problem, both in connected mode and in islanded mode, under reliability requirements, is presented in Equation (36). \[ \begin{split} F.O_{st1}=C_{pvo}.P_{insto}+C_{pvh}.P_{insth}+C_{ bess}.E_{bess}+C_{invc}.P_{invc}\\ -p(s)\cdot\biggl{[}\sum\limits_{t\in T}(C_{out}.\ P_{out}(t,s))- \sum\limits_{t\in T}\biggl{(}C_{grid}.\ P_{grid}(t,s)\biggr{)}\biggr{]}\cdot \Delta t\end{split}  \tag{36}\]  
where \(p(s)\) is the probability for each scenario \(s\). Constraints:  
During normal operation:  
\[P_{grid}(t,s)+P_{pv}(t)-P_{ch}(t,s)+P_{dch}(t,s)-P_{out}(t,s)=Dem(t) \tag{37}\]  
During fault hours:  
\[P_{grid}(t,s)=0,\ P_{out}(t,s)=0\]  
\[\text{Case 1}:\qquad P_{pv}(t)-P_{ch}(t,s)+P_{dch}(t,s)\geq Dem(t) \tag{38}\]  
\[\text{Case 2}:\qquad P_{pv}(t)-P_{ch}(t,s)+P_{dch}(t,s)\geq Demc(t) \tag{39}\]  
This way of representing the objective function of the stochastic problem (Equation (36)) is known as the \"implicit form of the stochastic programming problem\" because it describes in a general way the decision variables of the second stage for \(s\) scenarios to be evaluated [40]. Identification of stochastic scenarios: In order to perform stochastic simulations of the models in Equation (36), it is necessary to identify the scenarios (fault start times in the power grid). In this sense, the strategy used in this research was to obtain the annual \"Fault Energy Demand Duration Curve\" (for each residential household and for each group of households). Each value of this curve corresponds to the total energy that would have been consumed during the next 8 h given a fault occurring in hour \(h\); after that, these values are sorted from highest to lowest. The steps to identify the scenarios are as follows:  
* Establish the duration of the continuous fault (1, 2,..., _d_) where \(d\) is the maximum duration of the fault in hours. * Establish moving windows for the size of the fault duration and sum the demands within the established range.