资源简介
在matlab程序中,可以实现粒子群算法优化bp神经网络的算法,可用不同数据
代码片段和文件信息
%% 该代码为基于PSO和BP网络的预测
%
%% 清空环境
clc
clear
%读取数据
% load maydata input output
data=xlsread(‘E:\数据处理\useful1.xlsx‘‘test1‘‘a2:i701‘);
input=data(:1:8);
output =data(:8);
%从1到326间随机排序
k=rand(1700);
[mn]=sort(k);
%节点个数
inputnum=8;
hiddennum=5;
outputnum=1;
%训练数据和预测数据
% input_train=input(1:299:)‘;
% input_test=input(300:326:)‘;
% output_train=output(1:299)‘;
% output_test=output(300:326)‘;
input_train=input(n(1:624):)‘;
output_train=output(n(1:624):)‘;
input_test=input(n(625:700):)‘;
output_test=output(n(625:700):)‘;
input_test1=input(651:700:)‘;
%选连样本输入输出数据归一化
[inputninputps]=mapminmax(input_train);
[outputnoutputps]=mapminmax(output_train);
%构建网络
net=newff(inputnoutputnhiddennum);
netBP=newff(inputnoutputnhiddennum);
% 参数初始化
%粒子群算法中的两个参数
c1 = 1.49445; %个体学习因子
c2 = 1.49445; %社会学习因子
maxgen=50; % 进化次数
sizepop=10; %种群规模
Vmax=1; %设置精度(在已知最小值时候用)
Vmin=-1;
popmax=5;
popmin=-5;
parnum = inputnum * hiddennum + hiddennum * outputnum + hiddennum + outputnum; %51
for i=1:sizepop %初始化种群个数
pop(i:)=5*rands(1parnum);
V(i:)=rands(1parnum);
fitness(i)=fun1(pop(i:)inputnumhiddennumoutputnumnetinputnoutputn);
end
% 个体极值和群体极值
[bestfitness bestindex]=min(fitness);
zbest=pop(bestindex:); %全局最佳
gbest=pop; %个体最佳
fitnessgbest=fitness; %个体最佳适应度值
fitnesszbest=bestfitness; %全局最佳适应度值
%% 迭代寻优
for i=1:maxgen
i
for j=1:sizepop
%速度更新
V(j:) = V(j:) + c1*rand*(gbest(j:) - pop(j:)) + c2*rand*(zbest - pop(j:));
V(jfind(V(j:)>Vmax))=Vmax;
V(jfind(V(j:)
%种群更新
pop(j:)=pop(j:)+0.2*V(j:);
pop(jfind(pop(j:)>popmax))=popmax;
pop(jfind(pop(j:)
%自适应变异
pos=unidrnd(21);
if rand>0.95
pop(jpos)=5*rands(11);
end
%适应度值
fitness(j)=fun1(pop(j:)inputnumhiddennumoutputnumnetinputnoutputn);
end
for j=1:sizepop
%个体最优更新
if fitness(j) < fitnessgbest(j)
gbest(j:) = pop(j:);
fitnessgbest(j) = fitness(j);
end
%群体最优更新
if fitness(j) < fitnesszbest
zbest = pop(j:);
fitnesszbest = fitness(j);
end
end
yy(i)=fitnesszbest;
end
%% 结果分析
plot(yy)
title([‘适应度曲线 ‘ ‘终止代数=‘ num2str(maxgen)]);
xlabel(‘进化代数‘);ylabel(‘适应度‘);
x=zbest;
%% 把最优初始阀值权值赋予网络预测
% %用遗传算法优化的BP网络进行值预测
w1=x(1:inputnum*hiddennum);
B1=x(inputnum*hiddennum+1:inputnum*hiddennum+hiddennum);
w2=x(inputnum*hiddennum+hiddennum+1:inputnum*hiddennum+hiddennum+hiddennum*outputnum);
B2=x(inputnum*hiddennum+hiddennum+hiddennum*outputnum+1:inputnum*hiddennum+hiddennum+hidd
相关资源
- Matlab路面裂缝识别69319
- 高灵敏度GPS接收机MATLAB仿真,附捕获
- 基于MATLAB的质点弹道计算与外弹道优
- 阵列天线的matlab仿真
- MATLAB 经典程序源代码大全
- MATLAB小波软阈值去噪代码33473
- 天线阵的波束形成在MATLAB仿真程序及
- 非线性SVM算法-matlab实现
- 《MATLAB 智能算法超级学习手册》-程序
- 组合导航matlab程序
- 读取txt文件内容matlab代码实现
- Matlab实现基于相关的模板匹配程序
- matlab优化工具箱讲解
- 基于MATLAB的快速傅里叶变换
- 光纤传输中的分布傅立叶算法matlab实
- 基于matlab的图像处理源程序
- matlab 椭圆拟合程序
- 算术编码解码matlab源代码
- optical_flow 光流法 matlab 实现程序
- 引导图像滤波器 Matlab实现
- 分形几何中一些经典图形的Matlab画法
- OFDM系统MATLAB仿真代码
- SVM工具箱(matlab中运行)
- 图像小波变换MatLab源代码
- LU分解的MATLAB实现
- 冈萨雷斯数字图像处理matlab版(第三
- 替代数据法的matlab程序
- 用matlab实现的多站定位系统性能仿真
- 通过不同方法进行粗糙集属性约简m
- k近邻算法matlab实现
评论
共有 条评论