-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection.m
More file actions
56 lines (49 loc) · 2.15 KB
/
Selection.m
File metadata and controls
56 lines (49 loc) · 2.15 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Tournament selection method for Genetic Algorithm. K number of
% candidates are chosen from the population and ranked from best to worst.
%
% Parameters:
% Inputs: fitnessSorted_breed - Fitness scores sorted in ascending order
% popSorted.binaryImage - Population sorted in ascending order
% according to their fitness value
% nPop_withoutBest - Number of population members excluding the
% best member
% numOfChildren - Number of children resulting from
% cross-over
% Output: nextParents.binaryImage - Set of parents used for reproduction for
% the next generation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function nextParents = Selection(fitnessBreed, popBreed, nPop_withoutBest, numOfChildren)
rng('shuffle');
%% Tounament selection method:
% Number of children output from selection
childrenPopulation = round(nPop_withoutBest / numOfChildren);
% Set k-value
if(nPop_withoutBest >= 5)
K = 5;
else
K = nPop_withoutBest;
end
% Pre-allocation of memory
nextParents.binaryImage = cell(1, childrenPopulation);
nextParents.dBetweenGratings = zeros(1, childrenPopulation);
candidates_pop.binaryImage = cell(1, K);
% Selection:
j = 1;
for i = 1:childrenPopulation
% Find 5 random indexes
candidates = randperm(nPop_withoutBest, K);
% Find those indexes' fitness and populations
candidates_fitness = fitnessBreed(candidates);
for ii = 1:K
candidates_pop.binaryImage{ii} = popBreed.binaryImage{candidates(ii)};
end
candidates_pop.dBetweenGratings = popBreed.dBetweenGratings(candidates);
% Chose best fitness from candidates
[~, idx] = min(candidates_fitness); % minimization problem
% Keep the winning parent candidate
nextParents.binaryImage{j} = candidates_pop.binaryImage{idx};
nextParents.dBetweenGratings(j) = candidates_pop.dBetweenGratings(idx);
j = j + 1;
end
end