## 3 Mathematical Formulation
### Constraints
#### 3.2.2 The Second-Stage Constraints  
1. Case 1: The reserve bid is activated:  
* RES operating constraint: For a given forecasted available power \(P_{RESf}^{t}\) and the minimum power threshold \(P_{RESmin}^{t}\), the power output of the RES must satisfy Equation (5). Equation (6) shows that RES can supply electricity directly to the local loads and contribute to the contract of the VPP in the BC market and the DA market. Besides, RES's excess energy (if any) is stored in the ESS to utilize RES's available power output. \[u_{RES}^{t}(\xi)P_{RESmin}^{t}\leq P_{RES,1}^{t}(\xi)\leq u_{RES}^{t}(\xi) \Big{(}P_{RESf}^{t}+P_{RES-error}^{t}P_{RESf}^{t}\Big{)}\] (5) \[P_{RES,1}^{t}(\xi)=P_{RES-SR}^{t}+P_{RES-grid,1}^{t}(\xi)+P_{RES- load,1}^{t}(\xi)+P_{RES-ESS,1}^{t}(\xi)\] (6)
* ESS constraints: This article uses ESS to store the excess power of RESs or purchased power from the DA market if the electricity price is low. The stored energy will be released to supply the peak load of VPP or sell to both BC and DA markets if the electricity price is high. Equations (7) and (8) show that the charging/discharging power of the ESS is limited by the rated power \(P_{ESSmax}\). In these constraints, the binary variable \(u_{SS}^{t}\) makes sure that the ESS can only be in one state: charging or discharging. Equation (9) shows that the energy \(E_{SS}^{t}\) stored in the ESS should be limited by its rated capacity at all times. Furthermore, Equation (10) makes sure that the energy stored in the ESS will be set to a certain value after each operating day. \[0\leq P_{ESS,1}^{t}(\xi)=P_{RES-ESS,1}^{t}(\xi)+P_{grid-ESS,1}^{t}(\xi)\leq u_ {SS,1}^{t}(\xi)P_{ESSmax}\] (7)\[0\leq P_{ESS,1}^{disk,t}(\xi)=P_{ESS-SR}^{t}+P_{ESS-grid,1}^{t}(\xi)+P_{ESS- load,1}^{t}(\xi)\leq\left(1-u_{SS,1}^{t}(\xi)\right)p_{ESSmax} \tag{8}\]  
\[E_{ESSmin}\leq E_{ESS,1}^{t}(\xi)=E_{ESS,1}^{t-1}(\xi)+\eta P_{ESS,1}^{ch,t}( \xi)-P_{ESS,1}^{disk,t}(\xi)/\eta\leq E_{ESSmax} \tag{9}\]  
\[E_{ESS}^{t=0}=E_{ESS}^{t=24} \tag{10}\]  
* Day-ahead market's constraints: Constraints (11) and (12) show the role of RES and ESS in the DA market. Meanwhile, constraint (13) shows that the arbitrage between the BC and the DA market is not permitted. This means that the VPP is not allowed to buy electricity from the DA in order to sell to the BC market at the same time. \[0\leq P_{sell,1}^{t}(\xi)=P_{RES-grid,1}^{t}(\xi)+P_{ESS-grid,1}^{t}(\xi)\leq \left(1-u_{buy,1}^{t}\right)(P_{ESSmax}+P_{RESrated})\] (11) \[0\leq P_{buy,1}^{t}(\xi)=P_{grid-load,1}^{t}(\xi)+P_{grid-ESS,1}^{t}(\xi) \leq u_{buy,1}^{t}(P_{ESSmax}+Load_{max})\] (12) \[u_{buy}^{t}+u_{SR}^{t}\leq 1;\] (13)
* Active power balance constraint: The VPP's operator must balance demand and supply in all operating scenarios. If the reserve bids are called on to produce, the total active power output from RES and ESS must meet the demand and the power supplied to the main grid according to the signed BC and DA contract. This equation is formulated as a probability constraint to guarantee that the probability of power imbalance is less than a risk level \(\varepsilon\), even if the difference between the real-time data and the predicted value of \(\xi\) is quite high. \[\Pr\left(P_{RES,1}^{t}(\xi)+P_{ESS,1}^{disk,t}(\xi)+P_{buy,1}^{t}(\xi)=SR^{t}+ P_{sell,1}^{t}(\xi)+P_{ESS,1}^{ch,t}(\xi)+P_{Df}^{t}+P_{D-error}^{t}P_{Df}^{t} \right)\geq 1-\varepsilon\] (14)  
* Case 2: The reserve bid is not activated: Similar to case 1, the constraints in case 2 are shown as follows:
* RES operating constraint: \[P_{RES,1}^{t}(\xi)=P_{RES-SR}^{t}+P_{RES-grid,1}^{t}(\xi)+P_{RES- load,1}^{t}(\xi)+P_{RES-ESS,1}^{t}(\xi)\]0 (15) \[P_{RES,1}^{t}(\xi)=P_{RES-SR}^{t}+P_{RES-grid,1}^{t}(\xi)+P_{RES- load,1}^{t}(\xi)+P_{RES-ESS,1}^{t}(\xi)\]1 (16)
* ESS constraints: \[P_{RES,1}^{t}(\xi)=P_{RES-SR}^{t}+P_{RES-grid,1}^{t}(\xi)+P_{RES- load,1}^{t}(\xi)+P_{RES-ESS,1}^{t}(\xi)\]2 (17) \[\left\{