# MPC for Optimized Energy Exchange between Two Renewable-Energy Prosumers
## 2 Problem Formulation
### MPC Objective Function and Reference Trajectory  
The MPC objective function is defined at each time step by summing all ten terms in Table 1. The function looks forward to a time horizon defined by \(N_{p}\), summing energy terms over a selected time interval according to  
\[J(k)=\sum_{i=k}^{k+N_{p}}\sum_{q=1}^{5}[J_{qR}(i)+J_{qC}(i)]. \tag{3}\]  
To develop an expression for \(J(k)\) that can be expressed as a quadratic, we define  
\[Y(k)=[Y^{T}(k),Y^{T}(k+1\,|\,k),...,Y^{T}(k+N_{p}-1\,|\,k)]^{T},\]  
where the notation \(y(k+i|k)\) represents the predicted value of \(y\) at step i associated with sampling time \(k\).  
Using the same notation, future predictions for residential and commercial loads are represented by \(P_{LR}(k+i|k)\) and \(P_{LC}(k+i|k)\), respectively. These could be generated from historical data, models for human behavior, weather forecasts, etc. Similarly, future predictions for residential and commercial RER outputs are represented by \(P_{RER}(k+i|k)\) and \(P_{RER_{C}}(k+i|k)\), respectively, as would be generated from weather forecasts and models for the various hardware components involved in renewable power generation. All the predicted quantities, for each time step into the future up to the prediction horizon \(N_{p}\), are combined into a single reference trajectory vector \(R(k)\), given by  
\[R(k)=\left[\begin{array}{c}C_{1}P_{LR}(k)\\ C_{1}P_{LC}(k)\\ C_{3}P_{RER_{R}}(k)\\ C_{3}P_{RER_{C}}(k)\\ 0_{6\times 1}\\ \vdots\\ C_{1}P_{LR}(k+N_{p}|k)\\ C_{1}P_{LC}(k+N_{p}|k)\\ C_{3}P_{RER_{R}}(k+N_{p}|k)\\ C_{3}P_{RER_{C}}(k+N_{p}|k)\\ 0_{6\times 1}\end{array}\right]. \tag{4}\]  
\begin{table}
\begin{tabular}{c c c} \hline \hline
**System Outputs** & **Objective Function Terms** & **Description** \\ \hline \(y_{mk}=C_{1}[P_{R}+P_{mk}+P_{CR}+P_{{\rm{}_{RC}}}]\) & \(I_{R}=C_{1}^{2}P_{R}^{2}=[C_{1}P_{LR}-y_{mk}]^{2}\) & Energy purchased from other network \\ \(y_{{\rm{}_{RC}}}=C_{1}[P_{RC}+P_{RC}+P_{RC}+P_{RC}]\) & \(I_{JC}=C_{1}^{2}P_{1c}^{2}=[C_{1}P_{LR}-y_{mk}]^{2}\) & prosumers or retail suppliers \\ \hline \(y_{mk}=C_{2}(P_{SR}+P_{{\rm{}_{RC}}})\) & \(I_{R}=C_{2}^{2}(P_{SR}+P_{{\rm{}_{RC}}})^{2}=y_{mk}^{2}\) & Charging and discharging energy \\ \(y_{{\rm{}_{RC}}}=C_{2}(P_{SC}+P_{{\rm{}_{RC}}})\) & \(I_{R}=C_{2}^{2}(P_{SC}+P_{{\rm{}_{RC}}})^{2}=y_{CC}^{2}\) & transfer to battery \\ \hline \(y_{MR}=C_{3}P_{SR}\) & \(y_{R}=C_{3}^{2}P_{SR}^{2}-y_{SR}^{2}\) & Energy sold to other network prosumers \\ \hline \(y_{RC}=C_{3}P_{SC}\) & \(I_{JC}=C_{3}^{2}P_{SC}^{2}-y_{CC}^{2}\) & \\ \hline \(y_{MR}=C_{4}P_{RC}\) & \(I_{JA}=C_{4}^{2}P_{RC}^{2}-y_{MR}^{2}\) & Energy transferred from the residential renewable energy resource (RER) \\ \(y_{AC}=C_{4}P_{CR}\) & \(I_{JC}=C_{4}^{2}P_{CR}^{2}-y_{MC}^{2}\) & supply to the commercial load and vice versa \\ \hline \(y_{R}=C_{5}P_{{\rm{}_{RC}}}\) & \(I_{SR}=C_{2}^{2}P_{{\rm{}_{RR}}}-y_{SR}^{2}\) & Energy transferred from the residential \\ \(y_{AC}=C_{5}P_{{\rm{}_{RC}}}\) & \(I_{JC}=C_{5}^{2}P_{{\rm{}_{RC}}}-y_{LC}^{2}\) & battery to the commercial load and vice versa \\ \hline \hline \end{tabular}
\end{table}
Table 1: System outputs and objective terms.  
The output vector \(Y(k)\) should follow this reference, and the minimization of the MPC objective can now be re-expressed in quadratic form:  
\[\min J(k)=\min(Y(k)-R(k))^{\top}(Y(k)-R(k)). \tag{5}\]