After using this code, 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, minimum velocity 0, and maximum velocity inf. Here is the code I am using. It would be very nice of you if anyone could help me with this.
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