*2.2. Optimization Approach*

The calculation of the cost-optimal charging schedule is based on [19] but has been modified in order to incorporate constraints such as EV availabilities over time. This section describes the mixed-integer linear programming (MILP) model that has been used to calculate the cost-optimal charging schedule. In this work, it is assumed that the prosumer prefers a cost-optimized solution to the scheduling problem. Therefore, the target function in Equation (1) is formulated as a cost minimization.

$$\min \left( \sum\_{t=1}^{T} \left( p\_t^{\text{int}} \cdot c\_t^{\text{int}} - p\_t^{\text{ex}} \cdot c\_t^{\text{ex}} + p\_t^{\text{gas}} \cdot c\_t^{\text{gas}} \right) \cdot \Delta t + K \cdot \Delta \text{SoC}\_t \right) \tag{1}$$

Here *T* is the set of time steps (*t*) considered throughout the scheduling horizon. Δ*t* is the duration of each time step, *pim* and *pex* are the electrical import and export power, while *cim* and *cex* are the corresponding electricity costs or revenues. *pgas* and *cgas* denote the volume of natural gas used and its specific cost, respectively. *K* represents a penalty coefficient that is multiplied with the variable Δ*SoCt* which is the difference between the desired final state of charge (SoC) and the actual SoC at the end of charging. This penalty term allows the optimizer to create a feasible problem even though the available time is not sufficient to charge a vehicle fully. Thereby, infeasible problems are avoided.

In addition, the energy balance and constraints for each appliance are critical to reflect a correct and realistic optimization. The constraints are as follows. The energy balance for electricity is represented by Equation (2) and for heat by Equation (3).

$$p\_t^{im} + \sum\_{\delta \in \text{Flex}\_{\text{cl}}} p\_t^{\delta} - p\_t^{ex} - p\_t^{load} = 0 \qquad \forall t \in [1, T] \tag{2}$$

$$\sum\_{\delta \in F \& \mathbf{x}\_{th}} p\_t^{\delta} - q\_t^{\text{load}} = 0 \qquad \qquad \forall t \in [1, T] \tag{3}$$

*pload and qload* represent the electrical and thermal load of the household. *p*<sup>δ</sup> is the power of one specific device, which belongs to one of the flexible appliance groups *Flexel* ∈ {*EV*, *CHP*, *HP*, *PV*, *Bat*} or *Flexth* ∈ {*CHP*, *HP*, *TES*}. The group *Flexel* includes electric vehicles (EV), combined heat and power (CHP), heat pumps (HP), photovoltaic (PV), and battery systems (Bat). The group *Flexth* covers CHP, HP and thermal energy storages (TES). CHPs and HPs are classified into both groups because CHP produce electricity and heat at the same time and heat pump can convert power to heat.

For each flexible appliance and storage system, multiple constraints exist concerning their operation. Because this paper focuses on the quantification of flexibility of EVs, Equations (4)–(10) only list the constraints for EVs.

$$\text{SoC}\_{t} = \text{SoC}\_{0} + \frac{1}{\text{C}\_{\text{Bat}}} \sum\_{s=1}^{t} \left( p\_{t}^{EV} \cdot \eta - p\_{t}^{\text{cons}} \right) \cdot \Delta t \qquad \forall t \in [1, T] \tag{4}$$

$$\text{SoC}\_{\text{min}} \le \text{SoC}\_{t} \le \text{SoC}\_{\text{max}} \qquad \forall t \in [1, T] \tag{5}$$

$$\text{SoC}\_{t} \ge \text{SoC}\_{t}^{\varepsilon} - \Delta \text{SoC}\_{t} \qquad \qquad \forall t \in \left\{ t\_{1}^{\varepsilon}, t\_{2^{\varepsilon}}^{\varepsilon} \dots \right\} \tag{6}$$

$$\text{SoC}\_{t} \leq \text{SoC}\_{t}^{s} \qquad \qquad \forall t \in \left\{ t\_{1}^{s}, t\_{2}^{s}, \dots \right\} \tag{7}$$

$$0 \le p\_t^{EV} \le A\_{l'} P\_{\text{max}}^{EV} \qquad \forall t \in [1, T] \tag{8}$$

$$A\_t = \left\{ \begin{array}{ll} 1 & \forall t \in \left[t\_1^s, t\_1^t\right] \cup \left[t\_2^s, t\_2^t\right] \cup \dots \\ 0 & \forall t \in \left[0, \ t\_1^s\right] \cup \left(t\_1^t, t\_2^s\right] \cup \dots \end{array} \right\} \tag{9}$$

2

$$\|p\_t^{\text{cons}} \ge 0 \qquad \forall t \in [1, T] \tag{10}$$

In Equations (4)–(7), *SoC* represents the SoC of the EV battery. *SoC*<sup>0</sup> denotes the initial SoC of the EV, *SoCmin* and *SoCmax* are minimal and maximal SoC. *SoCs <sup>t</sup>* refers to the SoC at time step zero and *SoC<sup>e</sup> t* represents the SoC at the last available time step. *SoCe <sup>t</sup>* may be reduced by Δ*SoCt* if the time the vehicle is parked at home is not sufficient to charge the battery from start SoC to the desired end SoC. *t <sup>s</sup>* and *t e* refer to the start and end time of each availability period of the EV. *CBat* is the battery capacity, *pEV <sup>t</sup>* is the charging power of the EV, and η is the charging efficiency. *pcons <sup>t</sup>* is a variable that covers the power demand if the EV is available for multiple periods and energy has been discharged from the battery in between.

1

Equations (8)–(10) describe the constraints for *pEV <sup>t</sup>* . *At* describes the vehicle availability for charging, which is 1 between *t <sup>s</sup>* and *t e* , and otherwise zero. Integrated into Equation (8), the availability does not allow the charging power to be greater than 0 if the vehicle is not available for charging. If the vehicle is available for charging, the charging power can be as high as the maximal charging power *PEV max*.

The formulated MILP model can be solved using commercial and open-source solvers, such as GLPK or Gurobi. Depending on the problem complexity, a conventional computer (e.g., Intel i7, 4 Cores, 24 GB RAM) presents a solution within a few seconds.

After optimizing the device's operating strategy, a market agent in the HEMS trades its excess and required energy on the energy market and a controller schedules the device's operation accordingly. After successful interaction with the energy market, the HEMS can start the flexibility calculation.
