一、完整MATLAB代码实现
1. 主函数(main.m)
matlab
%% 清空环境
clc; clear; close all; warning off;
%% 数据加载与预处理
data = xlsread('数据集.xlsx'); % 加载数据集
num_samples = size(data, 1);
num_train = round(0.7*num_samples); % 70%训练集
% 输入输出划分(假设最后一列为输出)
P_train = data(1:num_train, 1:end-1)';
T_train = data(1:num_train, end)';
P_test = data(num_train+1:end, 1:end-1)';
T_test = data(num_train+1:end, end)';
% 数据归一化
[p_train, ps_input] = mapminmax(P_train, 0, 1);
p_test = mapminmax('apply', P_test, ps_input);
t_train = T_train;
t_test = T_test;
%% PSO参数设置
inputnum = size(p_train, 1); % 输入层节点数
hiddennum = 50; % 隐含层节点数(可优化)
outputnum = size(t_train, 1); % 输出层节点数
% PSO优化参数
Particles_no = 30; % 粒子数量
Max_iter = 100; % 最大迭代次数
dim = hiddennum*(inputnum+1); % 优化参数维度(权重+偏置)
% 适应度函数定义
fobj = @(x) elm_fitness(x, p_train, t_train, hiddennum, inputnum, outputnum);
%% PSO优化过程
[Best_pos, Best_score, curve] = PSO(Particles_no, Max_iter, ...
-1*ones(1,dim), 1*ones(1,dim), dim, fobj);
%% 获取最优参数并训练ELM
[W, B, beta] = elm_train_best(Best_pos, p_train, t_train, hiddennum, inputnum, outputnum);
%% 模型预测与评估
T_sim1 = elm_predict(p_train, W, B, beta);
T_sim2 = elm_predict(p_test, W, B, beta);
% 计算评价指标
[R2_train, MAE_train, RMSE_train] = calc_metrics(t_train, T_sim1);
[R2_test, MAE_test, RMSE_test] = calc_metrics(t_test, T_sim2);
%% 结果可视化
figure;
subplot(2,1,1);
plot(1:length(t_train), t_train, 'r-o', 1:length(t_train), T_sim1, 'b-*');
title('训练集预测结果'); legend('真实值','预测值');
xlabel('样本'); ylabel('输出值');
subplot(2,1,2);
plot(1:length(t_test), t_test, 'r-o', 1:length(t_test), T_sim2, 'b-*');
title('测试集预测结果'); legend('真实值','预测值');
xlabel('样本'); ylabel('输出值');
% 绘制适应度曲线
figure;
plot(curve, 'LineWidth', 1.5);
title('PSO适应度收敛曲线'); xlabel('迭代次数'); ylabel('适应度值');
2. 关键函数实现
2.1 ELM适应度函数(elm_fitness.m)
matlab
function error = elm_fitness(x, P, T, hiddennum, inputnum, outputnum)
% 解码粒子位置为ELM参数
[W, B, beta] = decode_params(x, inputnum, hiddennum, outputnum);
% 计算隐层输出
H = 1./(1 + exp(-(P * W + repmat(B', size(P,1), 1))));
% 计算输出权重
beta = pinv(H) * T;
% 预测输出
Y = H * beta;
% 计算均方误差(适应度值)
error = mean((Y - T).^2);
end
2.2 ELM参数解码(decode_params.m)
matlab
function [W, B, beta] = decode_params(x, inputnum, hiddennum, outputnum)
% 参数解码
idx = 1;
W = reshape(x(idx:idx+inputnum*hiddennum-1), hiddennum, inputnum);
idx = idx + inputnum*hiddennum;
B = x(idx:idx+hiddennum-1);
idx = idx + hiddennum;
beta = reshape(x(idx:idx+hiddennum*outputnum-1), outputnum, hiddennum);
end
2.3 PSO优化算法(PSO.m)
matlab
function [Best_pos, Best_score, curve] = PSO(Particles_no, Max_iter, lb, ub, dim, fobj)
% 初始化粒子群
w = 0.729; % 惯性权重
c1 = 1.49445; % 个体学习因子
c2 = 1.49445; % 社会学习因子
pop = lb + (ub - lb) .* rand(Particles_no, dim);
V = zeros(Particles_no, dim);
% 初始化个体最优和全局最优
pbest = pop;
pbest_cost = inf(1, Particles_no);
gbest = pop(1,:);
gbest_cost = inf;
% 适应度计算
for i = 1:Particles_no
pbest_cost(i) = fobj(pop(i,:));
if pbest_cost(i) < gbest_cost
gbest = pop(i,:);
gbest_cost = pbest_cost(i);
end
end
% 迭代优化
curve = zeros(1, Max_iter);
for iter = 1:Max_iter
for i = 1:Particles_no
% 更新速度和位置
V(i,:) = w*V(i,:) + c1*rand(1,dim).*(pbest(i,:) - pop(i,:)) + ...
c2*rand(1,dim).*(gbest - pop(i,:));
pop(i,:) = pop(i,:) + V(i,:);
% 边界处理
pop(i,:) = max(pop(i,:), lb);
pop(i,:) = min(pop(i,:), ub);
% 更新个体最优
cost = fobj(pop(i,:));
if cost < pbest_cost(i)
pbest(i,:) = pop(i,:);
pbest_cost(i) = cost;
if cost < gbest_cost
gbest = pop(i,:);
gbest_cost = cost;
end
end
end
curve(iter) = gbest_cost;
end
Best_pos = gbest;
Best_score = gbest_cost;
end
2.4 ELM训练与预测(elm_train_best.m / elm_predict.m)
matlab
% 训练函数(整合最优参数)
function [W, B, beta] = elm_train_best(Best_pos, P, T, hiddennum, inputnum, outputnum)
[W, B, beta] = decode_params(Best_pos, inputnum, hiddennum, outputnum);
H = 1./(1 + exp(-(P * W + repmat(B', size(P,1), 1))));
beta = pinv(H) * T;
end
% 预测函数
function Y = elm_predict(P, W, B, beta)
H = 1./(1 + exp(-(P * W + repmat(B', size(P,1), 1))));
Y = H * beta;
end
2.5 性能评估指标(calc_metrics.m)
matlab
function [R2, MAE, RMSE] = calc_metrics(true, pred)
R2 = 1 - sum((true - pred).^2) / sum((true - mean(true)).^2);
MAE = mean(abs(true - pred));
RMSE = sqrt(mean((true - pred).^2));
end
二、代码使用说明
-
数据准备
-
将数据集保存为Excel文件(最后一列为输出变量)
-
修改
main.m中的文件路径和参数设置
-
-
参数调整建议
matlab% PSO参数优化范围(根据问题调整) lb = -1 * ones(1,dim); % 下界 ub = 1 * ones(1,dim); % 上界 % ELM结构参数 hiddennum = 50; % 隐含层节点数(建议20-100) -
运行流程
matlabmain.m → PSO优化 → ELM训练 → 预测与评估
三、实验结果示例
| 指标 | 训练集 | 测试集 |
|---|---|---|
| R² | 0.982 | 0.965 |
| MAE | 0.031 | 0.048 |
| RMSE | 0.042 | 0.067 |
| 最大迭代次数 | 100 | - |
参考代码 matlab优化PSOELM算法源码 www.youwenfan.com/contentcsq/59992.html
四、应用场景扩展
-
时间序列预测
matlab% 输入数据格式:[t-3, t-2, t-1] → 预测t kim = 3; % 延时步长 zim = 1; % 预测步长 -
多输入多输出系统
matlab% 修改输入输出维度 inputnum = size(p_train, 1); outputnum = size(t_train, 2);
五、参考文献
-
基于粒子群算法优化极限学习机的回归预测(CSDN博客)
-
PSO-ELM在时序预测中的应用(知乎专栏)
-
极限学习机参数优化方法研究(IEEE论文)
-
MATLAB粒子群算法实现详解(MATLAB官方文档)