## 1 简介

Elman 神经网络是一种局部反馈递归神经网络，包括输入层、隐藏层、承接层和输出层，由于其良好的动态记忆和时变能力，自1990年Elman首次提出以来，Elman神经网络在时间序列预测中得到了广泛的应用，但由于随机选择初始值和阈值，且采用梯度下降法寻优，其网络学习速度较慢且预测的精度比较低。本文利用 GOA 优化 Elman神经网络的初始权值及阈值，提高了 Elman 神经网络的预测精度。

## 2 部分代码

% The Grasshopper Optimization Algorithm

function [TargetFitness,TargetPosition,Convergence_curve,Trajectories,fitness_history, position_history]=GOA(N, Max_iter, lb,ub, dim, fobj)

tic

disp(‘GOA is now estimating the global optimum for your problem….’)

flag=0;

if size(ub,1)==1

ub=ones(dim,1)*ub;

lb=ones(dim,1)*lb;

end

if (rem(dim,2)~=0) % this algorithm should be run with a even number of variables. This line is to handle odd number of variables

dim = dim+1;

ub = [ub; 100];

lb = [lb; -100];

flag=1;

end

%Initialize the population of grasshoppers

GrassHopperPositions=initialization(N,dim,ub,lb);

GrassHopperFitness = zeros(1,N);

fitness_history=zeros(N,Max_iter);

position_history=zeros(N,Max_iter,dim);

Convergence_curve=zeros(1,Max_iter);

Trajectories=zeros(N,Max_iter);

cMax=1;

cMin=0.00004;

%Calculate the fitness of initial grasshoppers

for i=1:size(GrassHopperPositions,1)

if flag == 1

GrassHopperFitness(1,i)=fobj(GrassHopperPositions(i,1:end-1));

else

GrassHopperFitness(1,i)=fobj(GrassHopperPositions(i,:));

end

fitness_history(i,1)=GrassHopperFitness(1,i);

position_history(i,1,:)=GrassHopperPositions(i,:);

Trajectories(:,1)=GrassHopperPositions(:,1);

end

[sorted_fitness,sorted_indexes]=sort(GrassHopperFitness);

% Find the best grasshopper (target) in the first population

for newindex=1:N

Sorted_grasshopper(newindex,:)=GrassHopperPositions(sorted_indexes(newindex),:);

end

TargetPosition=Sorted_grasshopper(1,:);

TargetFitness=sorted_fitness(1);

% Main loop

l=2; % Start from the second iteration since the first iteration was dedicated to calculating the fitness of antlions

while l<Max_iter+1

c=cMax-l*((cMax-cMin)/Max_iter); % Eq. (2.8) in the paper

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

for i=1:size(GrassHopperPositions,1)

temp= GrassHopperPositions’;

% for k=1:2:dim

S_i=zeros(dim,1);

for j=1:N

if i~=j

Dist=distance(temp(:,j), temp(:,i)); % Calculate the distance between two grasshoppers

r_ij_vec=(temp(:,j)-temp(:,i))/(Dist+eps); % xj-xi/dij in Eq. (2.7)

xj_xi=2+rem(Dist,2); % |xjd – xid| in Eq. (2.7)

s_ij=((ub – lb)*c/2)*S_func(xj_xi).*r_ij_vec; % The first part inside the big bracket in Eq. (2.7)

S_i=S_i+s_ij;

end

end

S_i_total = S_i;

% end

X_new = c * S_i_total’+ (TargetPosition); % Eq. (2.7) in the paper

GrassHopperPositions_temp(i,:)=X_new’;

end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% GrassHopperPositions

GrassHopperPositions=GrassHopperPositions_temp;

for i=1:size(GrassHopperPositions,1)

% Relocate grasshoppers that go outside the search space

Tp=GrassHopperPositions(i,:)>ub’;Tm=GrassHopperPositions(i,:)<lb’;GrassHopperPositions(i,:)=(GrassHopperPositions(i,:).*(~(Tp+Tm)))+ub’.*Tp+lb’.*Tm;

% Calculating the objective values for all grasshoppers

if flag == 1

GrassHopperFitness(1,i)=fobj(GrassHopperPositions(i,1:end-1));

else

GrassHopperFitness(1,i)=fobj(GrassHopperPositions(i,:));

end

fitness_history(i,l)=GrassHopperFitness(1,i);

position_history(i,l,:)=GrassHopperPositions(i,:);

Trajectories(:,l)=GrassHopperPositions(:,1);

% Update the target

if GrassHopperFitness(1,i)<TargetFitness

TargetPosition=GrassHopperPositions(i,:);

TargetFitness=GrassHopperFitness(1,i);

end

end

Convergence_curve(l)=TargetFitness;

disp([‘In iteration #’, num2str(l), ‘ , target”s objective = ‘, num2str(TargetFitness)])

l = l + 1;

end

if (flag==1)

TargetPosition = TargetPosition(1:dim-1);

end

time=toc

## 4 参考文献

[1]朱昶胜, 康亮河, and 冯文芳. “基于自适应鲸鱼优化算法结合Elman神经网络的股市收盘价预测算法.” 计算机应用 40.5(2020):9.

[2]江远强. “一种基于蝗虫算法优化MLP神经网络的信用风险预测方法.”.