-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Cross.m
27 lines (23 loc) · 982 Bytes
/
Cross.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
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Alp Sayin - alpsayin[at]alpsayin[dot]com - https://alpsayin.com
% Matlab Genetic Algorithm
% Spring 2012
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function newChromosomePair = Cross( chromosome1, chromosome2)
nGenes = size(chromosome1,2) ;
crossoverPoint = 1 + fix(rand*(nGenes-1));
assert(crossoverPoint>0 && crossoverPoint<=nGenes);
newChromosomePair(1, :) = [chromosome1(1:crossoverPoint) chromosome2(crossoverPoint+1:end)];
newChromosomePair(2, :) = [chromosome2(1:crossoverPoint) chromosome1(crossoverPoint+1:end)];
% % Deprecated - to be deleted in the next iteration
% for j = 1: nGenes
% if (j < crossoverPoint)
% newChromosomePair(1,j) = chromosome1(j);
% newChromosomePair(2,j) = chromosome2(j);
% else
% newChromosomePair(1,j) = chromosome2(j);
% newChromosomePair(2,j) = chromosome1(j);
% end
% end