To accommodate the ZDT5 benchmark problem, which is typically formulated for binary-coded genetic algorithms, I'll provide a full MATLAB code that sets up the problem, implements binary-specific initialization, crossover, and mutation functions, and deploys the NSGA-II and MOEMA algorithms.
Here's a full implementation:
clc;
clear;
close all;
% Problem parameters
dim = 80; % Number of variables (adjust according to ZDT5 specifications)
bit_length = 11; % Binary length for each variable in ZDT5
% Function placeholders for lower/upper bounds; not directly applicable for binary GA
lb = zeros(1, dim);
ub = ones(1, dim);
% Convert dimensions for binary encoding
dim_encoded = dim * bit_length;
% Run MOEMA
NPop_MOEMA = 50;
T_MOEMA = 500;
[paretoFront_MOEMA, ~] = MOEMA(NPop_MOEMA, T_MOEMA, lb, ub, dim_encoded, @zdt5);
% Run NSGA-II
NPop_NSGA2 = 50;
T_NSGA2 = 500;
[paretoFront_NSGA2, ~] = NSGA2(NPop_NSGA2, T_NSGA2, lb, ub, dim_encoded);
% Plotting results can be challenging for ZDT5 due to the high-dimensional binary nature.
% Typically, you would have visualization specific to each problem.
% The plotting code might need adjustments depending on the evaluation function and results interpretation.
% Function Definitions
function [paretoFront, paretoSet] = MOEMA(NPop, T, lb, ub, dim, FITNESSFCN)
pop = binary_initialization(NPop, dim);
pop_dad = pop(1:NPop/2, :);
pop_mum = pop(NPop/2+1:end, :);
obj_dad = evaluate_population(pop_dad, FITNESSFCN);
obj_mum = evaluate_population(pop_mum, FITNESSFCN);
obj = [obj_dad; obj_mum];
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
Imates = 1 + (var(k1x(k,:)) - var(k2x(k,:)));
if Imates >= 0
pop_child = binary_crossover_single(k1x(k,:), k2x(k,:));
else
pop_child = binary_crossover_single(k2x(k,:), k1x(k,:));
end
if ~isempty(archive.pop)
best_pop = archive.pop(randi(size(archive.pop, 1)), :);
mask = rand(1, dim) < 0.8;
pop_child(mask) = best_pop(mask);
end
pop_child = max(min(ub, pop_child), lb);
obj_child = FITNESSFCN(pop_child);
[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));
end
pop = [pop_dad; pop_mum];
obj = [obj_dad; obj_mum];
archive = update_archive(archive.pop, archive.obj, pop, obj);
disp(['Iteration #', num2str(gen), ' completed. Archive size: ', num2str(size(archive.pop, 1))]);
end
paretoFront = archive.obj;
paretoSet = archive.pop;
end
function [paretoFront, paretoSet] = NSGA2(NPop, T, lb, ub, dim)
pop = binary_initialization(NPop, dim);
fitness = evaluate_population(pop, @zdt5);
for gen = 1:T
[fronts, ranks] = non_dominated_sort(fitness);
mating_pool = tournament_selection(pop, ranks, NPop);
offspring = binary_crossover(mating_pool, 0.9);
offspring = binary_mutation(offspring, 1/dim);
offspring_fitness = evaluate_population(offspring, @zdt5);
comb_pop = [pop; offspring];
comb_fitness = [fitness; offspring_fitness];
[fronts, ~] = non_dominated_sort(comb_fitness);
[pop, fitness] = select_new_population(comb_pop, comb_fitness, fronts, NPop);
disp(['Generation #', num2str(gen), ' completed.']);
end
paretoFront = fitness;
paretoSet = pop;
end
function pop = binary_initialization(NPop, dim)
pop = randi([0, 1], NPop, dim);
end
function mutated_pop = binary_mutation(population, mutation_prob)
mutated_pop = population;
for i = 1:size(population, 1)
for j = 1:size(population, 2)
if rand < mutation_prob
mutated_pop(i, j) = 1 - population(i, j);
end
end
end
end
function offspring = binary_crossover(parents, crossover_prob)
NPop = size(parents, 1);
num_bits = size(parents, 2);
offspring = zeros(size(parents));
for i = 1:2:NPop
if rand < crossover_prob
cross_point = randi([1, num_bits-1]);
offspring(i, :) = [parents(i, 1:cross_point), parents(i+1, cross_point+1:end)];
offspring(i+1, :) = [parents(i+1, 1:cross_point), parents(i, cross_point+1:end)];
else
offspring(i:i+1, :) = parents(i:i+1, :);
end
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 [fronts, rank] = non_dominated_sort(fitness)
num_individuals = size(fitness, 1);
domination_count = zeros(num_individuals, 1);
dominated_set = cell(num_individuals, 1);
rank = zeros(num_individuals, 1);
fronts = cell(1);
for i = 1:num_individuals
for j = 1:num_individuals
if i ~= j
if dominates(fitness(i, :), fitness(j, :))
dominated_set{i} = [dominated_set{i}, j];
elseif dominates(fitness(j, :), fitness(i, :))
domination_count(i) = domination_count(i) + 1;
end
end
end
if domination_count(i) == 0
rank(i) = 1;
fronts{1} = [fronts{1}, i];
end
end
current_front = 1;
while ~isempty(fronts{current_front})
next_front = [];
for i = fronts{current_front}
for j = dominated_set{i}
domination_count(j) = domination_count(j) - 1;
if domination_count(j) == 0
rank(j) = current_front + 1;
next_front = [next_front, j];
end
end
end
current_front = current_front + 1;
fronts{current_front} = next_front;
end
fronts = fronts(1:current_front-1);
end
function [pop, fitness] = select_new_population(comb_pop, comb_fitness, fronts, NPop)
pop = [];
fitness = [];
for i = 1:length(fronts)
if size(pop, 1) + length(fronts{i}) > NPop
break;
end
pop = [pop; comb_pop(fronts{i}, :)];
fitness = [fitness; comb_fitness(fronts{i}, :)];
end
remaining = NPop - size(pop, 1);
if remaining > 0
selected_front = fronts{i};
distances = crowding_distance(comb_fitness(selected_front, :));
[~, sorted_indices] = sort(distances, 'descend');
selected = selected_front(sorted_indices(1:remaining));
pop = [pop; comb_pop(selected, :)];
fitness = [fitness; comb_fitness(selected, :)];
end
end
function distances = crowding_distance(fitness)
num_individuals = size(fitness, 1);
num_objectives = size(fitness, 2);
distances = zeros(num_individuals, 1);
for m = 1:num_objectives
[sorted_values, sorted_idx] = sort(fitness(:, m));
distances(sorted_idx(1)) = inf;
distances(sorted_idx(end)) = inf;
for i = 2:num_individuals-1
if sorted_values(end) ~= sorted_values(1)
distances(sorted_idx(i)) = distances(sorted_idx(i)) + ...
(sorted_values(i+1) - sorted_values(i-1)) / (sorted_values(end) - sorted_values(1));
end
end
end
end
function fitness = evaluate_population(pop, FITNESSFCN)
fitness = zeros(size(pop, 1), 2);
for i = 1:size(pop, 1)
fitness(i, :) = FITNESSFCN(pop(i, :));
end
end
function is_dominated = dominates(x, y)
is_dominated = all(x <= y) && any(x < y);
end
function [is_dominated, domination_count] = pareto_dominance(objectives)
[n_solutions, n_objectives] = 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 f = zdt5(x)
% ZDT5 problem function
l = 11; % length of each binary bit string
num_blocks = length(x) / l;
x = reshape(x, l, num_blocks)';
% Objective function calculation
f1 = sum(x(:, 1)); % Number of ones in the first block
u = sum(x, 2); % Number of ones in each block
v = arrayfun(@(ui) 2 + (ui >= 5), u); % v is 2 if u < 5, else 1
% Calculate g
g = sum(v);
% Calculate h
h = 1 / f1;
% Objective f2
f2 = g * h;
f = [f1, f2];
end
This code handles binary encoding necessary for the ZDT5 objective function. Each binary string is processed as a sequence of blocks, where local objectives are computed for each block. Make sure to adjust the parameters as needed and verify the fitness evaluations for your specific ZDT5 configuration.