实验一利用MATLAB实现遗传算法一、实验目的1、熟悉MATLAB语言编程环境2、掌握MATLAB语言命令3、学会利用MATLAB编程实现遗传算法二、实验原理MATLAB是美国Math Works公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,MATLAB可以进行矩阵运算、绘制函数和数据、实现算法、创建用户界面、连接其他编程语言的程序等,主要应用于工程计算、控制设计等领域。
通过学习遗传算法原理,使用MATLAB编写程序,实现其求解策略。
三、实验内容通过MATLAB编程,利用遗传算法求解:xx=求[-2,2]f-)(f.x,max∈exp05xsin(),.0)(x)200(三、实验要求1、程序设计2、调试3、实验结果4、撰写实验报告实验二 MATLAB神经网络工具箱的使用一、实验目的1、掌握MATLAB语言命令2、提高MATLAB程序设计能力3、学会使用MATLAB神经网络工具箱二、实验原理MATLAB语言是Math Works公司推出的一套高性能计算机编程语言,集数学计算、图形显示、语言设计于一体,其强大的扩展功能为用户提供了广阔的应用空间。
它附带有30多个工具箱,神经网络工具箱就是其中之一。
利用该工具箱可以方便的构建神经网络的结构模型、设计、训练等,实现神经网络算法。
三、实验内容通过MATLAB编程,利用神经网络工具箱预测公路运量:公路运量主要包括公路客运量和公路货运量两个方面。
据研究,某地区的公路运量主要与该地区的人数、机动车数量和公路面积有关,上表给出了该地区20年的公路运量相关数据。
根据有关部门数据,该地区2010和2011年的人数分别为和万人,机动车数量分别为和万辆,公路面积分别为和万平方千米。
请利用BP网络预测该地区2010和2011年的公路客运量和公路货运量。
某地区20年公路运量数据三、实验要求1、程序设计2、调试3、实验结果4、撰写实验报告运用遗传算法求解函数最大值:所有的子程序为M文件%子程序:计算适应度函数,函数名称存储为function[Fitvalue,sumsump]=fitnessfun(population);global BitLengthglobal boundsbeginglobal boundsendpopsize=size(population,1);for i=1:popsizex=transform2to10(population(i,:)); xx=boundsbegin+x*(boundsend-boundsbegin)/(power((boundsend),BitLength)-1);Fitvalue(i)=targetfun(xx);endFitvalue(i)=Fitvalue'+230;fsum=sum(Fitvalue);Pperpopulation=Fitvalue/fsum;cumsump(1)=Pperpopulation(1);for i=2:popsizecumsump(i)=cumsumo(i-1)+Pperpopulation(i);endcumsump=cumsump';%子程序:新种群交叉操作,函数名称存储为function scro=crossover(population,seln,pc)BitLength=size(population,2);pcc=IfCroIfMut(pc);if pcc==1chb=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)];elsescro(1,:)=population(seln(1),:);scro(2,:)=population(seln(2),:);end%子程序:新种群变异操作,函数名称存储为function snnew=mutation(snew,pmutation);BitLength=size(snew,2);snnew=snew;pmm=IfCroIfMut(pmutation);if pmm==1chb=round(rand*(BitLlength-1))+1;end%子程序:判断遗传运算是否需要进行交叉或变异,函数名称存储为function pcc=IfCroIfMut(mutORcro);test(1:100)=0;1=round(100*mutORcro);test(1:1)=1;n=round(rand*99)+1;pcc=test(n);%子程序:新种群选择操作,函数名称存储为function seln=selection(population,cumsump);for i=1:2r=rand;prand=cumsump-r;j=1;whlie prand(j)<0j=j+1;endseln(i)=j;end%子程序:将二进制数转换为十进制数,函数名称存储为function x=transform2to10(Population);BitLength=size(Population,2);x=Population(BitLength);for i=1:BitLength-1x=x+Population(BitLength-i)*power(2,i);end%子程序:对于优化最大值或者极大值函数问题,目标函数可以作为适应度函数,%函数名称存储为functiony=targetfun(x);y=200*exp*x).*sin(x);%主程序:用遗传算法求解y=200*exp(*x).*sin(x)在[-2 2]区间上的最大值clc;clear all;close all;global BitLengthglobal boundsbeginglobal boundsendbounds=[-2 2];precision=;boundsbegin=bounds(:,1);boundsend=bounds(:,2);BitLength=cell(log2((boundsend-boundsbegin)'./precision));popsize=50; Generationnmax=12; pcrossover=; pmutation=; population=round(rand(popsize,BitLength)); [Fitvalue,cumsump]=fitnessfun(population);cumsumpGeneration=1;while Generation<Generationnmax+1for j=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,:),pmutation);endpopulation=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;endGeneration=Generation-1;Bestpopulation=xx;Besttargetfunvalue=targetfun(xx);figure(1);hand1=plot(1:Generation,ymax);set(hand1,'linestyle','-','linewidth',,'marker','*','markersize',6)hold on;hand2=polt(1:Generation,ymean);set(hand2,'color','linestyle','linewidth',,'marker','h','mrkersize',6)xlabel;ylabel;xlim([1 Generationnmax]);legend;box off;hold off附件二(参考程序)利用神经网络工具箱预测公路运量:为了了解利用BP网络求解问题的过程,把问题分为六个模块处理:1.原始数据的输入;2.数据归一化;3.网络训练;4.对原始数据进行仿真;5.将原始数据仿真的结果与已知样本进行对比;6.对新数据进行仿真。
clc%原始数据%人数(单位:万人)sqrs=[ ];%机动车数(单位:万辆)sqjdcs=[ ];%公路面积(单位:万平方千米)sqglmj=[ ];%公路客运量(单位:万人)glkyl=[5126 6217 7730 9145 10460 11387 12353 15750 18304 19836 21024 19490 20433 22598 25107 33442 36836 40548 42927 43462];%公路货运量(单位:万吨)glhyl=[1237 1379 1385 1399 1663 1714 1834 4322 8132 8936 11099 11203 10524 11115 13320 16762 18673 20724 20803 21804];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'); %建立模型,并用梯度下降法训练%1000轮回显示一次结果%学习速率为%最大训练轮回为5000次%均方误差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=[]; %2010年和2011年的相关数据pnewn=tramnmx(pnew,minp,maxp); %利用原始输入数据的归一化参数对新数据进行归一化anewn=sim(net,pnewn); %利用归一化后的数据进行仿真anew=postmnmx(anewn,mint,maxt) %把仿真得到的数据还原为原始的数量级11。