回归预测 | Matlab实现DE-BP差分算法优化BP神经网络多变量回归预测

2024-01-09 13:49:54

回归预测 | Matlab实现DE-BP差分算法优化BP神经网络多变量回归预测

效果一览

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

基本介绍

1.Matlab实现DE-BP差分算法优化BP神经网络多变量回归预测(完整源码和数据)
2.运行环境为Matlab2018b;
3.excel数据集,输入多个特征,输出单个变量,多变量回归预测预测,DE_BP.m为主程序,运行即可,所有文件放在一个文件夹;
4.输出优化前后对比图,误差对比图;
代码特点:参数化编程、参数可方便更改、代码编程思路清晰、注释明细。

程序设计

%%  清空环境变量
%选连样本输入输出数据归一化
[inputn,inputps]=mapminmax(input_train);
[outputn,outputps]=mapminmax(output_train);
%构建网络
net=newff(inputn,outputn,hiddennum);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%优化前的BP
net.trainParam.epochs=100;
net.trainParam.lr=0.1;
net.trainParam.goal=0.00001;
[net,~]=train(net,inputn,outputn);
inputn_test=mapminmax('apply',input_test,inputps);
an=sim(net,inputn_test);


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% 利用差分进化算法选择最佳的BP参数
D=inputnum*hiddennum+hiddennum+hiddennum*outputnum+outputnum;%变量个数%变量的维数
NP=5;                                %个体数目                               
G=30;                                %最大进化代数

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Xs=1*ones(D,1);                                 %上限
Xx=-1*ones(D,1);                                %下限
%%%%%%%%%%%%%%%%%%%%%%%%%赋初值%%%%%%%%%%%%%%%%%%%%%%%%   
xx=zeros(D,NP);                        %初始种群
v=zeros(D,NP);                        %变异种群
u=zeros(D,NP);                        %选择种群
xchu=rand(D,NP);
for i=1:NP
xx(:,i)=xchu(:,i).*(Xs-Xx)+Xx;              %赋初始种群初值
end
%%%%%%%%%%%%%%%%%%%%计算目标函数%%%%%%%%%%%%%%%%%%%%%%%

trace(1)=min(Ob);
gbest=100;
%%%%%%%%%%%%%%%%%%%%%%%差分进化循环%%%%%%%%%%%%%%%%%%%%%
for gen=1:G
    %%%%%%%%%%%%%%%%%%%%%%变异操作%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%r1,r2,r3和m互不相同%%%%%%%%%%%%%%%
    for m=1:NP
        r1=randi([1,NP],1,1);
        while (r1==m)
            r1=randi([1,NP],1,1);
        end
        r2=randi([1,NP],1,1);
        while (r2==m)||(r2==r1)
            r2=randi([1,NP],1,1);
        end
        r3=randi([1,NP],1,1);
        while (r3==m)||(r3==r1)||(r3==r2)
            r3=randi([1,NP],1,1);
        end
        v(:,m)=xx(:,r1)+F*(xx(:,r2)-xx(:,r3));
    end
    %%%%%%%%%%%%%%%%%%%%%%交叉操作%%%%%%%%%%%%%%%%%%%%%%%
    r=randi([1,D],1,1);
    for n=1:D
        cr=rand(1);
        if (cr<=CR)||(n==r)
            u(n,:)=v(n,:);
        else
            u(n,:)=xx(n,:);
        end
    end
    %%%%%%%%%%%%%%%%%%%边界条件的处理%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%边界吸收%%%%%%%%%%%%%%%%%%%%%%%%%
    for n=1:D
        for m=1:NP
            if u(n,m)<Xx(n)
                u(n,m)=Xx(n);
            end
            if u(n,m)>Xs(n)
                u(n,m)=Xs(n);
            end
        end
    end
    %%%%%%%%%%%%%%%%%%%%%%选择操作%%%%%%%%%%%%%%%%%%%%%%%
    for m=1:NP
        Ob1(m)=fitness(u(:,m));
    end
    for m=1:NP
        if Ob1(m)<Ob(m)
            xx(:,m)=u(:,m);
        end
    end
    for m=1:NP
        Ob(m)=fitness(xx(:,m));
    end
    fbest=min(Ob);

    if(fbest<gbest)        
        gbest=fbest;
        histor(gen)=fbest;
    else
        histor(gen)=gbest;
    end


end


参考资料

[1] https://blog.csdn.net/kjm13182345320/article/details/129215161
[2] https://blog.csdn.net/kjm13182345320/article/details/128105718

文章来源:https://blog.csdn.net/kjm13182345320/article/details/135477685
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。