函数最值问题:F=X2+Y2-Z2,clearclc%%初始化pc=0.9;%交叉概率pm=0.05;%变异概率popsize=500;chromlength1=21;chromlength2=23;chromlength3=20;chromlength=chromlength1+chromlength2+chromlength3;pop=initpop(popsize,chromlength);%产生初始种群fori=1:500[objvalue]=calobjvalue(pop);%计算目标函数值[fitvalue]=calfitvalue(objvalue);%计算个体适应度[newpop]=selection(pop,fitvalue);%选择[newpop1]=crossover(newpop,pc);%交叉[newpop2]=mutation(newpop1,pm);%变异[newobjvalue]=newcalobjvalue(newpop2);%计算最新代目标函数值[newfitvalue]=newcalfitvalue(newobjvalue);%计算新种群适应度值[bestindividual,bestfit]=best(newpop2,newfitvalue);%求出群体中适应值最大的个体及其适应值y(i)=max(bestfit);%储存最优个体适应值pop5=bestindividual;%储存最优个体n(i)=i;%记录最优代位置%解码x1(i)=0+decodechrom(pop5,1,21)*2/(pow2(21)-1);x2(i)=decodechrom(pop5,22,23)*6/(pow2(23)-1)-1;x3(i)=decodechrom(pop5,45,20)*1/(pow2(20)-1);pop=newpop2;end%%绘图figure(1)%最优点变化趋势图i=1:500;plot(y(i),'-b*')xlabel('迭代次数');ylabel('最优个体适应值');title('最优点变化趋势');legend('最优点');gridon[zindex]=max(y);%计算最大值及其位置PO=n(index)%最优个体的位置X=x1(index)Y=x2(index)Z=x3(index)F=zfunction[bestindividual,bestfit]=best(newpop2,newfitvalue)%求出群体中最大得适应值及其个体%遗传算法子程序%Name:best.m[px,py]=size(newpop2);bestindividual=newpop2(1,:);bestfit=newfitvalue(1);fori=2:pxifnewfitvalue(i)bestfitbestindividual=newpop2(i,:);bestfit=newfitvalue(i);endendfunction[fitvalue]=calfitvalue(objvalue)%计算个体的适应值%遗传算法子程序%Name:calfitvalue.mfitvalue=objvalue;function[objvalue]=calobjvalue(pop)%计算目标函数值%遗传算法子程序%Name:calobjvalue.mtemp1=decodechrom(pop,1,21);%将pop每行转化成十进制数相当于X'temp2=decodechrom(pop,22,23);temp3=decodechrom(pop,45,20);x1=temp1*2/(pow2(21)-1);%将二值域中的数转化为变量域的数相当于十进制的Xx2=temp2*6/(pow2(23)-1)-1;x3=temp3*1/(pow2(20)-1);objvalue=x1.^2+x2.^2-x3.^2;%计算目标函数值function[newpop1]=crossover(newpop,pc)%交叉%遗传算法子程序%Name:crossover.m[px,py]=size(newpop);newpop1=zeros(size(newpop));fori=1:2:px-1po=rand(1);ifpopccpoint=round(rand*py);%随机寻找交叉点newpop1(i,:)=[newpop(i+1,1:cpoint),newpop(i,cpoint+1:py)];%相邻两个染色体在交叉点位置交叉newpop1(i+1,:)=[newpop(i,1:cpoint),newpop(i+1,cpoint+1:py)];elsenewpop1(i,:)=newpop(i,:);%不产生新染色体newpop1(i+1,:)=newpop(i+1,:);endendfunctionpop2=decodebinary(pop)%将二进制数转化为十进制数(1)%遗传算法子程序%Name:decodebinary.m%产生[2^n2^(n-1)...1]的行向量,然后求和,将二进制转化为十进制[px,py]=size(pop);%求pop行和列数fori=1:pypop1(:,i)=2.^(py-i).*pop(:,i);endpop2=sum(pop1,2);%求pop1的每行之和functionpop2=decodechrom(pop,spoint,length)%decodechrom.m函数的功能是将染色体(或二进制编码)转换为十进制,参数spoint表示待解码的二进制串的起始位置%(对于多个变量而言,如有两个变量,采用20为表示,每个变量10为,则第一个变量从1开始,另一个变量从11开始。)%参数1ength表示所截取的长度。%Name:decodechrom.mpop1=pop(:,spoint:spoint+length-1);pop2=decodebinary(pop1);functionpop=initpop(popsize,chromlength)%初始化(编码)%initpop.m函数的功能是实现群体的初始化,popsize表示群体的大小,chromlength表示染色体的长度(二值数的长度),%长度大小取决于变量的二进制编码的长度。%遗传算法子程序%Name:initpop.mpop=round(rand(popsize,chromlength));%rand随机产生每个单元为{0,1}行数为popsize,列数为chromlength的矩阵,%round对矩阵的每个单元进行圆整。这样产生的初始种群。function[newpop2]=mutation(newpop1,pm)%变异%Name:mutation.m[px,py]=size(newpop1);newpop2=zeros(px,py);fori=1:pxps=rand;ifpspmmpoint=round(rand*py);ifmpoint=0mpoint=1;endifnewpop1(i,mpoint)==0newpop1(i,mpoint)=1;elsenewpop1(i,mpoint)=0;endelseendendnewpop2=newpop1;function[newpop2]=mutation(newpop1,pm)%变异%Name:mutation.m[px,py]=size(newpop1);newpop2=zeros(px,py);fori=1:pxps=rand;ifpspmmpoint=round(rand*py);ifmpoint=0mpoint=1;endifnewpop1(i,mpoint)==0newpop1(i,mpoint)=1;elsenewpop1(i,mpoint)=0;endelseendendnewpop2=newpop1;function[newobjvalue]=newcalobjvalue(newpop2)%计算目标函数值,最新代%遗传算法子程序%Name:newcalobjvalue.mtemp1=decodechrom(newpop2,1,21);%将pop每行转化成十进制数相当于X'temp2=decodechrom(newpop2,22,23);temp3=decodechrom(newpop2,45,20);x1=temp1*2/(pow2(21)-1);%将二值域中的数转化为变量域的数相当于十进制的Xx2=temp2*6/(pow2(23)-1)-1;x3=temp3*1/(pow2(20)-1);newobjvalue=x1.^2+x2.^2-x3.^2;%计算目标函数值function[newpop]=selection(pop,fitvalue)%选择操作objvalue=calobjvalue(pop);fitvalue=calfitvalue(objvalue);totalfit=sum(fitvalue);%求适应度值之和pfitvalue=fitvalue/totalfit;%单个个体被选择的概率ifpfitvalue0pfitvalue==0;endmfitvalue=cumsum(pfitvalue);%如fitvalue=[1234],则cumsum(fitvalue)=[13610][px,py]=size(pop);ms=sort(rand(px,1));%从小到大排列fitin=1;newin=1;newpop=zeros(px,py);whilenewin=pxifmfitvalue(fitin)ms(newin)newpop(newin,:)=pop(fitin,:);newin=newin+1;elsefitin=fitin+1;endend