-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCrossover.asv
More file actions
57 lines (45 loc) · 1.89 KB
/
Crossover.asv
File metadata and controls
57 lines (45 loc) · 1.89 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
%*************************************************************************%
%@BeginVerbatim
% Function: Crossover
% Description: Create an offspring with the 2 given chromosomes
% Parameter(s): parent_1, matrix; parent_2, matrix
% Filename: Crossover.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 [ child ] = Crossover( parent_1, parent_2 )
%@Intialization*******************************************************%
CHILD_DNA_SIZE = 10; %default dna size
STARTING_INDEX = 1; %start index
STARTING_INDEX_LIMIT = CHILD_DNA_SIZE/2; %start index limit
ENDING_INDEX_LIMIT = 4; %limit for the range
START_RANGE = randi([STARTING_INDEX STARTING_INDEX_LIMIT],1); %random number from 1 to the half CHILD_DNA_SIZE to determine start index
END_RANGE = START_RANGE + ENDING_INDEX_LIMIT;
child = zeros(1,10); %Initialize child with 1x10 zeros
%*********************************************************************%
%Get the specified DNA from the parent_1
parent_1 = parent_1(1,START_RANGE:END_RANGE);
%Remove existing dna from parent_2
for parent_1_dna = 1:length(parent_1)
for dna = 1:length(parent_2)-1
if parent_1(parent_1_dna) == parent_2(dna)
parent_2(dna)=[];
end
end
end
%place the remaining dna from parent_1 in place
child(start_range:END_RANGE) = parent_1;
parent_2_dna = 1;
%place the remaining nonexisting dna from parent_2 in order
for dna = 1:CHILD_DNA_SIZE
if child(dna) == 0
child(dna) = parent_2(parent_2_dna);
parent_2_dna = parent_2_dna + 1;
end
end
end