⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gann_sinf.m

📁 遗传算法优化bp神经网络,神经网络为三层网络,遗传算法带有精英保留策略
💻 M
字号:
%**************************************************************
%                 GANN模型
%**************************************************************

clear;
clc;

%  装载数据
PP=-1:0.1:1;
PP=PP';

TT=[-0.9602  -0.577   -0.0729   0.3771   0.6405   0.66     0.4609...
    0.1336  -0.2013  -0.4344  -0.5     -0.393   -0.1647   0.0988...
    0.3072   0.396    0.3449   0.1816  -0.0312  -0.2189  -0.3201];
TT=TT';

%****************************************************
%      遗传算法求解
%****************************************************

% 神经网络结构初始化
[h,l]=size(PP);  
II=l;               %  输入层节点数(PP矩阵的列数)
JJ=15;              %  中间层节点数

%  参数设定
Size=40;           %  群体总个体数量   
G=1000;            %  允许最大迭代次数
CodeL=30;          %  每个变量拥有的染色体数量
 
umax1=10.0;        %  连接权值的最大值
umin1=-10.0;       %  连接权值的最小值
umax2=3.5;         %  阈值的最大值
umin2=-3.5;        %  阈值的最小值

%  对所求参数进行初始二进制编码
E=round(rand(Size,((II+2)*JJ+1)*CodeL));        %  初始化个体代码(染色体)

%  遗传神经网络训练开始
gbestf=0;
for k=1:G
    k=k              
   % ********************** 对各个体进行解码、求适应值*********
   for m=1:1:Size
       mm=E(m,:);                  %  取要解码的染色体
       for ii=1:(II+2)*JJ+1        %  清零
           yoy(ii,m)=0; 
       end

       % *********  解码操作  ******************
       for ii=1:1:(II+2)*JJ+1
           m1=mm((ii-1)*CodeL+1:1:ii*CodeL);
           for i=1:1:CodeL
               yoy(ii,m)=yoy(ii,m)+m1(i)*2^(i-1);
           end
           if ii<((II+1)*JJ+1)
               wb(ii,m)=(umax1-umin1)*yoy(ii,m)/(2^CodeL-1)+umin1;      %  连接权值解码 
           else
               wb(ii,m)=(umax2-umin2)*yoy(ii,m)/(2^CodeL-1)+umin2;      %  阈值解码 
           end
       end
       clear yoy;
       
       %  将解码后的值代入目标函数求适应值(一定要为正数)
        ee0(m)=0;
        for kk=1:h                 %  数据组数
            for j=1:JJ             %  中间层节点数
                net1(j)=0;
                for i=1:II         %  输入层节点数
                    net1(j)=net1(j)+PP(kk,i)*wb(j+(i-1)*JJ,m);
                end
                net1(j)=net1(j)+wb((II+1)*JJ+j,m);
                o1(j)=(exp(net1(j))-exp(-net1(j)))/(exp(net1(j))+exp(-net1(j)));     %  tansig 函数
            end
            net2=0;
            for j=1:JJ
                net2=net2+o1(j)*wb(j+II*JJ,m);
            end
            net2=net2+wb((II+2)*JJ+1,m);
            y(kk)=(exp(net2)-exp(-net2))/(exp(net2)+exp(-net2));
            ee0(m)=ee0(m)+(TT(kk)-y(kk))^2;
        end
        ee(m)=ee0(m)/2;
        F(m)=1/ee(m);             %  求最小值转为求最大值
    end
    
    %  保留最优量
    [OderF,IndexF]=sort(F); 
    BestS=wb(:,IndexF(Size)); 
    bestF(k)=max(F);          
    if bestF(k)>gbestf
        gbestf=bestF(k);          %  保留全局最好个体适应值
        Bestwb=BestS;             %  保留最后的全局最好个体 
    end
    gbestfstep(k)=gbestf;         %  保留每一步的全局最好个体
  
    %  用轮盘赌法来选择
    F_sum=sum(F);
    temp=0;
    for i=1:Size
        temp=temp+F(i)/F_sum;
        F_size(i)=temp;   
    end
    clear temp;
    temp=rand(1,Size);
    for i=1:1:Size
        for j=1:1:Size
            if j<2
                if temp(i)<=F_size(j)
                    TempE(i,:)=E(j,:);  
                end
            else
                 if ((F_size(j-1)<temp(i))&(temp(i)<=F_size(j)));  %
                    TempE(i,:)=E(j,:);  
                end
            end
        end
    end
    clear temp;
    a=randperm(Size);           %  选择后的编码数据随机重新排列
    for a0=1:Size
        a1=a(a0);
        TempE0(a0,:)=TempE(a1,:);
    end
    clear TempE;
    TempE=TempE0;
    clear TempE0;    clear a;    clear a0;    clear a1;  clear BestS;
   
    %************   交叉操作  ************
    pc=0.70;          %  交叉率设定(人为调试)
    n=ceil(((II+2)*JJ+1)*CodeL*rand);      %  随机产生交叉操作点
    for i=1:2:(Size-1)
        temp=rand;
        if pc>temp  
            for j=n:1:(((II+2)*JJ+1)*CodeL)
                temp=TempE(i,j);
                TempE(i,j)=TempE(i+1,j);
                TempE(i+1,j)=temp;
                clear temp;
            end
        end
    end
   
    %************ 变异操作 **************
    pm=0.05;         % 变异率设定(人为调试)
    for i=1:Size
        for j=1:(((II+2)*JJ+1)*CodeL)
            temp=rand;
            if pm>temp      
                if TempE(i,j)==0
                    TempE(i,j)=1;
                else
                    TempE(i,j)=0;
                end
            end
        end
    end
   clear temp;   clear E;
   
    %*********** 保存最优个体 **************
    E=TempE;
end

% ****** 输出结果 **************
Bestwb;                        %  输出最优变量
GABest_Value=1/gbestf          %  输出最优适应值-GANN
%        遗传算法求解结束
% ***********************************************


%************************************************
%         输出结果
%************************************************

%  GANN计算最优结果
for k=1:h              
    for j=1:JJ          %  中间层节点数
        net1(j)=0;
        for i=1:II      %  输入层节点数
            net1(j)=net1(j)+PP(k,i)*Bestwb(j+(i-1)*JJ);
        end
        net1(j)=net1(j)+Bestwb((II+1)*JJ+j);
        o1(j)=(exp(net1(j))-exp(-net1(j)))/(exp(net1(j))+exp(-net1(j)));   % tansig函数
    end
    net2=0;
    for j=1:JJ
        net2=net2+o1(j)*Bestwb(j+II*JJ);
    end
    net2=net2+Bestwb((II+2)*JJ+1);
    yGA(k)=(exp(net2)-exp(-net2))/(exp(net2)+exp(-net2));
end

%******************************************************
%                 输出、显示、比较结果
%******************************************************
%  训练结果对比
figure
plot(1:G,1./gbestfstep(1:G),'--b')
xlabel('迭代代数');                                %  坐标标注
ylabel('最小误差值');
title('GANN训练过程轨迹:');

figure
plot(1:h,TT(1:h),'-k',1:h,yGA(1:h),'-.r')
xlabel('x轴');                                %  坐标标注
ylabel('y轴');
title('函数对比图:');
legend('实际值','GANN计算值');


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -