# Leveraging Urban Water Distribution Systems with Smart Sensors for Sustainable Cities
## 4 Proposed Methodology
### Optimization Algorithm  
Three algorithms were developed to optimize the above-created WDNs. We use an LR technique to feed our simulation output. When programming LR, three global variables or parameters have been considered:  
1. threshold_optimization: This indicates when a node has successfully optimized its water demand resources. It is set initially to 0.8, meaning optimization applies once the threshold is below this value. It should not exceed 1.0 (100%). An 80% threshold is used here, meaning optimization occurs when lps_demand (\(L_{D}\)) is less than 80% of base_demand (\(B_{D}\)). Thus, \(L_{D}\) values must be at least 20% lower than \(B_{D}\), reflecting theoretical demand based on area characteristics like average water consumption per inhabitant and population. 2. Unit_cost: This parameter calculates the cost per unit of base demand, enabling the estimation of total resource costs. It is critical for the evaluation of the financial savings achieved through water optimization. It has no impact on the LR; however, it is used to calculate the costs when predicting with the model, which will allow the estimation of the total resource costs in the network, serving as a cost analysis. For this situation, it has been chosen to be set initially to 0.5, a random value less or equal to the unit. The user can set their own value. The difference concerning the initial value is that the unit cost will be reduced proportionally to the value set, with no interference from the variation in unit_cost in the LR, as demonstrated by tests carried out varying this parameter from 0.25 and 0.75. That parameter has been added to obtain details on the number of resources that could be saved when the optimization condition is reached at nodes. 3. total_resources: This parameter is set initially to 0 because that symbolizes the starting point where total resources will start to accumulate. This is the ideal starting point, as with each LR performance, total resources that will have been optimized will be obtained; meanwhile, if the value is initialized to a state other than 0, it will always start from a state that is not the one we want for our analysis. It should be pointed out that this parameter is related to the unit_cost parameter, so any change in its value has a proportional effect upon unit_cost. This parameter can collect the total resources used in the system to perform node optimization. Next, we describe the three above-mentioned algorithms. Algorithm 1 has the functionality to read the provided dataset, which in this case contains the base demands and the lps (liter per second) demands of the nodes of our water distribution scenarios. A machine learning model is trained to obtain a prediction of the \(L_{D}\) based on the \(B_{D}\), where the model's performance will be evaluated through the mean absolute error (MAE). Subsequently, a loop is performed at each node, where the cost of the resources that would have to be used to supply that demand will be calculated. For example, suppose the simulated demand, being the \(L_{D}\) value, is lower than the \(B_{D}\) at the threshold determined (80%). In that case, a message on the screen will indicate that optimization has been achieved, reducing the resources by 10%. The 10% reduction criterion was selected based on our preliminary simulation results. When LD (Local Demand) is lower than BD (Base Demand), a conservative reduction of 10% ensures sustainability without risking undersupply, particularly when the system operates near the 80% threshold, a critical level in balancing resource conservation and user demand. In the opposite case, a message will be displayed indicating that optimization has not been achieved, where the total resources of all nodes will be accumulated. This algorithm optimizes water distribution using an ML model to predict when the \(L_{D}\) will be less than the \(B_{D}\) at the threshold to be determined, reducing the resources supplied to those nodes.