ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

【预测模型】基于殖民竞争算法优化BP神经网络进行风电功率预测matlab源码

2021-09-14 23:02:08  阅读:101  来源: 互联网

标签:AlgorithmParams 模型 神经网络 源码 BP matlab output ProblemParams


1 算法介绍

说明:1.1节主要是概括和帮助理解考虑影响因素的BP神经网络算法原理,即常规的BP模型训练原理讲解(可根据自身掌握的知识是否跳过)。1.2节开始讲基于历史值影响的BP神经网络预测模型。

使用BP神经网络进行预测时,从考虑的输入指标角度,主要有两类模型:




1.1 受相关指标影响的BP神经网络算法原理

如图一所示,使用MATLAB的newff函数训练BP时,可以看到大部分情况是三层的神经网络(即输入层,隐含层,输出层)。这里帮助理解下神经网络原理:
1)输入层:相当于人的五官,五官获取外部信息,对应神经网络模型input端口接收输入数据的过程。
2)隐含层:对应人的大脑,大脑对五官传递来的数据进行分析和思考,神经网络的隐含层hidden Layer对输入层传来的数据x进行映射,简单理解为一个公式hiddenLayer_output=F(w*x+b)。其中,w、b叫做权重、阈值参数,F()为映射规则,也叫激活函数,hiddenLayer_output是隐含层对于传来的数据映射的输出值。换句话说,隐含层对于输入的影响因素数据x进行了映射,产生了映射值。
3)输出层:可以对应为人的四肢,大脑对五官传来的信息经过思考(隐含层映射)之后,再控制四肢执行动作(向外部作出响应)。类似地,BP神经网络的输出层对hiddenLayer_output再次进行映射,outputLayer_output=w *hiddenLayer_output+b。其中,w、b为权重、阈值参数,outputLayer_output是神经网络输出层的输出值(也叫仿真值、预测值)(理解为,人脑对外的执行动作,比如婴儿拍打桌子)。
4)梯度下降算法:通过计算outputLayer_output和神经网络模型传入的y值之间的偏差,使用算法来相应调整权重和阈值等参数。这个过程,可以理解为婴儿拍打桌子,打偏了,根据偏离的距离远近,来调整身体使得再次挥动的胳膊不断靠近桌子,最终打中。

再举个例子来加深理解:

图一所示BP神经网络,具备输入层、隐含层和输出层。BP是如何通过这三层结构来实现输出层的输出值outputLayer_output,不断逼近给定的y值,从而训练得到一个精准的模型的呢?

从图中串起来的端口,可以想到一个过程:坐地铁,将图一想象为一条地铁线路。王某某坐地铁回家的一天:在input起点站上车,中途经过了很多站(hiddenLayer),然后发现坐过头了(outputLayer对应现在的位置),那么王某某将会根据现在的位置离家(目标Target)的距离(误差Error),返回到中途的地铁站(hiddenLayer)重新坐地铁(误差反向传递,使用梯度下降算法更新w和b),如果王某某又一次发生失误,那么将再次进行这个调整的过程。

从在婴儿拍打桌子和王某某坐地铁的例子中,思考问题:BP的完整训练,需要先传入数据给input,再经过隐含层的映射,输出层得到BP仿真值,根据仿真值与目标值的误差,来调整参数,使得仿真值不断逼近目标值。比如(1)婴儿受到了外界的干扰因素(x),从而作出反应拍桌(predict),大脑不断的调整胳膊位置,控制四肢拍准(y、Target)。(2)王某某上车点(x),过站点(predict),不断返回中途站来调整位置,到家(y、Target)。

在这些环节中,涉及了影响因素数据x,目标值数据y(Target)。根据x,y,使用BP算法来寻求x与y之间存在的规律,实现由x来映射逼近y,这就是BP神经网络算法的作用。再多说一句,上述讲的过程,都是BP模型训练,那么最终得到的模型虽然训练准确,但是找到的规律(bp network)是否准确与可靠呢。于是,我们再给x1到训练好的bp network中,得到相应的BP输出值(预测值)predict1,通过作图,计算Mse,Mape,R方等指标,来对比predict1和y1的接近程度,就可以知道模型是否预测准确。这是BP模型的测试过程,即实现对数据的预测,并且对比实际值检验预测是否准确。
在这里插入图片描述
图一 3层BP神经网络结构图

1.2 基于历史值影响的BP神经网络

以电力负荷预测问题为例,进行两种模型的区分。在预测某个时间段内的电力负荷时:

一种做法,是考虑 t 时刻的气候因素指标,比如该时刻的空气湿度x1,温度x2,以及节假日x3等的影响,对 t 时刻的负荷值进行预测。这是前面1.1所说的模型。

另一种做法,是认为电力负荷值的变化,与时间相关,比如认为t-1,t-2,t-3时刻的电力负荷值与t时刻的负荷值有关系,即满足公式y(t)=F(y(t-1),y(t-2),y(t-3))。采用BP神经网络进行训练模型时,则输入到神经网络的影响因素值为历史负荷值y(t-1),y(t-2),y(t-3),特别地,3叫做自回归阶数或者延迟。给到神经网络中的目标输出值为y(t)。

 1.3 帝国殖民竞争算法

 

 

