📄 nsga_2.m
字号:
% Main program to run the NSGA-II MOEA.
function nsga_2()
% Initialize the variables
% Declare the variables and initialize their values
% pop - population
% gen - generations
% pro - problem number
pop = 100;
gen = 5;
pro = 1;
switch pro
case 1
% M is the number of objectives.
M = 2;
% V is the number of decision variables. In this case it is
% difficult to visualize the decision variables space while the
% objective space is just two dimensional.
V = 6;
case 2
M = 3;
V = 12;
end
% Initialize the population
chromosome = initialize(pop,pro);
% Sort the initialized population
chromosome = non_domination(chromosome,pro);
% Start the evolution process
% The following are performed in each generation.Select the parents.Perfrom crossover and Mutation operator.Perform Selection
for i = 1 : gen
% Select the parents.Parents are selected for reproduction to generate offspring.
% pool - size of the mating pool. It is common to have this to be half the population size.
% tour - Tournament size. Original NSGA-II uses a binary tournament selection, but to see the effect of tournament size
% this is kept arbitary, to be choosen by the user.
pool = round(pop/2);
tour = 2;
parent = tournament(chromosome,pool,tour);
% Perfrom crossover and Mutation operator
mu = 20;
mum = 20;
offspring = genetic(parent,pro,mu,mum);
% Intermediate population
% Intermediate population is the combined population of parents and offsprings of the current generation. The population
% size is almost 1 and half times the initial population.
[main_pop,temp] = size(chromosome);
[offspring_pop,temp] = size(offspring);
intermediate(1:main_pop,:) = chromosome;
intermediate(main_pop + 1 : main_pop + offspring_pop,1 : M+V) = offspring;
% Non-domination-sort of intermediate population
% The intermediate population is sorted again based on non-domination sort before the replacement operator is performed on
% the intermediate population.
intermediate = non_domination(intermediate,pro);
% Perform Selection
% Once the intermediate population is sorted only the best solution is selected based on it rank and crowding distance.
% Each front is filled in ascending order until the addition of population size is reached. The last front is included in
%the population based on the individuals with least crowding distance
chromosome = replace(intermediate,pro,pop)
if ~mod(i,10)
fprintf('%d\n',i);
end
end
%% Result
% Save the result in ASCII text format.
save solution.txt chromosome -ASCII
%% Visualize
% The following is used to visualize the result for the given problem.
switch pro
case 1
plot(chromosome(:,V + 1),chromosome(:,V + 2),'*');
title('MOP1 using NSGA-II');
xlabel('f(x_1)');
ylabel('f(x_2)');
case 2
plot3(chromosome(:,V + 1),chromosome(:,V + 2),chromosome(:,V + 3),'*');
title('MOP2 using NSGA-II');
xlabel('f(x_1)');
ylabel('f(x_2)');
zlabel('f(x_3)');
end
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -