-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection.m
More file actions
58 lines (47 loc) · 2.16 KB
/
Selection.m
File metadata and controls
58 lines (47 loc) · 2.16 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
%*************************************************************************%
%@BeginVerbatim
% Function: Selection
% Description: Select two chromosomes for reproduction
% Parameter(s): population, matrix
% Filename: Selection.m
% Version: v00.02
% Author: Group 1
% Yr&Sec: 3-4
% Subject: Computational Intelligence
%@EndVebartim
%*************************************************************************%
% Last Reviewed by: Periabras, 17 February 2017
%@Code Body***************************************************************%
function [ parent_1, parent_2 ] = Selection( population )
%@Intialization*******************************************************%
max_fitness = 100;
max_cost = 100;
matingpool = []; %serves as basket from where we are going to choose chromosomes for the reproduction
%*********************************************************************%
%Loope through the whole population
for chromosome = 1:length(population)
%Get the fitness score of the current chromosome
fitness_score = Fitness(population(chromosome));
%Subtract the fitness score to the max cost to invert the
% probability that they will be chosen
n = floor(max_cost - fitness_score);
%Loop through the total number that they will be added to the
% mating pool
for entry = 1:n
matingpool = cat(2, matingpool, population(chromosome,:));
end
end
%Get a random number from 1 to the length of the mating pool
chosen_index = randi([1 length(matingpool)], 1);
%Get the chromosome using the chosen_index
chosen_chromosome = matingpool(chosen_index);
%Duplicate the chromosome from population to parent_1
parent_1 = population(chosen_chromosome,:);
%Get a random number from 1 to the length of the mating pool
chosen_index = randi([1 length(matingpool)], 1);
%Get the chromosome using the chosen_index
chosen_chromosome = matingpool(chosen_index);
%Duplicate the chromosome from population to parent_2
parent_2 = population(chosen_chromosome,:);
end
%*************************************************************************%