# Energy-Water Management System Based on MPC for a Greenhouse in a Mapuche Indigenous Community
## 5 Model Predictive Control Strategies for the EWMS
### Short Term
#### 5.1.2 Pump and Irrigation Controller (EMS)  
Once the ideal climate conditions are achieved inside the greenhouse, it is necessary to manage the use of water and energy resources in the system. For this objective, the controller minimizes  
\[J=\sum_{i=1}^{3}C_{i}\delta_{i}(k)+\lambda_{I}\sum_{i=1}^{N_{h}}sign(I(k+i-1))+ \lambda_{P}\sum_{i=1}^{N_{h}}\max(P_{dem}(k+i-1),0), \tag{23}\]  
to determine when the crop needs irrigation and how long that action has to be applied within the sample period (10 min). This decision regarding the volume of irrigation is represented by the variable \(I(k)\). Additionally, from the minimization of (23), the model can determine when to use the pump to refill the water tank \(Rec_{pump}(k)\). Three main components related to the conditions in the systems are included in the objective function (23). The first term is associated with the daily irrigation objective and is used to penalize the difference between the daily irrigation objective \(\bar{V}\) and the water used each day for irrigation. This difference is denoted as \(\delta\) and varies each day. The values of \(\delta\) for each possible day within a prediction horizon of 48 h are obtained with the following equations:  
\[\delta_{1}(k) =\left[V_{irr}(k)+\sum_{j=1}^{N(k)}\Delta t\rho_{r}I(k+j-1) \right]-\bar{V}(1), \tag{24}\] \[\delta_{2}(k) =\sum_{j=1+N(k)}^{N(k)+H}\Delta t\rho_{r}I(k+j-1)-\bar{V}(2),\] (25) \[\delta_{3}(k) =\left\{ \begin{matrix}\frac{N(k)-H}{H}\Big{[}\sum_{j=H+N(k)+1}^{2 H}\Delta t\rho_{r}I(k+j-1)-\bar{V}(3)\Big{]}&if\;\;N(k)<H\\ 0&\sim\end{matrix} \right., \tag{26}\]  
where \(\rho_{r}\) is the volume of irrigation per second, and \(N(k)\) is the first of the prediction steps \(j\) for which an instant \(k\) coincides with a day change. Since \(H\) represents the number of prediction steps observed within 24 h, the predictions \(N(k)+k\) and \(N(k)+H+k\) are always associated with the first instant of a day. Additionally, Equations (24)-(26)  
Figure 3: Membership functions. (**a**) Daytime temperature; (**b**) night-time temperature; (**c**) relative humidity; (**d**) total window change. show that the interactions among the objective functions vary each day because of the daily fluctuations in the irrigation objective \(\bar{V}(i)\). For example, in (24), on the first day, only the next \(N(k)\) prediction steps and the volume of water \(V_{irr}(k)\) used on that day up to instant \(k\) are considered. Moreover, the third day in (26) is either not or partially considered, indicating that decision processes do not require information from the full third day. To compensate for this issue, the adjustment factor \(\frac{N(k)-H}{H}\) was added to decrease the weight of this decision. Additionally, in the first term of the objective function (23), the penalty parameters \(C_{i}\) are defined as follows:  
\[C_{i}=\left\{ \begin{matrix}C_{ex}&si&\delta_{i}(k)\geq 0,\\ -C_{lack}&si&\delta_{i}(k)<0,\end{matrix} \right. \tag{27}\]  
where \(C_{ex}\) and \(C_{lack}\) are the costs of excess and insufficient irrigation, respectively. The second term in (23) is used to minimize the number of irrigation instances to avoid the excessive activation/deactivation of the actuators. In addition, the third term is used to minimize the power \(P_{dem}\) that the system uses to maintain operation. This third objective was included to ensure the proper functioning of the greenhouse system with optimal energy use. At this point in the model predictive control design, monitoring the resource conditions, such as the volume of water in the water tank and the state of the battery charge, was important for achieving optimal system operation.