%读取数据
clc
clear
close all
clear global ver
%训练数据和预测数据
%训练数据和预测数据
num2 = xlsread('训练集.xlsx');
num1 = reshape(num2,24,20);
num1 = num1';
num4 = xlsread('预测真实值.xlsx');
for ii = 1:18
num(ii,1:48) = [num1(ii,:) num1(ii+1,:)] ;
num(ii,49:72) =num1(2+ii,:);
end
num(19,1:48) = [num1(19,:) num1(19+1,:)];
num(19,49:72) = num4';
input_train=num((1:19),1:48)';%训练数据的输入数据
output_train=num((1:19),49:72)';%训练数据的输出数据
input_test=num(19,1:48)';%测试数据的输入数据
output_test=num(19,49:72)'; %测试数据的输出数据
% global minAllSamOut;
% global maxAllSamOut;
[AllSamInn,minAllSamIn,maxAllSamIn,AllSamOutn,minAllSamOut,maxAllSamOut]=premnmx(input_train,output_train);
% Evaluating Sample
EvaSamIn=input_test;
EvaSamInn=tramnmx(EvaSamIn,minAllSamIn,maxAllSamIn); % preprocessing
% TargetOfTestSam=output_test; % add reall output of testing samples
% global Ptrain;
Ptrain = AllSamInn;
% global Ttrain;
Ttrain = AllSamOutn;
% Ptest = input_train(:,350:360);
% Ttest = output_train(:,350:360);
% Initialize BPN parameters
% global indim;
indim=48;
% global hiddennum;
hiddennum=50;
% global outdim;
outdim=24;
% Initialize PSO parameters
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
% particles are initialized between (a,b) randomly
a=-1;
b=1;
%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;
% Initialize positions of particles
% rand('state',sum(100*clock));
X=a+(b-a)*rand(N,D,1); %取值范围[-1,1] rand * 2 - 1 ,rand 产生[0,1]之间的随机数
%Initialize velocities of particles
V=0.2*(m+(n-m)*rand(N,D,1));
%
% global fvrec;
MinFit=[];
BestFit=[];
%Function to be minimized, performance function,i.e.,mse of net work 神经网络建立
% global net;
net=newff(minmax(Ptrain),[hiddennum,outdim],{'tansig','purelin'});
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
%Matrix composed of gbest vector
for p=1:N
G(p,:,1)=gbest(1,:);
end
for i=1:N;
pbest(i,:,1)=X(i,:);
end
V(:,:,2)=W(1)*V(:,:,1)+c1*rand*(pbest(:,:,1)-X(:,:,1))+c2*rand*(G(:,:,1)-X(:,:,1));
%V(:,:,2)=cf*(W(1)*V(:,:,1)+c1*rand*(pbest(:,:,1)-X(:,:,1))+c2*rand*(G(:,:,1)-X(:,:,1)));
%V(:,:,2)=cf*(V(:,:,1)+c1*rand*(pbest(:,:,1)-X(:,:,1))+c2*rand*(G(:,:,1)-X(:,:,1)));
% limits velocity of particles by vmax
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 j=2:itmax
disp('Iteration and Current Best Fitness')
disp(j-1)
disp(B(1,1,j-1))
% Calculation of new positions
fitness=fitcal(X,net,indim,hiddennum,outdim,D,Ptrain,Ttrain,minAllSamOut,maxAllSamOut);
% fvrec(:,1,j)=fitness(:,1,j);
%[maxC,maxI]=max(fitness(:,1,j));
%MaxFit=[MaxFit maxC];
%MeanFit=[MeanFit mean(fitness(:,1,j))];
[C,I]=min(fitness(:,1,j));
MinFit=[MinFit C];
BestFit=[BestFit min(MinFit)];
L(:,1,j)=fitness(:,1,j);
B(1,1,j)=C;
gbest(1,:,j)=X(I,:,j);
[C,I]=min(B(1,1,:));
% keep gbest is the best particle of all have occured
if B(1,1,j)<=C
gbest(1,:,j)=gbest(1,:,j);
else
gbest(1,:,j)=gbest(1,:,I);
end
if C<=minerr, break, end
%Matrix composed of gbest vector
if j>=itmax, break, end
for p=1:N
G(p,:,j)=gbest(1,:,j);
end
for i=1:N;
[C,I]=min(L(i,1,:));
if L(i,1,j)<=C
pbest(i,:,j)=X(i,:,j);
else
pbest(i,:,j)=X(i,:,I);
end
end
V(:,:,j+1)=W(j)*V(:,:,j)+c1*rand*(pbest(:,:,j)-X(:,:,j))+c2*rand*(G(:,:,j)-X(:,:,j));
%V(:,:,j+1)=cf*(W(j)*V(:,:,j)+c1*rand*(pbest(:,:,j)-X(:,:,j))+c2*rand*(G(:,:,j)-X(:,:,j)));
%V(:,:,j+1)=cf*(V(:,:,j)+c1*rand*(pbest(:,:,j)-X(:,:,j))+c2*rand*(G(:,:,j)-X(:,:,j)));
for ni=1:N
for di=1:D
if V(ni,di,j+1)>vmax
V(ni,di,j+1)=vmax;
elseif V(ni,di,j+1)<-vmax
V(ni,di,j+1)=-vmax;
else
V(ni,di,j+1)=V(ni,di,j+1);
end
end
end
X(:,:,j+1)=X(:,:,j)+V(:,:,j+1);
for ni=1:N
for di=1:D
if X(ni,di,j+1)>1
X(ni,di,j+1)=1;
elseif X(ni,di,j+1)<-1
X(ni,di,j+1)=-1;
else
X(ni,di,j+1)=X(ni,di,j+1);
end
end
end
end
disp('Iteration and Current Best Fitness')
disp(j)
disp(B(1,1,j))
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,j);
end
for r=1:outdim
x2lw(r,:)=gbest(1,(indim*hiddennum+1):(indim*hiddennum+hiddennum),j);
end
x2b=gbest(1,((indim+1)*hiddennum+1):D,j);
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;
[BPoutput1,error1] = bpp(num);
%% BP网络训练
%网络进化参数
net.trainParam.epochs=1;
net.trainParam.lr=0.01;
net.trainParam.goal=0.0000001;
net.trainParam.show=100;
net.trainParam.showWindow=1;
tic
%网络训练
%[net,per2]=train(net,AllSamInn,AllSamOutn);
net=train(net,AllSamInn,AllSamOutn);
% nettesterr=mse(sim(net,Ptest)-Ttest);
% testsamout = postmnmx(sim(net,Ptest),minAllSamOut,maxAllSamOut);
% realtesterr=mse(testsamout-TargetOfTestSam)
toc
EvaSamOutn = sim(net,EvaSamInn);
EvaSamOut = postmnmx(EvaSamOutn,minAllSamOut,maxAllSamOut);%反归一化
error=EvaSamOut-output_test;
errormape=(EvaSamOut-output_test)./output_test;
figure(1)
grid
hold on
plot((BestFit),'r');
title(['粒子群算法优化bp ' '最优代数=' I]);
xlabel('进化代数');ylabel('误差');
disp('适应度变量');
figure(2)
grid
plot(EvaSamOut,':og')
hold on
hold on
plot(output_test,'-*r');
axis([1 24 300 600])
legend('粒子群优化BP预测输出','期望输出')
title('粒子群优化BP网络预测输出','fontsize',12)
ylabel('函数输出','fontsize',12)
xlabel('样本','fontsize',12)
神经网络机器学习智能算法画图绘图
- 粉丝: 2819
- 资源: 660
最新资源
- 网上书城系统(Struts+Hibernate+Mysql).rar
- 网上书店(struts+hibernate+css+mysql).rar
- 网上书店系统(论文+jsp源程序)130220.rar
- 网上书店系统(论文+jsp源程序).rar
- 网上书店(struts+hibernate+css+mysql)130223.rar
- 系统详细配置方法.rar
- 文本编辑器.rar
- 项目申报系统(Struts2+Spring+Hibernate+Jsp+Mysql5).rar
- 纯电动汽车再生制动策略,Cruise和Simulink联合仿真,提供Cruise整车模型和simuink策略模型,有详细解析文档,可运行
- 学生成绩管理系统(SSH+MYSQL)130221.rar
- 学生成绩管理系统(SSH+MYSQL).rar
- 项目申报系统(Struts2+Spring+Hibernate+Jsp+Mysql5)130223.rar
- 移动ssh项目(struts+spring+hibernate+oracle).rar
- 阳光酒店管理系统(javaapplet+SQL)130425.rar
- 移动ssh项目(struts+spring+hibernate+oracle)130222.rar
- 音乐网站(JSP+SERVLET)130222.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