-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubjselect_mutationuniform.m
executable file
·49 lines (44 loc) · 1.88 KB
/
subjselect_mutationuniform.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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function mutationChildren = subjselect_mutationuniform(parents,options,GenomeLength,FitnessFcn,state,thisScore,thisPopulation,mutationRate)
%SUBJSELECT_MUTATIONUNIFORM Mutation function for subject selection.
%
% This function is exactly the same as the built in mutationuniform
% function, with a small tweak to ensure that children generated by
% crossover contain a non-redundant set of subject indices, all of
% which are whole numbers.
if(nargin < 8)
mutationRate = 0.3; % default mutation rate
end
if(strcmpi(options.PopulationType,'doubleVector'))
mutationChildren = zeros(length(parents),GenomeLength);
for i=1:length(parents)
child = thisPopulation(parents(i),:);
% Each element of the genome has mutationRate chance of being mutated.
mutationPoints = find(rand(1,length(child)) < mutationRate);
% each gene is replaced with a value chosen randomly from the range.
range = options.PopInitRange;
% range can have one column or one for each gene.
[r,c] = size(range);
if(c ~= 1)
range = range(:,mutationPoints);
end
lower = range(1,:);
upper = range(2,:);
span = upper - lower;
while 1
child(mutationPoints) = lower + rand(1,length(mutationPoints)) .* span;
child=floor(child);
if size(child)==size(unique(child))
break
end
end
mutationChildren(i,:) = child;
end
elseif(strcmpi(options.PopulationType,'bitString'))
mutationChildren = zeros(length(parents),GenomeLength);
for i=1:length(parents)
child = thisPopulation(parents(i),:);
mutationPoints = find(rand(1,length(child)) < mutationRate);
child(mutationPoints) = ~child(mutationPoints);
mutationChildren(i,:) = child;
end
end