遗传算法优化BP神经网络-非线性函数拟合

上传人:人*** 文档编号:564470901 上传时间:2022-10-28 格式:DOC 页数:4 大小:55KB
返回 下载 相关 举报
遗传算法优化BP神经网络-非线性函数拟合_第1页
第1页 / 共4页
遗传算法优化BP神经网络-非线性函数拟合_第2页
第2页 / 共4页
遗传算法优化BP神经网络-非线性函数拟合_第3页
第3页 / 共4页
遗传算法优化BP神经网络-非线性函数拟合_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

《遗传算法优化BP神经网络-非线性函数拟合》由会员分享,可在线阅读,更多相关《遗传算法优化BP神经网络-非线性函数拟合(4页珍藏版)》请在金锄头文库上搜索。

1、%读取数据data=xlsread(data.xls);%训练预测数据 data_train=data(1:113,:);data_test=data(118:123,:);input_train=data_train(:,1:9); output_train=data_train(:,10);input_test=data_test(:,1:9); output_test=data_test(:,10);%数据归一化inputn,mininput,maxinput,outputn,minoutput,maxoutput=premnmx(input_train,output_train); 对

2、 p 和 t 进行字标准化预处理net=newff(minmax(inputn),10,1,tansig,purelin,trainlm);net.trainParam.epochs=100;net.trainParam.lr=0.1; net.trainParam.goal=0.00001;%net.trainParam.show=NaN%网络训练 net=train(net,inputn,outputn);%数据归一化inputn_test = tramnmx(input_test,mininput,maxinput);an=sim(net,inputn);test_simu=postmn

3、mx(an,minoutput,maxoutput);error=test_simu-output_train;plot(error)k=error./output_train%codefunction ret=Code(lenchrom,bound) %本函数将变量编码成染色体,用于随机初始化一个种群% lenchrom input : 染色体长度% bound input : 变量的取值范围% ret output: 染色体的编码值 flag=0;while flag=0pick=rand(1,length(lenchrom);retret=bound(:,1)+(bound(:,2)-b

4、ound(:,1).*pick; % 线性插值,编码结果以实数向量存入 中flag=test(lenchrom,bound,ret);%检验染色体的可行性end%cross function ret=Cross(pcross,lenchrom,chrom,sizepop,bound)%本函数完成交叉操作% pcorss% lenchrom% chrominput% sizepop% retinput : 交叉概率input : 染色体的长度 染色体群input : 种群规模output : 交叉后的染色体for i=1:sizepop %每一轮 for 循环中,可能会进行一次交叉操作,染色体是

5、随机选择的, 交叉位置也是随机选择的,% 但该轮 for 循环中是否进行交叉操作则由交叉概率决定continue 控制)% 随机选择两个染色体进行交叉pick=rand(1,2);while prod(pick)=0pick=rand(1,2); endindex=ceil(pick.*sizepop);% 交叉概率决定是否进行交叉 pick=rand;while pick=0pick=rand;endif pickpcrosscontinue;endflag=0;while flag=0% 随机选择交叉位pick=rand;while pick=0pick=rand;endpos=ceil(

6、pick.*sum(lenchrom); % 随机选择进行交叉的位置,即选择第几个变量进 行交叉,注意:两个染色体交叉的位置相同pick=rand; % 交叉开始v1=chrom(index(1),pos);v2=chrom(index(2),pos);chrom(index(1),pos)=pick*v2+(1-pick)*v1;chrom(index(2),pos)=pick*v1+(1-pick)*v2; % 交叉结束 flag1=test(lenchrom,bound,chrom(index(1),:);% 检验染色体 1 的可行性flag2=test(lenchrom,bound,c

7、hrom(index(2),:);% 检验染色体 2 的可行性if flag1*flag2=0flag=0;else flag=1;end%如果两个染色体不是都可行,则重新交叉endendret=chrom;%Decodefunction ret=Decode(lenchrom,bound,code,opts)% 本函数对染色体进行解码% lenchrominput : 染色体长度% boundinput : 变量取值范围% codeinput :编码值% optsinput : 解码方法标签% retoutput: 染色体的解码值switch optscase binary % binary

8、 codingfor i=length(lenchrom):-1:1data(i)=bita nd(code,2e nchrom(i)-l);%并低十位,然后将低十位转换成十进制数存在data(i)里面code=(code-data(i)/(2Ale nchrom(i);% 低十位清零,然后右移十位endret=bound(:,1)+data./(2.Alenchrom-1).*(bound(:,2)-bound(:,1);%分段解码, 以实数向量的形式存入 ret 中case grey % grey codingfor i=sum(lenchrom):-1:2 code=bitset(cod

9、e,i-1,bitxor(bitget(code,i),bitget(code,i-1);endfor i=length(lenchrom):-1:1data(i)=bitand(code,2Alenchrom(i)-1);code=(code-data(i)/(2Alenchrom(i);endret=bound(:,1)+data./(2.Alenchrom-1).*(bound(:,2)-bound(:,1); % 分段解码, 以实数向 量的形式存入 ret 中endcase float % float codingret=code; %解码结果就是编码结果(实数向量) ,存入 ret 中

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 解决方案

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号