-
Notifications
You must be signed in to change notification settings - Fork 29
/
selection.m
63 lines (59 loc) · 2.12 KB
/
selection.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
function [ new_pop ] = selection(old_pop, new_pop)
% Applies tournament selection to generate a child population
% We did not vectorize this function as we want to keep this procedure
% separate from those of the crossover and the mutation. Otherwise
% implementation of different kind of selection strategy will not be
% straight forward.
global nreal ;
global nbin ;
global nbits ;
global min_binvar ;
global max_binvar ;
global pcross_bin ;
global min_realvar ;
global max_realvar ;
global pcross_real ;
global eta_c ;
[popsize,~] = size(old_pop);
old_pop = old_pop(randperm(end),:);
% old_pop = shuffle(old_pop); % SLOW !!!
for i = 1:4:popsize
p1i = tournament(old_pop,i, i+1);
p2i = tournament(old_pop,i+2,i+3);
if(nreal > 0)
[c1, c2] = real_cross(old_pop(p1i,1:nreal), ...
old_pop(p2i,1:nreal), ...
pcross_real, eta_c, ...
min_realvar, max_realvar);
elseif(nbin > 0)
[c1, c2] = bin_cross_two_point(old_pop(p1i, 1:sum(nbits)), ...
old_pop(p2i, 1:sum(nbits)), ...
nbits, pcross_bin);
end
new_pop(i, 1:nreal) = c1 ;
new_pop(i+1,1:nreal) = c2 ;
p1i = tournament(old_pop,i, i+1);
p2i = tournament(old_pop,i+2,i+3);
if(nreal > 0)
[c1, c2] = real_cross(old_pop(p1i,1:nreal), ...
old_pop(p2i,1:nreal), ...
pcross_real, eta_c, ...
min_realvar, max_realvar);
elseif(nbin > 0)
[c1, c2] = bin_cross_two_point(old_pop(p1i,1:sum(nbits)), ...
old_pop(p2i,1:sum(nbits)), ...
pcross_bin);
end
new_pop(i+2,1:nreal) = c1 ;
new_pop(i+3,1:nreal) = c2 ;
end
end
function [shuffled_pop] = shuffle(pop)
% This function uses the legacy rng for shuffling,
% but too slow.
[popsize,cols] = size(pop);
shuffled_pop = zeros(popsize,cols);
for i = 1:popsize
shuffled_pop(i,:) = pop(rnd(1,popsize),:);
end
end