-
Notifications
You must be signed in to change notification settings - Fork 2
/
Selection_Mechanism.m
36 lines (30 loc) · 1.3 KB
/
Selection_Mechanism.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
function [P1,P2] = Selection_Mechanism(Population,PopulationSize, SelectionIDX,TournamentSize)
try
Pop_Ind = 1:PopulationSize;
switch SelectionIDX
case 0 %random
Selected_Parents = randsample(PopulationSize,2);
P1 = Selected_Parents(1);
P2 = Selected_Parents(2);
case 1 %Tournament
% Find the 1st parent
% Randomly select a number of chromosomes from the current population
Selected_Index = randsample(Pop_Ind,TournamentSize);
%Selected_Individuals = Population(Selected_Index,:);
% Select the fittest chromosome from above random sample
% Because individuals are sorted in the current population in a
% non-deacreasing order based on their cost
% So individual with smaller index has lower cost
%[~,I]= sort([Selected_Individuals{:,1}]);
[~,I]= sort(Selected_Index);
P1 = Selected_Index(I(1));
% Find the 2nd parent
Pop_Ind(P1) = [];
Selected_Index = randsample(Pop_Ind,TournamentSize);
[~,I]= sort(Selected_Index);
P2 = Selected_Index(I(1));
end
catch ME
display(ME)
end
end