Optimization of water distribution network, objective is to get optimal cost
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:UsersAdminDesktopPipeNetworkPN_codeHanoi.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
endI 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:UsersAdminDesktopPipeNetworkPN_codeHanoi.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 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:UsersAdminDesktopPipeNetworkPN_codeHanoi.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 optimization of epanet network using ga toolkit MATLAB Answers — New Questions