*4.4. Inner-Loop Optimization*

The goal of the inner-loop optimization model is to minimize the daily operating cost of the MMGS. The daily operating cost is mainly composed of system energy transaction cost and carbon emissions cost. The objective function is as follows:

$$f\_1 = \min C\_{OTC} \tag{14}$$

$$\mathcal{C}\_{\text{OTC}} = \sum\_{m=1}^{M} \mathcal{C}\_{\text{OCm}} \tag{15}$$

$$\mathcal{C}\_{\text{OCm}} = \mathcal{C}\_{\text{exm}} + \mathcal{C}\_{\text{cin}} \tag{16}$$

$$C\_{cim} = E\_{CIm} \bullet k\_c \tag{17}$$

*COCm* is the operating cost of the *m*-th MG that is obtained from the inner-loop model. *Cexm* is the energy transaction cost in the *m*-th MG. *Ccim* is the carbon emissions cost due to energy exchange in the inner-loop model.

### 4.4.1. Energy Transaction Cost

The energy transaction cost is the sum of RESs cost, energy exchange cost between MMGS and EVs, MMGS and distribution network, and the additional cycle cost of EV batteries. *PEV <sup>m</sup>*,*<sup>t</sup>* , *PPV <sup>m</sup>*,*<sup>t</sup>* , *PWT <sup>m</sup>*,*<sup>t</sup>* , and *P<sup>G</sup> <sup>m</sup>*,*<sup>t</sup>* are the optimization variables.

$$\mathsf{C}\_{\rm exm} = \mathsf{C}\_{\rm resm} - \mathsf{C}\_{\rm evm} + \mathsf{C}\_{\rm gm} + \mathsf{C}\_{\rm cym} \tag{18}$$

$$\mathbb{C}\_{\text{resm}} = \mathbb{C}\_{PVm} + \mathbb{C}\_{WTm} \tag{19}$$

$$\mathbf{C}\_{PVm} + \mathbf{C}\_{WTm} = \sum\_{t=1}^{T} P\_{m,t}^{PV} \mathbf{C}\_{m,t}^{PV} \Delta t + \sum\_{t=1}^{T} P\_{m,t}^{WT} \mathbf{C}\_{m,t}^{WT} \Delta t \tag{20}$$

$$\mathbb{C}\_{\text{cvm}} = \sum\_{t=1}^{T} \sum\_{n=1}^{N} P\_{m,n,t}^{EV} \bullet \eta\_{\text{CEV}} \mathbb{C}\_{m,t}^{CEV} \Delta t \quad , \quad if \quad P\_{m,n,t}^{EV} \ge 0 \tag{21}$$

$$\mathbb{C}\_{\text{evm}} = \sum\_{t=1}^{T} \sum\_{n=1}^{N} P\_{m,n,t}^{EV} \mathbb{C}\_{m,t}^{DEV} \Delta t \quad , \quad \text{if} \quad P\_{m,n,t}^{EV} < 0 \tag{22}$$

$$\mathbf{C}\_{\mathcal{S}^m} = \sum\_{t=1}^T P\_{m,t}^G \mathbf{C}\_{m,t}^G \Delta t \tag{23}$$

$$\mathbb{C}\_{\text{cyn}} = \sum\_{n=1}^{N} k\_{\text{cy}} \mathbb{C}\_{\text{cyn}}^{EV} \tag{24}$$

where *Cresm* is the cost of RESs of the *m*-th MG in a day, *CPVm*, and *CWTm* are the cost of PVs and WTs. *PPV <sup>m</sup>*,*<sup>t</sup>* is the power output of PVs in the *m*-th MG, at *t*-th hour, *CPV <sup>m</sup>*,*<sup>t</sup>* is the PV power generation cost, *PWT <sup>m</sup>*,*<sup>t</sup>* is the power output of WTs, *CWT <sup>m</sup>*,*<sup>t</sup>* is the WT power generation cost. *Cevm* is the cost of energy exchange between MMGS and EVs, *CCEV <sup>m</sup>*,*<sup>t</sup>* is the charging price of EVs in *m*-th MG, *CDEV <sup>m</sup>*,*<sup>t</sup>* is the discharging price, Δ*t* = 1 h, *T* = 24 h. *Cgm* is the energy exchange cost between the MG and the distribution network through the DC/AC converters, *P<sup>G</sup> <sup>m</sup>*,*<sup>t</sup>* is the active power output between the MG and the distribution network through the DC/AC converters. If *P<sup>G</sup> <sup>m</sup>*,*<sup>t</sup>* ≥ 0, MG purchases electricity from the distribution network. If *P<sup>G</sup> <sup>m</sup>*,*<sup>t</sup>* < 0, MMGS sells electricity to the distribution network. *C<sup>G</sup> m*,*t* is the electricity price that MG purchases/sells to the distribution network. *Ccym* is the additional cycle cost of EV batteries, *CEV cyn* is the additional battery charging/discharging cycle cost of *n*-th EV, *kcy* is the number of additional charging/discharging cycles, *N* is the number of EVs.

