利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用.docx
《利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用.docx》由会员分享,可在线阅读,更多相关《利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用.docx(11页珍藏版)》请在冰豆网上搜索。
利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用
实验一利用MATLAB实现遗传算法
一、实验目的
1、熟悉MATLAB语言编程环境
2、掌握MATLAB语言命令
3、学会利用MATLAB编程实现遗传算法
二、实验原理
MATLAB是美国MathWorks公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,MATLAB可以进行矩阵运算、绘制函数和数据、实现算法、创建用户界面、连接其他编程语言的程序等,主要应用于工程计算、控制设计等领域。
通过学习遗传算法原理,使用MATLAB编写程序,实现其求解策略。
三、实验内容
通过MATLAB编程,利用遗传算法求解:
求
.
三、实验要求
1、程序设计
2、调试
3、实验结果
4、撰写实验报告
实验二MATLAB神经网络工具箱的使用
一、实验目的
1、掌握MATLAB语言命令
2、提高MATLAB程序设计能力
3、学会使用MATLAB神经网络工具箱
二、实验原理
MATLAB语言是MathWorks公司推出的一套高性能计算机编程语言,集数学计算、图形显示、语言设计于一体,其强大的扩展功能为用户提供了广阔的应用空间。
它附带有30多个工具箱,神经网络工具箱就是其中之一。
利用该工具箱可以方便的构建神经网络的结构模型、设计、训练等,实现神经网络算法。
三、实验内容
通过MATLAB编程,利用神经网络工具箱预测公路运量:
公路运量主要包括公路客运量和公路货运量两个方面。
据研究,某地区的公路运量主要与该地区的人数、机动车数量和公路面积有关,上表给出了该地区20年的公路运量相关数据。
根据有关部门数据,该地区2010和2011年的人数分别为73.39和75.55万人,机动车数量分别为3.9635和4.0975万辆,公路面积分别为0.9880和1.0268万平方千米。
请利用BP网络预测该地区2010和2011年的公路客运量和公路货运量。
某地区20年公路运量数据
年份
人口数量/
万人
机动车数量/
万辆
公路面积/
万平方千米
公路客运量/万人
公路货运量/万吨
1990
20.55
0.6
0.09
5126
1237
1991
22.44
0.75
0.11
6217
1379
1992
25.37
0.85
0.11
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
0.23
12353
1834
1997
34.06
1.60
0.32
15750
4322
1998
36.42
1.70
0.32
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
0.36
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
0.59
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
三、实验要求
1、程序设计
2、调试
3、实验结果
4、撰写实验报告
运用遗传算法求解函数最大值:
所有的子程序为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:
popsize
cumsump(i)=cumsumo(i-1)+Pperpopulation(i);
end
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:
BitLength)];
else
scro(1,:
)=population(seln
(1),:
);
scro(2,:
)=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;
end
%子程序:
判断遗传运算是否需要进行交叉或变异,函数名称存储为IfCroIfMut.m
functionpcc=IfCroIfMut(mutORcro);
test(1:
100)=0;
1=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;
whlieprand(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
%子程序:
对于优化最大值或者极大值函数问题,目标函数可以作为适应度函数,
%函数名称存储为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;
globalBitLength
globalboundsbegin
globalboundsend
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;
whileGenerationforj=1:
2:
popsize
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,:
),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,:
));xx=boundsbegin+x*(boundsend-boundsbegin)/(power((boundsend),BitLength)-1);
xmax(Generation)=xx;
Generation=Generation+1;
end
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','linestyle','linewidth',1.8,'marker','h','mrkersize',6)
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','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,'r-o',x,glhyl,'b--+');%绘制公路货运量对比图
legend('网络输出货运量','实际货运量');
xlabel('年份');ylabel('货运量/万吨');
title('运用工具箱货运量学习和测试对比图');%利用训练好的网络进行预测
%利用训练好的网络进行预测
%当用训练好的网络对新数据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)%把仿真得到的数据还原为原始的数量级