THE TRUE PARETO LOOK WRONG, CAN YOU FIX IT?
% MOEMA_ZDT3_PerformanceMetrics.m
% Main script to run MOEMA optimization and evaluate performance metrics for ZDT3
clc;
clear;
close all;
%% 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
num_runs =2; % Number of runs for statistical evaluation
%% Plot 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(sprintf('Pareto Front for ZDT3 using MOEMA (Run #%d)', num_runs));
legend('Obtained Pareto Front', 'True Pareto Front');
grid on;
hold off;
%% FUNCTIONS
% ------------------- Main MOEMA Function -------------------
function [paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, FITNESSFCN)
% Multi-Objective Evolutionary Mating Algorithm (MOEMA)
% Optimize the multi-objective problem defined by FITNESSFCN
%
% Inputs:
% NPop - Population size
% T - Number of iterations
% lb - Lower bounds (1 x dim)
% ub - Upper bounds (1 x dim)
% dim - Number of decision variables
% FITNESSFCN - Handle to the objective function
%
% Outputs:
% paretoFront - Set of non-dominated solutions in objective space
% paretoSet - Set of non-dominated solutions in decision space
% Trajectories - Best solution trajectory over iterations
% fitness_history - History of fitness values
% position_history - History of population positions
% Preallocation
fitness_history = zeros(NPop, T, 2); % Assuming 2 objectives
position_history = zeros(NPop, T, dim);
Trajectories = zeros(dim, T);
% Generation of initial population
pop = initialization(NPop, dim, ub, lb);
pop_dad = pop(1:NPop/2, :); % Parent group 1
pop_mum = pop(NPop/2+1:end, :); % Parent group 2
% Evaluation of objective function for initialization
obj_dad = evaluate_population(pop_dad, FITNESSFCN);
obj_mum = evaluate_population(pop_mum, FITNESSFCN);
obj = [obj_dad; obj_mum];
% Initialize archive with non-dominated solutions
archive = update_archive([], [], pop, obj);
for gen = 1:T % Start iterations
k1 = randperm(NPop/2);
k2 = randperm(NPop/2);
k1x = pop_dad(k1, :);
k2x = pop_mum(k2, :);
for k = 1:NPop/2
% Mating process
I_mates = 1 + (var(k1x(k, :)) - var(k2x(k, :)));
p = randn(1, dim);
r = rand();
if I_mates >= 0 % Upper mating strategy
temp1 = p .* k1x(k, :);
temp2 = (1-p) .* k2x(k, :);
pop_child = temp1 + temp2;
else % Lower mating strategy
temp1 = p .* k2x(k, :);
temp2 = (1-p) .* k1x(k, :);
pop_child = temp1 + temp2;
end
% Impact of best environment
if ~isempty(archive.pop)
best_pop = archive.pop(randi(size(archive.pop, 1)), :);
K = rand(1, dim) < 0.8;
pop_child = best_pop .* (1-K) + pop_child .* K;
end
% Bounding of the solution
pop_child = max(min(ub, pop_child), lb);
% Evaluate child
obj_child = FITNESSFCN(pop_child);
% Update population using non-dominated sorting
[pop_dad, obj_dad] = update_population(pop_dad, obj_dad, pop_child, obj_child, k1(k));
[pop_mum, obj_mum] = update_population(pop_mum, obj_mum, pop_child, obj_child, k2(k));
% Exploration process
if r < 0.2
if ~isempty(archive.pop)
temp1 = archive.pop(randi(size(archive.pop, 1)), :);
pop_child = rand(1, dim) .* temp1;
pop_child = max(min(ub, pop_child), lb);
obj_child = FITNESSFCN(pop_child);
% Update population using non-dominated sorting
[pop_dad, obj_dad] = update_population(pop_dad, obj_dad, pop_child, obj_child, randi(NPop/2));
[pop_mum, obj_mum] = update_population(pop_mum, obj_mum, pop_child, obj_child, randi(NPop/2));
end
end
end
% Combine updated dad and mum pools
pop = [pop_dad; pop_mum];
obj = [obj_dad; obj_mum];
% Update archive
archive = update_archive(archive.pop, archive.obj, pop, obj);
% Record the history at each iteration
fitness_history(:, gen, :) = obj;
position_history(:, gen, :) = pop;
% Update Trajectories (using a random non-dominated solution)
if ~isempty(archive.pop)
Trajectories(:, gen) = archive.pop(randi(size(archive.pop, 1)), :)';
end
fprintf(' Iteration #%d completed. Archive size: %d\n', gen, size(archive.pop, 1));
end
% Return Pareto front and Pareto set
paretoFront = archive.obj;
paretoSet = archive.pop;
end
% ------------------- Initialization Function -------------------
function pop = initialization(NPop, dim, ub, lb)
% Initialize population within the given bounds
pop = rand(NPop, dim) .* (ub - lb) + lb;
end
% ------------------- Evaluate Population Function -------------------
function obj = evaluate_population(pop, FITNESSFCN)
% Evaluate objective functions for a population
num_sol = size(pop, 1);
obj = zeros(num_sol, 2); % Assuming 2 objectives
parfor i = 1:num_sol % Parallel loop for efficiency
obj(i, :) = FITNESSFCN(pop(i, :));
end
end
% ------------------- Update Population Function -------------------
function [pop, obj] = update_population(pop, obj, child, child_obj, index)
% Update the population with a child if it dominates or is non-dominated
if dominates(child_obj, obj(index, :))
pop(index, :) = child;
obj(index, :) = child_obj;
elseif ~dominates(obj(index, :), child_obj)
if rand() < 0.5
pop(index, :) = child;
obj(index, :) = child_obj;
end
end
end
% ------------------- Update Archive Function -------------------
function archive = update_archive(archive_pop, archive_obj, pop, obj)
% Update the archive with non-dominated solutions from the combined population
combined_pop = [archive_pop; pop];
combined_obj = [archive_obj; obj];
for i = 1:n_solutions
for j = i+1:n_solutions
if dominates(objectives(i, :), objectives(j, :))
is_dominated(j) = true;
domination_count(i) = domination_count(i) + 1;
elseif dominates(objectives(j, :), objectives(i, :))
is_dominated(i) = true;
domination_count(j) = domination_count(j) + 1;
end
end
end
end
% ------------------- Dominance Check Function -------------------
function result = dominates(obj1, obj2)
% Check if obj1 dominates obj2
result = all(obj1 <= obj2) && any(obj1 < obj2);
end
% ------------------- True Pareto Front Function for ZDT3 -------------------
function true_pareto = get_true_pareto_zdt3()
% Generate the true Pareto front for ZDT3
% ZDT3 has a discontinuous Pareto front with multiple segments.
% For simplicity, we'll approximate it by excluding regions where the Pareto front is not defined.
% ------------------- Generational Distance (GD) Function -------------------
function gd = generational_distance(obtained_pf, true_pf)
% Calculate Generational Distance (GD)
distances = sqrt(sum((obtained_pf - closest_point(obtained_pf, true_pf)).^2, 2));
gd = mean(distances);
end
% ------------------- Inverted Generational Distance (IGD) Function -------------------
function igd = inverted_generational_distance(obtained_pf, true_pf)
% Calculate Inverted Generational Distance (IGD)
distances = sqrt(sum((true_pf - closest_point(true_pf, obtained_pf)).^2, 2));
igd = mean(distances);
end
% ------------------- Diversity Metric Function -------------------
function spread = diversity_metric(obtained_pf, true_pf)
% Calculate Diversity Metric (Spread)
% Normalize the obtained Pareto front based on the true Pareto front
sorted_pf = sortrows(obtained_pf, 1); % Sort based on f1
num_obtained = size(sorted_pf, 1);
% Handle edge case with insufficient points
if num_obtained < 2
spread = Inf;
return;
end
% Calculate gaps between consecutive points
gaps = sqrt(sum(diff(sorted_pf).^2, 2));
spread = sum(gaps) / (sqrt(2) * (num_obtained - 1));
end
% ------------------- Closest Point Helper Function -------------------
function closest = closest_point(src, target)
% Find the closest point in target for each point in src
num_src = size(src, 1);
num_target = size(target, 1);
closest = zeros(size(src));
for i = 1:num_src
distances = sqrt(sum((target - src(i,:)).^2, 2));
[~, idx] = min(distances);
closest(i,:) = target(idx,:);
end
end
% ------------------- ZDT3 Objective Function -------------------
function f = zdt3(x)
f1 = x(1);
g = 1 + 9sum(x(2:end))/(length(x)-1);
h = 1 - sqrt(f1/g) - (f1/g)sin(10pif1);
f2 = g * h;
f = [f1, f2];
end
It appears that the issue with the true Pareto front in your MATLAB script stems from the way the get_true_pareto_zdt3 function is generating the true Pareto front for the ZDT3 test function. The current implementation approximates the Pareto front by defining multiple linear segments and using a simplistic calculation for ( f_2 ), which does not accurately reflect the true Pareto front of ZDT3.
Understanding the ZDT3 True Pareto Front
The ZDT3 function is designed with a discontinuous Pareto front, meaning that the Pareto-optimal solutions are split into multiple non-continuous segments. This characteristic tests the ability of optimization algorithms to maintain diversity and locate multiple Pareto-optimal regions.
However, not all combinations of ( f_1 ) and ( f_2 ) are valid. The function ( h(f_1) = 1 - \sqrt{f_1} - f_1 \cdot \sin(10\pi f_1) ) must be non-negative to constitute a valid Pareto-optimal solution.
Issues in the Current Implementation
Incorrect Calculation of ( f_2 ):
The current function calculates ( f_2 = 1 - f_1^2 ), which does not align with the actual ZDT3 definition.
Simplistic Segmentation:
The segmentation does not account for the discontinuities in the true Pareto front of ZDT3. The segments should be based on the regions where ( h(f_1) ) is positive.
Incompleteness:
The function does not capture the multiple disconnected segments accurately, leading to an incorrect representation of the true Pareto front.
Proposed Fix
To accurately represent the true Pareto front of ZDT3, we need to:
Accurately Compute ( f_2 ) Based on ( f_1 ):
Use the correct formula ( f_2 = 1 - \sqrt{f_1} - f_1 \cdot \sin(10\pi f_1) ).
Define Valid Regions for ( f_1 ):
Identify intervals where ( h(f_1) ) is non-negative, ensuring that only valid Pareto-optimal points are included.
Sample ( f_1 ) Within These Regions:
Generate ( f_1 ) values within these valid intervals to construct the true Pareto front accurately.
Updated get_true_pareto_zdt3 Function
Below is the corrected version of the get_true_pareto_zdt3 function. It accurately computes the true Pareto front by evaluating ( h(f_1) ) and only including points where ( h(f_1) \geq 0 ):
% ------------------- True Pareto Front Function for ZDT3 -------------------
function true_pareto = get_true_pareto_zdt3()
% Generate the true Pareto front for ZDT3
% ZDT3 has a discontinuous Pareto front with multiple segments.
% Define f1 values with fine granularity
f1 = linspace(0, 1, 1000)';
% Calculate h(f1) based on ZDT3's definition
h = 1 - sqrt(f1) - f1 .* sin(10 * pi * f1);
% Only include points where h(f1) >= 0
valid_indices = h >= 0;
f1 = f1(valid_indices);
f2 = h(valid_indices);
% Combine f1 and f2 to form the true Pareto front
true_pareto = [f1, f2];
end
Explanation of the Fix
Fine-Grained Sampling:
f1 is sampled with 1000 points between 0 and 1 to capture the nuances of the Pareto front.
Accurate ( h(f_1) ) Calculation:
The function computes ( h(f_1) = 1 - \sqrt{f_1} - f_1 \cdot \sin(10\pi f_1) ), which aligns with the ZDT3 definition.
Filtering Valid Points:
Only points where ( h(f_1) \geq 0 ) are retained, ensuring that the Pareto front is accurately represented without invalid regions.
Constructing the Pareto Front:
The valid ( f_1 ) and corresponding ( f_2 ) values are combined to form the true Pareto front.
Integrating the Fix into Your Script
Replace your existing get_true_pareto_zdt3 function with the corrected version provided above. Here's how your updated script should look:
% ------------------- True Pareto Front Function for ZDT3 -------------------
function true_pareto = get_true_pareto_zdt3()
% Generate the true Pareto front for ZDT3
% ZDT3 has a discontinuous Pareto front with multiple segments.
% Define f1 values with fine granularity
f1 = linspace(0, 1, 1000)';
% Calculate h(f1) based on ZDT3's definition
h = 1 - sqrt(f1) - f1 .* sin(10 * pi * f1);
% Only include points where h(f1) >= 0
valid_indices = h >= 0;
f1 = f1(valid_indices);
f2 = h(valid_indices);
% Combine f1 and f2 to form the true Pareto front
true_pareto = [f1, f2];
end
Additional Recommendations
Plotting Improvements:
To better visualize the multiple segments of the Pareto front, consider plotting markers with transparency or using different colors for different segments.
Validation:
After implementing the fix, visualize the true Pareto front alongside the obtained Pareto front from MOEMA to ensure accuracy.
Performance Optimization:
If computational resources permit, increase the number of points in f1 for an even more precise Pareto front.
Final Thoughts
By accurately generating the true Pareto front, you can better evaluate the performance metrics of your MOEMA algorithm. This fix ensures that the generational distance (GD), inverted generational distance (IGD), convergence, and diversity metrics are computed against a valid and precise representation of the ZDT3 Pareto-optimal solutions.
If you encounter any further issues or have additional questions, feel free to ask!