利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用Word格式文档下载.docx
《利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用Word格式文档下载.docx》由会员分享,可在线阅读,更多相关《利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用Word格式文档下载.docx(11页珍藏版)》请在冰豆网上搜索。
1990
20.55
0.6
0.09
5126
1237
1991
22.44
0.75
0.11
6217
1379
1992
25.37
0.85
7730
1385
1993
27.13
0.90
0.14
9145
1399
1994
29.45
1.05
0.20
10460
1663
1995
30.10
1.35
0.23
11387
1714
1996
30.96
1.45
12353
1834
1997
34.06
1.60
0.32
15750
4322
1998
36.42
1.70
18304
8132
1999
38.09
1.85
0.34
19836
8936
2000
39.13
2.15
0.36
21024
11099
2001
39.99
2.20
19490
11203
2002
41.93
2.25
0.38
20433
10524
2003
44.59
2.35
0.49
22598
11115
2004
47.30
2.50
0.56
25107
13320
2005
52.89
2.60
0.59
33442
16762
2006
55.73
2.70
36836
18673
2007
56.76
2.85
0.67
40548
20724
2008
59.17
2.95
0.69
42927
20803
2009
60.63
3.10
0.79
43462
21804
运用遗传算法求解函数最大值:
所有的子程序为M文件
%子程序:
计算适应度函数,函数名称存储为fitnessfu.m
function[Fitvalue,sumsump]=fitnessfun(population);
globalBitLength
globalboundsbegin
globalboundsend
popsize=size(population,1);
fori=1:
popsize
x=transform2to10(population(i,:
));
xx=boundsbegin+x*(boundsend-boundsbegin)/(power((boundsend),BitLength)-1);
Fitvalue(i)=targetfun(xx);
end
Fitvalue(i)=Fitvalue'
+230;
fsum=sum(Fitvalue);
Pperpopulation=Fitvalue/fsum;
cumsump
(1)=Pperpopulation
(1);
fori=2:
cumsump(i)=cumsumo(i-1)+Pperpopulation(i);
cumsump=cumsump'
;
新种群交叉操作,函数名称存储为crossover.m
functionscro=crossover(population,seln,pc)
BitLength=size(population,2);
pcc=IfCroIfMut(pc);
ifpcc==1
chb=round(rand*(BitLength-2))+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:
else
)=population(seln
(1),:
);
)=population(seln
(2),:
end
%子程序:
新种群变异操作,函数名称存储为mutation.m
functionsnnew=mutation(snew,pmutation);
BitLength=size(snew,2);
snnew=snew;
pmm=IfCroIfMut(pmutation);
ifpmm==1
chb=round(rand*(BitLlength-1))+1;
判断遗传运算是否需要进行交叉或变异,函数名称存储为IfCroIfMut.m
functionpcc=IfCroIfMut(mutORcro);
test(1:
100)=0;
1=round(100*mutORcro);
1)=1;
n=round(rand*99)+1;
pcc=test(n);
新种群选择操作,函数名称存储为selection.m
functionseln=selection(population,cumsump);
2
r=rand;
prand=cumsump-r;
j=1;
whlieprand(j)<
j=j+1;
seln(i)=j;
将二进制数转换为十进制数,函数名称存储为transform2to10.m
functionx=transform2to10(Population);
BitLength=size(Population,2);
x=Population(BitLength);
BitLength-1
x=x+Population(BitLength-i)*power(2,i);
对于优化最大值或者极大值函数问题,目标函数可以作为适应度函数,
%函数名称存储为targetfun.m
function
y=targetfun(x);
y=200*exp(-0.05*x).*sin(x);
%主程序:
用遗传算法求解y=200*exp(-0.05*x).*sin(x)在[-22]区间上的最大值
clc;
clearall;
closeall;
bounds=[-22];
precision=0.0001;
boundsbegin=bounds(:
1);
boundsend=bounds(:
2);
BitLength=cell(log2((boundsend-boundsbegin)'
./precision));
popsize=50;
Generationnmax=12;
pcrossover=0.90;
pmutation=0.09;
population=round(rand(popsize,BitLength));
[Fitvalue,cumsump]=fitnessfun(population);
cumsump
Generation=1;
whileGeneration<
Generationnmax+1
forj=1:
2:
seln=selection(population,cumsump);
scro=crossover(popuoation,seln,pcrossover);
scnew(j,:
)=scro(1,:
scnew(j+1,:
)=scro(2,:
smnew(j,:
)=mutation(scnew(j,:
),pmutation);
smnew(j+1,:
)=mutation(scnew(j+1,:
population=smnew;
[Fitvalue,cumsump]=fitnessfun(population);
[fmax,nmax]=max(Fitvalue);
fmean=mean(Fitvalue);
ymax(Generation)=fmax;
ymean(Generation)=fmean;
x=transform2to10(population(nmax,:
xmax(Generation)=xx;
Generation=Generation+1;
Generation=Generation-1;
Bestpopulation=xx;
Besttargetfunvalue=targetfun(xx);
figure
(1);
hand1=plot(1:
Generation,ymax);
set(hand1,'
linestyle'
'
-'
linewidth'
1.8,'
marker'
*'
markersize'
6)
holdon;
hand2=polt(1:
Generation,ymean);
set(hand2,'
color'
h'
mrkersize'
xlabel;
ylabel;
xlim([1Generationnmax]);
legend;
boxoff;
holdoff
附件二(参考程序)
利用神经网络工具箱预测公路运量:
为了了解利用BP网络求解问题的过程,把问题分为六个模块处理:
1.原始数据的输入;
2.数据归一化;
3.网络训练;
4.对原始数据进行仿真;
5.将原始数据仿真的结果与已知样本进行对比;
6.对新数据进行仿真。
clc
%原始数据
%人数(单位:
万人)
sqrs=[20.5522.4425.3727.1329.453.1030.9634.0636.4238.0939.1339.9941.9344.5947.3052.8955.7356.7659.1760.63];
%机动车数(单位:
万辆)
sqjdcs=[0.60.750.850.91.051.351.451.61.71.852.152.22.252.352.52.62.72.852.953.1];
%公路面积(单位:
万平方千米)
sqglmj=[0.090.110.110.140.200.230.230.320.320.340.360.360.380.490.560.590.590.670.690.79];
%公路客运量(单位:
glkyl=[512662177730914510460113871235315750183041983621024194902043322598251073344236836405484292743462];
%公路货运量(单位:
万吨)
glhyl=[123713791385139916631714183443228132893611099112031052411115133201676218673207242080321804];
p=[sqrs;
sqjdcs;
sqglmj];
%输入数据矩阵
t=[glkyl;
glhyl];
%目标数据矩阵
%利用函数premnmx对数据进行归一化
[pn,minp,maxp,tn,mint,maxt]=premnmx(p,t);
%对于输入矩阵p和输出矩阵t进行归一化处理
dx=[-1,1;
-1,1;
-1,1];
%归一化处理后最小值为-1,最大值为1
%BP网络训练
net=newff(dx,[3,7,2],{'
tansig'
purelin'
},'
traingdx'
%建立模型,并用梯度下降法训练
net.trainParam.show=1000;
%1000轮回显示一次结果
net.trainParam.Lr=0.05;
%学习速率为0.05
net.trainParam.epochs=5000;
%最大训练轮回为5000次
net.trainParam.goal=0.65*10^(-3);
%均方误差
net=train(net,pn,tn);
%开始训练,其中pn,tn分别为输入输出样本
%利用原始数据对BP网络仿真
an=sim(net,pn);
%用训练好的模型进行仿真
a=postmnmx(an,mint,maxt);
%把仿真得到的数据还原为原始的数量级
%本例因样本容量有限使用训练数据进行测试,通常必须用新鲜数据进行测试
x=1990:
2009;
newk=a(1,:
newh=a(2,:
figure
(2);
subplot(2,1,1);
plot(x,newk,'
r-o'
x,glkyl,'
b--+'
%绘制公路客运量对比图
legend('
网络输出客运量'
实际客运量'
xlabel('
年份'
ylabel('
客运量/万人'
title('
运用工具箱客运量学习和测试对比图'
subplot(2,1,2);
plot(x,newh,'
x,glhyl,'
%绘制公路货运量对比图
网络输出货运量'
实际货运量'
货运量/万吨'
运用工具箱货运量学习和测试对比图'
%利用训练好的网络进行预测
%利用训练好的网络进行预测
%当用训练好的网络对新数据pnew进行预测时,也应做相应的处理
pnew=[73.3975.55
3.96354.0975
0.98801.0268];
%2010年和2011年的相关数据
pnewn=tramnmx(pnew,minp,maxp);
%利用原始输入数据的归一化参数对新数据进行归一化
anewn=sim(net,pnewn);
%利用归一化后的数据进行仿真
anew=postmnmx(anewn,mint,maxt)%把仿真得到的数据还原为原始的数量级