4.4.2. Carbon Emissions and Cost

The electricity of the distribution network mainly comes from thermal power generation. When MG exchanges energy with the distribution network, the distribution network emits more CO2. To reduce carbon emissions as much as possible and increase the use of RESs, in this paper, the cost of carbon emissions is used as the penalty cost of CO2 generated by the energy exchange between the MMGS and the distribution network [29].

$$C\_{\rm circ} = E\_{\rm C,Im} \bullet k\_{\rm c} \tag{25}$$

$$E\_{CIm} = \sum\_{t=1}^{T} P\_{m,t}^{G} \Delta t \bullet c\_{c} \tag{26}$$

*4.5. Constraints of the Inner-Loop Model*

### 4.5.1. EVs Power Constraint

The charging/discharging power of EVs cannot exceed the rated power of EVCDIs.

$$\left|P\_{m,n,t}^{EV}\right| \le P\_{m,n,\mathcal{R}}^{EVCDIs} \tag{27}$$

where *PEVCDIs <sup>m</sup>*,*n*,*<sup>R</sup>* is the rated power of the EVCDI serving the *n*-th EV.

### 4.5.2. EVs Capacity Constraint

The remaining power of EVs must meet the constraints of rated capacity.

$$SOC\_{EVm,n,\text{min}} \le SOC\_{EVm,n,t} \le SOC\_{EVm,n,\text{max}}\tag{28}$$

where *SOCEVm*,*n*,min, and *SOCEVm*,*n*,max are the minima and maximum capacity, respectively, of *n*-th EV in *m*-th MG.

### 4.5.3. RESs Output Constraint

Considering the performance limitations of renewable energy, the output of RESs in *m*-th MG has a certain upper limit.

$$0 \le P\_{m,t}^{\mathsf{WT}} \le P\_{m,\mathsf{max}}^{\mathsf{WT}} \tag{29}$$

$$0 \le P\_{m,t}^{PV} \le P\_{m,\text{max}}^{PV} \tag{30}$$

## 4.5.4. System Power Balance Constraint

For MMGS, the active power output should meet the power balance constraint.

$$P\_{m,t}^{EV} + P\_{m,t}^{G} + P\_{m,t}^{WT} + P\_{m,t}^{PV} = P\_{m,t}^{L} \tag{31}$$

where *P<sup>L</sup> <sup>m</sup>*,*<sup>t</sup>* is the total load of the *m*-th MG at time *t*.

### *4.6. Outer-Loop Optimization*

The outer-loop optimization model takes the network loss as the optimization goal. By optimizing the reactive power output of the DC/AC converters *Q<sup>G</sup> <sup>m</sup>*,*t*, the daily network loss of the distribution network is minimized, thereby reducing network loss cost and carbon emissions of MMGS and the distribution network [30]. This paper assumes that the *m*-th MG is connected to node *i* of the distribution network.

$$f\_2 = \min W\_S^G \tag{32}$$

$$\mathcal{W}\_S^G = \sum\_{t=1}^T \sum\_{i,j=1}^{N\_{br}} k\_i R\_{ij} \frac{P\_{ij,t}^2 + Q\_{ij,t}^2}{V\_{ij,t}^2} \tag{33}$$

$$\mathcal{W}\_{\mathcal{S}}^{I} = \mathcal{W}\_{\mathcal{S}}^{G} - \mathcal{W}\_{\mathcal{S}}^{B} \tag{34}$$

$$P\_{ij,t} = P\_{ij,t}^{0} + P\_{m,t}^{G} \tag{35}$$

$$Q\_{ij,t} = Q\_{ij,t}^0 + Q\_{m,t}^G \tag{36}$$

$$\mathcal{W}\_{\mathcal{S}}^{I} = f\_{\mathcal{Z}} - \mathcal{W}\_{\mathcal{S}}^{B} \tag{37}$$

$$\mathbf{C}\_{il} + \mathbf{C}\_{co} = \mathcal{W}\_{\mathbb{S}}^{I} \bullet \Delta t \bullet k\_{il} + \mathcal{W}\_{\mathbb{S}}^{I} \bullet \Delta t \bullet \mathbf{e}\_{c} \bullet k\_{c} \tag{38}$$