2 部分代码

% 清除环境变量
close all
clc; clear

%% Problem Statement
ProblemParams.CostFuncName = 'fitcal';    % You should state the name of your cost function here.
ProblemParams.CostFuncExtraParams = [];
ProblemParams.NPar = 31;                           % Number of optimization variables of your objective function. "NPar" is the dimention of the optimization problem.
ProblemParams.VarMin = -1;                         % Lower limit of the optimization parameters. You can state the limit in two ways. 1)   2)
ProblemParams.VarMax = 1;                       % Lower limit of the optimization parameters. You can state the limit in two ways. 1)   2)

% Modifying the size of VarMin and VarMax to have a general form
if numel(ProblemParams.VarMin)==1   %numel 数组元素个数计数
    ProblemParams.VarMin=repmat(ProblemParams.VarMin,1,ProblemParams.NPar); %复制矩阵,行数不变,仍然是roblemParams.VarMin,列数重复ProblemParams.NPar遍ProblemParams.VarMin
    ProblemParams.VarMax=repmat(ProblemParams.VarMax,1,ProblemParams.NPar);
end

ProblemParams.SearchSpaceSize = ProblemParams.VarMax - ProblemParams.VarMin; %搜索区间

%% Algorithmic Parameter Setting
AlgorithmParams.NumOfCountries = 200;               % Number of initial countries.
AlgorithmParams.NumOfInitialImperialists = 10;      % Number of Initial Imperialists.
AlgorithmParams.NumOfAllColonies = AlgorithmParams.NumOfCountries - AlgorithmParams.NumOfInitialImperialists;
AlgorithmParams.NumOfDecades = 100;      %迭代次数
AlgorithmParams.RevolutionRate = 0.3;               % Revolution is the process in which the socio-political characteristics of a country change suddenly.
AlgorithmParams.AssimilationCoefficient = 2;        % In the original paper assimilation coefficient is shown by "beta".  每次趋近的系数
AlgorithmParams.AssimilationAngleCoefficient = .5;  % In the original paper assimilation angle coefficient is shown by "gama".  夹角度数
AlgorithmParams.Zeta = 1;                        % Total Cost of Empire = Cost of Imperialist + Zeta * mean(Cost of All Colonies);
AlgorithmParams.DampRatio = 0.99;
AlgorithmParams.StopIfJustOneEmpire = false;         % Use "true" to stop the algorithm when just one empire is remaining. Use "false" to continue the algorithm. 停止迭代的标志
AlgorithmParams.UnitingThreshold = 0.0001;            % The percent of Search Space Size, which enables the uniting process of two Empires.

zarib = 1.05;                       % **** Zarib is used to prevent the weakest impire to have a probability equal to zero
alpha = 0.03;                        % **** alpha is a number in the interval of [0 1] but alpha<<1. alpha denotes the importance of mean minimum compare to the global mimimum.

%% Display Setting
DisplayParams.PlotEmpires = true;    % "true" to plot. "false" to cancel ploting. 殖民者参数
if DisplayParams.PlotEmpires
    DisplayParams.EmpiresFigureHandle = figure('Name','Plot of Empires','NumberTitle','off');
    DisplayParams.EmpiresAxisHandle = axes;
end

DisplayParams.PlotCost = true;    % "true" to plot. "false" 消耗参数
if DisplayParams.PlotCost
    DisplayParams.CostFigureHandle = figure('Name','Plot of Minimum and Mean Costs','NumberTitle','off');
    DisplayParams.CostAxisHandle = axes; 
end

ColorMatrix = [1   0   0  ; 0 1   0    ; 0   0 1    ; 1   1   0  ; 1   0 1    ; 0 1   1    ; 1 1 1       ;
               0.5 0.5 0.5; 0 0.5 0.5  ; 0.5 0 0.5  ; 0.5 0.5 0  ; 0.5 0 0    ; 0 0.5 0    ; 0 0 0.5     ;
               1   0.5 1  ; 0.1*[1 1 1]; 0.2*[1 1 1]; 0.3*[1 1 1]; 0.4*[1 1 1]; 0.5*[1 1 1]; 0.6*[1 1 1]];
DisplayParams.ColorMatrix = [ColorMatrix ; sqrt(ColorMatrix)]; %sqrt 平方根,什么用?

DisplayParams.AxisMargin.Min = ProblemParams.VarMin;
DisplayParams.AxisMargin.Max = ProblemParams.VarMax;
%%
for i=1
%% Creation of Initial Empires
InitialCountries = GenerateNewCountry(AlgorithmParams.NumOfCountries , ProblemParams);%建立国家 子函数调用 一个列向量 (AlgorithmParams.NumOfCountriesx1) 值为约束范围内的随机数

% Calculates the cost of each country. The less the cost is, the more is the power.
if isempty(ProblemParams.CostFuncExtraParams)
    InitialCost = feval(ProblemParams.CostFuncName,InitialCountries);    
