MATLAB程序实现初始化:%初始化种群%pop_size:种群大小%chromo_size:染色体长度functioninitilize(pop_size,chromo_size)globalpop;fori=1:pop_sizeforj=1:chromo_sizepop(i,j)=round(rand);endendcleari;clearj;计算适应度:(该函数应该根据具体问题进行修改,这里优化的函数是前述的一维函数)%计算种群个体适应度,对不同的优化目标,此处需要改写%pop_size:种群大小%chromo_size:染色体长度functionfitness(pop_size,chromo_size)globalfitness_value;globalpop;globalG;fori=1:pop_sizefitness_value(i)=0.;endfori=1:pop_sizeforj=1:chromo_sizeifpop(i,j)==1fitness_value(i)=fitness_value(i)+2^(j-1);endendfitness_value(i)=-1+fitness_value(i)*(3.-(-1.))/(2^chromo_size-1);fitness_value(i)=-(fitness_value(i)-1).^2+4;endcleari;clearj;对个体按照适应度大小进行排序:%对个体按适应度大小进行排序,并且保存最佳个体%pop_size:种群大小%chromo_size:染色体长度functionrank(pop_size,chromo_size)globalfitness_value;globalfitness_table;globalfitness_avg;globalbest_fitness;globalbest_individual;globalbest_generation;globalpop;globalG;fori=1:pop_sizefitness_table(i)=0.;endmin=1;temp=1;temp1(chromo_size)=0;fori=1:pop_sizemin=i;forj=i+1:pop_sizeiffitness_value(j)fitness_value(min);min=j;endendifmin~=itemp=fitness_value(i);fitness_value(i)=fitness_value(min);fitness_value(min)=temp;fork=1:chromo_sizetemp1(k)=pop(i,k);pop(i,k)=pop(min,k);pop(min,k)=temp1(k);endendendfori=1:pop_sizeifi==1fitness_table(i)=fitness_table(i)+fitness_value(i);elsefitness_table(i)=fitness_table(i-1)+fitness_value(i);endendfitness_tablefitness_avg(G)=fitness_table(pop_size)/pop_size;iffitness_value(pop_size)best_fitnessbest_fitness=fitness_value(pop_size);best_generation=G;forj=1:chromo_sizebest_individual(j)=pop(pop_size,j);endendcleari;clearj;cleark;clearmin;cleartemp;cleartemp1;选择操作:%轮盘赌选择操作%pop_size:种群大小%chromo_size:染色体长度%cross_rate:是否精英选择functionselection(pop_size,chromo_size,elitism)globalpop;globalfitness_table;fori=1:pop_sizer=rand*fitness_table(pop_size);first=1;last=pop_size;mid=round((last+first)/2);idx=-1;while(first=last)&&(idx==-1)ifrfitness_table(mid)first=mid;elseifrfitness_table(mid)last=mid;elseidx=mid;break;endmid=round((last+first)/2);if(last-first)==1idx=last;break;endendforj=1:chromo_sizepop_new(i,j)=pop(idx,j);endendifelitismp=pop_size-1;elsep=pop_size;endfori=1:pforj=1:chromo_sizepop(i,j)=pop_new(i,j);endendcleari;clearj;clearpop_new;clearfirst;clearlast;clearidx;clearmid;交叉操作:%单点交叉操作%pop_size:种群大小%chromo_size:染色体长度%cross_rate:交叉概率functioncrossover(pop_size,chromo_size,cross_rate)globalpop;fori=1:2:pop_sizeif(randcross_rate)cross_pos=round(rand*chromo_size);ifor(cross_pos==0,cross_pos==1)continue;endforj=cross_pos:chromo_sizetemp=pop(i,j);pop(i,j)=pop(i+1,j);pop(i+1,j)=temp;endendendcleari;clearj;cleartemp;clearcross_pos;变异操作:%单点变异操作%pop_size:种群大小%chromo_size:染色体长度%cross_rate:变异概率functionmutation(pop_size,chromo_size,mutate_rate)globalpop;fori=1:pop_sizeifrandmutate_ratemutate_pos=round(rand*chromo_size);ifmutate_pos==0continue;endpop(i,mutate_pos)=1-pop(i,mutate_pos);endendcleari;clearmutate_pos;打印算法迭代过程:%打印算法迭代过程%generation_size:迭代次数functionplotGA(generation_size)globalfitness_avg;x=1:1:generation_size;y=fitness_avg;plot(x,y)算法主函数:%遗传算法主函数%pop_size:输入种群大小%chromo_size:输入染色体长度%generation_size:输入迭代次数%cross_rate:输入交叉概率%cross_rate:输入变异概率%elitism:输入是否精英选择%m:输出最佳个体%n:输出最佳适应度%p:输出最佳个体出现代%q:输出最佳个体自变量值function[m,n,p,q]=GeneticAlgorithm(pop_size,chromo_size,generation_size,cross_rate,mutate_rate,elitism)globalG;%当前代globalfitness_value;%当前代适应度矩阵globalbest_fitness;%历代最佳适应值globalfitness_avg;%历代平均适应值矩阵globalbest_individual;%历代最佳个体globalbest_generation;%最佳个体出现代fitness_avg=zeros(generation_size,1);disphheefitness_value(pop_size)=0.;best_fitness=0.;best_generation=0;initilize(pop_size,chromo_size);%初始化forG=1:generation_sizefitness(pop_size,chromo_size);%计算适应度rank(pop_size,chromo_size);%对个体按适应度大小进行排序selection(pop_size,chromo_size,elitism);%选择操作crossover(pop_size,chromo_size,cross_rate);%交叉操作mutation(pop_size,chromo_size,mutate_rate);%变异操作endplotGA(generation_size);%打印算法迭代过程m=best_individual;%获得最佳个体n=best_fitness;%获得最佳适应度p=best_generation;%获得最佳个体出现代%获得最佳个体变量值,对不同的优化目标,此处需要改写q=0.;forj=1:chromo_sizeifbest_individual(j)==1q=q+2^(j-1);endendq=-1+q*(3.-(-1.))/(2^chromo_size-1);cleari;clearj;2.案例研究对上一节中的函数进行优化,设置遗传算法相关参数,程序如下functionrun_ga()elitism=true;%选择精英操作pop_size=20;%种群大小chromo_size=16;%染色体大小generation_size=200;%迭代次数cross_rate=0.6;%交叉概率mutate_rate=0.01;%变异概率[m,n,p,q]=GeneticAlgorithm(pop_size,chromo_size,generation_size,cross_rate,mutate_rate,elitism);disp最优个体mdisp最优适应度ndisp最优个体对应自变量值qdisp得到最优结果的代数pclear;结果如下:最优个体m=1111111111111110最优适应度n=4.0000最优个体对应自变量值q=1.0000得到最优结果的代数p=74此结果非常准确。