%读取数据
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)
神经网络机器学习智能算法画图绘图
- 粉丝: 2854
- 资源: 660
最新资源
- Java毕业设计-基于springboot+Vue的周边游平台个人管理模块的设计与实现(附源码,部署教程).zip
- Java毕业设计-基于SpringBoot+Vue的火车票订票系统的设计与实现(附源码,部署教程).zip
- 正则表达式的高阶知识内容
- 基于纳什谈判理论的风光氢多主体能源系统合作博弈运行策略优化方法 MATLAB实现,MATLAB代码:基于纳什谈判理论的风–光–氢多主体能源系统合作运行方法 关键词:合作博弈 纳什谈判 风–光–氢系统
- Java毕业设计-基于springboot+Vue的华府便利店信息管理系统(附源码,部署教程).zip
- Java毕业设计-基于springboot+Vue的制造装备物联及生产管理erp系统(附源码,部署教程).zip
- Java毕业设计-基于springboot+Vue的志同道合交友网站(附源码,部署教程).zip
- Java毕业设计-基于SpringBoot+Vue的和Vue的餐饮管理系统的设计与实现2(附源码,部署教程).zip
- Java毕业设计-基于SpringBoot+Vue的和Vue技术的智慧生活商城系统设计与实现(附源码,部署教程).zip
- Java毕业设计-基于SpringBoot+Vue的政府管理的系统设计(附源码,部署教程).zip
- Java毕业设计-基于springboot+Vue的个人博客系统的设计与实现(附源码,部署教程).zip
- 基于主从博弈理论的共享储能与微网优化运行研究:系统框架分析、博弈模型建立与均衡解探讨,MATLAB代码:基于主从博弈理论的共享储能与综合能源微网优化运行研究 关键词:主从博弈 共享储能 综合能源微网
- Java毕业设计-基于SpringBoot+Vue的和Vue的餐饮管理系统的设计与实现(附源码,部署教程).zip
- upload-labs通关指南
- Java毕业设计-基于springboot+Vue的在线考试系统(附源码,部署教程).zip
- Java毕业设计-基于SpringBoot+Vue的在线远程考试系统的设计与实现(附源码,部署教程).zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