## 5 Energy Management
### Optimization Model
In the following, the mathematical formulation of the model is defined:  
\[E^{t}-V^{t}=C^{t}-I^{t},\quad t\in T \tag{1}\] \[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\] (2) \[I^{t}=\sum_{g\in G}\sum_{p\in P_{g}}q_{gp}^{t}y_{gp},\quad t\in T\] (3) \[E^{t}\leq\lambda_{E}^{t}E_{max}^{t},\quad t\in T\] (4) \[V^{t}\leq\lambda_{V}^{t}V_{max}^{t},\quad t\in T\] (5) \[\lambda_{E}^{t}+\lambda_{V}^{t}\leq 1,\quad t\in T\] (6) \[E^{t},V^{t}\geq 0,\quad t\in T\] (7) \[\lambda_{E^{t}}^{t},\lambda_{V}^{t}\in\{0,1\},\quad t\in T\] (8) \[\sum_{p\in P_{l}}y_{lp}=1,\quad l\in L\] (9) \[\sum_{p\in P_{g}}w_{gp}=1,\quad g\in G\] (10) \[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\]0 (11) \[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\]1  
Equation (1) defines, for each time slot \(t\), the power \(E^{t}\) (\(V^{t}\)) that must be taken from (fed to) the grid to satisfy the internal power requirements. Given a time slot \(t\in T\), Equation (2) defines the total load required by profile-able systems, whereas Equation (3) defines the total power provided by the VPP generators. Moreover, constraints (4)-(6), together with non-negativity and binary conditionson the involved variables (7)-(8), ensure that the power is taken from or supplied to the grid, but not both simultaneously (i.e., during the same time slot). Note that constraints (4) and (5) also ensure the system reliability with respect to the maximum values \(E_{max}^{t}\) and \(V_{max}^{t}\). Finally, to achieve feasible schedules, only one profile has to be chosen for each profile-able piece of equipment and for each generator, as stated by constraints (9)-(12). The DR policy can be tackled by the following constraints:  
\[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\]2  
\[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\]3  
\[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\]4  
\[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\]5  
\[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\]6  
\[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\]7  
\[C^{t}=\sum_{l\in L}\sum_{p\in P_{l}}q_{lp}^{t}y_{lp},\quad t\in T\]8  
In particular, constraints (13) and (14) manage the requests of reducing the power taken from and supplied to the grid, respectively. More precisely, when the request \(d\) is satisfied in time slot \(t\) (i.e., when \(w^{dt}=1\)), then \(E^{t}\) and \(V^{t}\) have to be less or equal than the power limitation associated to the request, otherwise (when \(w^{dt}=0\)) the limitation is represented by the nominal maximum values. Constraints (15) and (16), instead, manage the requests of increasing, respectively, the power taken from and supplied to the grid, ensuring that \(E^{t}\) and \(V^{t}\) are greater than or equal to the minimum power associated with the request \(d\) if satisfied in time slot \(t\). Finally, constraint (17) states that a request \(d\) is totally satisfied (\(z^{d}=1\)) only if \(d\) is satisfied in all of the time slots \(t\in T^{d}\) involved in the DR request. Constraints (18) and (19) are binary conditions on variables. The optimization is guided by the objective function defined in (20), which aims at minimizing the total VPP systems costs and, simultaneously, at maximizing the comfort associated with the use of the prosumer's equipment:  
\[ \begin{array}{c}min\ \alpha\bigg{[}\alpha_{1}\bigg{(}\sum\limits_{t\in T}f^{t} E^{t}-\sum\limits_{t\in T}v^{t}V^{t}+\sum\limits_{l\in L}\sum\limits_{p\in P_{l}}F_{lp}y_{lp}+ \sum\limits_{g\in G}\sum\limits_{p\in P_{g}}F_{gp}y_{gp}-\sum\limits_{t\in T }\sum\limits_{g\in G}\sum\limits_{p\in P_{g}}a_{g}q_{gp}^{t}y_{gp}\bigg{)}- \alpha_{2}\sum\limits_{d\in D}z^{d}B^{d}\bigg{]}\\ \hskip 14.226378 pt-\beta\sum\limits_{t\in T}\sum\limits_{l\in L}\sum\limits_{p \in P_{l}}\gamma_{l}^{t}q_{l}^{d}y_{lp}.\end{array}