%读取数据
clc
clear
close all
clear global ver
[num,ax,ay] = xlsread('数据.xlsx');
num(:,1:2) = [];
num(45:end,:) = [];
num(5:5:44,:) = [];
num = [num(:,1:13) ];
n = randperm(length(num));
m = 30;
input_train=num(n,1:12)';
output_train= num(n,13)';
input_test=num((m+1:end),1:12)';
output_test= num(m+1:end,13)';
[BPoutput,error1,net] = bpp(num,n,m);
[AllSamInn,minAllSamIn,maxAllSamIn,AllSamOutn,minAllSamOut,maxAllSamOut]=premnmx(input_train,output_train);
EvaSamIn=input_test;
EvaSamInn=tramnmx(EvaSamIn,minAllSamIn,maxAllSamIn);
EvaSamInn1 = tramnmx(input_test,minAllSamIn,maxAllSamIn);
Ptrain = AllSamInn;
Ttrain = AllSamOutn;
AllSamOutnn=tramnmx(output_test,minAllSamOut,maxAllSamOut);
indim=12;
hiddennum=10;
outdim=1;
% Initialize PSO
vmax=0.0151; % Maximum velocity
minerr=0.001; % Minimum error
wmax=0.90;
wmin=0.30;
% global itmax; %Maximum iteration number
itmax=100;
c1=2;
c2=2;
for iter=1:itmax
W(iter)=wmax-((wmax-wmin)/itmax)*iter; % weight declining linearly
end
%Between (m,n), (which can also be started from zero)
m=-1;
n=1;
% global N; % number of particles
N=20;
% global D; % length of particle
D=(indim+1)*hiddennum+(hiddennum+1)*outdim;
gbests = [reshape(net.IW{1,1},1,120) reshape(net.LW{2,1},1,10) reshape(net.b{1},1,10) reshape(net.b{2},1,1)] ;
% particles are initialized between (a,b) randomly
a=abs(gbests)*0.5+gbests;
b=-abs(gbests)*0.5+gbests;
% Initialize positions of particles
% rand('state',sum(100*clock));
X = [];
for ii = 1:N
X =[X;a+(b-a).*rand(1,D,1)]; %取值范围[-1,1] rand * 2 - 1 ,rand 产生[0,1]之间的随机数
end
%Initialize velocities of particles
V=0.2*(m+(n-m)*rand(N,D,1));
%
% global fvrec;
MinFit=[];
BestFit=[];
net=newff(minmax(Ptrain),[hiddennum,outdim],{'logsig','tansig'},'traingdx');
fitness=fitcal(X,net,indim,hiddennum,outdim,D,Ptrain,Ttrain,minAllSamOut,maxAllSamOut);
fvrec(:,1,1)=fitness(:,1,1);
[C,I]=min(fitness(:,1,1));
MinFit=[MinFit C];
BestFit=[BestFit C];
L(:,1,1)=fitness(:,1,1); %record the fitness of particle of every iterations
B(1,1,1)=C; %record the minimum fitness of particle
gbest(1,:,1)=X(I,:,1); %the global best x in population
AllSamIn=tramnmx(EvaSamIn,minAllSamIn,maxAllSamIn);
%Matrix composed of gbest vector
for p=1:N
G(p,:,1)=gbest(1,:);
end
for ii=1:N;
pbest(ii,:,1)=X(ii,:);
end
V(:,:,2)=W(1)*V(:,:,1)+c1*rand*(pbest(:,:,1)-X(:,:,1))+c2*rand*(G(:,:,1)-X(:,:,1));
for ni=1:N
for di=1:D
if V(ni,di,2)>vmax
V(ni,di,2)=vmax;
elseif V(ni,di,2)<-vmax
V(ni,di,2)=-vmax;
else
V(ni,di,2)=V(ni,di,2);
end
end
end
X(:,:,2)=X(:,:,1)+V(:,:,2);
for ni=1:N
for di=1:D
if X(ni,di,2)>1
X(ni,di,2)=1;
elseif X(ni,di,2)<-1
X(ni,di,2)=-1;
else
X(ni,di,2)=X(ni,di,2);
end
end
end
%******************************************************
for jj=2:itmax
disp('Iteration and Current Best Fitness')
disp(jj-1)
disp(B(1,1,jj-1))
bit = 1;
for k=1:N
bit = bit&(range(X(k,:))<0.02);
end
if bit==1 % bit=1时对粒子位置及速度进行随机重置
for ik = 1:N
X(ik,:) = funx; % present 当前位置,随机初始化
X(ik,:) = [0.02*rand()-0.01 0.02*rand()-0.01]; % 速度初始化
end
end
% Calculation of new positions
fitness=fitcal(X,net,indim,hiddennum,outdim,D,Ptrain,Ttrain,minAllSamOut,maxAllSamOut);
[C,I]=min(fitness(:,1,jj));
MinFit=[MinFit C];
BestFit=[BestFit min(MinFit)];
L(:,1,jj)=fitness(:,1,jj);
B(1,1,jj)=C;
gbest(1,:,jj)=X(I,:,jj);
[C,I]=min(B(1,1,:));
% keep gbest is the best particle of all have occured
if B(1,1,jj)<=C
gbest(1,:,jj)=gbest(1,:,jj);
else
gbest(1,:,jj)=gbest(1,:,I);
end
if C<=minerr
break
end
%Matrix composed of gbest vector
if jj>=itmax
break
end
for p=1:N
G(p,:,jj)=gbest(1,:,jj);
end
for ii=1:N;
[C,I]=min(L(ii,1,:));
if L(ii,1,jj)<=C
pbest(ii,:,jj)=X(ii,:,jj);
else
pbest(ii,:,jj)=X(ii,:,I);
end
end
V(:,:,jj+1)=W(jj)*V(:,:,jj)+c1*rand*(pbest(:,:,jj)-X(:,:,jj))+c2*rand*(G(:,:,jj)-X(:,:,jj));
for ni=1:N
for di=1:D
if V(ni,di,jj+1)>vmax
V(ni,di,jj+1)=vmax;
elseif V(ni,di,jj+1)<-vmax
V(ni,di,jj+1)=-vmax;
else
V(ni,di,jj+1)=V(ni,di,jj+1);
end
end
end
X(:,:,jj+1)=X(:,:,jj)+V(:,:,jj+1);
for ni=1:N
for di=1:D
if X(ni,di,jj+1)>1
X(ni,di,jj+1)=1;
elseif X(ni,di,jj+1)<-1
X(ni,di,jj+1)=-1;
else
X(ni,di,jj+1)=X(ni,di,jj+1);
end
end
end
end
disp('Iteration and Current Best Fitness')
disp(jj)
disp(B(1,1,jj))
disp('Global Best Fitness and Occurred Iteration')
[C,I]=min(B(1,1,:));
% simulation network 网络拟合
for t=1:hiddennum
x2iw(t,:)=gbest(1,((t-1)*indim+1):t*indim,jj);
end
for r=1:outdim
x2lw(r,:)=gbest(1,(indim*hiddennum+1):(indim*hiddennum+hiddennum),jj);
end
x2b=gbest(1,((indim+1)*hiddennum+1):D,jj);
x2b1=x2b(1:hiddennum).';
x2b2=x2b(hiddennum+1:hiddennum+outdim).';
net.IW{1,1}=x2iw;
net.LW{2,1}=x2lw;
net.b{1}=x2b1;
net.b{2}=x2b2;
net=newff(minmax(AllSamIn),[10,1],{'tansig','tansig'},'traingdx');
%% BP网络训练
%网络进化参数
net.trainParam.epochs=1000;
net.trainParam.lr=0.1;
net.trainParam.goal=0.001;
% net.trainParam.show=100;
% net.trainParam.showWindow=1;
tic
%网络训练··2·
net=train(net,AllSamIn,AllSamOutnn);
toc
EvaSamOutn = sim(net,EvaSamInn);
EvaSamOutn1 = sim(net,EvaSamInn1);
EvaSamOut = (postmnmx(EvaSamOutn,minAllSamOut,maxAllSamOut));%反归一化
EvaSamOut1 = (postmnmx(EvaSamOutn1,minAllSamOut,maxAllSamOut));%反归一化
% [mx,output_test]=max(output_test);
error=EvaSamOut-output_test;
errormape=(EvaSamOut-output_test)./output_test;
p1 = sum(EvaSamOut==output_test)/length(EvaSamOut);
figure(1)
grid
hold on
plot((BestFit),'r');
title(['PSO适应度曲线 ' '最优代数=' I]);
xlabel('进化代数');ylabel('适应度');
legend('平均适应度','最佳适应度');
disp('适应度 变量');
figure
grid
plot(EvaSamOut(1,:)','r-^')
hold on
plot(BPoutput(1,:)','b-^')
hold on
plot(output_test(1,:)' ,'k-*');
hold on
ylabel('黄酮含量')
legend('粒子群优化BP预测输出','BP预测输出','期望输出')%,
title('PSO-BP网络预测输出','fontsize',12)
figure
grid
plot(error(1,:),'k-o')
hold on
plot(error1(1,:),'b-*')
hold off
ylabel('黄酮含量')
legend('粒子群优化BP训练误差','BP训练误差')%,
figure
grid
plot(BPoutput(1,:)','b-^')
hold on
plot(output_test(1,:)' ,'k-*');
hold on
ylabel('黄酮含量')
legend('BP预测输出','期望输出')%,
title('BP网络预测输出','fontsize',12)
没有合适的资源?快使用搜索试试~ 我知道了~
基于粒子群算法优化BP神经网络的黄酮含量预测,基于pso-bp的黄酮水平预测,基于bp神经网络的黄酮预测(代码完整,数据齐全)
共7个文件
m:4个
jpg:2个
xlsx:1个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 198 浏览量
2024-02-23
18:08:50
上传
评论
收藏 305KB RAR 举报
温馨提示
基于MATLAB编程,基于粒子群算法优化BP神经网络的黄酮含量预测,基于pso-bp的黄酮水平预测,基于bp神经网络的黄酮预测,代码完整,包含数据,有注释,方便扩展应用 1,如有疑问,不会运行,可以私信, 2,需要创新,或者修改可以扫描二维码联系博主, 3,本科及本科以上可以下载应用或者扩展, 4,内容不完全匹配要求或需求,可以联系博主扩展。
资源推荐
资源详情
资源评论
收起资源包目录
23-2.rar (7个子文件)
psobp.m 7KB
2.jpg 337KB
fitcal.m 691B
1.jpg 345KB
main0.m 1KB
数据.xlsx 22KB
bpp.m 860B
共 7 条
- 1
资源评论
神经网络机器学习智能算法画图绘图
- 粉丝: 2794
- 资源: 659
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功