## 3 Optimal Model for Scheduling Virtual Microgrids Topology
### Model without Demand Flexibility  
The virtual microgrids topology scheduling (VMTS) model is described as follows:  
\[Min\ F=\mathrm{Y}\sum_{i\in\mathbb{B}}\sum_{l\in\mathbb{T}}\delta^{p}_{i,t}+ \delta^{q}_{i,t}+\sum_{(i,j)\in\mathbb{L}}\sum_{l\in\mathbb{T}}\mid p^{t}_{i, j}\mid+\mid q^{t}_{i,j}\mid. \tag{1}\]  
Figure 1: Methodology structure for self-adaptive distribution networks. The objective function in Equation (1) minimizes the active (\(\delta^{p}_{i,t}\)) and reactive (\(\delta^{q}_{i,t}\)) power not supplied, and the active (\(p^{t}_{i,j}\)) and reactive (\(q^{t}_{i,j}\)) power flow between the nodes. Thus, this structure allows us to:  
* Promote the fulfillment of the electricity load since the positive variables \(\delta^{p}_{i,t}\) and \(\delta^{q}_{i,t}\) are penalized (Y) when their values are greater than zero, if and only if, the power generation and the energy storage is not enough to meet the load. * Prioritize the loads with or closer to a DG or BESS when it is not a chance to meet the power consumption through the minimization of the flow variables (\(p^{t}_{i,j}\)) and (\(q^{t}_{i,j}\)). In order to penalize the flow between nodes, the absolute value for the active and reactive power flow between nodes is considered, which can take negative and positive values depending on the flow direction, and because an absolute value requires a simple change of variable to become linear. On the other hand, the ENS penalization (Y) is a parameter that is defined by the DSO regarding its power quality, i.e., if the ENS is more relevant than the energy losses through the lines, then Y is greater than the power flow through the lines. In the opposite case, Y must be equal to or lower than the power flow in lines. The formulation constraints are as follows: Equations (2) and (3) correspond to active and reactive nodal power flow equations; therefore, \(\forall i\in\mathbb{B},\forall t\in\mathbb{T}\),  
\[vs_{i,t}[G^{i}_{i}+\sum_{(i,j)\in\mathbb{L}}G^{ij}_{i,j}]+\sum_{(i,j)\in \mathbb{L}}p_{i,j,t}=\sum_{k\in G_{typ}}pg^{k}_{i,t}+\lambda^{p}_{i,t}-PL_{i,t }+\sum_{l\in\mathbb{B}_{typ}}(ds^{l}_{i,t}-ch^{l}_{i,t}), \tag{2}\]  
\[-vs_{i,t}[B^{i}_{i}+\sum_{(i,j)\in\mathbb{L}}(B^{ij}_{i,j}-Bs_{i,j})]+\sum_{(i,j)\in\mathbb{L}}q_{i,j,t}=\sum_{k\in G_{typ}}q^{k}_{i,t}+\lambda^{q}_{i,t}- QL_{i,t}. \tag{3}\]  
Both equations, and the remaining constraints, have already been linearized using the methodology presented in [28], holding the left side of these equations as the classic AC-OPF. However, on the right side, we consider the active power injected by the distributed resource (\(pg^{k}_{i,t}\)); a virtual active power (\(\lambda^{p}_{i,t}\)), which represents the power remaining to meet the load in case of low generation; the active power demanded by the node (\(PL_{i,t}\)), wherein this formulation is a parameter; and the power absorbed (\(ch^{l}_{i,t}\)) or injected (\(ds^{l}_{i,t}\)) from the battery to the grid. The same structure follows Equation (3). The set of constraints from (4) to (7) correspond to the linear version of the active and reactive power flow equation which have been widely explained in previous AC-OPF formulations [13]; therefore, \(\forall(i,j)\in\mathbb{L}\), \(\forall t\in\mathbb{T}\),  
\[G^{ij}_{i,j}\left[\frac{vs_{i,t}-vs_{j,t}}{2}\right]-B^{ij}_{i,j}(\Delta \theta_{i,j,t})+LP_{i,j}=p_{i,j,t}, \tag{4}\]  
\[-B^{ij}_{i,j}\left[\frac{vs_{i,t}-vs_{j,t}}{2}\right]-G^{ij}_{i,  
\[\lambda^{p}_{i,t} \leq\delta^{p}_{i,t}, \tag{9}\] \[QL_{i,t} =\gamma^{q}_{i,t}+\delta^{q}_{i,t},\] (10) \[\lambda^{q}_{i,t} \leq\delta^{q}_{i,t}. \tag{11}\]  
Equation (8) represents the total power load which depends on the power supplied (\(\gamma^{p}_{i,t}\)) and not supplied (\(\delta^{p}_{i,t}\)) in every node \(i\), thus, the total load is composed by these two types of variable, where the power not supplied is penalized in the objective function, to ensure that \(\delta^{p}_{i,t}\) is as small as possible.