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

120 阅读3分钟

 1 算法介绍

 

 

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.

5 MATLAB代码与数据下载地址

见博客主页