-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mutate.m
65 lines (44 loc) · 1.27 KB
/
Mutate.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
function y=Mutate(x)
%Using Equal Probabilities
method = randi([1 3]);
switch method
case 1
y=DoSwap(x);
case 2
y=DoInsertion(x);
case 3
y=DoReversion(x);
end
end
function y=DoSwap(x)
n = numel(x);
i = randsample(n,2);
i1=i(1);
i2=i(2);
y = x;
y([i1 i2]) = y([i2 i1]);
end
function y=DoInsertion(x)
n = numel(x);
i = randsample(n,2);
i1=i(1);
i2=i(2);
if i1<i2
% 1 ... i1-1 i1 i1+1 ..... i2-1 i2 i2+1 ... n
% i1 is going to be inserted between i2 and i2+1
y = x([1:i1-1 i+1:i2 i1 i2+1:end]);
else
% i2 < i1
% 1 ... i2-1 i2 i2+1 ..... i1-1 i1 i1+1 ... n
% i1 is going to be inserted between i2 and i2+1
y = x([1:i2 i1 i2+1:i1-1 i1+1:end]);
end
end
function y=DoReversion(x)
n = numel(x);
i = randsample(n,2);
i1=min(i);
i2=max(i);
y = x;
y(i1:i2) = x(i2:-1:i1);
end