-
Notifications
You must be signed in to change notification settings - Fork 33
/
xovmp.m
87 lines (74 loc) · 2.72 KB
/
xovmp.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
% XOVMP.m Multi-point crossover
%
% Syntax: NewChrom = xovmp(OldChrom, Px, Npt, Rs)
%
% This function takes a matrix OldChrom containing the binary
% representation of the individuals in the current population,
% applies crossover to consecutive pairs of individuals with
% probability Px and returns the resulting population.
%
% Npt indicates how many crossover points to use (1 or 2, zero
% indicates shuffle crossover).
% Rs indicates whether or not to force the production of
% offspring different from their parents.
%
%
% Author: Carlos Fonseca, Updated: Andrew Chipperfield
% Date: 28/09/93, Date: 27-Jan-94
%
% tested under MATLAB v6 by Alex Shenfield (22-Jan-03)
function NewChrom = xovmp(OldChrom, Px, Npt, Rs);
% Identify the population size (Nind) and the chromosome length (Lind)
[Nind,Lind] = size(OldChrom);
if Lind < 2, NewChrom = OldChrom; return; end
if nargin < 4, Rs = 0; end
if nargin < 3, Npt = 0; Rs = 0; end
if nargin < 2, Px = 0.7; Npt = 0; Rs = 0; end
if isnan(Px), Px = 0.7; end
if isnan(Npt), Npt = 0; end
if isnan(Rs), Rs = 0; end
if isempty(Px), Px = 0.7; end
if isempty(Npt), Npt = 0; end
if isempty(Rs), Rs = 0; end
Xops = floor(Nind/2);
DoCross = rand(Xops,1) < Px;
odd = 1:2:Nind-1;
even = 2:2:Nind;
% Compute the effective length of each chromosome pair
Mask = ~Rs | (OldChrom(odd, :) ~= OldChrom(even, :));
Mask = cumsum(Mask')';
% Compute cross sites for each pair of individuals, according to their
% effective length and Px (two equal cross sites mean no crossover)
xsites(:, 1) = Mask(:, Lind);
if Npt >= 2,
xsites(:, 1) = ceil(xsites(:, 1) .* rand(Xops, 1));
end
xsites(:,2) = rem(xsites + ceil((Mask(:, Lind)-1) .* rand(Xops, 1)) ...
.* DoCross - 1 , Mask(:, Lind) )+1;
% Express cross sites in terms of a 0-1 mask
Mask = (xsites(:,ones(1,Lind)) < Mask) == ...
(xsites(:,2*ones(1,Lind)) < Mask);
if ~Npt,
shuff = rand(Lind,Xops);
[ans,shuff] = sort(shuff);
for i=1:Xops
OldChrom(odd(i),:)=OldChrom(odd(i),shuff(:,i));
OldChrom(even(i),:)=OldChrom(even(i),shuff(:,i));
end
end
% Perform crossover
NewChrom(odd,:) = (OldChrom(odd,:).* Mask) + (OldChrom(even,:).*(~Mask));
NewChrom(even,:) = (OldChrom(odd,:).*(~Mask)) + (OldChrom(even,:).*Mask);
% If the number of individuals is odd, the last individual cannot be mated
% but must be included in the new population
if rem(Nind,2),
NewChrom(Nind,:)=OldChrom(Nind,:);
end
if ~Npt,
[ans,unshuff] = sort(shuff);
for i=1:Xops
NewChrom(odd(i),:)=NewChrom(odd(i),unshuff(:,i));
NewChrom(even(i),:)=NewChrom(even(i),unshuff(:,i));
end
end
% end of function