CrossOver in Matlab -
i ask how performe crossover in genetic algorithm using matlab. let suppose have following 2 sequences,
sequence1 = 1 2 3 7 9 10 5 4 6 8
sequence2 = 4 3 9 2 1 7 6 10 8 5
now know how make code perform crossover in matlab such new child sequence cross-overed either @ single-point or two-point , result has no repetition of number in child sequence.
regards,
with 1 point crossover have unique points in range 1..numel(sequence)-1
. n
unique numbers can use randperm. 2 point crossover bit more difficult, total number of unique combinations sum of arithmetical progression 1..numel(sequence)-2
.
here example of single point crossover:
function [result] = crossoveratpoint(sequence1, sequence2, point) result = [sequence1(1:point-1), sequence2(point:end)]; end function [result] = crossoversinglepoint(sequence1, sequence2, number) len = numel(sequence1); % calculate length points = randperm(len-1, number)+1; % generate set of crossover points in range 2..len result = zeros(number, len); % preallocate matrix = 1:number result(i,:) = crossoveratpoint(sequence1, sequence2, points(i)); end end seq1 = [1 2 3 7 9 10 5 4 6 8]; seq2 = [4 3 9 2 1 7 6 10 8 5]; crossoversinglepoint(seq1, seq2, 3)
the result similar one:
1 2 3 7 9 10 5 4 8 5 1 2 9 2 1 7 6 10 8 5 1 2 3 7 1 7 6 10 8 5
update.
this function guarantees pivot unique children. not guarantee children have unique elements (this requirement sounds strangely wrong me), have make manual check that.
Comments
Post a Comment