您好,欢迎访问三七文档
%%GAclc%清屏clear%删除工作区间变量%%参数初始化popsize=100;lenchrom=2;pc=0.8;pm=0.03;maxgen=100;bound=[060;035];%%a矩阵,各区重心点坐标a=[13.9646.390;11.26514.095;9.75517.925;9.75022.868;10.01027.668;17.7606.682;19.87011.660;17.96018.880;17.39523.000;18.19229.131;23.3125.203;22.11412.617;23.67019.639;23.16723.413;22.80427.228;28.4007.940;28.87012.820;28.86618.521;28.29023.690;28.30427.915;33.97623.961;37.89716.702;39.27011.630;43.50014.700;39.8547.780;52.9309.170];%%计算第一个超市到全区距离和S1=0;form=1:26S1=S1+sqrt((18.450-a(m,1))^2+(17.420-a(m,2))^2);end%%产生初始粒子和速度fori=1:popsize%随机产生一个种群GApop(i,:)=Code(lenchrom,bound);%计算适应度fitness(i)=fun(GApop(i,:),a);end%找最好的染色体[bestfitnessbestindex]=min(fitness);zbest=GApop(bestindex,:);gbest=GApop;fitnessgbest=fitness;fitnesszbest=bestfitness;%%迭代寻优fori=1:maxgeni;%种群更新GAGApop=Select(GApop,fitness,popsize);%交叉操作GAGApop=Cross(pc,lenchrom,GApop,popsize,bound);%%GAclc%清屏clear%删除workplace变量%%参数初始化popsize=100;%种群规模lenchrom=2;%变量字串长度pc=0.8;pm=0.03;maxgen=100;%种群bound=[060;035];%%a矩阵,各区重心点坐标a=[13.9646.390;11.26514.095;9.75517.925;9.75022.868;10.01027.668;17.7606.682;19.87011.660;17.96018.880;17.39523.000;18.19229.131;23.3125.203;22.11412.617;23.67019.639;23.16723.413;22.80427.228;28.4007.940;28.87012.820;28.86618.521;28.29023.690;28.30427.915;33.97623.961;37.89716.702;39.27011.630;43.50014.700;39.8547.780;52.9309.170];%%计算第一个超市到全区距离和Pz2=0;form=1:26Pz2=Pz2+55/((18.450-a(m,1))^2+(17.420-a(m,2))^2);end%%产生初始粒子和速度fori=1:popsize%随机产生一个种群GApop(i,:)=Code(lenchrom,bound);%计算适应度fitness(i)=fun(GApop(i,:),a);end%找最好的染色体[bestfitnessbestindex]=max(fitness);zbest=GApop(bestindex,:);gbest=GApop;fitnessgbest=fitness;fitnesszbest=bestfitness;%%迭代寻优fori=1:maxgeni;%种群更新GAGApop=Select(GApop,fitness,popsize);%交叉操作GAGApop=Cross(pc,lenchrom,GApop,popsize,bound);%变异操作GAGApop=Mutation(pm,lenchrom,GApop,popsize,[imaxgen],bound);pop=GApop;forj=1:popsizePz1=fun(pop(j,:),a);ifPz2Pz1fitness(j)=fun(pop(j,:),a);end%个体最优更新iffitness(j)fitnessgbest(j)gbest(j,:)=pop(j,:);fitnessgbest(j)=fitness(j);end%群体最优更新iffitness(j)fitnesszbestzbest=pop(j,:);fitnesszbest=fitness(j);endendyy(i)=fitnesszbest;end%%结果disp'*************bestcoordinate***********'zbestplot(yy,'linewidth',2);title(['适应度曲线''终止代数='num2str(maxgen)]);xlabel('进化代数');ylabel('适应度');gridon%%适应度函数functionPz1=fun(x,a)Pz1=0;form=1:26Pz1=Pz1+33/((x(1)-a(m,1))^2+(x(2)-a(m,2))^2);endend%染色体编码算法如下:functionret=Code(lenchrom,bound)%本函数将变量编码成染色体,用于随机初始化一个种群%lenchrominput:染色体长度%boundinput:变量的取值范围%retoutput:染色体的编码值flag=0;whileflag==0pick=rand(1,lenchrom);ret=bound(:,1)'+(bound(:,2)-bound(:,1))'.*pick;flag=test(lenchrom,bound,ret);end%选择算子程序如下:functionret=Select(individuals,fitness,sizepop)%本函数对每一代种群中的染色体进行选择,以进行后面的交叉和变异%individualsinput:种群信息%fitnessinput:适应度%sizepopinput:种群规模%optsinput:选择方法的选择%retoutput:经过选择后的种群fitness=1./(fitness);sumfitness=sum(fitness);sumf=fitness./sumfitness;index=[];fori=1:sizepoppick=rand;whilepick==0pick=rand;endforj=1:sizepoppick=pick-sumf(j);ifpick0index=[indexj];break;endendendindividuals=individuals(index,:);fitness=fitness(index);ret=individuals;%交叉算子MATLAB程序如下;functionret=Cross(pcross,lenchrom,chrom,sizepop,bound)%本函数完成交叉操作%pcrossinput:交叉概率%lenchrominput:染色体的长度%chrominput:染色体群%sizepopinput:种群规模%retoutput:交叉后的染色体fori=1:sizepop%随机选择两个染色体进行交叉pick=rand(1,2);whileprod(pick)==0pick=rand(1,2);endindex=ceil(pick.*sizepop);%交叉概率决定是否进行交叉pick=rand;whilepick==0pick=rand;endifpickpcrosscontinue;endflag=0;whileflag==0%随机选择交叉位置pick=rand;whilepick==0pick=rand;endpos=ceil(pick.*sum(lenchrom));pick=rand;%交叉开始v1=chrom(index(1),pos)v2=chrom(index(2),pos)chrom(index(1),pos)=pick*v2+(1-pick)*v1;chrom(index(2),pos)=pick*v1+(1-pick)*v2;flag1=test(lenchrom,bound,chrom(index(1),:));flag2=test(lenchrom,bound,chrom(index(2),:));ifflag1*flag2==0flag=0;elseflag=1;endendendret=chrom;%检验染色体可行性程序如下:functionflag=test(lenchrom,bound,code)%lenchrominput:染色体长度%boundinput:变量的取值范围%codeoutput:染色体的编码值%初始变量flag=1;[n,m]=size(code);fori=1:nifcode(i)bound(i,1)||code(i)bound(i,2)flag=0;endend%变异算子程序如下:functionret=Mutation(pmutation,lenchrom,chrom,sizepop,pop,bound)%本函数完成变异操作%pmutationinput:变异概率%lenchrominput:染色体的长度%chrominput:染色体群%sizepopinput:种群规模%popinput:当前种群的进化代数和最大的进化代数信息%retoutput:变异后的染色体fori=1:sizepop%随机选择一个染色体进行变异pick=rand;whilepick==0pick=rand;endindex=ceil(pick*sizepop);%变异概率决定该轮循环是否进行变异pick=rand;ifpickpmutationcontinue;endflag=0;whileflag==0%变异位置pick=rand;whilepick==0pick=rand;endpos=ceil(pick*sum(lenchrom));v=chrom(i,pos);v1=v-bound(pos,1);v2=bound(pos,2)-v;pick=rand;ifpick0.5delta=v2*(1-pick^((1-pop(1)/pop(2))^2));chrom(i,pos)=v+delta;elsedelta=v1*(1-pick^((1-pop(1)/pop(2))^2));chrom(i,pos)=v-delta;endflag=test(lenchrom,bound,chrom(i,:));endendret=chrom;
本文标题:遗传算法选址代码
链接地址:https://www.777doc.com/doc-5647029 .html