% 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
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
附录 七变异函数mut源代码 :(由谢菲尔德大学Andrew Chipperfield编写)
% MUT.m
%
% This function takes the representation of the current population,
% mutates each element with given probability and returns the resulting
% population.
%
% Syntax: NewChrom = mut(OldChrom,Pm,BaseV)
%
% Input parameters:
%
% OldChrom - A matrix containing the chromosomes of the
% current population. Each row corresponds to
% an individuals string representation.
%
% Pm - Mutation probability (scalar). Default value
% of Pm = 0.7/Lind, where Lind is the chromosome
% length is assumed if omitted.
%
% BaseV - Optional row vector of the same length as the
% chromosome structure defining the base of the
% individual elements of the chromosome. Binary
% representation is assumed if omitted.
%
% Output parameter:
%
% NewChrom - A Matrix containing a mutated version of
% OldChrom.
%
% Author: Andrew Chipperfield
% Date: 25-Jan-94
function NewChrom = mut(OldChrom,Pm,BaseV)
% get population size (Nind) and chromosome length (Lind)
[Nind, Lind] = size(OldChrom) ;
% check input parameters
if nargin < 2, Pm = 0.7/Lind ; end
if isnan(Pm), Pm = 0.7/Lind; end
if (nargin < 3), BaseV = crtbase(Lind); end
if (isnan(BaseV)), BaseV = crtbase(Lind); end
if (isempty(BaseV)), BaseV = crtbase(Lind); end
if (nargin == 3) & (Lind ~= length(BaseV))
error('OldChrom and BaseV are incompatible'), end
% create mutation mask matrix
BaseM = BaseV(ones(Nind,1),:) ;
% perform mutation on chromosome structure
NewChrom = rem(OldChrom+(rand(Nind,Lind)<Pm).*ceil(rand(Nind,Lind).*(BaseM-1)),BaseM);
附录 八基于遗传算法的最大类间方差法对JPG格式图像分割的程序源代码:
clear, close all
B=imread('she.jpg'); %读入原始jpg格式图像
figure(1);
imshow(B),title('原始jpg格式图像');
I1=rgb2gray(B); %将原图像转化为灰度图象
figure(2);
imshow(I1),title('灰度格式图像');
BW1 = edge(I1,'sobel');
BW2 = edge(I1,'canny');
figure(6),imshow(BW1),title('边缘检测1'); %边缘检测
figure(5), imshow(BW2),title('边缘检测2');
[I1,map1]=gray2ind(I1,255); %将灰度图像转化为索引图像
I1=double(I1); %将unit8数组转化为double型数组
Z=I1 %将double型数组I1转存到Z中
figure(3) %画出未进行分割的原始图像
image(Z),title('未进行分割的原始图像');colormap(map1);
NIND=40; %个体