## 2 Problem Formulation
### Flexibility Potential Quantification  
Here, the flexibility potential quantification for the flexible multi-energy resources within the prosumer aggregators is presented. Flexibility is, in general, defined as the shifting of supply-demand behavior in response to price signal or incentives [25]. For any flexible multi-energy resources, the allowable schedule is the resource flexibility, which represents the boundaries of the possible variations from the optimal operation without violating its physical constraints. For example, a battery under normal operation follows an optimal operation plan, and the maximum deviation at any timestep is constrained by its state-of-charge (SOC) limits. Based on the above definition, positive flexibility is defined as a prosumer service that results in a net addition of energy to the grid. Conversely, negative flexibility is a prosumer service that results in a net removal of energy from the grid. The flexibility potential quantification would obtain the available positive and negative flexibility in terms of power, energy and time. The flexibility potential for all flexible resource can be expressed using constraint (1). \[e_{t,n,flex}=P_{t,n,flex}\cdot\lambda_{t,n,flex}\cdot\Delta t \tag{1}\]  
Battery energy storage (BES) has a high level of flexibility, which can provide both positive and negative flexibility. The negative flexibility of BES is additional energy charged from outside. The maximum amount of available flexibility is subject to the SOC levels of the BES. constraint (2) shows the available maximum negative flexible energy \(ebes_{t,n,max}\) from a BES, which is the difference between the current and maximum SOC. The negative flexible power might not always be the maximum charging. For instance, if the BES is scheduled to discharge, the negative flexible power will be the maximum charge and the scheduled discharge, as shown in constraint (3). \[ebes_{t,n,\max-}=(SOC_{e,n,\max}-SOC_{e,n,n})\cdot E_{bes} \tag{2}\]  
\[Pbes_{t,n,flex-}=E_{e,e,h,n,\max}+E_{e,dis,t,n}-E_{e,ch,t,n} \tag{3}\]  
The maximum flexible energy in constraint (2) can be offered continuously only if the flexible power is equal or greater in the next few timesteps. The possible time is subject to constraint (4), which is until this constant power curtailment is feasible. Here, \(N(f(n))\) is used to obtain the number of timesteps subjected to the \(f(n)\). Constraint (5) finds the number of continuous timesteps that are subjected to both the limitation of the maximum flexible energy and the operation of constant flexible power. \[\lambda_{k,bes-}=N\Big{(}Pbes_{t,n,flex-}\geq Pbes_{t,n,flex-}\Big{)}\mathbb{k }\in[t+1,\ldots T] \tag{4}\]  
\[\lambdabes_{t,n,flex-}=\min\Bigg{\{}\frac{ebes_{t,n,\max-}}{Pbes_{t,n,flex-} \cdot\Delta t},\lambda_{k,bes-}\Bigg{\}} \tag{5}\]  
The positive flexible power can be calculated by the maximum discharge and BES charging, as shown in (7). Similar to (2) and (5), the maximum positive flexible energy and the minimum number of steps subjected to continuous power operation and SOC limits are established in (6) and (9). \[ebes_{t,n,\max+}=(SOC_{e,t,n}-SOC_{e,n,\min})\cdot E_{bes} \tag{6}\]  
\[Pbes_{t,n,flex+}=E_{e,dis,n,\max}-E_{e,dis,t,n}+E_{e,ch,t,n} \tag{7}\]  
\[\lambda_{k,bes+}=N\Big{(}Pbes_{k,n,flex+}\geq Pbes_{t,n,flex+}\Big{)}\mathbb{k }\in[t+1,\ldots T] \tag{8}\]\[\lambda bes_{t,n,flex+}=\min\left\{\frac{eges_{t,n,\max+}}{Pges_{t,n,flex+}\cdot \Delta t},\lambda_{k,bes+}\right\} \tag{9}\]  
Similarly, gas storage tanks can also provide both positive and negative flexibility services, which are constrained by SOC, the scheduled charge and discharge. Constraint (10) represents the available maximum negative flexible energy \(egas_{t,n,\max}\). from a gas storage tank, which is the difference between the current and maximum SOC. The flexible output of gas storage tanks and the minimum number of steps subjected to continuous power operation and SOC limits are established in (11)-(13).