figure(1);fplot('variable.*sin(10*pi*variable)+2.0',[-1,2]); %画出函数曲线%定义遗传算法参数NIND=40; %个体数目(Number of individuals)MAXGEN=25; %最大遗传代数(Maximum number of generations)PRECI=20; %变量的二进制位数(Precision of variables)GGAP=0.9; %代沟(Generation gap)trace=zeros(2, MAXGEN); %寻优结果的初始值FieldD=[20;-1;2;1;0;1;1]; %区域描述器(Build field descriptor)Chrom=crtbp(NIND, PRECI); %初始种群gen=0; %代计数器variable=bs2rv(Chrom, FieldD); %计算初始种群的十进制转换ObjV=variable.*sin(10*pi*variable)+2.0; %计算目标函数值while gen<MAXGEN FitnV=ranking(-ObjV); %分配适应度值(Assign fitness values) SelCh=select('sus', Chrom, FitnV, GGAP); %选择 SelCh=recombin('xovsp', SelCh, 0.7); %重组 SelCh=mut(SelCh); %变异 variable=bs2rv(SelCh, FieldD); %子代个体的十进制转换 ObjVSel=variable.*sin(10*pi*variable)+2.0; %计算子代的目标函数值 [Chrom ObjV]=reins(Chrom, SelCh, 1, 1, ObjV, ObjVSel); %重插入子代的新种群 variable=bs2rv(Chrom, FieldD); gen=gen+1; %代计数器增加 %输出最优解及其序号,并在目标函数图像中标出,Y为最优解,I为种群的序号 [Y, I]=max(ObjV);hold on; plot(variable(I), Y, 'bo'); trace(1, gen)=max(ObjV); %遗传算法性能跟踪 trace(2, gen)=sum(ObjV)/length(ObjV);endvariable=bs2rv(Chrom, FieldD); %最优个体的十进制转换hold on grid;plot(variable,ObjV,'b*');figure(2);plot(trace(1,:));hold on;plot(trace(2,:),'-.');gridlegend('解的变化','种群均值的变化')
CRTBP.m - Create an initial population This function creates a binary population of given size and structure. Syntax: [Chrom Lind BaseV] = crtbp(Nind, Lind, Base) Input Parameters: Nind - Either a scalar containing the number of individuals in the new population or a row vector of length two containing the number of individuals and their length. Lind - A scalar containing the length of the individual chromosomes. Base - A scalar containing the base of the chromosome elements or a row vector containing the base(s) of the loci of the chromosomes. Output Parameters: Chrom - A matrix containing the random valued chromosomes row wise. Lind - A scalar containing the length of the chromosome. BaseV - A row vector containing the base of the chromosome loci. Author: Andrew Chipperfield Date: 19-Jan-94 Tested under MATLAB v6 by Alex Shenfield (20-Jan-03)
BS2RV.m - Binary string to real vector This function decodes binary chromosomes into vectors of reals. The chromosomes are seen as the concatenation of binary strings of given length, and decoded into real numbers in a specified interval using either standard binary or Gray decoding. Syntax: Phen = bs2rv(Chrom,FieldD) Input parameters: Chrom - Matrix containing the chromosomes of the current population. Each line corresponds to one individual's concatenated binary string representation. Leftmost bits are MSb and rightmost are LSb. FieldD - Matrix describing the length and how to decode each substring in the chromosome. It has the following structure: [len; (num) lb; (num) ub; (num) code; (0=binary | 1=gray) scale; (0=arithmetic | 1=logarithmic) lbin; (0=excluded | 1=included) ubin]; (0=excluded | 1=included) where len - row vector containing the length of each substring in Chrom. sum(len) should equal the individual length. lb, ub - Lower and upper bounds for each variable. code - binary row vector indicating how each substring is to be decoded. scale - binary row vector indicating where to use arithmetic and/or logarithmic scaling. lbin, ubin - binary row vectors indicating whether or not to include each bound in the representation range Output parameter: Phen - Real matrix containing the population phenotypes. Author: Carlos Fonseca, Updated: Andrew Chipperfield, Date: 08/06/93, Date: 26-Jan-94, Tested under MATLAB v6 by Alex Shenfield (17-Jan-03) RANKING.M (RANK-based fitness assignment) This function performs ranking of individuals. Syntax: FitnV = ranking(ObjV, RFun, SUBPOP) This function ranks individuals represented by their associated cost, to be * minimized*, and returns a column vector FitnV containing the corresponding individual fitnesses. For multiple subpopulations the ranking is performed separately for each subpopulation. Input parameters: ObjV - Column vector containing the objective values of the individuals in the current population (cost values). RFun - (optional) If RFun is a scalar in [1, 2] linear ranking is assumed and the scalar indicates the selective pressure. If RFun is a 2 element vector: RFun(1): SP - scalar indicating the selective pressure RFun(2): RM - ranking method RM = 0: linear ranking RM = 1: non-linear ranking If RFun is a vector with length(Rfun) > 2 it contains the fitness to be assigned to each rank. It should have the same length as ObjV. Usually RFun is monotonously increasing. If RFun is omitted or NaN, linear ranking and a selective pressure of 2 are assumed. SUBPOP - (optional) Number of subpopulations if omitted or NaN, 1 subpopulation is assumed Output parameters: FitnV - Column vector containing the fitness values of the individuals in the current population. Author: Hartmut Pohlheim (Carlos Fonseca) History: 01.03.94 non-linear ranking 10.03.94 multiple populations 21.01.03 updated for MATLAB v6 by Alex Shenfield SELECT.M (universal SELECTion) This function performs universal selection. The function handles multiple populations and calls the low level selection function for the actual selection process. Syntax: SelCh = select(SEL_F, Chrom, FitnV, GGAP, SUBPOP) Input parameters: SEL_F - Name of the selection function Chrom - Matrix containing the individuals (parents) of the current population. Each row corresponds to one individual. FitnV - Column vector containing the fitness values of the individuals in the population. GGAP - (optional) Rate of individuals to be selected if omitted 1.0 is assumed SUBPOP - (optional) Number of subpopulations if omitted 1 subpopulation is assumed Output parameters: SelCh - Matrix containing the selected individuals. Author: Hartmut Pohlheim History: 10.03.94 file created 22.01.03 tested under MATLAB v6 by Alex Shenfield 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) 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 Tested under MATLAB v6 by Alex Shenfield (21-Jan-03) REINS.M (RE-INSertion of offspring in population replacing parents) This function reinserts offspring in the population. Syntax: [Chrom, ObjVCh] = reins(Chrom, SelCh, SUBPOP, InsOpt, ObjVCh, ObjVSel) Input parameters: Chrom - Matrix containing the individuals (parents) of the current population. Each row corresponds to one individual. SelCh - Matrix containing the offspring of the current population. Each row corresponds to one individual. SUBPOP - (optional) Number of subpopulations if omitted or NaN, 1 subpopulation is assumed InsOpt - (optional) Vector containing the insertion method parameters ExOpt(1): Select - number indicating kind of insertion 0 - uniform insertion 1 - fitness-based insertion if omitted or NaN, 0 is assumed ExOpt(2): INSR - Rate of offspring to be inserted per subpopulation (% of subpopulation) if omitted or NaN, 1.0 (100%) is assumed ObjVCh - (optional) Column vector containing the objective values of the individuals (parents - Chrom) in the current population, needed for fitness-based insertion saves recalculation of objective values for population ObjVSel - (optional) Column vector containing the objective values of the offspring (SelCh) in the current population, needed for partial insertion of offspring, saves recalculation of objective values for population Output parameters: Chrom - Matrix containing the individuals of the current population after reinsertion. ObjVCh - if ObjVCh and ObjVSel are input parameters, then column vector containing the objective values of the individuals of the current generation after reinsertion. Author: Hartmut Pohlheim History: 10.03.94 file created 19.03.94 parameter checking improved 26.01.03 tested under MATLAB v6 by Alex Shenfield