上海网站建设服务市价,金方时代网站建设,自适应网站制作教程,公司管理流程图大全二元函数为yx1^2x2^2,x∈[-5,5]NIND121; %初始种群的个数(Number of individuals)NVAR2; %一个染色体(个体)有多少基因PRECI20; %变量的二进制位数(Precision of variables)MAXGEN200; %最大遗传代数(Maximum number of generations)GGAP0.8; %代沟(Generation gap)#xff0c…二元函数为yx1^2x2^2,x∈[-5,5]NIND121; %初始种群的个数(Number of individuals)NVAR2; %一个染色体(个体)有多少基因PRECI20; %变量的二进制位数(Precision of variables)MAXGEN200; %最大遗传代数(Maximum number of generations)GGAP0.8; %代沟(Generation gap)以一定概率选择父代遗传到下一代tracezeros(MAXGEN,2); %寻优结果的初始值Chromcrtbp(NIND,PRECI*NVAR); %初始种群%区域描述器(Build field descriptor)%确定每个变量的二进制位数取值范围及取值范围是否包括边界等。FieldD[rep([PRECI],[1,NVAR]);rep([-5;5][1,NVAR]);rep([1;0;1;1],[1,NVAR])];Objvobjfun(bs2rv(Chrom,FieldD))gen1; %代计数器while genMAXGENFitvranking(Objv); %分配适应度值(Assign fitness values)SelChselect(sus,Chrom,Fitv,GGAP); %选择SelChrecombin(xovsp,SelCh,1); %重组SelChmut(SelCh); %变异ObjVSelobjfun(bs2rv(SelCh,FieldD));%子代个体的十进制转换%重插入子代的新种群[Chrom,Objv]reins(Chrom,SelCh,1,1,Objv,ObjVSel);trace(gen,1)min(Objv); %遗传算法性能跟踪trace(gen,2)sum(Objv)/length(Objv);gengen1; %代计数器增加endplot(trace(:,1));hold onplot(trace(:,2),.)gridlegend(最优解的变化,解的平均值的变化)根据上面的求解模型可以写出模型的.M文件如下即适应度函数% OBJFUN.M% Syntax: ObjVal  objfun1(Chrom,rtn_type)%% Input parameters:% Chrom - Matrix containing the chromosomes of the current% population. Each row corresponds to one individuals% string representation.% if Chrom  [], then special values will be returned% rtn_type - if Chrom  [] and% rtn_type  1 (or []) return boundaries% rtn_type  2 return title% rtn_type  3 return value of global minimum%% Output parameters:% ObjVal - Column vector containing the objective values of the% individuals in the current population.% if called with Chrom  [], then ObjVal contains% rtn_type  1, matrix with the boundaries of the function% rtn_type  2, text for the title of the graphic output% rtn_type  3, value of global minimum% Author: YQ_youngerfunction ObjVal  objfun(Chrom,rtn_type);% Dimension of objective functionDim  2;% Compute population parameters[Nind,Nvar]  size(Chrom);% Check size of Chrom and do the appropriate thing% if Chrom is [], then define size of boundary-matrix and valuesif Nind  0% return text of title for graphic outputif rtn_type  2ObjVal  [DE JONG function 1- int2str(Dim)];% return value of global minimumelseif rtn_type  3ObjVal  0;% define size of boundary-matrix and valueselse% lower and upper bound, identical for all n variablesObjVal  1*[-5; 5];ObjVal  ObjVal(1:2,ones(Dim,1));end% if Dim variables, compute values of functionelseif Nvar  Dim% function 1, sum of xi^2 for i  1:Dim (Dim30)% n  Dim, -5  xi  5% global minimum at (xi)(0) ; fmin0ObjVal  sum((Chrom .* Chrom));% ObjVal  diag(Chrom * Chrom); % both lines produce the same% otherwise error, wrong format of Chromelseerror(size of matrix Chrom is not correct for function evaluation);end% End of function注释种群表示和初始化函数 bs2rv:二进制串到实值的转换Phenbs2rv(Chrom,FieldD) FieldD[len, lb, ub, code, scale, lbin, ubin]code(i)1为标准的二进制编码code(i)0为格雷编码scale(i)0为算术刻度scale(i)1为对数刻度函数 crtbp:创建初始种群[Chrom,Lind,BaseV]crtbp(Nind,Lind)[Chrom,Lind,BaseV]crtbp(Nind,BaseV)[Chrom,Lind,BaseV]crtbp(Nind,Lind,BaseV)Nind指定种群中个体的数量Lind指定个体的长度函数 crtrp:创建实值原始种群Chromcrtrp(Nind,FieldDR)适应度计算函数 ranking:基于排序的适应度分配(此函数是从最小化方向对个体进行排序的)FitVranking(ObjV)FitVranking(ObjV, RFun)FitVranking(ObjV, RFun, SUBPOP)Rfun(1)线性排序标量在[1 2]间为非线性排序在[1 length(ObjV)-2]Rfun(2)指定排序方法0为线性排序1为非线性排序SUBPOP指明ObjV中子种群的数量默认为1选择高级函数 select:从种群中选择个体SelChselect(SEL_F, Chrom, FitnV)SelChselect(SEL_F, Chrom, FitnV, GGAP)SelChselect(SEL_F, Chrom, FitnV, GGAP, SUBPOP)SEL_F是一字符串为一低级选择函数名如rws或susGGAP指出了代沟默认为1也可大于1允许子代数多于父代的数量rws: 轮盘赌选择NewChrIxrws(FitnV, Nsel) 使用轮盘赌选择从一个种群中选择Nsel个个体NewChrIx 是为育种选择的个体的索引值sus:随机遍历抽样NewChrIxsus(FitnV, Nsel)交叉高级函数 recombin:重组个体NewChromrecombin(REC_F, Chrom)NewChromrecombin(REC_F, Chrom, RecOpt)NewChromrecombin(REC_F, Chrom, RecOpt, SUBPOP)REC_F是包含低级重组函数名的字符串例如recdis,recint,reclin,xovdp, xovdprs, xovmp, xovsh, xovshrs, xovsp, xovsprsrecdis:离散重组NewChromrecdis(OldChorm)recint:中间重组NewChromrecint(OldChorm)reclin:线性重组NewChromreclin(OldChorm)xovdp:两点交叉NewChromxovdp(OldChrom, XOVR)XOVR为交叉概率 默认为0.7Xovdprs:减少代理的两点交叉NewChromxovdprs(OldChrom, XOVR)Xovmp:多点交叉NewChromxovmp(OldChrom, XOVR, Npt, Rs)Npt指明交叉点数 0 洗牌交叉1 单点交叉2 两点交叉默认为0Rs指明使用减少代理 0 不减少代理1 减少代理默认为0Xovsh:洗牌交叉NewChromxovsh(OldChrom, XOVR)Xovshrs:减少代理的洗牌交叉NewChromxovshrs(OldChrom, XOVR)Xovsp:单点交叉NewChromxovsp(OldChrom, XOVR)Xovsprs:减少代理的单点交叉NewChromxovsprs(OldChrom, XOVR)变异高级函数 mutate:个体的变异NewChormmutate(MUT_F, OldChorm, FieldDR) NewChormmutate(MUT_F, OldChorm, FieldDR, MutOpt) NewChormmutate(MUT_F, OldChorm, FieldDR, MutOpt, SUBPOP) MUT_F为包含低级变异函数的字符串例如mut, mutbga, recmutmut:离散变异算子NewChrommut(OldChorm, Pm) NewChrommut(OldChorm, Pm, BaseV)Pm为变异概率默认为Pm0.7/Lindmutbga:实值种群的变异(遗传算法育种器的变异算子) NewChrommutbga(OldChorm, FieldDR)NewChrommubga(OldChorm, FieidDR, MutOpt)MutOpt(1)是在[ 0 1]间的重组概率的标量默认为1MutOpt(2)是在[0 1]间的压缩重组范围的标量默认为1(不压缩)recmut:具有突变特征的线性重组NewChromrecmut(OldChorm, FieldDR)NewChromrecmut(OldChorm, FieidDR, MutOpt)重插入函数 reins:重插入子群到种群Chormreins(Chorm, SelCh)Chormreins(Chorm, SelCh, SUBPOP)Chormreins(Chorm, SelCh, SUBPOP, InsOpt, ObjVch)[Chorm, ObjVch]reins(Chorm, SelCh, SUBPOP, InsOpt, ObjVch, ObjVSel)InsOpt(1)指明用子代代替父代的选择方法0为均匀选择1为基于适应度的选择默认为0InsOpt(2)指明在[0 1]间每个子种群中重插入的子代个体在整个子种群的中个体的比率默认为1ObjVch包含Chorm中个体的目标值对基于适应度的重插入是必需的ObjVSel包含Selch中个体的目标值如子代数量大于重插入种群的子代数量是必需的其他函数矩阵复试函数 rep:MatOutrep(MatIn, REPN)REPN为复制次数以上这篇使用遗传算法求二元函数的最小值就是小编分享给大家的全部内容了希望能给大家一个参考也希望大家多多支持脚本之家。