Certainly! To enhance your MOEMA (Multi-Objective Evolutionary Mating Algorithm) MATLAB code with comprehensive performance metrics—Generational Distance (GD), Convergence Metric, Diversity Metric, and statistics for Inverted Generational Distance (IGD)—you can follow the steps below. This will allow you to evaluate the quality of the obtained Pareto front more thoroughly.
Overview of Metrics
-
Generational Distance (GD): Measures the average distance from the obtained Pareto front to the true Pareto front. A lower GD indicates better convergence.
-
Inverted Generational Distance (IGD): Measures the average distance from the true Pareto front to the obtained Pareto front. It evaluates both convergence and diversity. Statistics such as best, average, median, and worst IGD across multiple runs provide a robust evaluation.
-
Convergence Metric: Assesses how close the obtained Pareto front is to the true Pareto front. It often overlaps conceptually with GD but can be defined differently based on specific formulations.
-
Diversity Metric: Evaluates the spread of solutions along the Pareto front, ensuring a well-distributed set of solutions.
Step-by-Step Implementation
1. Define the True Pareto Front
For the ZDT1 problem, the true Pareto front is defined by:
[ f_2 = 1 - \sqrt{f_1} \quad \text{for} \quad f_1 \in [0, 1] ]
We'll generate a dense set of points to represent the true Pareto front.
function true_pareto = get_true_pareto()
num_points = 100; % Number of points in the true Pareto front
f1 = linspace(0, 1, num_points)';
f2 = 1 - sqrt(f1);
true_pareto = [f1, f2];
end
2. Implement Metric Calculation Functions
a. Generational Distance (GD)
function gd = generational_distance(obtained_pf, true_pf)
distances = sqrt(sum((obtained_pf - closest_point(obtained_pf, true_pf)).^2, 2));
gd = mean(distances);
end
b. Inverted Generational Distance (IGD)
function igd = inverted_generational_distance(obtained_pf, true_pf)
distances = sqrt(sum((true_pf - closest_point(true_pf, obtained_pf)).^2, 2));
igd = mean(distances);
end
c. Diversity Metric
We can use the Spread metric, which measures the extent of spread in the obtained Pareto front.
function spread = diversity_metric(obtained_pf, true_pf)
% Normalize the true Pareto front
f1 = obtained_pf(:,1);
f2 = obtained_pf(:,2);
num_points = size(true_pf, 1);
% Calculate gaps between consecutive points
sorted_pf = sortrows(obtained_pf, 1);
gaps = sqrt(sum(diff(sorted_pf).^2, 2));
spread = sum(gaps) / (sqrt(2) * (num_points - 1));
end
d. Helper Function to Find Closest Points
function closest = closest_point(src, target)
closest = zeros(size(src,1), size(target,2));
for i = 1:size(src,1)
distances = sqrt(sum((target - src(i,:)).^2, 2));
[~, idx] = min(distances);
closest(i,:) = target(idx,:);
end
end
3. Modify the Main Script to Calculate and Display Metrics
Below is the updated main script incorporating the calculation and display of the specified metrics. Additionally, to compute statistics for IGD (best, average, median, worst), we’ll perform multiple runs of MOEMA.
% Main script to run MOEMA optimization and evaluate performance metrics
clc;
clear;
close all;
% Set problem parameters
NPop = 50; % Population size
T = 200; % Number of iterations
dim = 30; % Number of decision variables
lb = zeros(1, dim); % Lower bound
ub = ones(1, dim); % Upper bound
% Number of runs for statistical evaluation
num_runs = 30;
% Initialize metrics storage
GD_values = zeros(num_runs,1);
IGD_values = zeros(num_runs,1);
Convergence_values = zeros(num_runs,1);
Diversity_values = zeros(num_runs,1);
for run = 1:num_runs
fprintf('Run #%d/%d\n', run, num_runs);
% Run MOEMA
[paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, @zdt1);
% Get True Pareto Front
true_pareto = get_true_pareto();
% Calculate Generational Distance (GD)
GD = generational_distance(paretoFront, true_pareto);
% Calculate Inverted Generational Distance (IGD)
IGD = inverted_generational_distance(paretoFront, true_pareto);
% Calculate Convergence Metric (using GD as an example)
Convergence = GD; % You can define a different convergence metric if needed
% Calculate Diversity Metric
Diversity = diversity_metric(paretoFront, true_pareto);
% Store metrics
GD_values(run) = GD;
IGD_values(run) = IGD;
Convergence_values(run) = Convergence;
Diversity_values(run) = Diversity;
end
% Compute IGD statistics
best_IGD = min(IGD_values);
average_IGD = mean(IGD_values);
median_IGD = median(IGD_values);
worst_IGD = max(IGD_values);
% Display Metrics
fprintf('\nPerformance Metrics over %d runs:\n', num_runs);
fprintf('Generational Distance (GD): Mean = %.4f, Std = %.4f\n', mean(GD_values), std(GD_values));
fprintf('Inverted Generational Distance (IGD):\n');
fprintf(' Best = %.4f\n', best_IGD);
fprintf(' Average = %.4f\n', average_IGD);
fprintf(' Median = %.4f\n', median_IGD);
fprintf(' Worst = %.4f\n', worst_IGD);
fprintf('Convergence Metric: Mean = %.4f, Std = %.4f\n', mean(Convergence_values), std(Convergence_values));
fprintf('Diversity Metric: Mean = %.4f, Std = %.4f\n', mean(Diversity_values), std(Diversity_values));
% Plot the Pareto front from the last run
figure;
plot(paretoFront(:,1), paretoFront(:,2), 'ro');
hold on;
plot(true_pareto(:,1), true_pareto(:,2), 'b--');
xlabel('f1');
ylabel('f2');
title('Pareto Front for ZDT1 using MOEMA');
legend('Obtained Pareto Front', 'True Pareto Front');
hold off;
4. Integrate Helper Functions
Ensure all helper functions are included in your script or as separate function files. Here's the complete set of functions you need:
% Function to get the true Pareto front for ZDT1
function true_pareto = get_true_pareto()
num_points = 100; % Number of points in the true Pareto front
f1 = linspace(0, 1, num_points)';
f2 = 1 - sqrt(f1);
true_pareto = [f1, f2];
end
% Generational Distance (GD) calculation
function gd = generational_distance(obtained_pf, true_pf)
distances = sqrt(sum((obtained_pf - closest_point(obtained_pf, true_pf)).^2, 2));
gd = mean(distances);
end
% Inverted Generational Distance (IGD) calculation
function igd = inverted_generational_distance(obtained_pf, true_pf)
distances = sqrt(sum((true_pf - closest_point(true_pf, obtained_pf)).^2, 2));
igd = mean(distances);
end
% Diversity Metric (Spread) calculation
function spread = diversity_metric(obtained_pf, true_pf)
% Normalize the true Pareto front
sorted_pf = sortrows(obtained_pf, 1);
num_points = size(sorted_pf, 1);
% Calculate gaps between consecutive points
gaps = sqrt(sum(diff(sorted_pf).^2, 2));
spread = sum(gaps) / (sqrt(2) * (num_points - 1));
end
% Helper function to find the closest point in target to each point in src
function closest = closest_point(src, target)
closest = zeros(size(src,1), size(target,2));
for i = 1:size(src,1)
distances = sqrt(sum((target - src(i,:)).^2, 2));
[~, idx] = min(distances);
closest(i,:) = target(idx,:);
end
end
% MOEMA function as provided (ensure it's included in the same file or as a separate file)
% ... [Include your MOEMA function here] ...
% ZDT1 objective function
function f = zdt1(x)
f1 = x(1);
g = 1 + 9 * sum(x(2:end)) / (length(x) - 1);
h = 1 - sqrt(f1 / g);
f2 = g * h;
f = [f1, f2];
end
5. Important Notes
-
Multiple Runs for Statistics: To obtain meaningful statistics (best, average, median, worst), the code performs multiple runs (num_runs = 30). Adjust this number based on your computational resources.
-
Convergence Metric Definition: In this example, the convergence metric is set equal to GD. Depending on your specific requirements or definitions, you might need to implement a different convergence measure.
-
Diversity Metric Normalization: The diversity metric calculation assumes normalization based on the true Pareto front's spread. Ensure this aligns with your specific diversity assessment needs.
-
Visualization: The Pareto front from the last run is plotted alongside the true Pareto front for visual assessment. You can modify this to visualize other runs or aggregate results.
-
Performance: Running multiple evolutionary algorithms can be computationally intensive. Consider implementing parallel processing using MATLAB’s Parallel Computing Toolbox if necessary.
Complete Updated Code
For convenience, here's the complete updated main script integrating all the components discussed above:
% Main script to run MOEMA optimization and evaluate performance metrics
clc;
clear;
close all;
% Set problem parameters
NPop = 50; % Population size
T = 200; % Number of iterations
dim = 30; % Number of decision variables
lb = zeros(1, dim); % Lower bound
ub = ones(1, dim); % Upper bound
% Number of runs for statistical evaluation
num_runs = 30;
% Initialize metrics storage
GD_values = zeros(num_runs,1);
IGD_values = zeros(num_runs,1);
Convergence_values = zeros(num_runs,1);
Diversity_values = zeros(num_runs,1);
for run = 1:num_runs
fprintf('Run #%d/%d\n', run, num_runs);
% Run MOEMA
[paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, @zdt1);
% Get True Pareto Front
true_pareto = get_true_pareto();
% Calculate Generational Distance (GD)
GD = generational_distance(paretoFront, true_pareto);
% Calculate Inverted Generational Distance (IGD)
IGD = inverted_generational_distance(paretoFront, true_pareto);
% Calculate Convergence Metric (using GD as an example)
Convergence = GD; % You can define a different convergence metric if needed
% Calculate Diversity Metric
Diversity = diversity_metric(paretoFront, true_pareto);
% Store metrics
GD_values(run) = GD;
IGD_values(run) = IGD;
Convergence_values(run) = Convergence;
Diversity_values(run) = Diversity;
end
% Compute IGD statistics
best_IGD = min(IGD_values);
average_IGD = mean(IGD_values);
median_IGD = median(IGD_values);
worst_IGD = max(IGD_values);
% Display Metrics
fprintf('\nPerformance Metrics over %d runs:\n', num_runs);
fprintf('Generational Distance (GD): Mean = %.4f, Std = %.4f\n', mean(GD_values), std(GD_values));
fprintf('Inverted Generational Distance (IGD):\n');
fprintf(' Best = %.4f\n', best_IGD);
fprintf(' Average = %.4f\n', average_IGD);
fprintf(' Median = %.4f\n', median_IGD);
fprintf(' Worst = %.4f\n', worst_IGD);
fprintf('Convergence Metric: Mean = %.4f, Std = %.4f\n', mean(Convergence_values), std(Convergence_values));
fprintf('Diversity Metric: Mean = %.4f, Std = %.4f\n', mean(Diversity_values), std(Diversity_values));
% Plot the Pareto front from the last run
figure;
plot(paretoFront(:,1), paretoFront(:,2), 'ro', 'MarkerSize',6, 'MarkerFaceColor','r');
hold on;
plot(true_pareto(:,1), true_pareto(:,2), 'b--', 'LineWidth',1.5);
xlabel('f1');
ylabel('f2');
title('Pareto Front for ZDT1 using MOEMA');
legend('Obtained Pareto Front', 'True Pareto Front');
grid on;
hold off;
Ensure that all helper functions (get_true_pareto, generational_distance, inverted_generational_distance, diversity_metric, closest_point, zdt1, and MOEMA) are included either within the same script file or as separate .m files accessible in MATLAB's path.
Final Remarks
By integrating these metrics into your MOEMA implementation, you can quantitatively assess the performance of your optimization algorithm in terms of convergence to the true Pareto front and the diversity of the obtained solutions. Adjust the metrics and their calculations as necessary to align with your specific research or application requirements.
If you encounter any issues or need further customization, feel free to ask!