-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathselection_T.m
More file actions
74 lines (58 loc) · 1.76 KB
/
selection_T.m
File metadata and controls
74 lines (58 loc) · 1.76 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
function [ parents ] = selection_T( pop, steps )
population = pop;
pop_size = size(pop,1);
parent1 = [];
parent2 = [];
offspring = [];
opponent = [];
rand_offspring = randi([1 pop_size],1,1); %randomly select offspring
rand_opponent = randi([1 pop_size],1,1); %randomly select opponent
%trap to assures offspring and opponent are not the same
while(rand_offspring == rand_opponent)
rand_opponent = randi([1 pop_size],1,1);
end
for i=1:size(population,1)
if(i == rand_offspring) %found the offspring in the population
offspring = population(i,:);
end
if(i == rand_opponent) %found the opponent in the population
opponent = population(i,:);
end
end
%compete for parent1
if(fitness(offspring) > fitness(opponent))
parent1 = offspring;
else
parent1 = opponent;
end
%removing parent1 from populatoin
for i=1:size(population,1)
if(parent1 == population(i,:))
population(i,:) = [];
break;
end
end
%update population size
pop_size = size(pop,1);
rand_offspring = randi([1 pop_size],1,1); %randomly select offspring
rand_opponent = randi([1 pop_size],1,1); %randomly select opponent
%trap to assures offspring and opponent are not the same
while(rand_offspring == rand_opponent)
rand_opponent = randi([1 pop_size],1,1);
end
for i=1:size(population,1)
if(i == rand_offspring) %found the offspring in the population
offspring = population(i,:);
end
if(i == rand_opponent) %found the opponent in the population
opponent = population(i,:);
end
end
%compete for parent2
if(fitness(offspring) > fitness(opponent))
parent2 = offspring;
else
parent2 = opponent;
end
parents = cat(1,parent1,parent2);
end