Modelling and Optimization of Water Quality in Distribution Networks