# Leveraging Urban Water Distribution Systems with Smart Sensors for Sustainable Cities
## 4 Proposed Methodology
### Optimization Algorithm
Before calculating savings, it is necessary to set an optimization condition (line 6), to obtain savings only for those nodes that optimize resources. Considering the previous condition is fulfilled, it is necessary to contrast base_demand with lps_demand to calculate savings. As a result of the variation between both fields, savings in litters per second are obtained, resulting in the lps_saving variable. In this scenario, we want to obtain the savings in liters per day, so using the conversion factor, the units are changed, obtaining variable lpd_saving, and subsequently, a savings value per inhabitant will be obtained. This function is applied to each row of the dataframe, where the column called saving_lpd containing the value of savings per inhabitant of each node is added. Finally, the total resources saved are obtained by adding up all the individual savings. To enhance visual analysis, a condition is set that the nodes are ordered from the greatest to the least in terms of resource savings. The objective of Algorithm 3 is to obtain the individual savings per inhabitant after optimization in a node and provide the total savings achieved due to optimizing all the nodes that make up the network. ```
1:Load Data:
2:\(df\leftarrow\) pd.read_excel('city.xlsx')
3:diameter\(\gets df[^{\prime}diameter^{\prime}].values\)
4:\(\mathit{base\_demand}\gets df[^{\prime}base\_demand^{\prime}].values\)
5:Create Linear Regression Model:
6:\(\mathit{model}\leftarrow\) LinearRegression()
7:Train the Model:
8:\(\mathit{model}.fit(\mathit{diameter}.reshape(-1,1),\mathit{base\_demand})\)
9:Extract Coefficients:
10:print(\(\mathit{model}.coef\_\))
11:Extract Intercept:
12:print(\(\mathit{model}.intercept\_\))
13:Generate Prediction Data:
14:\(\mathit{line\_diameters}\leftarrow\) np.linspace(min(diameter), max(diameter), 100)
15:\(\mathit{predictions}\leftarrow\) model.predict(diameters_line.reshape(\(-1\),1))
```  
**Algorithm 2** Linear Regression Model Training and Prediction  
```
1:Th: Threshold value for optimization
2:Load Data:
3:\(df\leftarrow\) Read Excel file named \(\mathit{file\_name}\)
4:functioncalculate_saving(\(\mathit{row}\))
5:\(\mathit{base\_demand}\leftarrow\) Demand base from \(\mathit{row}\)
6:\(\mathit{lps\_demand}\leftarrow\) Demand lps from \(\mathit{row}\)
7:if\(\mathit{demanda\_lps}<\mathit{base\_demand}\times TH\)then
8:\(\mathit{saving\_lps}\leftarrow\mathit{base\_demand}-\mathit{lps\_demand}\)
9:\(\mathit{saving\_lpd}\leftarrow\mathit{saving\_lps}\times 60\times 60\times 24\)
10:\(\mathit{Inhabitants}\leftarrow\) Inhabitants of node from \(\mathit{row}\)
11:\(\mathit{saving\_inhabitant}\leftarrow\mathit{saving\_lpd}/\mathit{inthabitants}\)
12:return\(\mathit{saving\_inhabitant}\)
13:else
14:print\"Optimization not achieved in \", \(\mathit{row}[^{\prime}id\_node^{\prime}]\)
15:return 0
16:endif
17:endfunction
18:functionoptimize_water_saving(\(df\))
19:\(df[^{\prime}saving\_lpd^{\prime}]\leftarrow\) Apply calculate_saving function to each row
20:\(\mathit{total\_saving}\leftarrow\) Sum of savings where \(\mathit{saving\_lpd}>0\)
21:print\"Total savings: \", \(\mathit{total\_saving}\), \" liters/day\"
22:\(df\_optimized\leftarrow\) Select rows where \(\mathit{saving\_lpd}>0\)
23:\(df\_optimized\leftarrow\) Sort rows by \(\mathit{saving\_lpd}\) in desc.ord. 24:return\(df\_optimized\), \(\mathit{total\_saving}\)
25:endfunction
```  
**Algorithm 3** Water Savings AlgorithmIn terms of complexity, the algorithm used for optimizing water distribution networks with a linear regression model is efficient both in time and space. The time complexity of training the linear regression model is \(O(n\times p^{2})\), where \(n\) represents the number of data points, and \(p\) represents the number of features. This is relatively efficient, especially when compared to genetic algorithm-based approaches common in related studies, which generally have a higher complexity of \(O(n^{2})\).