利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用文档格式.docx
- 文档编号:16716290
- 上传时间:2022-11-25
- 格式:DOCX
- 页数:10
- 大小:36.64KB
利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用文档格式.docx
《利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用文档格式.docx》由会员分享,可在线阅读,更多相关《利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用文档格式.docx(10页珍藏版)》请在冰豆网上搜索。
0.09
5126
1237
1991
22.44:
0.75
0.11
6217
1379—
1992
25.37
0.85
7730
1385
1993
27.131
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
18341
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
111151
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
1、程序设计
2、调试
运用遗传算法求解函数最大值:
所有的子程序为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
scro(1,:
)=population(seln
(1),:
);
)=population(seln
(2),:
%子程序:
新种群变异操作,函数名称存储为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;
endseln(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);
对于优化最大值或者极大值函数问题,目标函数可以作为适应度函数,%函数名称存储为targetfun.m
functiony=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);
cumsumpGeneration=1;
whileGeneration<
Generationnmax+1
forj=1:
2:
popsizeseln=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,:
xx=boundsbegin+x*(boundsend-boundsbegin)/(power((boundsend),BitLength)-1);
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'
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进行归一化处理
归一化处理后最小值为-1,最
dx=[-1,1;
-1,1;
-1,1];
%大值为1
%BF网络训练
次结果
net.trainParam.Lr=0.05;
为0.05
net.trainParam.epochs=5000;
5000次
net.trainParam.goal=0.65*10A(-3);
均方误差
net=train(net,pn,tn);
输出样本
%^J用原始数据对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
年和2011年的相关数
0.98801.0268];
%2010
pnewn=tramnmx(pnew,minp,maxp);
%利用原始输入数据的归一化参数对新数据进行归一化anewn=sim(net,pnewn);
%利用归一化后的数据进行仿
真
把仿真得到的数据还原为原始的数量级
anew=postmnmx(anewn,mint,maxt)
1>
□
V
b电q劭倉老zi目“口
圧同工用枯壽话电■习忙网iX时氐用
RIEI
禅NHR出客诞■玉厅瞎匾・
i£
fl■事R代需蜜对比囲
闵焙牺世謀咗社
Xl闌丄1邛IjM10.BIf-iukaJ>
lf-
:
“J-7-一「,£
』£
精品资料
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 利用 MATLAB 实现 遗传 算法 神经网络 工具箱 使用