## 3 Methodology
### Formulation of the Optimization Algorithm
#### 3.7.5 Constraints  
The objective function has been linearized but. to prevent obtaining simultaneous charge and discharge of the battery, the following constrains are included (Equations (19)-(23)):  
\[P_{t,p}^{bat^{*}}=P_{t,p}^{bat_{char}}-P_{t,p}^{bat_{disch}}\ \forall t\in T^{ EMS},\ \forall\ p\in T^{intra} \tag{19}\]  
\[P_{t,p}^{bat_{char}}\leq P^{mxB}X_{t,p}^{char}\ \forall t\in T^{ EMS},\ \forall\ p\in T^{intra} \tag{20}\]  
\[P_{t,p}^{bat_{disch}}\leq P^{mxB}(X_{t,p}^{char}-1)\ \forall\ t\in T^{ EMS},\ \forall\ p\in T^{intra} \tag{21}\]  
\[P_{t,p}^{bat_{char}}\geq 0\ \forall\ t\in T^{ EMS},\ \forall\ p\in T^{intra} \tag{22}\]  
\[P_{t,p}^{bat_{disch}}\geq 0\ \forall\ t\in T^{ EMS},\ \forall\ p\in T^{intra} \tag{23}\]  
Then, the power balance at each period must be accomplished. This is forced by the restriction in Equation (24).  
\[P_{t,p,s}^{pv}+\sum_{d\in N^{Diesel}}P_{t,p,s,d}^{dies}+P_{t,p}^{bat^{*}}-L_{t,p,s}^{c}=0\forall\ t\in T^{EMS},\ \forall\ p\in T^{intra},\ \forall\ s\in S \tag{24}\]  
Then, as commented above, a margin of diesel generation is reserved for frequency regulation. Thus, the maximum diesel generation is limited (Equation (25)):  
\[\sum_{d\in N^{Diesel}}P_{t,p,s,d}^{dies}\leq\sum_{d\in N^{Diesel}}ON_{t,d}^{ dies}P^{mxD}-marge_{dies}\forall\ t\in T^{EMS},\ \forall\ p\in T^{intra},\ \forall\ s\in S \tag{25}\]The relationship between the SOC at instant \(t\) and the SOC at instant \(t-1\) is shown in Equation (26). The SOC is between 0 and 1 p.u. This constraint is formulated as Equation (27). On the other hand, the battery power limits constraint is Equation (28).  
\[\begin{array}{ll}\text{-If}&T^{EMS}=1\text{ and }T^{intra}=1\\ SOC_{t,p}^{batt}=SOC_{t,p}^{initial}-P_{t,p}^{batt}\frac{\Delta t}{Cap^{batt}} \;\forall\;t\in T^{EMS},\;\forall\;p\in T^{intra}\\ \text{-If}&T^{EMS}\geq 1\text{ and }T^{intra}=1\\ SOC_{t,p}^{batt}=SOC_{t-1,|p|}^{batt}-P_{t,p}^{batt}\frac{\Delta t}{Cap^{batt}} \;\forall\;t\in T^{EMS},\;\forall\;p\in T^{intra}\\ \text{-If}&T^{intra}\
eq 1\\ SOC_{t,p}^{batt}=SOC_{t,p-1}^{batt}-P_{t,p}^{batt}\frac{\Delta t}{Cap^{batt}} \;\forall\;t\in T^{EMS},\;\forall\;p\in T^{intra}\\ \end{array} \tag{26}\]  
\[0\leq SOC_{t,p}^{batt}\leq 1\;\forall\;t\in T^{EMS},\;\forall\;p\in T^{intra} \tag{27}\]  
\[P^{mmB}\leq P_{t,p}^{batt}\leq P^{mxB}\;\forall\;t\in T^{EMS},\;\forall\;p\in T ^{intra} \tag{28}\]  
Then, the PV power cannot be greater than the available PV power of the corresponding scenario. Thus, Equation (29) must be included into the optimization algorithm. The PV power must also be lower than the maximum PV power setpoint of Equation (30).  
\[P_{t,p,s}^{PV}\leq L_{t,p,s}^{PV}\;\forall\;t\in T^{EMS},\;\forall\;p\in T^{intra },\;\forall\;s\in S \tag{29}\]  
\[P_{t,p,s}^{PV}\leq P_{t,p}^{PV_{max}}\;\forall\;t\in T^{EMS},\;\forall\;p\in T ^{intra},\;\forall\;s\in S \tag{30}\]  
Each diesel unit has a maximum and a minimum power at each scenario, which is formulated as Equation (31).  
\[ON_{d,t,s}^{dies}P_{d}^{mmD}\leq P_{t,p}^{dies}\leq P^{mxD}ON_{d,t,s}^{dse }\forall\;t\in T^{EMS},\forall\;p\in T^{intra},\;\forall\;s\in S \tag{31}\]  
Finally, the minimum frequency constraint is included in the optimization model. In the previous section, it has been shown how to express the minimum frequency reached in the microgrid after a maximum power imbalance. This constraint is written as Equation (32).  
\[f^{mn}\leq\theta_{ind}+\theta_{d}\sum_{d}ON_{t,d}^{dies}+\theta_{bat}P_{t,p}^{ bat^{\prime}}+\theta_{pv}P_{t,p}^{PV_{max}}\forall\;t\in T^{EMS},\;\forall\;p\in T ^{intra} \tag{32}\]