%% 清空环境变量
warning off % 关闭报警信息
close all % 关闭开启的图窗
clear % 清空变量
clc % 清空命令行
%% 导入数据
res = xlsread('特征值选择_0mm.xlsx');
%% 划分训练集和测试集
temp = randperm(91);
P_train = res(temp(1: 80), 1: 6)';
T_train = res(temp(1: 80), 7)';
M = size(P_train, 2);
P_test = res(temp(81: end), 1: 6)';
T_test = res(temp(81: end), 7)';
N = size(P_test, 2);
%% 数据归一化
[p_train, ps_input] = mapminmax(P_train, 0, 1);
p_test = mapminmax('apply', P_test, ps_input);
[t_train, ps_output] = mapminmax(T_train, 0, 1);
t_test = mapminmax('apply', T_test, ps_output);
%% 节点个数
inputnum = size(p_train, 1); % 输入层节点数
hiddennum = 5; % 隐藏层节点数
outputnum = size(t_train, 1); % 输出层节点数
%% 设置遗传算法参数
maxgen = 30; % 迭代次数
sizepop = 5; % 种群规模
pcross = 0.7; % 交叉概率
pmutation = 0.1; % 变异概率
%% 初始化种群
pop = rand(sizepop, inputnum * hiddennum + hiddennum + hiddennum * outputnum + outputnum);
%% 进化
for i = 1:maxgen
% 计算适应度
fitness = zeros(sizepop, 1);
for j = 1:sizepop
fitness(j) = fun(pop(j,:), hiddennum, p_train, t_train, ps_output);
end
% 选择
[~, idx] = sort(fitness);
best_individual = pop(idx(1), :);
% 交叉
for j = 1:2:sizepop-1
if rand < pcross
parent1 = pop(j, :);
parent2 = pop(j+1, :);
cross_point = randi([1, numel(parent1)]);
child1 = [parent1(1:cross_point), parent2(cross_point+1:end)];
child2 = [parent2(1:cross_point), parent1(cross_point+1:end)];
pop(j, :) = child1;
pop(j+1, :) = child2;
end
end
% 变异
for j = 1:sizepop
if rand < pmutation
mutate_point = randi([1, numel(pop(j, :))]);
pop(j, mutate_point) = rand;
end
end
% 保留最优个体
pop(end, :) = best_individual;
end
%% 提取最优初始权值和阈值
w1 = best_individual(1 : inputnum * hiddennum);
B1 = best_individual(inputnum * hiddennum + 1 : inputnum * hiddennum + hiddennum);
w2 = best_individual(inputnum * hiddennum + hiddennum + 1 : ...
inputnum * hiddennum + hiddennum + hiddennum * outputnum);
B2 = best_individual(inputnum * hiddennum + hiddennum + hiddennum * outputnum + 1 : ...
inputnum * hiddennum + hiddennum + hiddennum * outputnum + outputnum);
%% 神经网络赋值
net = newff(p_train, t_train, hiddennum);
net.Iw{1, 1} = reshape(w1, hiddennum, inputnum );
net.Lw{2, 1} = reshape(w2, outputnum, hiddennum);
net.b{1} = reshape(B1, hiddennum, 1);
net.b{2} = B2';
%% 网络训练
net.trainParam.showWindow = 1; % 打开训练窗口
net = train(net, p_train, t_train);
%% 仿真预测
t_sim1 = sim(net, p_train);
t_sim2 = sim(net, p_test );
%% 数据反归一化
T_sim1 = mapminmax('reverse', t_sim1, ps_output);
T_sim2 = mapminmax('reverse', t_sim2, ps_output);
%% 均方根误差
error1 = sqrt(sum((T_sim1 - T_train).^2, 2)' ./ M);
error2 = sqrt(sum((T_sim2 - T_test) .^2, 2)' ./ N);
%% 绘图
figure
plot(1: M, T_train, 'r-*', 1: M, T_sim1, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'训练集预测结果对比'; ['RMSE=' num2str(error1)]};
title(string)
xlim([1, M])
grid
figure
plot(1: N, T_test, 'r-*', 1: N, T_sim2, 'b-o', 'LineWidth', 1)
legend('真实值', '预测值')
xlabel('预测样本')
ylabel('预测结果')
string = {'测试集预测结果对比'; ['RMSE=' num2str(error2)]};
title(string)
xlim([1, N])
grid
没有合适的资源?快使用搜索试试~ 我知道了~
温馨提示
GA-BP神经网络是一种结合了遗传算法(GA)和反向传播神经网络(BP)的模型,用于多变量单结果的预测问题。下面我将对这个模型进行描述: 多变量单结果的预测问题: 多变量:指输入数据包含多个变量或特征,例如多个传感器采集到的数据。 单结果:指模型的输出是一个结果,例如对某个目标的预测或分类。 遗传算法(GA): GA是一种启发式搜索算法,模拟了自然界中的生物进化过程,通过对潜在解的搜索和优化来寻找问题的最优解。 在GA中,解决方案被表示为个体的染色体,通过交叉和变异等操作产生新的个体,并通过选择策略保留适应度高的个体。 在GA-BP神经网络中,GA用于优化BP神经网络的权重和阈值,以提高模型的预测性能。 适合人群:工科类研究、本科或研究生课设
资源推荐
资源详情
资源评论
收起资源包目录
GA_BP.rar (3个子文件)
GA_BP
特征值选择_0mm.xlsx 27KB
fun.m 1KB
GA_BP.m 4KB
共 3 条
- 1
资源评论
qq_45576745
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功