## 1 简介

BSA 算法优化 BP 神经网络的基本思想是: 利 用 BSA 算法的全局搜索能力, 优化 BP 神经网络初始的权值和阈值, 也就是决策变量, 其中每一组决策变量均包含在鸟群个体所处的空间位置中. 然后, 通过适应度函数来衡量个体所处空间位置的优劣度, 并利用鸟群觅食过程中的觅食行为、警戒行为和飞行行为等策略不断更新个体空间位置, 直至获取最佳的个体空间位置, 即获得待优化问题的最佳决策变量

BSA-BP 算法预测 PMV 指标主要包括以下几个部分: 确定训练样本数据、设计 BP 神经网络结构、利用 BSA 算法优化 BP 神经网络初始的权值和阈值、训练优化后的网络. 具体实现步骤如下:

5.步骤 5. BSA 算法优化 BP 神经网络初始的权值和阈值. 依据 BSA 算法的步骤, 不断迭代进行寻优, 直到迭代停止, 输出全局最优值, 也就是最优网络初始的权值和阈值, 并将其赋给 BP 神经网络.

## 2 部分代码

```% ------------------------------------------------------------------------
% Bird Swarm Algorithm (BSA) (demo)
% This is a simple demo version only implemented the basic idea of BSA for
% solving the unconstrained problem, namely Sphere function.
%
%
% The parameters in BSA are presented as follows.
% FitFunc    % The objective function
% M          % Maxmimal generations (iterations)
% pop        % Population size
% dim        % Dimension
% FQ         % The frequency of birds' flight behaviours
% c1         % Cognitive accelerated coefficient
% c2         % Social accelerated coefficient
% a1, a2     % Two paramters which are related to the indirect and direct
%              effect on the birds' vigilance bahaviors.
%
% Using the default value, BSA can be executed using the following code.
% [ bestX, fMin ] = BSA
% ------------------------------------------------------------------------
% Main programs
function [ bestX, fMin ,yy] = BSA( FitFunc, M, pop, dim, FQ, c1, c2, a1, a2 )
% Display help
help BSA.m
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% set the default parameters
% set the parameters
lb= -100*ones( 1,dim );   % Lower bounds
ub= 100*ones( 1,dim );    % Upper bounds
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Initialization
for i = 1 : pop
x( i, : ) = lb + (ub - lb) .* rand( 1, dim );
fit( i ) = FitFunc( x( i, : ) );
end
pFit = fit; % The individual's best fitness value
pX = x;     % The individual's best position corresponding to the pFit
[ fMin, bestIndex ] = min( fit );  % fMin denotes the global optimum
% bestX denotes the position corresponding to fMin
bestX = x( bestIndex, : );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Start the iteration.
for iteration = 1 : M
prob = rand( pop, 1 ) .* 0.2 + 0.8;%The probability of foraging for food
if( mod( iteration, FQ ) ~= 0 )
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Birds forage for food or keep vigilance
sumPfit = sum( pFit );
meanP = mean( pX );
for i = 1 : pop
if rand < prob(i)
x( i, : ) = x( i, : ) + c1 * rand.*(bestX - x( i, : ))+ ...
c2 * rand.*( pX(i,:) - x( i, : ) );
else
person = randiTabu( 1, pop, i, 1 );
x( i, : ) = x( i, : ) + rand.*(meanP - x( i, : )) * a1 * ...
exp( -pFit(i)/( sumPfit + realmin) * pop ) + a2 * ...
( rand*2 - 1) .* ( pX(person,:) - x( i, : ) ) * exp( ...
-(pFit(person) - pFit(i))/(abs( pFit(person)-pFit(i) )...
+ realmin) * pFit(person)/(sumPfit + realmin) * pop );
end
x( i, : ) = Bounds( x( i, : ), lb, ub );
fit( i ) = FitFunc( x( i, : ) );
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
else
FL = rand( pop, 1 ) .* 0.4 + 0.5;    %The followed coefficient
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Divide the bird swarm into two parts: producers and scroungers.
[ans, minIndex ] = min( pFit );
[ans, maxIndex ] = max( pFit );
choose = 0;
if ( minIndex < 0.5*pop && maxIndex < 0.5*pop )
choose = 1;
end
if ( minIndex > 0.5*pop && maxIndex < 0.5*pop )
choose = 2;
end
if ( minIndex < 0.5*pop && maxIndex > 0.5*pop )
choose = 3;
end
if ( minIndex > 0.5*pop && maxIndex > 0.5*pop )
choose = 4;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
if choose < 3
for i = (pop/2+1) : pop
x( i, : ) = x( i, : ) * ( 1 + randn );
x( i, : ) = Bounds( x( i, : ), lb, ub );
fit( i ) = FitFunc( x( i, : ) );
end
if choose == 1
x( minIndex,: ) = x( minIndex,: ) * ( 1 + randn );
x( minIndex, : ) = Bounds( x( minIndex, : ), lb, ub );
fit( minIndex ) = FitFunc( x( minIndex, : ) );
end
for i = 1 : 0.5*pop
if choose == 2 || minIndex ~= i
person = randi( [(0.5*pop+1), pop ], 1 );
x( i, : ) = x( i, : ) + (pX(person, :) - x( i, : )) * FL( i );
x( i, : ) = Bounds( x( i, : ), lb, ub );
fit( i ) = FitFunc( x( i, : ) );
end
end
else
for i = 1 : 0.5*pop
x( i, : ) = x( i, : ) * ( 1 + randn );
x( i, : ) = Bounds( x( i, : ), lb, ub );
fit( i ) = FitFunc( x( i, : ) );
end
if choose == 4
x( minIndex,: ) = x( minIndex,: ) * ( 1 + randn );
x( minIndex, : ) = Bounds( x( minIndex, : ), lb, ub );
fit( minIndex ) = FitFunc( x( minIndex, : ) );
end
for i = (0.5*pop+1) : pop
if choose == 3 || minIndex ~= i
person = randi( [1, 0.5*pop], 1 );
x( i, : ) = x( i, : ) + (pX(person, :) - x( i, : )) * FL( i );
x( i, : ) = Bounds( x( i, : ), lb, ub );
fit( i ) = FitFunc( x( i, : ) );
end
end
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Update the individual's best fitness vlaue and the global best one
for i = 1 : pop
if ( fit( i ) < pFit( i ) )
yy(iteration)=fit( i );
pFit( i ) = fit( i );
pX( i, : ) = x( i, : );
end
if( pFit( i ) < fMin )
fMin = pFit( i );
yy(iteration)= fMin ;
bestX = pX( i, : );
else
%             yy(iteration)=yy(iteration-1);
end
end
end
% End of the main program
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% The following functions are associated with the main program
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% This function is the objective function```

## 4 参考文献

[1]郭彤颖, 陈露. 基于鸟群算法优化BP神经网络的热舒适度预测[J]. 计算机系统应用, 2018, 27(4):5.