matlab遗传算法求最大值问题例题完整代码_精品文档.txt
《matlab遗传算法求最大值问题例题完整代码_精品文档.txt》由会员分享,可在线阅读,更多相关《matlab遗传算法求最大值问题例题完整代码_精品文档.txt(5页珍藏版)》请在冰豆网上搜索。
![matlab遗传算法求最大值问题例题完整代码_精品文档.txt](https://file1.bdocx.com/fileroot1/2022-10/6/b6c6f4c3-689f-4f06-8c8f-aaef9ad73426/b6c6f4c3-689f-4f06-8c8f-aaef9ad734261.gif)
���ļ���Ŀ���Ǽ��������ֵķ���
%���������Ŵ��㷨���y=200*exp(-0.05*x).*sin(x)��[-22]�����ϵ����ֵ
clc;
clearall;
closeall;
globalBitLength
globalboundsbegin
globalboundsend
bounds=[-22];%һά�Ա�����ȡֵ��Χ
precision=0.0001;%���㾫��
boundsbegin=bounds(:
1);
boundsend=bounds(:
2);
%�������������⾫��������Ҫ���Ⱦɫ��
BitLength=ceil(log2((boundsend-boundsbegin)'./precision));
popsize=50;%��ʼ��Ⱥ��С
Generationnmax=12;%������
pcrossover=0.90;%�������
pmutation=0.09;%�������
%������ʼ��Ⱥ
population=round(rand(popsize,BitLength));
%������Ӧ�ȣ�������Ӧ��Fitvalue���ۻ�����cumsump
[Fitvalue,cumsump]=fitnessfun(population);
Generation=1;
whileGeneration<Generationnmax+1
forj=1:
2:
popsize
%ѡ�����
seln=selection(population,cumsump);
%�������
scro=crossover(population,seln,pcrossover);
scnew(j,:
)=scro(1,:
);
scnew(j+1,:
)=scro(2,:
);
%�������
smnew(j,:
)=mutation(scnew(j,:
),pmutation);
smnew(j+1,:
)=mutation(scnew(j+1,:
),pmutation);
end
population=smnew;%�������µ���Ⱥ
%��������Ⱥ����Ӧ��
[Fitvalue,cumsump]=fitnessfun(population);
%��¼��ǰ����õ���Ӧ�Ⱥ�ƽ����Ӧ��
[fmax,nmax]=max(Fitvalue);
fmean=mean(Fitvalue);
ymax(Generation)=fmax;
ymean(Generation)=fmean;
%��¼��ǰ�������Ⱦɫ�����
x=transform2to10(population(nmax,:
));
%�Ա���ȡֵ��Χ��[-22]����Ҫ�Ѿ����Ŵ���������Ⱦɫ�����ϵ�[-22]����
xx=boundsbegin+x*(boundsend-boundsbegin)/(power((boundsend),BitLength)-1);
xmax(Generation)=xx;
Generation=Generation+1;
end
Generation=Generation-1;
%��ʾ���
Bestpoplation=xx
Besttargetfunvalue=targetfun(xx)
%���ƾ����Ŵ���������Ӧ�����ߡ�һ��ģ����������������Ⱥ��ƽ����Ӧ���������Ӧ�������������
%��ͬ����̬����ʾ�㷨�������еĺ�˳����û�г�����������ǰ���£������Ӧ�ȸ���������ɴ���û�з�������������Ⱥ�Ѿ�����
%��ͼ
figure
(1);
hand1=plot(1:
Generation,ymax);
set(hand1,'linestyle','-','linewidth',1.8,'marker','*','markersize',6)
holdon;
hand2=plot(1:
Generation,ymean);
set(hand2,'color','r','linestyle','-','linewidth',1.8,'marker','h','markersize',6)
xlabel('��������');ylabel('���/ƽ����Ӧ��');xlim([1Generationnmax]);
legend('�����Ӧ��','ƽ����Ӧ��');
boxoff;
holdoff;
%�ӳ�������Ⱥ����������������ƴ洢Ϊcrossover.m
functionscro=crossover(population,seln,pc);
BitLength=si
ze(population,2);
pcc=IfCroIfMut(pc);%���ݽ�����ʾ����Ƿ���н��������1���ǣ�0���
ifpcc==1
chb=round(rand*(BitLength-2))+1;%��[1,BitLength-1]��Χ���������һ������λ
scro(1,:
)=[population(seln
(1),1:
chb)population(seln
(2),chb+1:
BitLength)];
scro(2,:
)=[population(seln
(2),1:
chb)population(seln
(1),chb+1:
BitLength)];
else
scro(1,:
)=population(seln
(1),:
);
scro(2,:
)=population(seln
(2),:
);
end
%�ӳ���:
������Ӧ�Ⱥ������������ƴ洢Ϊfitnessfun
function[Fitvalue,cumsump]=fitnessfun(population);
globalBitLength
globalboundsbegin
globalboundsend
popsize=size(population,1);%��popsize������
fori=1:
popsize
x=transform2to10(population(i,:
));%��������ת��Ϊʮ����
%ת��Ϊ[-22]�����ʵ��
xx=boundsbegin+x*(boundsend-boundsbegin)/(power((boundsend),BitLength)-1);
Fitvalue(i)=targetfun(xx);%���㺯��ֵ������Ӧ��
end
%����Ӧ�Ⱥ�������һ����С��������Ա㱣֤��Ⱥ��ӦֵΪ����
Fitvalue=Fitvalue'+230;
%����ѡ�����
fsum=sum(Fitvalue);
Pperpopulation=Fitvalue/fsum;
%�����ۻ�����
cumsump
(1)=Pperpopulation
(1);
fori=2:
popsize
cumsump(i)=cumsump(i-1)+Pperpopulation(i);
end
cumsump=cumsump';
%�ӳ�������Ⱥ����������������ƴ洢Ϊmutation.m
functionsnnew=mutation(snew,pmutation);
BitLength=size(snew,2);
snnew=snew;
pmm=IfCroIfMut(pmutation);%���ݱ�����ʾ����Ƿ���б��������1���ǣ�0���
ifpmm==1
chb=round(rand*(BitLength-1))+1;%��[1��BitLength]��Χ���������һ������λ
snnew(chb)=abs(snew(chb)-1);
end
%�ӳ����ж��Ŵ������Ƿ���н������죬�������ƴ洢ΪIfCroIfMut.m
functionpcc=IfCroIfMut(mutORcro);
test(1:
100)=0;
l=round(100*mutORcro);
test(1:
1)=1;
n=round(rand*99)+1;
pcc=test(n);
%�ӳ���:
����Ⱥѡ��������������ƴ洢Ϊselection.m
functionseln=selection(population,cumsump);
%����Ⱥ��ѡ����������
fori=1:
2
r=rand;%����һ�������
prand=cumsump-r;
j=1;
whileprand(j)<0
j=j+1;
end
seln(i)=j;%ѡ�и�������
end
%�ӳ�����������ת��Ϊʮ���������������ƴ洢Ϊtransform2to10.m
functionx=transform2to10(Population);
BitLength=size(Population,2);
x=Population(BitLength);
fori=1:
BitLength-1
x=x+Population(BitLength-i)*power(2,i);
end
%�ӳ���:
�����Ż����ֵ��ֵ�������⣬Ŀ�꺯��������Ϊ��Ӧ�Ⱥ���
%�������ƴ洢Ϊtargetfu
n.m
functiony=targetfun(x);%Ŀ�꺯��
y=200*exp(-0.05*x).*sin(x);