Optimization of water distribution network, objective is to get optimal cost

5 views (last 30 days)
I want to ask how I should use any optimization algorithm with EPANET to obtained optimal diameter and optimal cost?
After using the code attached, I am unable to get the optimal cost; it is always coming 'inf' even if I am using a minimum pressure requirement of 30 m for nodes, minimum velocity 0, and maximum velocity 'inf' for links. I am using the Hanoi network, which has 34 links, for hydraulic simulation, I am using EPANET I am also attaching the Matlab code that I am using. It would be very nice of you if anyone could help me with this.
% GENETIC ALGORITHM (GA) SETUP
clear; close('all'); clc;
start_toolkit;
%d = epanet('C:\Users\Admin\Desktop\PipeNetwork\PN_code\Hanoi.inp');
% Define GA options for optimization
options = optimoptions('ga', 'Display', 'iter', 'PopulationSize', 16, ...
'MaxGenerations', 6, 'UseParallel', false);
% Define number of pipes in the network
nvars = 34; % Number of pipes
diameterOptions = [304.8, 406.4, 508, 609.6, 762, 1016]; % Available diameters in mm
costPerDiameter = [45.73, 70.4, 98.38, 129.333, 180.8, 278.3]; % Cost per unit length for each diameter
% Define lower and upper bounds for pipe diameters
lb = repmat(min(diameterOptions), 1, nvars); % Minimum diameter constraint
ub = repmat(max(diameterOptions), 1, nvars); % Maximum diameter constraint
% Run Genetic Algorithm (GA) for optimal pipe diameter selection
[optimalDiameters, optimalCost] = ga(@pipeNetworkObjective, nvars, [], [], [], [], lb, ub, [], options);
% Display optimization results
fprintf('Optimal Pipe Diameters:\n');
disp(optimalDiameters);
fprintf('Optimal Cost:\n');
disp(optimalCost);
function cost = pipeNetworkObjective(diameters)
% Load EPANET-MATLAB Toolkit
start_toolkit;
d = epanet('Optimal_Configuration_eta_2.00.inp');
% Set pipe diameters based on GA input
for i = 1:length(diameters)
d.setLinkDiameter(i, diameters(i));
end
% Run hydraulic simulation
d.solveCompleteHydraulics;
% Check constraints
pressures = d.getNodePressure;
velocities = d.getLinkVelocity;
if any(pressures < 0) || any(velocities < 0) || any(velocities > inf)
cost = inf; % Penalize infeasible solutions
else
% Calculate cost based on diameters
cost = calculateCost(diameters);
end
% Close EPANET
d.unload;
end
function totalCost = calculateCost(diameters)
% Define cost per diameter (example values)
diameterOptions = [304.8, 406.4, 508, 609.6, 762, 1016]; % Available diameters in mm
costPerDiameter = [45.73, 70.4, 98.38, 129.333, 180.8, 278.3]; % Cost per unit length for each diameter
% Calculate total cost
totalCost = 0;
for i = 1:length(diameters)
idx = find(diameterOptions == diameters(i));
totalCost = totalCost + costPerDiameter(idx);
end
end
  13 Comments
Torsten
Torsten on 6 Feb 2025
Edited: Torsten on 6 Feb 2025
From the documentation of "ga":
x = ga(fun,nvars,A,b,Aeq,beq,lb,ub,nonlcon,intcon,options)
requires that the variables listed in intcon take integer values.
Note
When there are integer constraints, ga does not accept nonlinear equality constraints, only nonlinear inequality constraints.
In your special case:
lb = ones(34,1);
ub = 6*ones(34,1);
intcon = 1:34;

Sign in to comment.

Answers (0)

Categories

Find more on Get Started with MATLAB in Help Center and File Exchange

Products


Release

R2024b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!