✅作者简介：热爱科研的Matlab仿真开发者，修心和技术同步精进，matlab项目合作可私信。

🍎个人主页：Matlab科研工作室

🍊个人信条：格物致知。

## ⛄ 部分代码

function [Convergence_curve,bestX]=SSA(N, dim, ub, lb,M,hiddennum_best, inputn, outputn, output_train, inputn_test ,outputps, output_test)

P_percent = 0.2;    % 发现者的种群规模占总种群规模的百分比

pNum = round(N*P_percent);    % 发现者数量20%

SD = pNum/2;      % 警戒者数量10%

ST = 0.8;           % 安全阈值

% 初始化

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

for i = 1:N

%     X(i, :) = lb + (ub - lb) .* rand(1, dim);

fitness1(i) = fitness(X(i, :),hiddennum_best, inputn, outputn, output_train, inputn_test ,outputps, output_test);

end

pFit = fitness1;

pX = X;                            % 与pFit相对应的个体最佳位置

[fMin, bestI] = min(fitness1);      % fMin表示全局最优解

bestX = X(bestI, :);             % bestX表示全局最优位置

%% 开始进化

Convergence_curve = ones(M,1);  % 初始化每次迭代得到的最佳的适应度

%% 迭代寻优

for t = 1 : M

[~, sortIndex] = sort(pFit);            % 排序

[fmax, B] = max(pFit);

worst = X(B, :);

%% 发现者位置更新

r2 = rand(1);

if r2 < ST

for i = 1:pNum      % Equation (3)

r1 = rand(1);

X(sortIndex(i), :) = pX(sortIndex(i), :)*exp(-(i)/(r1*M));

X(sortIndex(i), :) = Bounds(X(sortIndex(i), :), lb, ub);

fitness1(sortIndex(i)) = fitness(X(sortIndex(i), :),hiddennum_best, inputn, outputn, output_train, inputn_test ,outputps, output_test);

end

else

for i = 1:pNum

X(sortIndex(i), :) = pX(sortIndex(i), :)+randn(1)*ones(1, dim);

X(sortIndex(i), :) = Bounds(X(sortIndex(i), :), lb, ub);

fitness1(sortIndex(i)) = fitness(X(sortIndex(i), :),hiddennum_best, inputn, outputn, output_train, inputn_test ,outputps, output_test);

end

end

[~, bestII] = min(fitness1);

bestXX = X(bestII, :);

%% 跟随者位置更新

for i = (pNum+1):N                     % Equation (4)

A = floor(rand(1, dim)*2)*2-1;

if i > N/2

X(sortIndex(i), :) = randn(1)*exp((worst-pX(sortIndex(i), :))/(i)^2);

else

X(sortIndex(i), :) = bestXX+(abs((pX(sortIndex(i), :)-bestXX)))*(A'*(A*A')^(-1))*ones(1, dim);

end

X(sortIndex(i), :) = Bounds(X(sortIndex(i), :), lb, ub);

fitness1(sortIndex(i)) = fitness(X(sortIndex(i), :),hiddennum_best, inputn, outputn, output_train, inputn_test ,outputps, output_test);

end

%% 警戒者位置更新

c = randperm(numel(sortIndex));

b = sortIndex(c(1:SD));

for j = 1:length(b)      % Equation (5)

if pFit(sortIndex(b(j))) > fMin

X(sortIndex(b(j)), :) = bestX+(randn(1, dim)).*(abs((pX(sortIndex(b(j)), :) -bestX)));

else

X(sortIndex(b(j)), :) = pX(sortIndex(b(j)), :)+(2*rand(1)-1)*(abs(pX(sortIndex(b(j)), :)-worst))/(pFit(sortIndex(b(j)))-fmax+1e-50);

end

X(sortIndex(b(j)), :) = Bounds(X(sortIndex(b(j)), :), lb, ub);

fitness1(sortIndex(b(j))) = fitness(X(sortIndex(b(j)), :),hiddennum_best, inputn, outputn, output_train, inputn_test ,outputps, output_test);

end

for i = 1:N

% 更新个体最优

if fitness1(i) < pFit(i)

pFit(i) = fitness1(i);

pX(i, :) = X(i, :);

end

% 更新全局最优

if pFit(i) < fMin

fMin = pFit(i);

bestX = pX(i, :);

end

end

Convergence_curve(t) = fMin;

disp(['SSA: At iteration ', num2str(t), ' ,the best fitness is ', num2str(fMin)]);

end

## ⛄ 参考文献

[1]李军, 李大超. 基于优化核极限学习机的风电功率时间序列预测[J]. 物理学报, 2016(13):10.

[2]冯磊华, 张杰, 詹毅. 基于改进麻雀搜索算法和核极限学习机的电站锅炉燃烧优化[J]. 热力发电, 2022(009):051.