Water network design¶
A town gets its water by gravity from a reservoir, through a network of pipes with loops. The pipes are to be bought from a catalog of diameters. Wide pipes cost more, narrow pipes lose more pressure to friction, and every node must keep a minimum pressure. The goal is the cheapest choice of diameters that still gives every node enough pressure.
In a network with loops, the water splits between paths, so the flow in each pipe, and the pressure at each node, depends on all the diameters at once. Each design is checked by solving the network's hydraulic equations. This example uses two classic test networks:
| Network | Pipes | Diameters | Designs | Best known cost |
|---|---|---|---|---|
| two-loop (Alperovits and Shamir, 1977) | 8 | 14 | 14^8, about 1.5 billion | 419 000 $ |
| Hanoi (Fujiwara and Khang, 1990) | 34 | 6 | 6^34, about 3 x 10^26 | 6 081 119 $ |
The data come from the benchmark collection of the Centre for Water Systems (University of Exeter) and from Zhou, Gao and Simpson (2013), checked against each other. Head losses follow the Hazen-Williams formula with the constant 10.667, as in EPANET. This matters: published costs and even feasibility depend on the constant. The Hanoi design of 6 081 119 $ (Sedki and Ouazar, 2012) keeps a lowest pressure of 30.006 m with 10.667, but drops below 30 m with 10.6744, and a well known cheaper design of 6 072 610 $ (Savic and Walters, 1997) is feasible with 10.5088, but not with 10.667. Some papers give the best known cost as 6 081 087 $. That is the same design priced at 98.378 $ per m for 20 inch pipes, where this example uses the cost formula of the instance, 1.1 D^1.5 $ per m, which gives 98.387 $.
The model¶
The variable is one Integer per pipe: an index into the diameter catalog. The objective is the cost, the sum of length times unit cost over the pipes. The constraint is the pressure: 30 - pressure at every node must be at most 0, so each node is one inequality constraint.
The pressures come from the global gradient algorithm of Todini and Pilati (1988), the method inside EPANET. It is Newton's method on two sets of equations: the Hazen-Williams head loss of every pipe, and the flow balance of every node. Each Newton step is one linear solve with the node heads as unknowns, and it converges in about 10 steps. The objective is vectorized: it solves the networks of a whole population at once.
import numpy as np
import ypea
# the two-loop network: node 1 is a reservoir with a fixed head of 210 m
demand = np.array([100, 100, 120, 270, 330, 200]) / 3600 # nodes 2 to 7, m3/s
elevation = np.array([150, 160, 155, 150, 165, 160.0]) # nodes 2 to 7, m
pipes = [(1, 2), (2, 3), (2, 4), (4, 5), (4, 6), (6, 7), (3, 5), (5, 7)] # (from node, to node)
length = np.full(8, 1000.0) # m
reservoir_head = 210.0 # m
inches = np.array([1, 2, 3, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24])
diameters = inches * 0.0254 # m
unit_cost = np.array([2, 5, 8, 11, 16, 23, 32, 50, 60, 90, 130, 170, 300, 550.0]) # $ per m
# incidence of the pipes and nodes 2 to 7: -1 where a pipe starts, +1 where it ends
A = np.zeros((8, 6))
fixed = np.zeros(8) # the reservoir head, for pipes that start at node 1
for k, (start, end) in enumerate(pipes):
if start == 1:
fixed[k] = -reservoir_head
else:
A[k, start - 2] = -1
A[k, end - 2] = 1
def heads(d):
"""Head at nodes 2 to 7 for a batch of designs (rows of diameters in m).
Global gradient algorithm: Newton's method on the Hazen-Williams head loss of every
pipe and the flow balance of every node.
"""
r = 10.667 * length / (130**1.852 * d**4.871) # pipe resistance
q = np.pi / 4 * d**2 # start with a velocity of 1 m/s
h = np.full((len(d), 6), reservoir_head)
for _ in range(50):
abs_q = np.maximum(np.abs(q), 1e-8)
slope = 1.852 * r * abs_q**0.852
pipe_error = r * abs_q**0.852 * q + h @ A.T + fixed # head loss - head drop
node_error = q @ A - demand # inflow - outflow - demand
matrix = (A.T / slope[:, None, :]) @ A
rhs = node_error - (pipe_error / slope) @ A
dh = np.linalg.solve(matrix, rhs[..., None])[..., 0]
dq = -(pipe_error + dh @ A.T) / slope
h = h + dh
q = q + dq
if np.max(np.abs(dq)) < 1e-9:
break
return h
def network_cost(sol):
cost = np.sum(unit_cost[sol.d] * length, axis=1)
pressure = heads(diameters[sol.d]) - elevation
return cost, 30 - pressure # at least 30 m of pressure head at every node
problem = ypea.Problem(
ypea.Integer("d", shape=8, lb=0, ub=13), # an index into the catalog for each pipe
network_cost,
vectorized=True,
name="two-loop",
optimum=419_000,
)
result = ypea.DE(max_nfe=20_000).solve(problem, seed=1)
print(result.summary())
best = result.best.solution.d
print("diameters (inches):", inches[best])
print("pressure heads (m):", np.round(heads(diameters[best][None])[0] - elevation, 2))
DE on two-loop
best objective value: 419000
error: 0
feasible: True (violation 0)
evaluations: 20000, iterations: 199, time: 0.411 s, stopped by: max_nfe
diameters (inches): [18 10 16 4 16 10 10 1]
pressure heads (m): [53.25 30.46 43.45 33.8 30.44 30.55]
Differs from Python: MATLAB draws its own random numbers, so the results differ from Python's. Each pipe takes a catalog position from 1 to 14.
% the two-loop network: node 1 is a reservoir with a fixed head of 210 m
net = struct();
net.demand = [100 100 120 270 330 200] / 3600; % nodes 2 to 7, m3/s
net.elevation = [150 160 155 150 165 160]; % nodes 2 to 7, m
pipes = [ % from node, to node
1 2
2 3
2 4
4 5
4 6
6 7
3 5
5 7
];
net.length = 1000 * ones(1, 8); % m
net.reservoir_head = 210; % m
inches = [1 2 3 4 6 8 10 12 14 16 18 20 22 24];
net.diameters = inches * 0.0254; % m
net.unit_cost = [2 5 8 11 16 23 32 50 60 90 130 170 300 550]; % $ per m
% incidence of the pipes and nodes 2 to 7: -1 where a pipe starts, +1 where it ends
net.A = zeros(8, 6);
net.fixed = zeros(1, 8); % the reservoir head, for pipes that start at node 1
for k = 1:8
start_node = pipes(k, 1);
end_node = pipes(k, 2);
if start_node == 1
net.fixed(k) = -net.reservoir_head;
else
net.A(k, start_node - 1) = -1;
end
net.A(k, end_node - 1) = 1;
end
problem = ypea.Problem( ...
vars=ypea.Integer("d", shape=8, lb=1, ub=14), ... % a position in the catalog for each pipe
objective=@(sol) network_cost(sol, net), ...
outputs=2, ...
vectorized=true, ...
name="two-loop", ...
optimum=419000);
result = ypea.DE(max_nfe=20000).solve(problem, seed=1);
disp(result.summary())
best = result.best.solution.d;
fprintf("diameters (inches): %s\n", mat2str(inches(best)))
fprintf("pressure heads (m): %s\n", mat2str(round(heads(net.diameters(best), net) - net.elevation, 2)))
function [cost, shortfall] = network_cost(sol, net)
choice = sol.d; % (n, 8): the catalog position of each pipe in a batch of designs
n = size(choice, 1);
cost = sum(net.unit_cost(choice) .* net.length, 2);
shortfall = zeros(n, 6);
for i = 1:n
pressure = heads(net.diameters(choice(i, :)), net) - net.elevation;
shortfall(i, :) = 30 - pressure; % at least 30 m of pressure head at every node
end
end
function h = heads(d, net)
% Head at nodes 2 to 7 for one design (a row of diameters in m).
%
% Global gradient algorithm: Newton's method on the Hazen-Williams head loss of every
% pipe and the flow balance of every node.
A = net.A;
r = 10.667 * net.length ./ (130^1.852 * d.^4.871); % pipe resistance
q = pi / 4 * d.^2; % start with a velocity of 1 m/s
h = net.reservoir_head * ones(1, 6);
for iteration = 1:50
abs_q = max(abs(q), 1e-8);
slope = 1.852 * r .* abs_q.^0.852;
pipe_error = r .* abs_q.^0.852 .* q + h * A' + net.fixed; % head loss - head drop
node_error = q * A - net.demand; % inflow - outflow - demand
matrix = A' * diag(1 ./ slope) * A;
rhs = node_error - (pipe_error ./ slope) * A;
dh = (matrix \ rhs')';
dq = -(pipe_error + dh * A') ./ slope;
h = h + dh;
q = q + dq;
if max(abs(dq)) < 1e-9
break
end
end
end
DE on two-loop
best objective value: 419000
error: 0
feasible: true (violation 0)
evaluations: 20000, iterations: 199, time: 6.5 s, stopped by: max_nfe
diameters (inches): [18 10 16 4 16 10 10 1]
pressure heads (m): [53.25 30.46 43.45 33.8 30.44 30.55]
Differential evolution finds the optimal design of 419 000 $ in under a second, with three nodes just above 30 m. The full script, examples/applications/water_network.py, also holds the Hanoi network, checks for a solver that fails to converge, and draws the network.
Results¶
Five algorithms, each with 10 seeded runs: 20 000 evaluations on the two-loop network and 100 000 on Hanoi. DE works on the encoded [0, 1] values, GA and SA use the native integer operators (space="native"), and tabu search and ILS use their own neighborhoods. Every run ended with a feasible design.
| Network | Algorithm | Best | Mean | Worst | Gap of best | Runs within 3% |
|---|---|---|---|---|---|---|
| two-loop | DE | 419,000 | 421,500 | 441,000 | 0.00% | 9 of 10 |
| two-loop | GA | 419,000 | 434,800 | 465,000 | 0.00% | 5 of 10 |
| two-loop | SA | 420,000 | 435,000 | 448,000 | 0.24% | 4 of 10 |
| two-loop | TS | 419,000 | 420,700 | 424,000 | 0.00% | 10 of 10 |
| two-loop | ILS | 419,000 | 423,800 | 448,000 | 0.00% | 9 of 10 |
| hanoi | DE | 6,081,119 | 6,146,872 | 6,300,297 | 0.00% | 7 of 10 |
| hanoi | GA | 6,226,567 | 6,355,913 | 6,454,880 | 2.39% | 2 of 10 |
| hanoi | SA | 6,177,581 | 6,252,867 | 6,336,645 | 1.59% | 7 of 10 |
| hanoi | TS | 6,154,594 | 6,619,443 | 7,202,880 | 1.21% | 1 of 10 |
| hanoi | ILS | 6,191,179 | 6,327,632 | 6,534,625 | 1.81% | 3 of 10 |
The two-loop network is easy: four of the five algorithms find the optimum, and tabu search stays within 3 percent of it in every run. Hanoi separates them. Only DE finds the best known design of 6 081 119 $. DE and SA both end within 3 percent in 7 of 10 runs, but SA never gets closer than 1.6 percent. GA, tabu search and ILS end further away on average, and tabu search varies most from run to run.
Why not a classical method? A common approach treats the diameters as continuous, solves that smooth problem with a gradient method, and then rounds up to the catalog. On Hanoi, 20 starts of SLSQP (about 7 500 network solves each) followed by rounding up leave designs 10 to 11 percent above the best known cost. A greedy descent after the rounding, which tries every change of one diameter and every pair of one smaller and one larger diameter, brings the best of the 20 starts to 6 245 371 $ (2.7 percent above), at about 17 000 network solves per start. DE reaches the best known design with 100 000 evaluations in total, so it wins on quality. The other four algorithms are about level with the rounding method or worse: their mean results are 2.8 to 8.9 percent above the best known cost, against a median of 3.15 percent for the rounding method. With the same small budget of 17 000 evaluations, DE and the rounding method end up close.
Try it on your own network¶
Replace the node and pipe tables with your own: demands and ground elevations of the nodes, the two end nodes and the length of each pipe, and the catalog of diameters and prices. The minimum pressure can differ by node: use an array instead of 30. Check your solver against a hydraulic program such as EPANET on a few designs before you trust the optimum, since a small difference in the head loss constant can make a design that looks feasible fail.
Give DE a budget of a few thousand evaluations per pipe, and run it several times with different seeds: on Hanoi the spread between runs is a few percent. Other rules fit the same model, for example a maximum velocity as another constraint, or existing pipes that can be kept or replaced as an extra catalog entry with zero cost.