close all;
clear all;
clc;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 2000年前2个月的电价数据作为样本,3月的数据作为检验,对4月的电价进行预测 %
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
load price.txt;
load demand.txt;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% 预测日类型(result: invalid)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
WE=4;
for i=1:366
if rem(i,7)==0
DataType(i) = 0.9/WE;
continue;
end;
if rem(i,7)==1
DataType(i) = 0.5/WE;
continue;
end;
if rem(i,7)==2
DataType(i) = 0.4/WE;
continue;
end;
if rem(i,7)==3
DataType(i) = 0.8/WE;
continue;
end;
if rem(i,7)==4
DataType(i) = 0.9/WE;
continue;
end;
if rem(i,7)==5
DataType(i) = 0.9/WE;
continue;
end;
if rem(i,7)==6
DataType(i) = 0.9/WE;
continue;
end;
end;
N = 60; %学习样本数
ForeOutput =[];
for ForeDate = 85:91;%预测日期
SampleMaxDate = ForeDate-1;%最大样本
% 电价数据归一化处理
SamplePrice = price(1:ForeDate,:);
MaxPrice=zeros(1,24);
MinPrice=ones(1,24)*100;
for i=1:SampleMaxDate
for t=1:24
if MaxPrice(t)
end;
if MinPrice(t)>SamplePrice(i,t)
MinPrice(t)=SamplePrice(i,t);
end;
end;
end;
for t=1:24
SamplePrice(:,t) = (SamplePrice(:,t)-MinPrice(t))/(1.5*MaxPrice(t)-MinPrice(t));
end;
% 电价数据星期趋势消除
for i=15:ForeDate
Temp = [];
Temp = SamplePrice(i-14:i-1,:);
PriceAvg(i,:) = sum(Temp,1)./14;
SamplePrice(i,:) = SamplePrice(i,:)./PriceAvg(i,:);
end;
% 负荷数据归一化处理
SampleDemand = demand(1:ForeDate,:);
MaxDemand = zeros(1,24);
MinDemand = ones(1,24)*100;
for i=1:ForeDate
for t=1:24
if MaxDemand(t)
end;
if MinDemand(t)>SampleDemand(i,t)
MinDemand(t)=SampleDemand(i,t);
end;
end;
end;
for t=1:24
SampleDemand(:,t) = (SampleDemand(:,t)-MinDemand(t))/(1.5*MaxDemand(t)-MinDemand(t));
end;
IN_N = 13; %输入层神经元个数
HIDE_N = 6; %隐层神经元个数
OUT_N = 1; %输出层神经元个数
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% 神经网络训练 %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
MaxLearnTime = 1000; %U最大学习次数
PreError=0.001; %设定误差
alpha = 0.95; %输出-隐层的学习率
beta = 0.95; %隐层-输入层的学习率
for t=1:24
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%% 神经网络样本数据 %%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
W_P=[0.5,0.6,0.7,0.8,1.5,2,0.5,0.6,0.8,1.5,4,10,1];
% W_P=[1,1,1,1,1,1,1,1,1,1,1,1,1];
for i=1:N
if t==1
Temp = [];
Temp = [SamplePrice((ForeDate-i)-8:(ForeDate-i)-7,t);...
SamplePrice((ForeDate-i)-3:(ForeDate-i)-1,t);...
SamplePrice((ForeDate-i)-7,t);...
SampleDemand((ForeDate-i)-8:(ForeDate-i)-7,t);...
SampleDemand((ForeDate-i)-2:(ForeDate-i),t);...
SampleDemand((ForeDate-i)-1,t)-SampleDemand(ForeDate-i,t);...
DataType(ForeDate-i)];
else
Temp = [];
Temp = [SamplePrice((ForeDate-i)-8:(ForeDate-i)-7,t);...
SamplePrice((ForeDate-i)-3:(ForeDate-i)-1,t);...
ForeOutput(ForeDate,t-1);...
SampleDemand((ForeDate-i)-8:(ForeDate-i)-7,t);...
SampleDemand((ForeDate-i)-2:(ForeDate-i),t);...
SampleDemand((ForeDate-i)-1,t)-SampleDemand(ForeDate-i,t);...
DataType(ForeDate-i)];
end;
SampleInput(i,t,:)=[W_P(1)*Temp(1),W_P(2)*Temp(2),W_P(3)*Temp(3),W_P(4)*Temp(4),...
W_P(5)*Temp(5),W_P(6)*Temp(6),W_P(7)*Temp(7),W_P(8)*Temp(8),...
W_P(9)*Temp(9),W_P(10)*Temp(10),W_P(11)*Temp(11),W_P(12)*Temp(12),...
W_P(13)*Temp(13)];
SampleTarget(i,t)= SamplePrice(ForeDate-i,t);
end;
%预测日神经网络输入
if t==1
Temp = [];
Temp = [SamplePrice(ForeDate-8:ForeDate-7,t);SamplePrice(ForeDate-3:ForeDate-1,t);...
SamplePrice((ForeDate-i)-1,t);
SampleDemand(ForeDate-8:ForeDate-7,t);SampleDemand(ForeDate-2:ForeDate,t);...
SampleDemand(ForeDate-1,t)-SampleDemand(ForeDate,t);DataType(SampleMaxDate+i)];
else
Temp = [];
Temp = [SamplePrice(ForeDate-8:ForeDate-7,t);SamplePrice(ForeDate-3:ForeDate-1,t);...
ForeOutput((ForeDate)-1,t-1);
SampleDemand(ForeDate-8:ForeDate-7,t);SampleDemand(ForeDate-2:ForeDate,t);...
SampleDemand(ForeDate-1,t)-SampleDemand(ForeDate,t);DataType(SampleMaxDate+i)];
end;
ForeInput(t,:) = [W_P(1)*Temp(1),W_P(2)*Temp(2),W_P(3)*Temp(3),W_P(4)*Temp(4),...
W_P(5)*Temp(5),W_P(6)*Temp(6),W_P(7)*Temp(7),W_P(8)*Temp(8)...
W_P(9)*Temp(9),W_P(10)*Temp(10),W_P(11)*Temp(11),W_P(12)*Temp(12),...
W_P(13)*Temp(13)];
%初始化I-H层阈值和权重
W=rand(HIDE_N,IN_N);
theta=rand(HIDE_N,1);
%初始化H-O层权重和阈值
V=rand(OUT_N,HIDE_N);
gama=rand(OUT_N,1);
for CurrentStudy=1:MaxLearnTime
for m=1:N
%nn输入训练数据
for i=1:IN_N
InputData(i)=SampleInput(m,t,i);
end;
for k=1:OUT_N
OutputData(k)=SampleTarget(m,t,k);
end;
%从输入层到隐含层
for j=1:HIDE_N
TempValue=0;
for i=1:IN_N
TempValue = TempValue+W(j,i)*InputData(i); %内积
end;
HideLayerInput(j)=TempValue+theta(j); %隐层输入
HideLayerOutput(j)=1/(1+exp(-HideLayerInput(j))); %隐层输出
end;
%从隐含层到输出层
for k=1:OUT_N
TempValue=0;
for j=1:HIDE_N
TempValue = TempValue+V(k,j)*HideLayerOutput(j); %内积
end;
OutLayerInput(k)=TempValue+gama(k); %输出层输入
OutLayerOutput(k)=1/(1+exp(-OutLayerInput(k))); %输出层输出
end;
%得到第m个样本的总误差
err_sqr=0;
for k=1:OUT_N
err_temp = OutputData(k)-OutLayerOutput(k);
err_sqr = err_sqr+0.5*(err_temp*err_temp);
end;
Error(m)=err_sqr;
%误差由输出层向隐层反向传播,并调整权重
for k=1:OUT_N
delta(k)= OutputData(k)-OutLayerOutput(k); %误差
delta_bar(k)=delta(k)*OutLayerOutput(k)*(1-OutLayerOutput(k)); %求导数diff(f(x))=f(x)*(1-f(x))
gama(k) = gama(k)+beta*delta_bar(k); %调整输出层阈值
for j=1:HIDE_N
V(k,j) = V(k,j)+beta*delta_bar(k)*HideLayerOutput(j); %调整输出层权重
end;
end;
%调整输入层至隐含层权重与阈值
for j=1:HIDE_N
sigma(j)=0;
for k=1:OUT_N
delta(k) = (OutputData(k)-OutLayerOutput(k))*OutLayerOutput(k)*(1-OutLayerOutput(k));
sigma(j) = sigma(j)+delta(k)*V(k,j)*HideLayerOutput(j)*(1-HideLayerOutput(j));
end;
for i=1:IN_N
W(j,i) = W(j,i)+alpha*sigma(j)*InputData(i); %调整权重
end;
theta(j) = theta(j)+alpha*sigma(j); %调整阈值
end;
end;
error=0;
for m=1:N
error = error+Error(m);
end;
error = error/(2*N);
if error < PreError
break;
end;
if error < PreError*20
alpha = 0.9; %输出-隐层的学习率
beta = 0.9; %隐层-输入层的学习率
end;
if error < PreError*10
alpha = 0.8; %输出-隐层的学习率
beta = 0.8; %隐层-输入层的学习率
end;
if error < PreError*5
alpha = 0.5; %输出-隐层的学习率
beta = 0.5; %隐层-输入层的学习率
end;
end;
% 用训练好的网络进行预测
for i=1:IN_N
InputData(i) = ForeInput(t,i);
end;
%从输入层到隐含层
for j=1:HIDE_N
TempValue=0;
for i=1:IN_N
TempValue = TempValue+W(j,i)*InputData(i); %内积
end;
HideLayerInput(j)=TempValue+theta(j); %隐层输入
HideLayerOutput(j)=1/(1+exp(-HideLayerInput(j))); %隐层输出
end;
%从隐含层到输出层
for k=1:OUT_N
TempValue=0;
for j=1:HIDE_N
TempValue = TempValue+V(k,j)*HideLayerOutput(j); %内积
end;
OutLayerInput(k)=TempValue+gama(k); %输出层输入
OutLayerOutput(k)=1/(1+exp(-OutLayerInput(k))); %输出层输出
end;
ForeOutput(ForeDate,t) = OutLayerOutput;
ForeResult(ForeDate-84,t) = OutLayerOutput*PriceAvg(ForeDate,t)*(1.5*MaxPrice(t)-MinPrice(t))+MinPrice(t);
ForeActual(ForeDate-84,t) = SamplePrice(ForeDate,t)*PriceAvg(ForeDate,t)*(1.5*MaxPrice(t)-MinPrice(t))+MinPrice(t);
% ForeResult(ForeDate-44,t) = OutLayerOutput*(1.5*MaxPrice(t)-MinPrice(t))+MinPrice(t);
% ForeActual(ForeDate-44,t) = SamplePrice(ForeDate,t)*(1.5*MaxPrice(t)-MinPrice(t))+MinPrice(t);
end;
end;
ForeError= 100*(ForeResult-ForeActual)./ForeActual;
[ForeResult;ForeActual;ForeError]
figure(1);
subplot(211);
plot(DataOutspread(ForeResult),'k');
hold on;
plot(DataOutspread(ForeActual),'r');
hold on;
subplot(212);
plot(DataOutspread(ForeError),'b');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%End