%% 基于模拟退火改进狮群算法elmk预测
clc;
clear all
close all
nntwarn off;
%% 数据载入
load data;
a=data;
%% 选取训练数据和测试数据
N=size(a,1);%样本数量
M=size(a,2);%数据维度
rate=0.6;%采样率
% 训练数据输入
for i=1:N-3
p(i,:)=[a(i,:),a(i+1,:),a(i+2,:)];
end
p_train=a(1:round(N*rate),1:M-1);
% 训练数据输出
t_train=a(1:round(N*rate),M);
% 测试数据输入
p_test=a((round(N*rate)+1):N,1:M-1);
% 测试数据输出
t_test=a((round(N*rate)+1):N,M);
% 为适应网络结构 做转置
%% 数据归一化
p_train=p_train';%n*m数据,n是输入特征数量,m是样本数量
t_train=t_train';%n*m数据,n是输入特征数量,m是样本数量
[p_train, ps_input] = mapminmax(p_train,0,1);
[t_train, ps_output] = mapminmax(t_train,0,1);
threshold=[0 2;0 2];%几个n几个[0,1]
shuchu=1;%输出个数
% [p_test ] = mapminmax(p_test' ,0,1);
p_test = mapminmax('apply',p_test',ps_input);
%统计结果
pop=30; % 种群数量
Max_iter=50; % 设定最大迭代次数
dim=1;%隐藏层
beta = 0.5;%成年狮所占比列
Nc = round(pop*beta);%成年狮数量
Np = pop-Nc;%幼师数量
lb=[2];
ub=[15];
if(max(size(ub)) == 1)
ub = ub.*ones(1,dim);
lb = lb.*ones(1,dim);
end
%种群初始化
X0=initialization(pop,dim,ub,lb);
X = X0;
%计算初始适应度值
fitness = zeros(1,pop);
for i = 1:pop
[rmse,y] = elm_fun(p_test,t_test,p_train,t_train,threshold,shuchu,X(i,:), ps_output);
fitness(i) = rmse;
end
T=max(fitness)-min(fitness);%初始温度
[value, index]= min(fitness);%找最小值
GBestF = value;%全局最优适应度值
GBestX = X(index,:);%全局最优位置
GBestY=y;%预测值
curve=zeros(1,Max_iter);
XhisBest = X;
fithisBest = fitness;
indexBest = index;
gbest = GBestX;
for t = 1: Max_iter
t
%母狮移动范围扰动因子计算
stepf = 0.1*(mean(ub) - mean(lb));
alphaf = stepf*exp(-30*t/Max_iter)^10;
%幼狮移动范围扰动因子计算
alpha = (Max_iter - t)/Max_iter;
%母狮位置更新
for i = 1:Nc
index = i;
while(index == i)
index = randi(Nc);%随机挑选一只母狮
end
X(i,:) = (X(i,:) + X(index,:)).*(1 + alphaf.*randn())./2;
end
%幼师位置更新
for i = Nc+1:pop
q=rand;
if q<=1/3
X(i,:) = (gbest + XhisBest(i,:)).*( 1 + alpha.*randn())/2;
elseif q>1/3&&q<2/3
indexT = i;
while indexT == i
indexT = randi(Nc) + pop - Nc;%随机位置
end
X(i,:) = (X(indexT,:) + XhisBest(i,:)).*( 1 + alpha.*randn())/2;
else
gbestT = ub + lb - gbest;
X(i,:) = (gbestT + XhisBest(i,:)).*( 1 + alpha.*randn())/2;
end
end
%边界控制
for j = 1:pop
for a = 1: dim
if(X(j,a)>ub)
X(j,a) =ub(a);
end
if(X(j,a)<lb)
X(j,a) =lb(a);
end
end
end
%计算适应度值
for j=1:pop
[rmse,y] = elm_fun(p_test,t_test,p_train,t_train,threshold,shuchu,X(j,:), ps_output);
fitness(j) = rmse;
end
for j = 1:pop
if(fitness(j)>fithisBest(j))
XhisBest(j,:) = X(j,:);
fithisBest(j) = fitness(j);
end
if(fitness(j) >GBestF)
GBestF = fitness(j);
GBestX = X(j,:);
indexBest = j;
end
end
C=max(fitness)-min(fitness);
S=[1,exp(-C/T)];
if rand<min(S)
%% 狮王更新
Temp = gbest.*(1 + randn().*abs(XhisBest(indexBest,:) - gbest));
Temp(Temp>ub)=ub(Temp>ub);
Temp(Temp<lb) = lb(Temp<lb);
[rmse,y] = elm_fun(p_test,t_test,p_train,t_train,threshold,shuchu,Temp, ps_output);
else
Temp = gbest;
[rmse,y] = elm_fun(p_test,t_test,p_train,t_train,threshold,shuchu,Temp, ps_output);
end
fitTemp = rmse;
if(fitTemp<GBestF)
GBestF =fitTemp;
GBestX = Temp;
GBestY= y;
X(indexBest,:)=Temp;
fitness(indexBest) = fitTemp;
curve(t) = GBestF;
else
curve(t) = GBestF;
GBestY=GBestY;
GBestX = GBestX;
end
% [value, index]= min(fitness);%找最小值
% gbest = X(index,:);%当前代,种群最优值
T=0.9*T;%更新温度
end
Best_pos = GBestX;
Best_score = curve(end);
figure
plot(curve,'Color','r','linewidth',1.5)
title(['模拟退火改进狮群优化elmk预测',num2str(Best_score)])
xlabel('Iteration');
ylabel('Best score obtained so far');