利用MATLAB实现遗传算法和MATLAB神经网络工具箱的使用

更新时间:2023-07-20 06:36:01 阅读量: 实用文档 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

实验一 利用MATLAB实现遗传算法

一、实验目的

1、熟悉MATLAB语言编程环境 2、掌握MATLAB语言命令

3、学会利用MATLAB编程实现遗传算法

二、实验原理

MATLAB是美国Math Works公司出品的商业数学软件,用于算法开发、数据可视化、数据分析以及数值计算的高级技术计算语言和交互式环境,MATLAB可以进行矩阵运算、绘制函数和数据、实现算法、创建用户界面、连接其他编程语言的程序等,主要应用于工程计算、控制设计等领域。通过学习遗传算法原理,使用MATLAB编写程序,实现其求解策略。

三、实验内容

通过MATLAB编程,利用遗传算法求解 :

f(x) 200exp( 0.05x)sin(x), 求maxf(x),x [-2,2].

三、实验要求

1、程序设计 2、调试 3、实验结果 4、撰写实验报告

实验二 MATLAB神经网络工具箱的使用

一、实验目的

1、掌握MATLAB语言命令 2、提高MATLAB程序设计能力 3、学会使用MATLAB神经网络工具箱

二、实验原理

MATLAB语言是Math Works公司推出的一套高性能计算机编程语言,集数学计算、图形显示、语言设计于一体,其强大的扩展功能为用户提供了广阔的应用空间。它附带有30多个工具箱,神经网络工具箱就是其中之一。利用该工具箱可以方便的构建神经网络的结构模型、设计、训练等,实现神经网络算法。

三、实验内容

通过MATLAB编程,利用神经网络工具箱预测公路运量 :

公路运量主要包括公路客运量和公路货运量两个方面。据研究,某地区的

公路运量主要与该地区的人数、机动车数量和公路面积有关,上表给出了该地区20年的公路运量相关数据。根据有关部门数据,该地区2010和2011年的人数分别为73.39和75.55万人,机动车数量分别为3.9635和4.0975万辆,公路面积分别为0.9880和1.0268万平方千米。请利用BP网络预测该地区2010和2011年的公路客运量和公路货运量。

某地区20年公路运量数据

三、实验要求

1、程序设计 2、调试 3、实验结果 4、撰写实验报告

运用遗传算法求解函数最大值: 所有的子程序为M文件

%子程序:计算适应度函数,函数名称存储为fitnessfu.m function[Fitvalue,sumsump]=fitnessfun(population); global BitLength global boundsbegin global boundsend

popsize=size(population,1); for i=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);

for i=2:popsize

cumsump(i)=cumsumo(i-1)+Pperpopulation(i); end

cumsump=cumsump';

%子程序:新种群交叉操作,函数名称存储为crossover.m function scro=crossover(population,seln,pc) BitLength=size(population,2);

pcc=IfCroIfMut(pc);

if pcc==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 function snnew=mutation(snew,pmutation); BitLength=size(snew,2); snnew=snew;

pmm=IfCroIfMut(pmutation); if pmm==1

chb=round(rand*(BitLlength-1))+1;

end

%子程序:判断遗传运算是否需要进行交叉或变异,函数名称存储为IfCroIfMut.m function pcc=IfCroIfMut(mutORcro); test(1:100)=0;

1=round(100*mutORcro); test(1:1)=1;

n=round(rand*99)+1; pcc=test(n);

%子程序:新种群选择操作,函数名称存储为selection.m function seln=selection(population,cumsump);

for i=1:2

r=rand; prand=cumsump-r; j=1;

whlie prand(j)<0 j=j+1; end

seln(i)=j; end

%子程序:将二进制数转换为十进制数,函数名称存储为transform2to10.m function x=transform2to10(Population); BitLength=size(Population,2); x=Population(BitLength);

for i=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)在[-2 2]区间上的最大值 clc;

clear all; close all; global BitLength

global boundsbegin global boundsend

bounds=[-2 2]; 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;

while Generation<Generationnmax+1 for j=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) hold on;

hand2=polt(1:Generation,ymean);

set(hand2,'color','linestyle','linewidth',1.8,'marker','h','mrkersize',6)

xlabel; ylabel; xlim([1 Generationnmax]);

legend; box off; hold off

附件二(参考程序)

利用神经网络工具箱预测公路运量:

为了了解利用BP网络求解问题的过程,把问题分为六个模块处理:1.原始数据的输入;2.数据归一化;3.网络训练;4.对原始数据进行仿真;5.将原始数据仿真的结果与已知样本进行对比;6.对新数据进行仿真。

clc

%原始数据

%人数(单位:万人)

sqrs=[20.55 22.44 25.37 27.13 29.45 3.10 30.96 34.06 36.42 38.09 39.13 39.99 41.93 44.59 47.30 52.89 55.73 56.76 59.17 60.63]; %机动车数(单位:万辆)

sqjdcs=[0.6 0.75 0.85 0.9 1.05 1.35 1.45 1.6 1.7 1.85 2.15 2.2 2.25 2.35 2.5 2.6 2.7 2.85 2.95 3.1]; %公路面积(单位:万平方千米)

sqglmj=[0.09 0.11 0.11 0.14 0.20 0.23 0.23 0.32 0.32 0.34 0.36 0.36 0.38 0.49 0.56 0.59 0.59 0.67 0.69 0.79];

%公路客运量(单位:万人)

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'); %建立模型,并用梯度下降法训练

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.39 75.55 3.9635 4.0975

0.9880 1.0268]; %2010年和2011年的相关数据 pnewn=tramnmx(pnew,minp,maxp); %利用原始输入数据的归一化参数对新数据进行归一化 anewn=sim(net,pnewn); %利用归一化后的数据进行仿真 anew=postmnmx(anewn,mint,maxt) %把仿真得到的数据还原为原始的数量级

本文来源:https://www.bwwdw.com/article/5a61.html

Top