-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrankxover.m
33 lines (31 loc) · 1.58 KB
/
rankxover.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
27
28
29
30
31
32
33
function xoverKids = rankxover(parents, options, nvars, FitnessFcn, ...
unused,thisPopulation)
%parents - Row vector of parents chosen by the selection function
%options - options structure
%nvars - Number of variables
%FitnessFcn - Fitness function
%unused - Placeholder not used
%thisPopulation - Matrix representing the current population. The number of rows of the matrix is Population size and the number of columns is Number of variables.
%%%%%%%%%%%%%%%%%%
xoverKids = zeros(length(parents)/2,nvars);
%NEED TO DETERMINE IF LENGTH OF PARENTS IS EVEN. --- YES
for i=1:2:length(parents)
parentPair={thisPopulation(parents(i),:), thisPopulation(parents(i+1),:)};
choiceparent = randsample(parentPair,1);
xoverKids(ceil(i/2),:) = choiceparent{1};
chooseTransposition = randsample(choiceparent{1},2);
xoverKids(ceil(i/2),chooseTransposition) = xoverKids(ceil(i/2),chooseTransposition([2,1]));
end
end
%The code block below can be substituted above to make the children differ only by
%transpositions that are directly next to each other. I did this after
%observing that many p are very close to each other. This may be offbase,
%it made runtime WAY longer
%for i=1:2:length(parents)
% parentPair={thisPopulation(parents(i),:), thisPopulation(parents(i+1),:)};
% choiceparent = randsample(parentPair,1);
% xoverKids(ceil(i/2),:) = choiceparent{1};
% chooseTransposition = randsample(choiceparent{1},1);
% xoverKids(ceil(i/2),[chooseTransposition mod(chooseTransposition+1,nvars)+1]) = xoverKids(ceil(i/2),[mod(chooseTransposition+1,nvars)+1 chooseTransposition]);
%end
%end