% Parameters
Np = params.Np; % Number of chromosomes in the population
maxgen = params.maxgen; % Maximum number of generations
pc = params.pc; % Probability of crossover
pm = params.pm; % Probability of mutation
ms = params.ms; % Mutation strength
fun = MultiObj.fun; % Objective function
nVar = MultiObj.nVar; % Number of variables (dimensions or objectives)
var_min = MultiObj.var_min(:); % Minimum value for each gen
var_max = MultiObj.var_max(:); % Maximum value for each gen
% Initialization
gen = 1;
P = repmat((var_max-var_min)',Np,1).*rand(Np,nVar) + repmat(var_min',Np,1);
Pfit = fun(P);
Prank = FastNonDominatedSorting_Vectorized(Pfit);
[P,~] = selectParentByRank(P,Prank);
Q = applyCrossoverAndMutation(P,pc,pm,ms,var_max,var_min);
% Plotting and verbose
if(size(Pfit,2) == 2)
h_fig = figure(1);
h_par=scatter(Pfit(:,1),Pfit(:,2),20,'filled', 'markerFaceAlpha',0.3,'MarkerFaceColor',[128 193 219]./255); hold on;
h_rep = plot(Pfit(:,1),Pfit(:,2),'ok'); hold on;
grid on; xlabel('f1'); ylabel('f2');
drawnow;
axis square;
end
if(size(Pfit,2) == 3)
h_fig = figure(1);
h_rep = plot3(Pfit(:,1),Pfit(:,2),Pfit(:,3),'ok'); hold on;
grid on; xlabel('f1'); ylabel('f2'); zlabel('f3');
drawnow;
axis square;
end
display(['Generation #' num2str(gen) ' - First front size: ' num2str(sum(Prank==1))]);
% Main NSGA-II loop
stopCondition = false;
while ~stopCondition
% Merge the parent and the children
R = [P; Q];
% Compute the new Pareto Fronts
Rfit = real(fun(R));
Rrank = FastNonDominatedSorting_Vectorized(Rfit);
% Plotting and verbose
if(size(Rfit,2) == 2)
figure(h_fig); delete(h_rep);
h_par=scatter(Rfit(1:Np,1),Rfit(1:Np,2),20,'filled', 'markerFaceAlpha',0.3,'MarkerFaceColor',[128 193 219]./255); hold on;
h_rep = plot(Rfit(1:Np,1),Rfit(1:Np,2),'ok'); hold on;
if(isfield(MultiObj,'truePF'))
try delete(h_pf); end
h_pf = plot(MultiObj.truePF(:,1),MultiObj.truePF(:,2),'.','color',0.8.*ones(1,3)); hold on;
end
grid on; xlabel('f1'); ylabel('f2');
drawnow;
axis square;
end
if(size(Rfit,2) == 3)
figure(h_fig); delete(h_rep);
h_rep = plot3(Rfit(1:Np,1),Rfit(1:Np,2),Rfit(1:Np,3),'ok'); hold on;
if(isfield(MultiObj,'truePF'))
try delete(h_pf); end
h_pf = plot3(MultiObj.truePF(:,1),MultiObj.truePF(:,2),MultiObj.truePF(:,3),'.','color',0.8.*ones(1,3)); hold on;
end
grid on; xlabel('f1'); ylabel('f2');
drawnow;
axis square;
end
display(['Generation #' num2str(gen) ' - First front size: ' num2str(sum(Rrank==1))]);
% Sort by rank
[Rrank,idx] = sort(Rrank,'ascend');
Rfit = Rfit(idx,:);
R = R(idx,:);
% Compute the crowding distance index
[Rcrowd,Rrank,~,R] = crowdingDistances(Rrank,Rfit,R);
% Select Parent
P = selectParentByRankAndDistance(Rcrowd,Rrank,R);
% Compute child
Q = applyCrossoverAndMutation(P,pc,pm,ms,var_max,var_min);
% Increment generation
gen = gen + 1;
if(gen>maxgen), stopCondition = true; end
end
% Function that performs a vectorized version of the Fast Non Dominated Sorting
% algorithm which speeds up the computation time
function [RANK] = FastNonDominatedSorting_Vectorized(fitness)
% Initialization
Np = size(fitness,1);
RANK = zeros(Np,1);
current_vector = [1:1:Np]';
current_pf = 1;
all_perm = [repmat([1:1:Np]',Np',1), reshape(repmat([1:1:Np],Np,1),Np^2,1)];
all_perm(all_perm(:,1)==all_perm(:,2),:) = [];