can you display the IGD, SP values, and MS values for this code? i want the Best, Avg , Worst, Stddev and Median for each of IGD, SP values, and MS values for this moema code.
% Main script to run MOEMA optimization
clc;
clear;
close all;
% Set problem parameters
NPop = 50; % Population size
T = 50; % Number of iterations
dim = 30; % Number of decision variables
lb = zeros(1, dim); % Lower bound
ub = ones(1, dim); % Upper bound
% Plot the Pareto front
figure;
plot(paretoFront(:,1), paretoFront(:,2), 'ro');
xlabel('f1');
ylabel('f2');
title('Pareto Front for ZDT1 using MOEMA');
% Calculate and display metrics
disp(['Number of solutions in Pareto Front: ', num2str(size(paretoFront, 1))]);
disp(['Minimum f1: ', num2str(min(paretoFront(:,1)))]);
disp(['Minimum f2: ', num2str(min(paretoFront(:,2)))]);
function [paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, FITNESSFCN)
% Multi-Objective Evolutionary Mating Algorithm (MOEMA)
% MOEMA attempts to solve multi-objective problems of the following form:
% min F(X) = [f1(X), f2(X), ..., fm(X)] subject to: lb <= X <= ub
%
% FITNESSFCN - function handle of the multi-objective fitness function
% lb - lower bounds on X
% ub - upper bounds on X
% T - number of iterations
% NPop - size of the population (class size)
% 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
% Generation of initial population
pop = initialization(NPop, dim, ub, lb);
pop_dad = pop(1:NPop/2, :); % Eqn (1) in the paper
pop_mum = pop(NPop/2+1:end, :); % Eqn (2) in the paper
% 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];
for k = 1:NPop/2
% Mating process
Imates = 1 + (var(k1x(k, :)) - var(k2x(k, :))); % Eqn (4) in the paper
p = randn(1, dim); % Eqn (6) in the paper
r = rand();
code
Assistant
User
give me full code
Assistant
User
give me full code
Assistant
User
i want the Best, Avg , Worst, Stddev and Median for each of IGD, SP values, and MS values for this moema code.
Assistant
User
give me full code
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
if Imates >= 0 % Upper part of Eqn (5) in the paper
temp1 = p .* k1x(k, :);
temp2 = (1-p) .* k2x(k, :);
pop_child = temp1 + temp2;
else % Lower part of Eqn (5) in the paper
temp1 = p .* k2x(k, :);
temp2 = (1-p) .* k1x(k, :);
pop_child = temp1 + temp2;
end
% Impact of best environment (using random non-dominated solution)
if ~isempty(archive.pop)
best_pop = archive.pop(randi(size(archive.pop, 1)), :);
K = rand(1, dim) < 0.8; % Eqn (8) in the paper
pop_child = best_pop .* (1-K) + pop_child .* K; % Eqn (7) in the paper
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; % Eqn (9) in the paper
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 pool dad and mum
pop = [pop_dad; pop_mum];
obj = [obj_dad; obj_mum];
% 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
archive.pop = combined_pop(non_dominated, :);
archive.obj = combined_obj(non_dominated, :);
end
function [is_dominated, domination_count] = pareto_dominance(objectives)
[n_solutions, ~] = size(objectives);
is_dominated = false(n_solutions, 1);
domination_count = zeros(n_solutions, 1); % Ensure this variable is initialized
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; % Track how many solutions 'i' dominates
elseif dominates(objectives(j, :), objectives(i, :))
is_dominated(i) = true;
domination_count(j) = domination_count(j) + 1; % Track how many solutions 'j' dominates
end
end
end
end
function result = dominates(obj1, obj2)
result = all(obj1 <= obj2) && any(obj1 < obj2);
end
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
To calculate and display the Best, Average, Worst, Standard Deviation, and Median for metrics like IGD (Inverted Generational Distance), SP (Spread), and MS (Maximum Spread), you would first need to implement these metric calculations within the context of your optimization code. I'll explain briefly what each metric means and then suggest how you might implement the calculations and display them:
Metrics
IGD (Inverted Generational Distance):
Measures how far the obtained Pareto front is from the true Pareto front.
To calculate IGD, you need a reference set of points that represent the true Pareto front. You then calculate the average distance from every point in this reference to the nearest point in the obtained Pareto front.
SP (Spread):
Measures the distribution of solutions along the obtained Pareto front. Ideally, solutions should be evenly distributed.
This could involve calculating the diversity across the Pareto front.
MS (Maximum Spread):
Measures the extent of the obtained Pareto front in the objective space.
Typically calculated as the perimeter of the bounding hyperrectangle encapsulating the Pareto front.
Implementation Approach
Here's a general approach for how you can implement these calculations in MATLAB, given your current code structure. I'm assuming you store the obtained Pareto front in paretoFront.
Additional Functions to Implement:
Below are simple forms of how these metrics might be computed. They will need to be integrated or modified to fit your specific implementation context:
matlab
function igd_value = calculate_igd(paretoFront, referenceFront)
d_sum = 0;
for i = 1:size(referenceFront, 1)
d_sum = d_sum + min(sqrt(sum((paretoFront - referenceFront(i, :)).^2, 2)));
end
igd_value = d_sum / size(referenceFront, 1);
end
function sp_value = calculate_sp(paretoFront)
% Assume paretoFront is sorted according to a particular order
n = size(paretoFront, 1);
d_f = sqrt(sum(diff(paretoFront).^2, 2)); % Distance between consecutive solutions
d_mean = mean(d_f);
sp_value = sum(abs(d_f - d_mean)) / ((n - 1) * d_mean);
end
function ms_value = calculate_ms(paretoFront)
maxObj1 = max(paretoFront(:, 1));
minObj1 = min(paretoFront(:, 1));
maxObj2 = max(paretoFront(:, 2));
minObj2 = min(paretoFront(:, 2));
ms_value = sqrt((maxObj1 - minObj1)^2 + (maxObj2 - minObj2)^2);
end
Calculate Statistics:
After computing these metrics, you would calculate statistics as follows:
These functions and calculations should be conditioned based on your setup (e.g., having a reference front) and adjusted if additional objectives are involved or if there are certain shapes to your Pareto fronts.
Sure, below is an extended version of your existing code that includes the calculation for IGD, SP, and MS metrics, along with statistical summaries for each. Please note that you will need a reference Pareto front (referenceFront) for the IGD calculation. Here I will illustrate how you could structure it with some example data.
Assuming you will provide or have a true Pareto front for zdt1 as a reference, here’s how you could extend your code:
matlab
% Main script to run MOEMA optimization
clc;
clear;
close all;
% Set problem parameters
NPop = 50; % Population size
T = 50; % Number of iterations
dim = 30; % Number of decision variables
lb = zeros(1, dim); % Lower bound
ub = ones(1, dim); % Upper bound
% Run MOEMA
[paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, @zdt1);
% Plot the Pareto front
figure;
plot(paretoFront(:,1), paretoFront(:,2), 'ro');
xlabel('f1');
ylabel('f2');
title('Pareto Front for ZDT1 using MOEMA');
% Calculate and display metrics
disp(['Number of solutions in Pareto Front: ', num2str(size(paretoFront, 1))]);
disp(['Minimum f1: ', num2str(min(paretoFront(:,1)))]);
disp(['Minimum f2: ', num2str(min(paretoFront(:,2)))]);
% Example reference front (in a real scenario, replace this with the actual reference front)
% Create a sample reference front for demonstration purposes:
referenceFront = generate_reference_front();
% Calculate metrics
igd_value = calculate_igd(paretoFront, referenceFront);
sp_value = calculate_sp(paretoFront);
ms_value = calculate_ms(paretoFront);
% Display metrics
disp('IGD Value: ');
disp(igd_value);
disp('SP Value: ');
disp(sp_value);
disp('MS Value: ');
disp(ms_value);
function referenceFront = generate_reference_front()
x = linspace(0, 1, 100);
referenceFront = [x; 1 - sqrt(x)]';
end
function igd_value = calculate_igd(paretoFront, referenceFront)
d_sum = 0;
for i = 1:size(referenceFront, 1)
d_sum = d_sum + min(sqrt(sum((paretoFront - referenceFront(i, :)).^2, 2)));
end
igd_value = d_sum / size(referenceFront, 1);
end
function sp_value = calculate_sp(paretoFront)
[n, ~] = size(paretoFront);
if n > 1
d_f = sqrt(sum(diff(paretoFront).^2, 2)); % Distance between consecutive solutions
d_mean = mean(d_f);
sp_value = sum(abs(d_f - d_mean)) / ((n - 1) * d_mean);
else
sp_value = NaN;
end
end
function ms_value = calculate_ms(paretoFront)
maxObj1 = max(paretoFront(:, 1));
minObj1 = min(paretoFront(:, 1));
maxObj2 = max(paretoFront(:, 2));
minObj2 = min(paretoFront(:, 2));
ms_value = sqrt((maxObj1 - minObj1)^2 + (maxObj2 - minObj2)^2);
end
% Rest of the MOEMA and helper function implementations go here as shown in your original code..
Key Points:
Reference Pareto Front: You must provide a meaningful referenceFront to calculate IGD accurately.
Handling Edge Cases: Make sure your paretoFront contains more than one point for SP calculation to make sense.
Display Values: The calculated metric values (IGD, SP, MS) are displayed at the end of the script.
Additional Considerations:
This code assumes your Pareto front is well captured and represents your problem’s solution space.
You might need to refine generate_reference_front() depending on your needs or obtain the true Pareto front through other means.
Certainly! Below is the complete code for the MOEMA optimization including the computation of IGD, SP, and MS metrics. This example includes a generated reference front for the purpose of calculating IGD. You might need to replace generate_reference_front with an actual reference specific to your application for better accuracy.
matlab
% Main script to run MOEMA optimization
clc;
clear;
close all;
% Set problem parameters
NPop = 50; % Population size
T = 50; % Number of iterations
dim = 30; % Number of decision variables
lb = zeros(1, dim); % Lower bound
ub = ones(1, dim); % Upper bound
% Run MOEMA
[paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, @zdt1);
% Plot the Pareto front
figure;
plot(paretoFront(:,1), paretoFront(:,2), 'ro');
xlabel('f1');
ylabel('f2');
title('Pareto Front for ZDT1 using MOEMA');
% Calculate and display metrics
disp(['Number of solutions in Pareto Front: ', num2str(size(paretoFront, 1))]);
disp(['Minimum f1: ', num2str(min(paretoFront(:,1)))]);
disp(['Minimum f2: ', num2str(min(paretoFront(:,2)))]);
% Example reference front (in a real scenario, replace this with the actual reference front)
referenceFront = generate_reference_front();
% Calculate metrics
igd_value = calculate_igd(paretoFront, referenceFront);
sp_value = calculate_sp(paretoFront);
ms_value = calculate_ms(paretoFront);
% Display metrics
disp(['IGD Value: ', num2str(igd_value)]);
disp(['SP Value: ', num2str(sp_value)]);
disp(['MS Value: ', num2str(ms_value)]);
function [paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, FITNESSFCN)
% Multi-Objective Evolutionary Mating Algorithm (MOEMA)
% 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, :);
pop_mum = pop(NPop/2+1:end, :);
% 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
k1 = randperm(NPop/2);
k2 = randperm(NPop/2);
k1x = pop_dad(k1, :);
k2x = pop_mum(k2, :);
for k = 1:NPop/2
% Mating process
Imates = 1 + (var(k1x(k, :)) - var(k2x(k, :)));
p = randn(1, dim);
r = rand();
if Imates >= 0
temp1 = p .* k1x(k, :);
temp2 = (1-p) .* k2x(k, :);
pop_child = temp1 + temp2;
else
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 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 pool dad and mum
pop = [pop_dad; pop_mum];
obj = [obj_dad; obj_mum];
% Update archive
archive = update_archive(archive.pop, archive.obj, pop, obj);
% Record history at each iteration
fitness_history(:, gen, :) = obj;
position_history(:, gen, :) = pop;
% Update Trajectories
if ~isempty(archive.pop)
Trajectories(:, gen) = archive.pop(randi(size(archive.pop, 1)), :)';
end
disp(['Iteration #', num2str(gen), ' completed. Archive size: ', num2str(size(archive.pop, 1))]);
end
% Return Pareto front and Pareto set
paretoFront = archive.obj;
paretoSet = archive.pop;
end
% Helper Functions
function pop = initialization(NPop, dim, ub, lb)
pop = rand(NPop, dim) .* (ub - lb) + lb;
end
function obj = evaluate_population(pop, FITNESSFCN)
obj = zeros(size(pop, 1), 2); % Assuming 2 objectives
for i = 1:size(pop, 1)
obj(i, :) = FITNESSFCN(pop(i, :));
end
end
function [pop, obj] = update_population(pop, obj, child, child_obj, index)
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
function archive = update_archive(archive_pop, archive_obj, pop, obj)
combined_pop = [archive_pop; pop];
combined_obj = [archive_obj; obj];
[is_dominated, ~] = pareto_dominance(combined_obj);
non_dominated = ~is_dominated;
archive.pop = combined_pop(non_dominated, :);
archive.obj = combined_obj(non_dominated, :);
end
function [is_dominated, domination_count] = pareto_dominance(objectives)
[n_solutions, ~] = size(objectives);
is_dominated = false(n_solutions, 1);
domination_count = zeros(n_solutions, 1);
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
function result = dominates(obj1, obj2)
result = all(obj1 <= obj2) && any(obj1 < obj2);
end
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
function referenceFront = generate_reference_front()
x = linspace(0, 1, 100);
referenceFront = [x; 1 - sqrt(x)]';
end
function igd_value = calculate_igd(paretoFront, referenceFront)
d_sum = 0;
for i = 1:size(referenceFront, 1)
d_sum = d_sum + min(sqrt(sum((paretoFront - referenceFront(i, :)).^2, 2)));
end
igd_value = d_sum / size(referenceFront, 1);
end
function sp_value = calculate_sp(paretoFront)
[n, ~] = size(paretoFront);
if n > 1
d_f = sqrt(sum(diff(paretoFront).^2, 2)); % Distance between consecutive solutions
d_mean = mean(d_f);
sp_value = sum(abs(d_f - d_mean)) / ((n - 1) * d_mean);
else
sp_value = NaN;
end
end
function ms_value = calculate_ms(paretoFront)
maxObj1 = max(paretoFront(:, 1));
minObj1 = min(paretoFront(:, 1));
maxObj2 = max(paretoFront(:, 2));
minObj2 = min(paretoFront(:, 2));
ms_value = sqrt((maxObj1 - minObj1)^2 + (maxObj2 - minObj2)^2);
end
Explanation:
Main Script and Metrics:
The main script sets up the MOEMA algorithm, runs it, and plots the results.
Calculation of metrics (IGD, SP, MS) is done using helper functions.
Metrics Functions:
calculate_igd: Computes the Inverted Generational Distance.
calculate_sp: Computes the Spread.
calculate_ms: Computes the Maximum Spread.
Reference Front:
A simple generate_reference_front is used to produce a reference front. Replace this with your actual problem-specific Pareto front if available.
MOEMA Algorithm:
The MOEMA function manages the optimization, including population initialization, evaluation, and evolution through generations.
Use this code structure and modify the placeholder functions as needed to reflect your specific use case or to incorporate actual problem constraints and objectives.
To calculate and display the Best, Average, Worst, Standard Deviation, and Median for the IGD, SP, and MS values across multiple runs of your MOEMA code, you need to run the optimization multiple times and collect the metric values for each run. Here's how you can modify the code to achieve this:
Step-by-Step Approach
Multiple Runs: Execute the MOEMA algorithm multiple times to gather data on different runs.
Store Metrics: Collect IGD, SP, and MS values for each run.
Calculate Statistics: Calculate statistical summaries (Best, Average, Worst, Standard Deviation, and Median) for the metrics.
Here's a complete code that includes these calculations:
matlab
% Multi-run settings
numRuns = 30; % Number of runs
igd_values = zeros(numRuns, 1);
sp_values = zeros(numRuns, 1);
ms_values = zeros(numRuns, 1);
% Parameters for MOEMA
NPop = 50;
T = 50;
dim = 30;
lb = zeros(1, dim);
ub = ones(1, dim);
% Run multiple times to collect metrics
for run = 1:numRuns
% Run MOEMA
[paretoFront, ~, ~, ~, ~] = MOEMA(NPop, T, lb, ub, dim, @zdt1);
% Example reference front
referenceFront = generate_reference_front();
% Calculate metrics for this run
igd_values(run) = calculate_igd(paretoFront, referenceFront);
sp_values(run) = calculate_sp(paretoFront);
ms_values(run) = calculate_ms(paretoFront);
end
% Calculate statistics
stats_igd = calculate_statistics(igd_values);
stats_sp = calculate_statistics(sp_values);
stats_ms = calculate_statistics(ms_values);
% Display the results
disp('IGD Statistics:');
disp(stats_igd);
disp('SP Statistics:');
disp(stats_sp);
disp('MS Statistics:');
disp(stats_ms);
% Functions remain the same
% MOEMA Algorithm omitted for brevity; use the implementation already shared
% Add new helper functions
function stats = calculate_statistics(values)
stats.Best = min(values);
stats.Average = mean(values);
stats.Worst = max(values);
stats.StdDev = std(values);
stats.Median = median(values);
end
function referenceFront = generate_reference_front()
x = linspace(0, 1, 100);
referenceFront = [x; 1 - sqrt(x)]';
end
function igd_value = calculate_igd(paretoFront, referenceFront)
d_sum = 0;
for i = 1:size(referenceFront, 1)
d_sum = d_sum + min(sqrt(sum((paretoFront - referenceFront(i, :)).^2, 2)));
end
igd_value = d_sum / size(referenceFront, 1);
end
function sp_value = calculate_sp(paretoFront)
[n, ~] = size(paretoFront);
if n > 1
d_f = sqrt(sum(diff(paretoFront).^2, 2)); % Distance between consecutive solutions
d_mean = mean(d_f);
sp_value = sum(abs(d_f - d_mean)) / ((n - 1) * d_mean);
else
sp_value = NaN;
end
end
function ms_value = calculate_ms(paretoFront)
maxObj1 = max(paretoFront(:, 1));
minObj1 = min(paretoFront(:, 1));
maxObj2 = max(paretoFront(:, 2));
minObj2 = min(paretoFront(:, 2));
ms_value = sqrt((maxObj1 - minObj1)^2 + (maxObj2 - minObj2)^2);
end
Explanation:
Multiple Runs: The code runs the MOEMA algorithm 30 times (or any number you set in numRuns).
Metric Calculation: After each run, metrics IGD, SP, and MS are calculated and stored.
Statistics Calculation: After all runs, statistics are calculated for each metric using calculate_statistics, providing a structured summary.
Display Results: The statistical results are displayed for IGD, SP, and MS.
This approach allows you to understand the variability and reliability of your algorithm across multiple runs. Adjust the numRuns value and ensure you have sufficient variability in your runs to get meaningful statistics.
Certainly! Below is the complete code to execute the MOEMA algorithm multiple times and compute statistics (Best, Average, Worst, Standard Deviation, and Median) for the IGD, SP, and MS metrics. This approach assumes you have a proper setup for the zdt1 function, and you are using a reference Pareto front for calculating IGD:
matlab
% Main script to run multiple MOEMA optimizations
clc;
clear;
close all;
% Multi-run settings
numRuns = 30; % Number of runs
igd_values = zeros(numRuns, 1);
sp_values = zeros(numRuns, 1);
ms_values = zeros(numRuns, 1);
% Parameters for MOEMA
NPop = 50; % Population size
T = 50; % Number of iterations
dim = 30; % Number of decision variables
lb = zeros(1, dim); % Lower bound
ub = ones(1, dim); % Upper bound
% Run multiple times to collect metrics
for run = 1:numRuns
% Run MOEMA
[paretoFront, ~, ~, ~, ~] = MOEMA(NPop, T, lb, ub, dim, @zdt1);
% Example reference front
referenceFront = generate_reference_front();
% Calculate metrics for this run
igd_values(run) = calculate_igd(paretoFront, referenceFront);
sp_values(run) = calculate_sp(paretoFront);
ms_values(run) = calculate_ms(paretoFront);
disp(['Run ', num2str(run), ' completed.']);
end
% Calculate statistics
stats_igd = calculate_statistics(igd_values);
stats_sp = calculate_statistics(sp_values);
stats_ms = calculate_statistics(ms_values);
% Display the results
disp('IGD Statistics:');
disp(stats_igd);
disp('SP Statistics:');
disp(stats_sp);
disp('MS Statistics:');
disp(stats_ms);
% MOEMA Algorithm and Helper Functions
function [paretoFront, paretoSet, Trajectories, fitness_history, position_history] = MOEMA(NPop, T, lb, ub, dim, FITNESSFCN)
% Multi-Objective Evolutionary Mating Algorithm (MOEMA)
% 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, :);
pop_mum = pop(NPop/2+1:end, :);
% 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
k1 = randperm(NPop/2);
k2 = randperm(NPop/2);
k1x = pop_dad(k1, :);
k2x = pop_mum(k2, :);
for k = 1:NPop/2
% Mating process
Imates = 1 + (var(k1x(k, :)) - var(k2x(k, :)));
p = randn(1, dim);
r = rand();
if Imates >= 0
temp1 = p .* k1x(k, :);
temp2 = (1-p) .* k2x(k, :);
pop_child = temp1 + temp2;
else
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 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 pool dad and mum
pop = [pop_dad; pop_mum];
obj = [obj_dad; obj_mum];
% Update archive
archive = update_archive(archive.pop, archive.obj, pop, obj);
% Record history at each iteration
fitness_history(:, gen, :) = obj;
position_history(:, gen, :) = pop;
% Update Trajectories
if ~isempty(archive.pop)
Trajectories(:, gen) = archive.pop(randi(size(archive.pop, 1)), :)';
end
disp(['Iteration #', num2str(gen), ' completed. Archive size: ', num2str(size(archive.pop, 1))]);
end
% Return Pareto front and Pareto set
paretoFront = archive.obj;
paretoSet = archive.pop;
end
function referenceFront = generate_reference_front()
x = linspace(0, 1, 100);
referenceFront = [x; 1 - sqrt(x)]';
end
function igd_value = calculate_igd(paretoFront, referenceFront)
d_sum = 0;
for i = 1:size(referenceFront, 1)
d_sum = d_sum + min(sqrt(sum((paretoFront - referenceFront(i, :)).^2, 2)));
end
igd_value = d_sum / size(referenceFront, 1);
end
function sp_value = calculate_sp(paretoFront)
[n, ~] = size(paretoFront);
if n > 1
d_f = sqrt(sum(diff(paretoFront).^2, 2)); % Distance between consecutive solutions
d_mean = mean(d_f);
sp_value = sum(abs(d_f - d_mean)) / ((n - 1) * d_mean);
else
sp_value = NaN;
end
end
function ms_value = calculate_ms(paretoFront)
maxObj1 = max(paretoFront(:, 1));
minObj1 = min(paretoFront(:, 1));
maxObj2 = max(paretoFront(:, 2));
minObj2 = min(paretoFront(:, 2));
ms_value = sqrt((maxObj1 - minObj1)^2 + (maxObj2 - minObj2)^2);
end
function stats = calculate_statistics(values)
stats.Best = min(values);
stats.Average = mean(values);
stats.Worst = max(values);
stats.StdDev = std(values);
stats.Median = median(values);
end
% Define ZDT1 Test 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
% Additional MOEMA Helper Functions
function pop = initialization(NPop, dim, ub, lb)
pop = rand(NPop, dim) .* (ub - lb) + lb;
end
function obj = evaluate_population(pop, FITNESSFCN)
obj = zeros(size(pop, 1), 2); % Assuming 2 objectives
for i = 1:size(pop, 1)
obj(i, :) = FITNESSFCN(pop(i, :));
end
end
function [pop, obj] = update_population(pop, obj, child, child_obj, index)
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
function archive = update_archive(archive_pop, archive_obj, pop, obj)
combined_pop = [archive_pop; pop];
combined_obj = [archive_obj; obj];
[is_dominated, ~] = pareto_dominance(combined_obj);
non_dominated = ~is_dominated;
archive.pop = combined_pop(non_dominated, :);
archive.obj = combined_obj(non_dominated, :);
end
function [is_dominated, domination_count] = pareto_dominance(objectives)
[n_solutions, ~] = size(objectives);
is_dominated = false(n_solutions, 1);
domination_count = zeros(n_solutions, 1);
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
function result = dominates(obj1, obj2)
result = all(obj1 <= obj2) && any(obj1 < obj2);
end
Explanation:
Multiple Runs: The script runs the MOEMA algorithm numRuns times, collecting IGD, SP, and MS metrics for each run.
Metric Computation: After each run, metrics are computed and stored in arrays (igd_values, sp_values, ms_values).
Statistic Calculation: Once all runs are complete, statistical summaries for each metric are calculated and displayed.
Helper Functions: The script includes all relevant functions necessary for the MOEMA algorithm, metric calculations, and statistical summaries.
This setup provides a detailed analysis of how the algorithm performs over multiple executions, allowing you to evaluate consistency and effectiveness. Adjust numRuns as needed for more extensive testing.