xovmp.m

来自「matlab遗传算法工具箱」· M 代码 · 共 84 行

M
84
字号
% 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-94function 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; endif nargin < 4, Rs = 0; endif nargin < 3, Npt = 0; Rs = 0; endif nargin < 2, Px = 0.7; Npt = 0; Rs = 0; endif isnan(Px), Px = 0.7; endif isnan(Npt), Npt = 0; endif isnan(Rs), Rs = 0; endif isempty(Px), Px = 0.7; endif isempty(Npt), Npt = 0; endif isempty(Rs), Rs = 0; endXops = floor(Nind/2);DoCross = rand(Xops,1) < Px;odd = 1:2:Nind-1;even = 2:2:Nind;% Compute the effective length of each chromosome pairMask = ~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));endxsites(:,2) = rem(xsites + ceil((Mask(:, Lind)-1) .* rand(Xops, 1)) ...                                .* DoCross - 1 , Mask(:, Lind) )+1;% Express cross sites in terms of a 0-1 maskMask = (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));        endend% Perform crossoverNewChrom(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 populationif rem(Nind,2),  NewChrom(Nind,:)=OldChrom(Nind,:);endif ~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));        endend

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?