# MPC for Optimized Energy Exchange between Two Renewable-Energy Prosumers
## 2 Problem Formulation
### System Constraints  
There are two main equality constraints in the system model, such that the power total flows into the loads meet those loads exactly at each time interval,  
\[P_{LR}(k)=P_{2R}(k)+P_{4R}(k)+P_{1R}(k)+P_{CR}(k)+P_{BCR}(k), \tag{6}\]  
\[P_{LC}(k)=P_{2C}(k)+P_{4C}(k)+P_{1C}(k)+P_{RC}(k)+P_{BRC}(k). \tag{7}\]  
The power flows that form the control vector \(u(k)\), must meet various inequality constraints. Each of these flows is non-negative and each has an upper bound. In addition, transfers from the RER and battery to the residential load cannot exceed the load  
\[P_{2R}(k)+P_{4R}(k)+P_{CR}(k)+P_{BCR}(k)\leq P_{LR}(k), \tag{8}\]  
\[P_{2C}(k)+P_{4C}(k)+P_{RC}(k)+P_{BRC}(k)\leq P_{LC}(k). \tag{9}\]  
Also, the three power flows from the RER supply cannot exceed the total available RER output at any time. \[P_{2R}(k)+P_{3R}(k)+P_{RC}(k)\leq P_{RER_{R}}(k), \tag{10}\]  
\[P_{2C}(k)+P_{3C}(k)+P_{CR}(k)\leq P_{RER_{C}}(k). \tag{11}\]  
The power flows \(P_{1R}(k)\) and \(P_{1C}(k)\) are bounded according to  
\[0\leq P_{1R}(k)\leq P_{1R}^{\max}, \tag{12}\]  
\[0\leq P_{1C}(k)\leq P_{1C}^{\max}. \tag{13}\]  
Due to equality constraints, Equations (8) and (9), these can be rewritten as  
\[P_{2R}(k)+P_{4R}(k)+P_{CR}(k)+P_{BCR}(k)\geq P_{1R}^{\max}-P_{LR}(k), \tag{14}\]  
\[P_{2C}(k)+P_{4C}(k)+P_{RC}(k)+P_{BRC}(k)\geq P_{1C}^{\max}-P_{LC}(k). \tag{15}\]  
The inequality constraints can now be summarized in a single expression  
\[P_{LC}(k)=P_{2C}(k)+P_{4C}(k)+P_{1C}(k)+P_{RC}(k)+P_{BRC}(k). \tag{7}\]0  
where \(\gamma_{1}=[\gamma_{R},~{}\gamma_{C}]^{\top}\), the inequality is taken elementwise, and \(\gamma_{R}\) and \(\gamma_{C}\) are defined as \[\gamma_{R}=\left[ \begin{array}{c}P_{RER_{R}}(k)\\ P_{2R}^{max}\\ P_{3R}^{max}\\ P_{4R}^{max}\\ P_{RC}^{max}\\ P_{1R}^{max}-P_{LR}(k)\\ P_{LR}(k)\\ 0_{6\times 1}\end{array} \right],\gamma_{C}=\left[ \begin{array}{c}0_{6\times 1 }\\ P_{RER_{C}}(k)\\ P_{2C}^{max}\\ P_{3C}^{max}\\ P_{4C}^{max}\\ P_{CR}^{max}\\ P_{BCR}^{max}\\ P_{1C}^{max}-P_{LC}(k)\\ P_{LC}(k)\end{array} \right].\]  
Furthermore, \(M_{1}=\left[M_{R},\ M_{C}\right]^{\top}\), where \(M_{R}\) and \(M_{C}\) are defined as  
\[M_{R}=\left[ \begin{array}{ccccccccc}1&0&1&0&0&0&1&0&0&0\\ -1&0&-1&0&0&0&-1&0&0&0\\ 1&0&0&0&0&0&0&0&0&0\\ -1&0&0&0&0&0&0&0&0&0\\ 0&0&1&0&0&0&0&0&0&0\\ 0&0&-1&0&0&0&0&0&0\\ 0&0&0&0&1&0&0&0&0\\ 0&0&0&0&-1&0&0&0&0\\ 0&0&0&0&0&1&0&0&0\\ 0&0&0&0&0&0&1&0&0\\ 0&0&0&0&0&0&0&-1&1&0\\ -1&0&0&0&-1&0&0&-1&0&-1\\ 1&0&0&0&1&0&0&1&0&1\end{array} \right],\]  
\[M_{C}=\left[ \begin{array}{ccccccccc}0&1&0&1&0&0&0&1&0&0\\ 0&-1&0&-1&0&0&0&-1&0&0\\ 0&1&0&0&0&0&0&0&0&0\\ 0&-1&0&0&0&0&0&0&0\\ 0&0&0&1&0&0&0&0&0\\ 0&0&0&-1&0&0&0&0&0\\ 0&0&0&0&0&1&0&0&0\\ 0&0&0&0&0&-1&0&0&0\\ 0&0&0&0&0&0&0&1&0&0\\ 0&0&0&0&0&0&0&-1&0&0\\ 0&0&0&0&0&0&0&0&1\\ 0&0&0&0&0&0&0&0&-1\\ 0&-1&0&0&0&-1&-1&0&-1&0\\ 0&1&0&0&0&1&1&0&1&0\end{array} \right].\]The constraints are applied to the predicted control flows at each future time step, using the following definition of the predictive control vector, where \(N_{C}\) is the control horizon. The constraints are then applied at each time step,  
\[ \begin{bmatrix}\gamma_{1}(k)\\ \gamma_{1}(k)\\ \vdots\\ \gamma_{1}(k)\end{bmatrix} \geq U(k) \begin{bmatrix}M_{1}&\ldots&0\\ \vdots&\ddots&\vdots\\ 0&\ldots&M_{1}\end{bmatrix} , \tag{17}\]  
where  
\[\overline{\gamma}_{1}(k)= \begin{bmatrix}\gamma_{1}(k)\\ \gamma_{1}(k)\\ \vdots\\ \gamma_{1}(k)\end{bmatrix} \text{ and }\overline{M}_{1}=\underbrace{ \begin{bmatrix}M_{1}& \ldots&0\\ \vdots&\ddots&\vdots\\ 0&\ldots&M_{1}\end{bmatrix} }_{N_{c}}.\]  
The predicted values of the state of charge can now be written as  
\[x_{m}(k+i|k)=x_{m}(k)+b_{m}\sum_{j=k}^{j\leq k+i-1}u(j). \tag{18}\]  
By defining the predictive state of charge \(X_{m}\) (\(k\)) as  
\[X_{m}(k)\triangleq \begin{bmatrix}x_{m}(k)\\ x_{m}(k+1|k)\\ x_{m}(k+N_{c}-1|k)\end{bmatrix} ,\text{ we can write} \tag{19}\]  
\[X_{m}(k)= \begin{bmatrix}I_{2\times 2}\\ I_{2\times 2}\\ \vdots\\ I_{2\times 2}\end{bmatrix} x_{m}(k)+B_{m}U(k),B_{m}=