-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCGA_Inversion.m
More file actions
32 lines (26 loc) · 977 Bytes
/
CGA_Inversion.m
File metadata and controls
32 lines (26 loc) · 977 Bytes
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
function chromosome = CGA_Inversion(chromosome, inversion_chance)
% CGA_INVERSION This function flips the gens starting from a random index
% and ending from another random index.
% If the chance achieved, then do the inversion.
active_chance = randi(100) / 100;
if active_chance >= inversion_chance
% Init two random value, and assign them into start and end indices.
rand_1 = randi(size(chromosome, 2));
rand_2 = randi(size(chromosome, 2));
if (rand_1 < rand_2)
starting_index = rand_1;
ending_index = rand_2;
else
starting_index = rand_2;
ending_index = rand_1;
end
% Flip the bits.
for gen_index = starting_index:ending_index
if (chromosome(1, gen_index) == '0')
chromosome(1, gen_index) = '1';
else
chromosome(1, gen_index) = '0';
end
end
end
end