matlab遗传算法求解超越方程.docx
《matlab遗传算法求解超越方程.docx》由会员分享,可在线阅读,更多相关《matlab遗传算法求解超越方程.docx(42页珍藏版)》请在冰豆网上搜索。
matlab遗传算法求解超越方程
Matlab实验报告
一.实验目的
利用遗传算法解超越方程
二.实验原理
遗传算法是一类借鉴生物界的进化规律(适者生存、优胜劣汰遗传机制)演化而来的随机化搜索方法。
遗传算法是一种群体型操作,该操作以群体中的所有个体为对象。
选择、交叉、变异是基本的遗传操作。
遗传算法的主要特点是直接对结构对象进行操作,不存在求导和函数连续性的限定;有更好的全局搜索能力,采用概率化的方法,自动获取并指导优化搜索空间。
三.实验内容及步骤
1.参数设定
clc
clear
globalBitLength%需要编码的长度
globalboundsbegin%自变量的起始点
globalboundsend
bounds=[060];%一维自变量的取值范围
precision=0.0001;%运算精度
boundsbegin=bounds(:
1);
boundsend=bounds(:
2);
BitLength=ceil(log2((boundsend-boundsbegin)'./precision));
%ceil不小于它的最小整数
%种群初始化
popsize=100;%初始种群大小
population=round(rand(popsize,BitLength));%初始种群,N个
%交叉概率
pc=0.8;
%变异概率
pmutation=0.06;
Generation=1;
globalGenerationnmax
Generationnmax=888;
设定自变量x范围[0,60]
交叉概率0.8变异概率0.06运算精度0.0001
初始种群大小100迭代次数888
2.迭代过程
whileGeneration<(Generationnmax+1)
forj=1:
2:
popsize
%1对1对的群体进行如下操作(交叉,变异)
[Fitvalue,cumsump]=fitnessfun(population);
%选择操作
seln=selection(population,cumsump)
%交叉操作
scro=crossover(population,seln,pc);
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);
%最好的适应度为fmax(即函数值最大),其对应的个体为nmax
fmean=mean(Fitvalue);%平均适应度为fmean
ymax(Generation)=fmax;%每代中最好的适应度
ymean(Generation)=fmean;%每代中的平均适应度
%记录当前代的最佳染色体个体
x=transform2to10(population(nmax,:
));
%population(nmax,:
)为最佳染色体个体
xx=boundsbegin+x*(boundsend-boundsbegin)/(power(2,BitLength)-1);
xmax(Generation)=xx;
Generation=Generation+1;
End
现分布实验迭代一次的步骤
①计算当前种群的函数值及适应度累积和
%计算适应度函数
function[Fitvalue,cumsump]=fitnessfun(population)
popsize=size(population,1);
boundsbegin=0;
boundsend=60;
BitLength=20;
fori=1:
popsize
x=transform2to10(population(i,:
));%将二进制转换为十进制
xx(i)=boundsbegin+x*(boundsend-boundsbegin)/(power(2,BitLength));
%转化为[0,60]区间的实数
Fitvalue(i)=targetfun(xx(i));%计算即适应值
end
totalfit=sum(Fitvalue);
p_fitvalue=1-Fitvalue/totalfit;
cumsump
(1)=p_fitvalue
(1);
fori=2:
popsize
cumsump(i)=cumsump(i-1)+p_fitvalue(i);%求累计概率
end
cumsump=cumsump';%累计概率
end
种群初始化时,产生100*20的0-1随机矩阵,若要计算其对应的函数值,首先需要将这100行数据由二进制转为10进制
functionx=transform2to10(Population);
BitLength=size(Population,2);%Population的列,即2进制的长度
x=Population(BitLength);
fori=1:
BitLength-1
x=x+Population(BitLength-i)*power(2,i);%从末位加到首位
end
end
按位乘方并相加(行求和),即可将数据由二进制转为十进制
使转化后得到的十进制种群个体数据落在自变量范围内。
functiony=targetfun(x)%计算目标函数
y=tan(x)-(x.^(-1));
end
代入计算得对应的函数值,即为适应值
遍历种群内所有个体得适应值列向量,求和,得总适应值
设定适应度函数为1-
,目标函数值越接近零越接近求解目标,适应程度越高,此时的适应值与总适应值的比越小,适应度函数需要与适应程度正相关,故而如此设定。
functionseln=selection(population,cumsump)%种群中选择两个体
fori=1:
2
r=rand;%产生一个随机数
prand=cumsump-r;%求出cumsump中第一个比r大的元素
j=1;
whileprand(j)<0
j=j+1;
end
seln(i)=j;%选中个体的序号
end
end
随机产生0-1之间的一个随机数,若累积概率和大于随机数,则选择该个体
循环两次选择两个个体
%子程序:
判断遗传运算是否需要进行交叉或变异,函数名称存储为IfCroIfMut.m
%mutORcro为动作(交叉、变异)发生的概率
%根据概率mutORcro决定是否进行操作,产生1的概率是mutORcro,产生0的概率为1-mutORcro
functionpcc=IfCroIfMut(mutORcro);
test(1:
100)=0;%1x100的行向量
l=round(100*mutORcro);%产生一个数为100*mutORcro,round为取靠近的整数
test(1:
l)=1;
n=round(rand*99)+1;
pcc=test(n);
end
根据交叉概率得到随机矩阵,并利用随机矩阵判断选择的两个个体是否交叉
functionscro=crossover(population,seln,pc)
%输入population为种群,seln为选择的两个个体,pc为交配的概率
BitLength=size(population,2);%二进制数的个数
pcc=IfCroIfMut(pc);%根据交叉概率决定交叉操作,
%1则是,0则否
ifpcc==1
chb=round(rand*(BitLength-2))+1;%随机产生交叉位
scro(1,:
)=[population(seln
(1),1:
chb)population(seln
(2),chb+1:
BitLength)];
%序号为seln
(1)的个体在交叉位chb前面的信息与序号为seln
(2)的个体在交叉位chb+1后面的信息重新组合
scro(2,:
)=[population(seln
(2),1:
chb)population(seln
(1),chb+1:
BitLength)];
%序号为seln
(2)的个体在交叉位chb前面的信息与序号为seln
(1)的个体在交叉位chb+1后面的信息重新组合
else%不交叉操作
scro(1,:
)=population(seln
(1),:
);
scro(2,:
)=population(seln
(2),:
);
end
end
若交叉,则随机产生交叉位并交换交叉位之后的染色体序列
functionsmnew=mutation(snew,pmutation);
smnew=snew;
BitLength=20;
pmm=IfCroIfMut(pmutation);%根据变异概率决定是否变异操作
ifpmm==1
chb=round(rand*(BitLength-1))+1;%随机产生变异位
smnew(chb)=abs(snew(chb)-1);%0变成1,1变成0
end
end
根据变异概率决定是否变异,若变异则在随机位取反
返回主函数,更新种群并对最佳个体,最佳适应值进行记录
重复迭代过程直至迭代次数上限
绘制原函数及最佳个体点图像
绘制平均适应度最大适应度图像
Generation=Generation-1;
%Generation加1、减1的操作是为了能记录各代中的最佳函数值xmax(Generation)
targetfunvalue=targetfun(xmax)
[Besttargetfunvalue,nmax]=max(targetfunvalue)
Bestpopulation=xmax(nmax)
%绘制原曲线
figure
(1);
ezplot('tan(x)-(x.^(-1))',[0,60])
holdon
gridon
fplot('(x.^(-1))',[0,60],'y')
plot(xmax,ymax,'r*');
title('函数曲线图')
xlabel('x')
ylabel('f(x)')
%绘制经过遗传运算后的适应度曲线
figure
(2);
hand1=plot(1:
Generation,ymax);
set(hand1,'linestyle','-','linewidth',1,'marker','o','markersize',4)
holdon;
hand2=plot(1:
Generation,ymean);
set(hand2,'color','y','linestyle','-','linewidth',1,'marker','+','markersize',3)
xlabel('进化代数');
ylabel('最大和平均适应度');
xlim([1Generationnmax]);
legend('最大适应度','平均适应度');
boxoff;
holdoff;
四.实验结果
附录:
Xmax
4.6908
1.4095
0.9701
4.6908
0.9701
0.9410
1.0580
1.4095
0.9410
0.9409
0.9994
0.9408
0.9408
0.9701
4.6908
0.9409
0.9554
1.4095
1.1751
0.9481
0.9701
1.4095
1.4095
0.9409
0.9410
0.9554
4.6908
1.4095
1.1751
1.4095
1.0580
0.9408
1.1751
4.6908
0.9774
4.6981
4.6981
1.0067
1.4168
1.1825
0.9481
1.4168
0.9627
4.6981
1.0653
1.1825
1.4168
0.9774
4.6981
1.1825
0.9627
0.9482
0.9517
4.6981
1.1825
4.6908
1.4095
0.9481
4.6908
0.9554
4.6908
4.6908
4.6908
1.4095
0.9701
1.1751
1.4095
1.4095
4.6908
1.1751
4.6908
0.9701
4.6908
4.6908
0.9701
4.6908
1.4095
1.0580
4.6908
4.6908
1.0580
1.0140
1.4242
4.7054
1.0131
4.7045
1.1889
4.7045
0.9838
1.0570
1.4086
0.9398
0.9401
0.9691
1.4086
0.9984
0.9691
1.2035
1.0863
1.0863
1.0863
1.0277
0.9728
0.9728
1.4379
1.0277
4.6899
1.4086
1.1742
0.9984
4.6899
4.6899
4.6899
1.0570
1.4086
1.1742
4.6899
0.9691
0.9398
1.0570
0.9984
4.6899
0.9984
1.1742
4.6899
0.9984
0.9435
4.6899
4.6899
1.4086
1.0570
4.6899
1.0570
0.9984
0.9691
0.9545
4.6899
1.1742
4.6899
0.9984
1.1742
1.1742
1.4086
4.6899
4.6899
1.4086
0.9691
1.0570
1.4086
4.6899
0.9545
1.4086
1.4086
1.4086
1.0570
0.9984
1.1742
4.6899
1.4086
0.9545
4.6899
1.4086
0.9408
1.4086
0.9691
0.9398
4.6899
0.9472
1.1742
4.6899
4.6899
0.9984
4.6899
4.6899
0.9691
1.1742
0.9691
0.9984
1.1742
1.4086
1.4086
1.1742
0.9691
0.9984
4.6899
4.6899
0.9398
0.9984
1.4086
0.9994
1.1751
1.4095
1.0580
1.0580
4.6908
1.4095
1.4095
0.9536
1.4077
4.6889
0.9389
1.4077
1.1733
1.4077
1.0561
4.6889
1.1733
0.9426
1.4077
1.4077
4.6889
1.1733
0.9426
4.6889
0.9389
3.0483
2.8213
10.3139
2.8139
0.9389
0.9389
2.8725
0.9389
6.5639
2.8213
0.9389
6.5639
3.2827
0.9389
3.0483
3.0483
6.5639
2.8725
3.2827
2.8213
32.8140
2.9311
3.2827
3.2827
2.8158
6.5639
6.5639
0.9389
32.8140
32.8140
3.2827
10.3139
3.2827
0.9389
10.3139
0.9389
2.9311
32.8140
10.3139
10.3139
10.3139
2.8432
10.3139
10.3139
0.9389
6.5639
32.8140
2.8139
2.8286
3.2827
10.3139
2.8725
0.9426
0.9426
0.9426
10.3176
3.0520
32.8176
2.8176
0.9426
3.2863
2.8469
10.3176
0.9426
21.5676
2.8176
25.3176
2.8176
15.9426
16.8801
16.8801
21.5676
17.9348
25.3176
15.9426
2.8176
47.8176
18.2864
16.8801
17.8469
18.0520
21.5676
21.5676
15.9426
21.5676
25.3176
16.0598
18.1692
21.6848
25.6692
18.1692
16.2942
16.2942
17.2317
17.2317
48.1765
48.1985
48.2278
48.2058
48.6453
48.2351
48.1765
48.1765
48.1765
48.6453
48.1765
48.1784
48.2351
48.1768
47.7078
47.2683
45.3640
47.2393
17.2390
45.3640
48.1765
50.9890
54.7390
45.3640
48.1765
48.1765
47.2976
17.2390
48.1765
17.2390
45.3640
50.9890
17.2390
45.3640
17.2390
47.2976
48.1765
54.7390
17.2390
45.3640
45.3640
47.2976
48.1765
47.2683
54.7390
47.2393
54.7390
45.3640
47.2409
50.9890
47.2390
54.7390
45.3640
45.3640
45.3640
48.1765
47.2976
45.3640
17.2390
54.7390
54.7390
32.2390
32.2390
54.7390
51.0476
54.7976
47.3123
48.2351
47.2976
51.0476
54.7976
47.2976
45.4226
51.0476
51.0476
48.2351
45.4226
45.4226
45.4226
48.2351
47.3269
47.3123
47.2976
48.2351
45.4226
45.4226
32.2976
45.4226
32.2976
32.2976
45.4226
48.2351
51.0476
45.4226
47.2976
54.7976
54.7976
54.7976
47.7664
47.7682
32.7664
47.7810
32.7664
47.7664
47.7957
47.7665
47.7665
32.7665
51.5165
51.5165
51.5165
51.5165
47.7802
47.7658
51.5156
47.7949
47.7656
47.7665
47.7667
51.5158
32.7658
32.7658
47.7667
51.5158
51.5158
32.7658
47.7667
47.7658
47.7951
51.5158
32.7658
51.5158
32.7658
32.7658
47.7804
47.7676
47.7804
47.7804
51.5158
47.7667
47.7658
32.7658
47.7658
47.7658
47.7676
47.7667
47.7804
47.7804
47.7951
32.7658
47.7951
32.6486
48.5861
47.6779
48.5861
32.6486
32.6486
48.5861
32.6486
47.6486
51.3986
47.6486
47.6779
47.6633
47.7804
47.6633
47.6633
51.4133
48.6008
32.6632
51.4133
48.6008
32.6632
48.6008
48.6008
47.6633
47.7804
47.7804
48.6008
47.6633
47.6926
47.7803
32.6631
47.7803
48.6006
48.6006
48.6006
48.6006
48.6006
51.4131
48.6006
51.4131
48.6006
51.4131
47.7803
48.6006
51.4131
51.4131
47.6633
47.6633
47.7803
32.6631
48.6006
47.6924
32.6631
47.7803
32.6631
32.6631
32.663