# An Algorithm for Nutrient Mixing Optimization in Aquaponics
## 2 Materials and Methods
### Method of Optimization
#### 2.2.1 Static Objective Function of the Optimization Problem
To increase the numeric stability of the program, \(V_{\text{MW,post,appr}}\) is used instead:  
\[V_{\text{MW,post,appr}}=\frac{V_{\text{MW,post,max}}+V_{\text{MW,post,min}}}{2}, \tag{10}\]  
with \(V_{\text{MW,post,max}}\) and \(V_{\text{MW,post,min}}\) being the upper and lower bounds of the constraint on the water volume that is stored in the mixing tank after the mixing procedure. Note that in the software implementations, errors caused by division by zero are caught and the user is advised to reformulate the optimization task. By generalizing the balancing weights, the program can be used for different systems and use cases without the need to edit the source code. The weights in this example are chosen such that they are mechanistically interpretable, e.g., increasing \(\omega_{\text{bal,N}}\) will punish the usage of fertilizer more. A general approach for choosing weights is to first balance them out such that they are in the same magnitude at a common operation point of the system, and then the user can fine-tune the parameters through expert knowledge. Note that the balancing weights presented in this paper are still just a suggestion and can be adjusted to one's needs. The first term \(e_{\text{N}_{\text{MW},\text{ref}}}\) of the objective function considers the sum of errors between the concentration of beneficial nutrients in the prepared solution and the corresponding reference concentration. Since the reference values for individual nutrients \(q\) can range over wide magnitudes, \(e_{\text{N}_{\text{MW},\text{ref}}}\) is set such that the relative error is minimized. The equation describing this part of the objective function is given as  
\[e_{\text{N}_{\text{MW},\text{ref}}}(u)=\sum_{q_{\text{mut}}}^{Q_{\text{mut}}} \omega_{\text{N}_{\text{ref},\text{fluid}}}\cdot\left(\frac{N_{\text{MW}_{ \text{fluid},\text{post}}}(u)-N_{\text{MW}_{\text{fluid},\text{ref}}}}{N_{\text{ MW}_{\text{fluid},\text{ref}}}}\right)^{2} \tag{11}\]  
where \(N_{\text{MW}_{\text{fluid},\text{post}}}\) is calculated in Equation (7) and \(N_{\text{MW}_{\text{fluid},\text{ref}}}\) is the product of \(V_{\text{MW},\text{post},\text{appr}}\) (see Equation (6)) and the given reference concentration of every nutrient  
\[N_{\text{MW}_{\text{fluid},\text{ref}}}=c_{\text{fluid},\text{ref}}\cdot V_{ \text{MW},\text{post},\text{appr}}. \tag{12}\]  
Furthermore, the relative error of every nutrient is multiplied with an additional weight factor \(\omega_{\text{N}_{\text{ref},\text{fluid}}}\) to specify the priority of each nutrient. Note that Equation (11) fails if the reference concentration is chosen equal to zero; hence, the error of nutrients whose concentration is to be minimized must be calculated separately. For that, the second error-term \(e_{\text{N}_{\text{MW},\text{min}}}\) is introduced:  
\[e_{\text{N}_{\text{MW},\text{min}}}(u)=\sum_{q_{\text{harm}}}^{Q_{\text{harm}}} \omega_{\text{N}_{\text{min}},q_{\text{harm}}}\cdot\left(\frac{N_{\text{MW}_{ \text{fluid},\text{post}}}(u)-N_{\text{MW}_{\text{fluid},\text{ref}}}}{c_{q_{ \text{harm},\text{max}}}V_{\text{MW},\text{post},\text{appr}}}\right)^{2}, \tag{13}\]  
which penalizes the relative difference between the ion concentration of the resulting solution and the reference \(N_{\text{MW}_{\text{harm},\text{ref}}}=0\). The resulting error for an individual ion can again be modified by the weights \(\omega_{\text{N}_{\text{min}},q_{\text{harm}}}\). The purpose of the two remaining terms, \(e_{\text{water}}\) and \(e_{\text{fertil}}\), is to regulate the use of the available water sources and fertilizers. Resources that increase the running costs of the system, e.g., TW and fertilizers, should only be used as little as possible and therefore, high values of \(u_{\text{N},f}\) and \(u_{\text{V},\text{TW}}\) should be avoided. Water that is stored in the fish drain and in the plant drain tank, on the other hand, should be used as much as possible, since both are waste products that should be recycled in the plant irrigation of an aquaponics system.