# Energy Trading Strategy for Heat and Electricity-Coupled Microgrid Based on Cooperative Game
## 6 Distribution Solution
### Algorithm Implementation  
As shown in (33) and (34), the information exchanges between MGO and users are limited. And it can be easily realized by many existing 1-to-N communication technologies. To achieve the NBS, the detailed implementation of distributed optimization algorithms for MGO and user is shown in Algorithms 1 and 2, respectively. In Algorithm 1, MGO can obtain the optimal solution \(\left\{P_{M,gas}^{h}\right.\), \(P_{B,gas}^{h}\), \(p_{Mi}^{h}\), \(e_{Mi}^{lh}\), \(\lambda_{Mi}^{h}\), \(t_{Mi}^{lh}\)} by solving the optimization problem in (33) according to the information collected from users. The variables \(\left\{p_{Mi}^{h}\right.\), \(el_{Mi^{\prime}}^{h}\), \(\lambda_{Mi^{\prime}}^{h}\), \(t_{Mi}^{lh}\)} will be used to update dual variables \(\left\{\alpha_{Mi^{\prime}}^{h}\), \(\beta_{Mi^{\prime}}^{h}\), \(\tilde{\xi}_{Mi^{\prime}}^{h}\), \(\phi_{Mi}^{h}\right\}\), and then sent to corresponding users. In Algorithm 2, after receiving the updated variables \(\left\{p_{Mi^{\prime}}^{h}\), \(e_{Mi^{\prime}}^{lh}\right.\), \(\lambda_{Mi^{\prime}}^{h}\), \(t_{Mi}^{lh}\)} from MGO, each user solves the local optimization problem in (34) to achieve the optimal solution \(\left\{p_{iM}^{h}\), \(el_{iMi^{\prime}}^{h}\), \(\lambda_{iM}^{h}\), \(t_{Mi}^{lh}\right\}\), and then sends them to MGO. Based on the variables \(\left\{p_{iM^{\prime}}^{h}\), \(el_{iMi^{\prime}}^{h}\), \(\lambda_{iM^{\prime}}^{h}\), \(t_{MiM}^{lh}\right\}\), the dual variables \(\left\{\alpha_{Mi^{\prime}}^{h}\), \(\beta_{iM^{\prime}}^{h}\), \(\tilde{\xi}_{Mi^{\prime}}^{h}\)} will be updated in each iterative. In addition, both algorithms will be executed iteratively until the convergence condition is satisfied, which can be described as follows:  
\[\left\|p_{iM}^{h}-p_{Mi}^{h}\right\|^{2}+\left\|e_{iM}^{lh}+e_{Mi}^{lh}\right\| ^{2}+\left\|\lambda_{iM}^{h}-\lambda_{Mi}^{h}\right\|^{2}+\left\|t_{iM}^{lh}+t_ {Mi}^{lh}\right\|^{2}\leq\varepsilon \tag{35}\]  
where \(\varepsilon\) is the convergence threshold of ADMM. The specific implementation algorithm executed by MGO and user are as follows:```
1:Initialized: \(\left\{u_{Mi}^{h},\beta_{Mi^{\prime}}^{h},\chi_{Mi^{\prime}}^{h},t_{Mi}^{h}\right\} ^{[k+1]}\) = 0, \(\varepsilon\) = 0.00001
2:repeat
3: (1) updated \(\left\{p_{Mi^{\prime}}^{h},el_{Mi^{\prime}}^{h},\lambda_{Mi^{\prime}}^{h},t_{Mi }^{h}\right\}^{[k+1]}\) from users in receive buffer
4: (2) solve local optimal problem in (34) \[\left\{p_{Mi^{\prime}}^{h},el_{Mi^{\prime}}^{h},\lambda_{Mi^{\prime}}^{h},t_{Mi }^{h}\right\}^{[k+1]}=\text{argmin}L_{MO}^{h}\]
5: (3) update dual variables \[ \begin{array}{l}\alpha_{Mi}^{h}[i+1]=\alpha_{Mi}^{h}[i+\rho_{1}(p_{Mi}^{h}[k +1]-p_{Mi}^{h}[k+1])\\ \beta_{Mi}^{h}[k+1]=\beta_{Mi}^{h}[k+2]+\rho_{2}(e_{Mi}^{h}[k+1]-e_{Mi}^{h}[k+1 ])\\ \beta_{Mi}^{h}[k+1]=\xi_{Mi}^{h}[k+\rho_{3}(\lambda_{Mi}^{h}[k+1]-\lambda_{Mi}^ {h}[k+1])\\ \xi_{Mi}^{h}[k+1]=\xi_{Mi}^{h}[k+\rho_{3}(\lambda_{Mi}^{h}[k+1]-\lambda_{Mi}^ {h}[k+1])\\ \xi_{Mi}^{h}[k+1]=\xi_{Mi}^{h}[k+\rho_{3}(\lambda_{Mi}^{h}[k+1]-\lambda_{Mi}^ {h}[k+1])\\ \end{array}