else
    InitialCost = feval(ProblemParams.CostFuncName,InitialCountries,ProblemParams.CostFuncExtraParams);
end
[InitialCost,SortInd] = sort(InitialCost);                          % Sort the cost in assending order. The best countries will be in higher places 排序 每行从小到大
InitialCountries = InitialCountries(SortInd,:);                     % Sort the population with respect to their cost. 按照相关耗费给国家排序

%stop 调试所用
Empires = CreateInitialEmpires(InitialCountries,InitialCost,AlgorithmParams, ProblemParams);%子函数调用 得到帝国种群(殖民国加殖民地)

%% Main Loop
MinimumCost = repmat(nan,AlgorithmParams.NumOfDecades,1); %把nan复制了AlgorithmParams.NumOfDecades次,一个超长的列向量
MeanCost = repmat(nan,AlgorithmParams.NumOfDecades,1);

if DisplayParams.PlotCost
    axes(DisplayParams.CostAxisHandle);
    if any(findall(0)==DisplayParams.CostFigureHandle)
        h_MinCostPlot=plot(MinimumCost,'r','LineWidth',1.5,'YDataSource','MinimumCost');
        hold on;
        h_MeanCostPlot=plot(MeanCost,'k:','LineWidth',1.5,'YDataSource','MeanCost');
        hold off;
        pause(0.05);
    end
end

for Decade = 1:AlgorithmParams.NumOfDecades
    AlgorithmParams.RevolutionRate = AlgorithmParams.DampRatio * AlgorithmParams.RevolutionRate;%进化率=0.99*原进化率

    Remained = AlgorithmParams.NumOfDecades - Decade
    for ii = 1:numel(Empires)
        %% Assimilation同化;  Movement of Colonies Toward Imperialists (Assimilation Policy)
        Empires(ii) = AssimilateColonies(Empires(ii),AlgorithmParams,ProblemParams);%子函数调用

        %% Revolution;  A Sudden Change in the Socio-Political Characteristics    有部分用重新生成的国家替代 
        Empires(ii) = RevolveColonies(Empires(ii),AlgorithmParams,ProblemParams);%子函数调用
        
        %% New Cost Evaluation
        if isempty(ProblemParams.CostFuncExtraParams)
            Empires(ii).ColoniesCost = feval(ProblemParams.CostFuncName,Empires(ii).ColoniesPosition);
        else
            Empires(ii).ColoniesCost = feval(ProblemParams.CostFuncName,Empires(ii).ColoniesPosition,ProblemParams.CostFuncExtraParams);
        end
            
        %% Empire Possession  (****** Power Possession, Empire Possession)
        Empires(ii) = PossesEmpire(Empires(ii));%子函数调用
        
        %% Computation of Total Cost for Empires
        Empires(ii).TotalCost = Empires(ii).ImperialistCost + AlgorithmParams.Zeta * mean(Empires(ii).ColoniesCost);
        
    end

    %% Uniting Similiar Empires
    Empires = UniteSimilarEmpires(Empires,AlgorithmParams,ProblemParams);%子函数调用

    %% Imperialistic Competition
    Empires = ImperialisticCompetition(Empires);%子函数调用
    
    if numel(Empires) == 1 && AlgorithmParams.StopIfJustOneEmpire%如果只剩下一个殖民者
        break
    end

    %% Displaying the Results
    DisplayEmpires(Empires,AlgorithmParams,ProblemParams,DisplayParams);%子函数调用
    
    ImerialistCosts = [Empires.ImperialistCost];
   [MinimumCost(Decade),index] = min(ImerialistCosts);% 调试 MinimumCost(Decade) = min(ImerialistCosts);
    MeanCost(Decade) = mean(ImerialistCosts);
    
 %% 导出

if Decade == AlgorithmParams.NumOfDecades
   positions(i,:)=[Empires(index).ImperialistPosition ];
     results(i,:)=[MinimumCost(Decade)];
     
end

    if DisplayParams.PlotCost
        refreshdata(h_MinCostPlot);
     %   refreshdata(h_MeanCostPlot);
        drawnow; grid on;hold on;
        xlabel('迭代代数/次');ylabel('目标函数值');
        pause(0.01); 
    end 

%     if DisplayParams.PlotCost
%         refreshdata(h_MinCostPlot);%刷新图片的数据
%         refreshdata(h_MeanCostPlot);
%         drawnow;grid on;hold on;
%         xlabel('迭代代数/次');ylabel('目标函数值');
%         pause(0.01);
%     end
    
end % End of Algorithm
%%
end

MinimumCost(end)
save Cost MinimumCost
save pos positions

3 仿真结果

4 参考文献

[1]张镱议,焦健,汪可,郑含博,房加珂,周浩.基于帝国殖民竞争算法优化支持向量机的电力变压器故障诊断模型[J].电力自动化设备,2018,38(01):99-104.

 

标签:AlgorithmParams,模型,神经网络,源码,BP,matlab,output,ProblemParams
来源: https://blog.csdn.net/m0_60703264/article/details/120298602

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有