*W<sup>I</sup> <sup>S</sup>* is the daily operating increased network loss of the distribution network. *Nbr* is the number of branches. *i*, *j* are the nodes, *ki* is the state variable of the *i*-th branch switch, 1 means closed, 0 means open; *Rij* is the resistance of branch *ij*, *Pij*,*t*, *Qij*,*<sup>t</sup>* are the active and reactive power of branch *ij* in the *t*-th hour, *Vij,t* is the voltage, *P*<sup>0</sup> *ij*,*t* , *Q*<sup>0</sup> *ij*,*<sup>t</sup>* are initially active, reactive power when connected without MMGS. *P<sup>G</sup> <sup>m</sup>*,*t*, *Q<sup>G</sup> <sup>m</sup>*,*<sup>t</sup>* are the active and reactive power through the DC/AC converters injected into node *i* by the *m*-th MG connected to node *i*. To facilitate the calculation of network loss, a day is divided into 12 small periods, with a time interval of 2 h.

### 4.6.1. Network Loss Cost

The operation of MMGS connected to the distribution network will cause increased network loss in the distribution network. Therefore, the distribution network will sign a contract with the operator of MMGS, and the operator needs to pay a certain network loss fee for the daily operating increased network loss in the distribution network.

$$\mathbb{C}\_{il} = \mathcal{W}\_{\mathbb{S}}^{I} \bullet \Delta \mathbf{t} \bullet \boldsymbol{k}\_{il} \tag{39}$$

### 4.6.2. Carbon Emissions and Cost

When the network loss of the distribution network increases by the operation of MMGS, more CO2 will be emitted. MMGS will still incur a penalty cost for carbon emissions by the increasing network loss, which differs from the carbon emissions cost due to energy exchange in the inner-loop model.

$$
\mathbb{C}\_{\mathcal{O}} = \mathcal{W}\_{\mathbb{S}}^{\mathrm{I}} \bullet \Delta \mathbf{t} \bullet \mathbf{e}\_{\mathbb{C}} \bullet \mathbf{k}\_{\mathbb{C}} \tag{40}
$$

### *4.7. Constraints of the Outer-Loop Model*

The model takes the actual power flow of the power grid as the constraints.

### 4.7.1. Node Power Flow Constraint

$$P\_{m,t}^G + P\_{i,t}^0 = P\_{Li,t} + V\_{i,t} \sum\_{j=1}^{N\_{\pi}} V\_{j,t} \left( G\_{i\bar{j}} \cos \delta\_{i\bar{j}} + B\_{i\bar{j}} \sin \delta\_{i\bar{j}} \right) \tag{41}$$

$$Q\_{m,t}^G + Q\_{i,t}^0 = Q\_{Li,t} + V\_{i,t} \sum\_{j=1}^{N\_u} V\_{j,t} (G\_{ij} \sin \delta\_{ij} + B\_{ij} \cos \delta\_{ij}) \tag{42}$$

where *P*<sup>0</sup> *<sup>i</sup>*,*<sup>t</sup>* and *<sup>Q</sup>*<sup>0</sup> *<sup>i</sup>*,*<sup>t</sup>* are the initial input active and reactive power of node *i* in the *t*-th hour, *PLi*,*<sup>t</sup>* and *QLi*,*<sup>t</sup>* are the active and reactive load, *Vi*,*<sup>t</sup>* and *Vj*,*<sup>t</sup>* are the voltage of node *i and j*, *Gij*, *Bij*, and *δij* are the conductance, susceptance, and phase angle difference of branch *ij*.

4.7.2. Node Voltage Constraint

$$V\_i^{\min} \le V\_{i,t} \le V\_i^{\max} \tag{43}$$

*V*min *<sup>i</sup>* and *<sup>V</sup>*max *<sup>i</sup>* are lower and upper limits of the node *i* voltage amplitude. 4.7.3. Branch Power Constraint

$$\left|P\_{ij,t}\right| \le P\_{ij, \text{max}}\tag{44}$$

$$\left|Q\_{ij,t}\right| \le Q\_{ij,\text{max}}\tag{45}$$

*Pij*,max, *Qij*,max are the maximum active and reactive power of the branch *ij*.

!

4.7.4. Branch Current Constraint

$$I\_{\rm ij} \stackrel{\ast}{\rightharpoonup} I\_{\rm ij}^{\rm max} \tag{46}$$

where *I*max *ij* is the upper limit of branch *ij* current carrying capacity.

### 4.7.5. Reactive Output Constraint of DC/AC Converter

