% 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)
%语法:新种群=mut(当前种群,变异概率,染色体个体元素的变异的基本字符)
%注意:变异概率省略时为0.7/Lind(Lind为染色体长度),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.
%变异概率-变异概率(标量)。
假定如果省略时,其默认值为0.7/Lind(Lind是染色体长度)%
% 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
%
% Tested under MATLAB v6 by Alex Shenfield (21-Jan-03)
%举例说明该函数,利用OldChrom=crtbp(5,5)得到OldChrom=
1 1 0 0 1
0 0 0 0 0
0 1 0 1 1
0 1 0 0 0
0 0 1 1 0
function NewChrom = mut(OldChrom,Pm,BaseV)
%新种群=mut(当前种群,变异概率,染色体个体元素的变异的基本字符).
% get population size (Nind) and chromosome length (Lind)
%得到个体数(Nind)和染色体长度(Lind)。
[Nind, Lind] = size(OldChrom) ; %返回值为Nind=5,Lind=5
% check input parameters
%检查输入参数
if nargin < 2, Pm = 0.7/Lind ; end
if isnan(Pm), Pm = 0.7/Lind; end
%上面2个if条件用来确定Pm的值。
%第一个if:如果输入参数个数小于2,返回Pm为0.7/Lind。
%第二个if:如果输入的Pm不是数,返回Pm为0.7/Lind。
%注:nargin是用来判断输入变量个数的函数。
Isnan的函数功能:判断函数组的元素是否是%NaN(Not a Number)。
例如输入isnan(NaN),返回值为1;输入isnan(3),返回值为0.
if (nargin < 3), BaseV = crtbase(Lind); end
if (isnan(BaseV)), BaseV = crtbase(Lind); end
if (isempty(BaseV)), BaseV = crtbase(Lind); end
%上面的3个if条件用来确定BaseV的值。
%第一个if:如果输入参数个数小于3,则执行BaseV= crtbase(Lind)的命令,并返回BaseV %的值。
%第二个if:如果输入的BaseV不是数(如:NaN),则也执行BaseV = crtbase(Lind)的命令,%并返回BaseV的值。
%第三个if:如果输入的BaseV为空(如:[]),则也执行BaseV = crtbase(Lind)的命令,并返%回BaseV的值。
%注:isempty的函数功能:判断一个数组是否为空。
如果为空,返回值为1;如果非空,返%回值为0.例如输入isempty([]),返回值为1,;输入isempty(1),返回值为0。
if (nargin == 3) & (Lind ~= length(BaseV))
error('OldChrom and BaseV are incompatible'), end
%如果输入参数个数为3,而且矩阵列数不等于BaseV的长度,则会出现‘当前种群和基本%字符不匹配’的错误
% create mutation mask matrix
%创建突变掩模矩阵
BaseM = BaseV(ones(Nind,1),:) ;
%ones(5,1)=
1
1
1
1
1
%由BaseM = BaseV(ones(Nind,1),:) 得到BaseM=
BaseM=
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
2 2 2 2 2
% perform mutation on chromosome structure
%在染色体结构上执行突变
NewChrom = rem(OldChrom+(rand(Nind,Lind)<Pm).*ceil(rand(Nind,Lind).*(BaseM-1)),BaseM);
%分布计算上式如下rand(5,5)=
0.9631 0.6241 0.0377 0.2619 0.1068
0.5468 0.6791 0.8852 0.3354 0.6538
0.5211 0.3955 0.9133 0.6797 0.4942
0.2316 0.3674 0.7962 0.1366 0.7791
0.4889 0.9880 0.0987 0.7212 0.7150
%注:rand(m,n)是返回一个m行n列的随机矩阵,其中每个元素都小于1且大于0。
% rand(Nind,Lind)<Pm=
0 0 1 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
% ceil(rand(Nind,Lind)=
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
%注:ceil函数功能是把矩阵中所有元素取整,且不小于原来元素的最小整数。
%BaseM-1=
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
1 1 1 1 1
%由上可知rand(Nind,Lind)<Pm).*ceil(rand(Nind,Lind).*(BaseM-1)=
0 0 1 0 1
0 0 0 0 0
0 0 0 0 0
0 0 0 1 0
0 0 1 0 0
%故有
NewChrom = rem(OldChrom+(rand(Nind,Lind)<Pm).*ceil(rand(Nind,Lind).*(BaseM-1)),BaseM)=
1 1 1 0 0
0 0 0 0 0
0 1 0 1 1
0 1 0 1 0
0 0 0 1 0
%注:rem在上面运算中的函数功能是:2个相同结构的矩阵取模,即其中每一个元素对应取模。