# 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  
As already stated, the objective function is a part of the design process and can be chosen in any form necessary. As an example, we select the goal of getting close to a target concentration at low costs. A mathematical description is stated below:\[J(u)=\omega_{\text{bal,ref}}\cdot e_{\text{N}_{\text{MW,ref}}}(u)+\omega_{\text{ bal,min}}\cdot e_{\text{N}_{\text{MW,min}}}(u)+\omega_{\text{bal,V}}\cdot e_{\text{ water}}(u_{\text{V}})+\omega_{\text{bal,N}}\cdot e_{\text{fertil}}(u_{\text{N}}). \tag{8}\]  
The error terms are explained in the following paragraphs. Overall, the objective function consists of two linear and two quadratic error terms \(e\) that are added up and, in that way, realize the function value. Furthermore, the errors are multiplied by weights \(\omega_{\text{bal}}\) that balance the influence of the respective terms due to different mathematical formulations and different dimensions as well as units of the inputs \(u\) that realize the optimization variables. In our example, the balancing is conducted as follows:  
* \(\omega_{\text{bal,ref}}\cdot e_{\text{N}_{\text{MW,ref}}}(u)\) sums up to 100 if the relative error between \(N_{\text{MW,start,post}}\) and the reference \(N_{\text{MW,start,ref}}\) for each nutrient \(q_{\text{mut}}\in Q_{\text{mut}}\) equals 10%. Note that this term scales quadratic to the error, i.e., higher deviations are punished more strongly. * \(\omega_{\text{bal,min}}\cdot e_{\text{N}_{\text{MW,min}}}(u)\) sums up to 100 if the concentration of every undesired ion \(q_{\text{harm}}\in Q_{\text{harm}}\) is equal to 50% of the maximum level. Note that this term also scales quadratic to the error, i.e., higher deviations are punished more strongly. * \(\omega_{\text{bal,V}}\cdot e_{\text{water}}(u_{\text{V}})\) increases linearly by 100 for every 25% of missing water added to fill the tank. * \(\omega_{\text{bal,N}}\cdot e_{\text{fertil}}(u_{\text{N}})\) compares the missing nutrient mass required to realize \(c_{\text{MW,ref}}\) at \(V_{\text{MW,post,appr}}\) for every nutrient to the amount of used fertilizers. If the cumulative mass of consumed fertilizers is equal to the missing nutrient mass, \(\omega_{\text{bal,N}}\cdot e_{\text{fertil}}(u_{\text{N}})\) linearly increases to 100. Note that since fertilizers only partially consist of the required nutrient, fertilizer mass will exceed the required nutrient mass. This is to punish fertilizer usage and encourage adding nutrients through FW and PW. To achieve this, the balancing weights \(\omega_{\text{bal}}\) are chosen as stated below:  
\[\omega_{\text{bal,ref}} =\frac{100(\frac{1}{0.1})^{2}}{q_{\text{mut,max}}}=\frac{10000}{ q_{\text{mut,max}}}, \tag{9}\] \[\omega_{\text{bal,min}} =\frac{100(\frac{1}{0.5})^{2}}{q_{\text{harm,max}}}=\frac{400}{ q_{\text{harm,max}}},\] \[\omega_{\text{bal,V}} =\frac{(\frac{1}{0.25})\cdot 100}{V_{\text{MW,post,appr}}-V_{\text{MW, pre}}},\] \[\omega_{\text{bal,N}} =\frac{100}{V_{\text{MW,post,appr}}\cdot\|c_{\text{MW,ref}}\|_{1 }-V_{\text{MW,pre}}\cdot\|c_{\text{MW,pre}}\|_{1}},\]  
where \(q_{\text{mut,max}}\) are the number of beneficial nutrients that should be close to a given reference and \(q_{\text{harm,max}}\) are the number of ions that should be minimized. The balancing weights of water and fertilizer consumption are chosen to be relative to the missing water volume and the missing nutrient mass to realize the reference concentration of the final volume \(V_{\text{MW,post}}\). However, \(V_{\text{MW,post}}\) cannot be directly used for the weights, because for \(V_{\text{MW,pre}}=V_{\text{MW,post}}\) a division through zero would occur in the objective function, which would lead to a failing of the solver.