The reactive power output of the DC/AC converters must satisfy the constraint:

$$\left| \mathbb{Q}\_{m,t}^{G} \right| \leq \sqrt{S\_m^2 - \left( P\_{m,t}^{G} \right)^2} \tag{47}$$

where *Sm* is the rated power of the DC/AC converter in *m*-th MG, *Q<sup>G</sup> <sup>m</sup>*,*<sup>t</sup>* is the reactive power that the DC/AC converter can output to the distribution network, *P<sup>G</sup> <sup>m</sup>*,*<sup>t</sup>* is the active power output by DC/AC converter.

### *4.8. Particle Swarm Algorithm*

### 4.8.1. Procedure of PSO

The steps of particle swarm optimization (PSO) are shown in Figure 5 [31].

### 4.8.2. Coding

In the inner-loop, the coding about the economic dispatch of RESs, EVs, and DC/AC converters can be represented by a real-valued matrix. *k* is the index of the particle of the inner-loop. *M* is the number of MG. *T* is the dispatching cycle.

$$I\_{\rm MG}^k = \begin{bmatrix} I\_{\rm MG1} \\ I\_{\rm MG2} \\ \vdots \\ I\_{\rm MGm} \\ \vdots \\ \vdots \\ I\_{\rm MGM} \end{bmatrix} \tag{48}$$

$$I\_{\rm MG,m} = \begin{bmatrix} P\_{m,1}^{PV} & P\_{m,2}^{PV} & \cdots & P\_{m,t}^{PV} & \cdots & P\_{m,T}^{PV} \\ P\_{m,1}^{VV} & P\_{m,2}^{WT} & \cdots & P\_{m,t}^{VV} & \cdots & P\_{m,T}^{WT} \\ P\_{m,1}^{G} & P\_{m,2}^{G} & \cdots & P\_{m,t}^{G} & \cdots & P\_{m,T}^{G} \\ P\_{m,1}^{EV} & P\_{m,2}^{EV} & \cdots & P\_{m,t}^{EV} & \cdots & P\_{m,T}^{EV} \end{bmatrix} \tag{49}$$

*PEV <sup>m</sup>*,*<sup>t</sup>* , *PPV <sup>m</sup>*,*<sup>t</sup>* , *PWT <sup>m</sup>*,*<sup>t</sup>* , and *P<sup>G</sup> <sup>m</sup>*,*<sup>t</sup>* are the power outputs of EVs, PVs, WTs, and DC/AC converters in the *m*-th MG in the *t*-th hour, respectively.

In the outer-loop, the coding about the reactive power output by DC/AC converters can be represented by a real-valued matrix. *s* is the index of the particle of the outer-loop.

**Figure 5.** Process of the PSO algorithm.

$$O\_{\rm MG}^{\rm s} = \begin{bmatrix} O\_{\rm MG1} \\ O\_{\rm MG2} \\ \vdots \\ O\_{\rm MGm} \\ \vdots \\ O\_{\rm MGM} \end{bmatrix} \tag{50}$$
 
$$\begin{bmatrix} O\_{\rm G}^{\rm G} & O\_{\rm G}^{\rm G} & O\_{\rm G}^{\rm G} & O\_{\rm G}^{\rm G} \end{bmatrix} \tag{51}$$

$$O\_{M\gets m}^{\epsilon} = \left[ \begin{array}{cccc} Q\_{m,1}^{G} & Q\_{m,2}^{G} & \cdots & Q\_{m,t}^{G} & \cdots & Q\_{m,T}^{G} \end{array} \right] \tag{51}$$

*Q<sup>G</sup> <sup>m</sup>*,*<sup>t</sup>* is the reactive power output of DC/AC converters in the *m*-th MG at *t*-th hour. However, the dispatch range of the outer-loop variable also changes when the variable of the DC/AC converters changes in the inner-loop. Therefore, a dynamic range adjustment algorithm is added to the outer-loop model.

$$\left| \mathbf{Q}\_{m,t}^{\rm G} \right|^{\rm max} = \sqrt{S\_m^2 - \left( P\_{m,t}^{\rm G} \right)^2} \tag{52}$$

The inner-loop and outer-loop cooperate to generate the optimal optimization results.

### **5. Case Study and Discussion**

### *5.1. Case Description*

There are 30 EVs concentrated in OBMG/RMG for the charging/discharging service [31]. The dispatching cycle is 24 h. This paper sets up four cases to analyze the optimization model. By using NWPs from Wuhan City, Hubei Province, China in June 2020, a day's renewable power generation in summer is predicted as the input of the model.
