-
Notifications
You must be signed in to change notification settings - Fork 33
/
recombin.m
65 lines (57 loc) · 2.45 KB
/
recombin.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
% RECOMBIN.M (RECOMBINation high-level function)
%
% This function performs recombination between pairs of individuals
% and returns the new individuals after mating. The function handles
% multiple populations and calls the low-level recombination function
% for the actual recombination process.
%
% Syntax: NewChrom = recombin(REC_F, OldChrom, RecOpt, SUBPOP)
%
% Input parameters:
% REC_F - String containing the name of the recombination or
% crossover function
% Chrom - Matrix containing the chromosomes of the old
% population. Each line corresponds to one individual
% RecOpt - (optional) Scalar containing the probability of
% recombination/crossover occurring between pairs
% of individuals.
% if omitted or NaN, 1 is assumed
% SUBPOP - (optional) Number of subpopulations
% if omitted or NaN, 1 subpopulation is assumed
%
% Output parameter:
% NewChrom - Matrix containing the chromosomes of the population
% after recombination in the same format as OldChrom.
%
% Author: Hartmut Pohlheim
% History: 18.03.94 file created
% 22.01.03 tested under MATLAB v6 by Alex Shenfield
% (NOTE : doesn't work with low level recmut.m)
function NewChrom = recombin(REC_F, Chrom, RecOpt, SUBPOP);
% Check parameter consistency
if nargin < 2, error('Not enough input parameter'); end
% Identify the population size (Nind)
[Nind,Nvar] = size(Chrom);
if nargin < 4, SUBPOP = 1; end
if nargin > 3,
if isempty(SUBPOP), SUBPOP = 1;
elseif isnan(SUBPOP), SUBPOP = 1;
elseif length(SUBPOP) ~= 1, error('SUBPOP must be a scalar'); end
end
if (Nind/SUBPOP) ~= fix(Nind/SUBPOP), error('Chrom and SUBPOP disagree'); end
Nind = Nind/SUBPOP; % Compute number of individuals per subpopulation
if nargin < 3, RecOpt = 0.7; end
if nargin > 2,
if isempty(RecOpt), RecOpt = 0.7;
elseif isnan(RecOpt), RecOpt = 0.7;
elseif length(RecOpt) ~= 1, error('RecOpt must be a scalar');
elseif (RecOpt < 0 | RecOpt > 1), error('RecOpt must be a scalar in [0, 1]'); end
end
% Select individuals of one subpopulation and call low level function
NewChrom = [];
for irun = 1:SUBPOP,
ChromSub = Chrom((irun-1)*Nind+1:irun*Nind,:);
NewChromSub = feval(REC_F, ChromSub, RecOpt);
NewChrom=[NewChrom; NewChromSub];
end
% End of function