遗传算法选址代码.txt
- 文档编号:2504132
- 上传时间:2023-05-03
- 格式:TXT
- 页数:7
- 大小:6.83KB
遗传算法选址代码.txt
《遗传算法选址代码.txt》由会员分享,可在线阅读,更多相关《遗传算法选址代码.txt(7页珍藏版)》请在冰点文库上搜索。
%%GA
clc%����
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:
26
S1=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:
maxgen
i;
%��Ⱥ����GA
GApop=Select(GApop,fitness,popsize);
%�������GA
GApop=Cross(pc,lenchrom,GApop,popsize,bound);
%%GA
clc%����
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:
26
Pz2=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:
maxgen
i;
%��Ⱥ����GA
GApop=Select(GApop,fitness,popsize);
%�������GA
GApop=Cross(pc,lenchrom,GApop,popsize,bound);
%�������GA
GApop=Mutation(pm,lenchrom,GApop,popsize,[imaxgen],bound);
pop=GApop;
forj=1:
popsize
Pz1=fun(pop(j,:
),a);
ifPz2 fitness(j)=fun(pop(j,: ),a); end %�������Ÿ��� iffitness(j) gbest(j,: )=pop(j,: ); fitnessgbest(j)=fitness(j); end %Ⱥ�����Ÿ��� iffitness(j) zbest=pop(j,: ); fitnesszbest=fitness(j); end end yy(i)=fitnesszbest; end %%��� disp'*************bestcoordinate***********' zbest plot(yy,'linewidth',2); title(['��Ӧ������''��ֹ����='num2str(maxgen)]); xlabel('��������');ylabel('��Ӧ��'); gridon %%��Ӧ�Ⱥ��� functionPz1=fun(x,a) Pz1=0; form=1: 26 Pz1=Pz1+33/((x (1)-a(m,1))^2+(x (2)-a(m,2))^2); end end %Ⱦɫ������㷨���£� functionret=Code(lenchrom,bound) %�����������������Ⱦɫ�壬���������ʼ��һ����Ⱥ %lenchrominput: Ⱦɫ�峤�� %boundinput: ������ȡֵ��Χ %retoutput: Ⱦɫ��ı���ֵ flag=0; whileflag==0 pick=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: sizepop pick=rand; whilepick==0 pick=rand; end forj=1: sizepop pick=pick-sumf(j); ifpick<0 index=[indexj]; break; end end end individuals=individuals(index,: ); fitness=fitness(index); ret=individuals; %��������MATLAB��������; functionret=Cross(pcross,lenchrom,chrom,sizepop,bound) %��������ɽ������ %pcrossinput: ������� %lenchrominput��Ⱦɫ��ij��� %chrominput��Ⱦɫ��Ⱥ %sizepopinput: ��Ⱥ��ģ %retoutput: ������Ⱦɫ�� fori=1: sizepop %���ѡ������Ⱦɫ����н��� pick=rand(1,2); whileprod(pick)==0 pick=rand(1,2); end index=ceil(pick.*sizepop); %������ʾ����Ƿ���н��� pick=rand; whilepick==0 pick=rand; end ifpick>pcross continue; end flag=0; whileflag==0 %���ѡ��λ�� pick=rand; whilepick==0 pick=rand; end pos=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==0 flag=0; elseflag=1; end end end ret=chrom; %����Ⱦɫ������Գ������£� functionflag=test(lenchrom,bound,code) %lenchrominput: Ⱦɫ�峤�� %boundinput��������ȡֵ��Χ %codeoutput��Ⱦɫ��ı���ֵ %��ʼ���� flag=1; [n,m]=size(code); fori=1: n ifcode(i) flag=0; end end %�������ӳ������£� functionret=Mutation(pmutation,lenchrom,chrom,sizepop,pop,bound) %��������ɱ������ %pmutationinput: ������� %lenchrominput��Ⱦɫ��ij��� %chrominput��Ⱦɫ��Ⱥ %sizepopinput: ��Ⱥ��ģ %popinput����ǰ��Ⱥ�Ľ������������Ľ���������Ϣ %retoutput: ������Ⱦɫ�� fori=1: sizepop %���ѡ��һ��Ⱦɫ����б��� pick=rand; whilepick==0 pick=rand; end index=ceil(pick*sizepop); %������ʾ�������ѭ���Ƿ���б��� pick=rand; ifpick>pmutation continue; end flag=0; whileflag==0 %����λ�� pick=rand; whilepick==0 pick=rand; end pos=ceil(pick*sum(lenchrom)); v=chrom(i,pos); v1=v-bound(pos,1); v2=bound(pos,2)-v; pick=rand; ifpick>0.5 delta=v2*(1-pick^((1-pop (1)/pop (2))^2)); chrom(i,pos)=v+delta; else delta=v1*(1-pick^((1-pop (1)/pop (2))^2)); chrom(i,pos)=v-delta; end flag=test(lenchrom,bound,chrom(i,: )); end end ret=chrom;
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 遗传 算法 选址 代码