# Leveraging Urban Water Distribution Systems with Smart Sensors for Sustainable Cities
## 4 Proposed Methodology
### Optimization Algorithm
Global variables are defined, and then the dataframe is loaded, consisting of \(B_{D}\) and \(L_{D}\), extracted from the EPANET simulation. In lines 4 to 6, the model is trained and evaluated by training the LR model, predicting values, and calculating the MAE error. For the optimization process, a dataframe is loaded (line 8) with data from each node, where for each node, the optimized resources will be calculated, where the id, base demand, and lpsdemand will be obtained, the initial resources will be calculated, and if the \(L_{D}\) is below the threshold, a 10% reduction in resources will be applied; otherwise, it will not be considered to have been optimized, and the resources that are optimized will be accumulated. The aim of Algorithm 1 is to predict the lps demand to optimize the resources by comparing the actual demand with the predicted demand at each node, where the final purpose is to reach water resource reduction through optimization of water demand. ```
1:Globals:\(threshold\_optimization=0.8\), \(unit\_cost=0.5\), \(total\_resources=0\)
2:LoadData:\(X\gets df[[^{\prime}base\_demand^{\prime}]],y\gets df[[^{\prime}lps\_demand^{\prime}]]\)
3:TrainModelandEvaluate:
4:\(X\_train,X\_test,y\_train,y\_test\leftarrow\text{train\_test\_split}(X,y,0.2,42)\)
5:\(model\leftarrow\text{LinearRegression}().fit(X\_train,y\_train)\)
6:\(mae\leftarrow\text{mean\_absolute\_error}(y\_test,model.predict(X\_test))\)
7:OptimizationProcess:
8:\(df\leftarrow\text{pd.read\_excel}(\text{'city\_}L_{D}andB_{D}.\text{xlsx'},\) names=['node\_id', 'base\_demand', 'lps\_demand']\)
9:for\(index\_row\)in\(df.interrows()\)do
10:\(node\_id\leftarrow\text{\emph{row}}[^{\prime}node\_id'],\)\(base\_demand,lps\_demand\gets row[[^{\prime}base\_demand^{\prime},lps\_demand^{\prime}]]\)
11:\(resources\gets base\_demand\times unit\_cost\)
12:if\(lps\_demand<base\_demand\times threshold\_optimization\)then
13:print(\"Successful optimization at node\", \(node\_id\))
14:\(resources\gets resources\times 0.9\)
15:else
16:print(\"Optimization was not achieved in the node\", \(node\_id\))
17:endif
18:\(total\_resources\gets total\_resources+resources\)
19:endfor
```  
**Algorithm 1** Water Distribution System Optimization  
Algorithm 2 mainly implements a linear regression model to predict the base demand as a function of pipe diameter in a water distribution environment, where the data loaded into the model come from the results of the simulations, where the measurements of the selected pipe diameters and the base demand corresponding to each diameter are collected for all the nodes deployed in each scenario. Data arrays of diameter and base demand data are extracted from the dataframe, and a linear sklearn regression model is created to be trained with the diameter data (as the independent variable) and base demand (as the dependent variable). Diameter is expected to have the shape that the model expects. Finally, the regression and intersection coefficient of the trained regression line are plotted. This linear regression is intended to derive a mathematical model that captures a relation between pipe diameter and water demand, where this model allows for the prediction of the demand for new diameter values. Algorithm 3 has the function of reading the set of data provided, which, in this case, will contain the values of node_id, base_demand, lps_demand, and node_inhabitants, to calculate and analyze the potential water savings per node, and achieve the total savings of the network, considering that water savings will only be calculated in those nodes that meet the optimization condition of Algorithm 1. In addition, this algorithm will allow the identification of nodes with a greater opportunity to optimize consumption. The dataframe is loaded in lines 1 and 2, where the values of base_demand and lps_demand, extracted from the EPANET simulation, and node_inhabitants will be obtained. Subsequently, in lines 3 to 15, a function is defined to calculate the savings per node.