中间重组与线性重组相似,然而recint对没对值使用了一新的Alpha值一起重组,而reclin对每队双亲使用一Alpha因子。
function NewChrom = reclin(OldChrom, XOVR);
% Identify the population size (Nind) and the number of variables (Nvar)
[Nind,Nvar] = size(OldChrom);
% Identify the number of matings
Xops = floor(Nind/2);% 向左取整
% Performs recombination
odd = 1:2:Nind-1;% 冒号运算符可以指定增量步长值,(first:step:last),1:2:Nind-1将产生1,3,5等等
even= 2:2:Nind;
% position of value of offspring compared to parents
Alpha = -0.25 + 1.5 * rand(Xops,1);%rand生成0~1之间均匀分布的随机数
Alpha = Alpha(1:Xops,ones(Nvar,1));%ones创建m*n的全1矩阵,%每列的Alpha值相同相当于,复制Nvar个Alpha
%交配的对是有序的,奇数行与它下一个偶行配对。
% recombination
NewChrom(odd,:) = OldChrom(odd,:) + Alpha .* (OldChrom(even,:) - OldChrom(odd,:));
% the same ones more for second half of offspring
Alpha = -0.25 + 1.5 * rand(Xops,1);
Alpha = Alpha(1:Xops,ones(Nvar,1));
NewChrom(even,:) = OldChrom(odd,:) + Alpha .* (OldChrom(even,:) - OldChrom(odd,:));
%最后一个奇数行不参与交配,直接加入到NewChrom的末尾
%余项函数,取余rem(10,3)=1
% 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
% End of function