# Optimizing Crop Yield and Reducing Energy Consumption in Greenhouse Control Using PSO-MPC Algorithm
## 3 Methodology
### PSO-MPC Control Algorithm  
In this work, a model predictive control (MPC) algorithm was adopted for greenhouse control to balance the crop yield. The objective of the MPC algorithm is to optimize the controllable parameters such as heating, ventilation, and lighting levels while ensuring that the crop yield is maximized and energy costs are minimized. To achieve this, an objective function is defined below that takes into account both the crop yield and energy costs:  
\[max_{\mathbf{u}}\sum_{i=j}^{j+T}(DM((i+1)\Delta t)-DM(i\Delta t))-\gamma\cdot \Delta Energy_{i} \tag{3}\]  
\[\mathbf{0}\leq\mathbf{u}\leq\mathbf{1}\]  
where \(\mathbf{u}\) represents the greenhouse control parameters during a horizontal time \([j\Delta t,(j+1)\Delta t,\ldots,(j+T)\Delta t]\). \(DM(i\Delta t)\) represents the dry matter at \(i\Delta t\). \(\Delta Energy_{i}\) represents the energy consumption of the lamp, boiler, and heat harvesting system calculated with the greenhouse climatic model during \([i\Delta t,(i+1)\Delta t]\) according to [22]. In specific, it is calculated as:  
\[\Delta Energy_{i}=\int_{i\Delta t}^{(i+1)\Delta t}lampEnergy(t)+boilEnergy(t) +hhEnergy(t)dt \tag{4}\]  
where \(\Delta Energy_{i}\) represents the consumed energy during \([i\Delta t,(i+1)\Delta t]\). \(lampEnergy(t)\), \(boilEnergy(t)\) and \(hhEnergy(t)\) represent the time-dependent lamp energy, boiler energy, and heat harvest system energy, respectively, as calculated in [22]. By solving the above optimization problem, we can maximize the crop growth while restraining the energy cost.  
To solve (3), we utilized a PSO algorithm. PSO is a population-based stochastic optimization algorithm inspired by the collective behavior of animals such as flocks of birds or schools of fish [23]. We first generated \(N\) solution candidates (also referred to as particles) \([X_{1}^{0},\ldots,X_{N}^{0}]\) and their corresponding velocities \([V_{1}^{0},\ldots,V_{N}^{0}]\). Each particle had an associated fitness value that could be evaluated using the objective function defined in (3). Particle values were updated with the following equations until the optimal solution was found:  
\[\begin{split} V_{i}^{g+1}&=wV_{i}^{g}+C_{1}*rand()*( PB_{i}-X_{i}^{g})+C_{2}*rand()*(GB^{g}-X_{i}^{g})\\ X_{i}^{g+1}&=X_{i}^{g}+V_{i}^{g+1}\end{split} \tag{5}\]  
where \(V_{i}^{g}\) and \(X_{i}^{g}\) represent the \(i\)-th velocity and and position of particle \(i\), respectively, in the \(g\)-th generation. \(C_{1}\), \(C_{2}\) represent the learning factors. \(rand()\) generates a random number in [0,1]. \(PB_{i}\) and \(GB^{g}\) represent the personal best performance for the \(i\)-th particle and the best performance of the group for the \(g\)-th generation, respectively, determined via the objective function. \(w\) is the inertial weight that shows the effect of the previous velocity vector on the new vector. By exploiting the PSO algorihtm, the optimal control parameters which minimize the objective function can be effectively found. And the comparisons between the PSO algorithm and other optimization methodologies, such as the genetic algorithm are provided in the experimental section.