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

📄 elman.m

📁 我自己编写的基于遗传算法的elman神经网络模型.大家参考!
💻 M
📖 第 1 页 / 共 2 页
字号:
        for i=1:px9
            if(rand<pm2)
                u1=min(overpop3(i,:));
                u2=max(overpop3(i,:));
                u3=u2-u1;
                mutionpop3(i,:)=u3.*cc3(i,:)+u1;
            else
                mutionpop3(i,:)=overpop3(i,:);
            end
        end 
        mutpop3=mutionpop3;
        %fitst4 mution
        %rand('seed',78341223);
        cc4=rand(px10,py10);
        for i=1:px10
            if(rand<pm2)
                u1=min(overpop4(i,:));
                u2=max(overpop4(i,:));
                u3=u2-u1;
                mutionpop4(i,:)=u3.*cc4(i,:)+u1;
            else
                mutionpop4(i,:)=overpop4(i,:);
            end
        end 
        mutpop4=mutionpop4;
        %fitst6 mution
        %rand('seed',78341223);
        cc6=rand(px12,py12);
        for i=1:px12
            if(rand<pm2)
                u1=min(overpopo(i,:));
                u2=max(overpopo(i,:));
                u3=u2-u1;
                mutionpopo(i,:)=u3.*cc6(i,:)+u1;
            else
                mutionpopo(i,:)=overpopo(i,:);
            end
        end 
        mutpopo=mutionpopo;
        %fitst5 mution
        %rand('seed',78341223);
        cc5=rand(px11,py11);
        for i=1:px11
            if(rand<pm3)
                u1=min(overyou(i,:));
                u2=max(overyou(i,:));
                u3=u2-u1;
                mutionyou(i,:)=u3.*cc5(i,:)+u1;   
            else
                mutionyou(i,:)=you(i,:);
            end
        end 
        %fitst7 mution
        %rand('seed',78341223);
        
        you=mutionyou;             
        pop=[mutpop1 mutpop2 mutpop3 mutpop4];
        popo=mutpopo;
        for i=1:popsize 
            n=you(i,1);%隐节点个数
            c_2=popo(i,1:n*n);
            p1_2=p1_(i,1:n)';
            c=pop(i,1:n*ny);%从输入层到隐层的权值
            d=pop(i,hh+1:hh+n)';%隐层的阈限
            e=pop(i,hh+yty+1:hh+yty+n);%从隐层到输出层的权值
            g=pop(i,chromlength);%输出层的阈限
            f=reshape(c,n,ny);%从输入层到隐层的权值作成n*ny的矩阵
            f_2=reshape(c_2,n,n);
            for j=1:it 
                aatq=f*p1(:,j);        %训练样本与从输入层到隐层的权值结合
                aatq_2=f_2*p1_2; 
                fert=aatq+d+aatq_2;
                fet=1./(1+exp(-fert));
                %记忆==
                p1_2=fet+(p1_2.*0.5);%将第m个样本的隐层输出作为连接层的第m+1个样本
                %==== 
                rewqt=e*fet;
                dmf=rewqt+g;                
                dmn(i,j)=postmnmx(dmf,mint,maxt);
            end
            obj(i)=1/(1+mean(abs(dmn(i,:)-t3)));         
        end  
        objval=obj';
        [maxfit,bestindex]=max(objval);
        maxfitting(generation,1)=maxfit;
        hyu1=pop(bestindex,:);
        hyu1o=popo(bestindex,:);
        bestpop(generation,:)=hyu1;
        bestpopo(generation,:)=hyu1o;
        hyu2=you(bestindex,:)
        bestyou(generation,:)=hyu2;  
        [mifit,worstindex]=min(objval);
        mifitting(generation,1)=mifit;
        hyu3=pop(worstindex,:);
        hyu3o=popo(worstindex,:);
        worstpop(generation,:)=hyu3;
        worstpopo(generation,:)=hyu3o;
        hyu4=you(worstindex,:);
        worstyou(generation,:)=hyu4;  
        if maxfit>maxfitting(generation-1,1)
            pop(worstindex,:)=hyu1;
            popo(worstindex,:)=hyu1o;
            you(worstindex,:)=hyu2;
        else
            pop(bestindex,:)=bestpop(generation-1,:);
            popo(bestindex,:)=bestpopo(generation-1,:);
            you(bestindex,:)=bestyou(generation-1,:);
            pop(worstindex,:)=hyu1;
            popo(worstindex,:)=hyu1o;
            you(worstindex,:)=hyu2;
        end
        for i=1:popsize 
            n=you(i,1);%隐节点个数
            c_2=popo(i,1:n*n);
            p1_2=p1_(i,1:n)';
            c=pop(i,1:n*ny);%从输入层到隐层的权值
            d=pop(i,hh+1:hh+n)';%隐层的阈限
            e=pop(i,hh+yty+1:hh+yty+n);%从隐层到输出层的权值
            g=pop(i,chromlength);%输出层的阈限
            f=reshape(c,n,ny);%从输入层到隐层的权值作成n*ny的矩阵
            f_2=reshape(c_2,n,n);
            for j=1:it 
                aatq=f*p1(:,j);        %训练样本与从输入层到隐层的权值结合
                aatq_2=f_2*p1_2; 
                fert=aatq+d+aatq_2;
                fet=1./(1+exp(-fert));
                %记忆==
                p1_2=fet+(p1_2.*0.5);%将第m个样本的隐层输出作为连接层的第m+1个样本
                %==== 
                rewqt=e*fet;
                dmf=rewqt+g;                
                dmn(i,j)=postmnmx(dmf,mint,maxt);
            end
            obj(i)=1/(1+mean(abs(dmn(i,:)-t3)));         
        end  
        objval=obj';
        [maxfit,bestindex]=max(objval);
        maxfitting(generation,1)=maxfit;
        hyu1=pop(bestindex,:);
        hyu1o=popo(bestindex,:);
        bestpop(generation,:)=hyu1;
        bestpopo(generation,:)=hyu1o;
        hyu2=you(bestindex,:)
        bestyou(generation,:)=hyu2;  
        [mifit,worstindex]=min(objval);
        mifitting(generation,1)=mifit;
        hyu3=pop(worstindex,:);
        hyu3o=popo(worstindex,:);
        worstpop(generation,:)=hyu3;
        worstpopo(generation,:)=hyu3o;
        hyu4=you(worstindex,:);
        worstyou(generation,:)=hyu4;  
        averageobjvalue(generation,1)=mean(objval);
        fitvalue=objval;
        fitvalue1=fitvalue/sum(fitvalue);             
        cfitness=cumsum(fitvalue1);                
        it
    end       
    close(bar);   
    %最大的解码过程再次包含了最后一带以及最大的适应度的编码的解码                 
    %===============================================================
    %===============================================================
    
    for mn=1:popsize
        n=you(mn,1);%隐节点个数
        c_2=popo(mn,1:n*n);
        p1_2=p1_(mn,1:n)';%连接层的权值
        c=pop(mn,1:n*ny);%从输入层到隐层的权值
        d=pop(mn,hh+1:hh+n)';%隐层的阈限
        e=pop(mn,hh+yty+1:hh+yty+n);%从隐层到输出层的权值
        g=pop(mn,chromlength);%输出层的阈限
        f=reshape(c,n,ny);%从输入层到隐层的权值作成n*ny的矩阵
        f_2=reshape(c_2,n,n);                  
        %=======================================================
        %========================================================
        max_epoch=cx;%最大叠代次数
        err_goal=dx;%目标误差
        max_epoch1=max_epoch;
        epoch=1;mt0=1;
        while  epoch<=max_epoch1
            mt=1;
            for m=1:it
                aatq=f*p1(:,m);        %训练样本与从输入层到隐层的权值结合
                aatq_2=f_2*p1_2;       %连接层与连接层到隐层权值结合
                QO(:,mt)=p1_2;
                fert=aatq+aatq_2+d;    %隐层加上隐层的阈限
                fet=1./(1+exp(-fert)); %第m个样本的隐层输出响亮
                %记忆==
                p1_2=fet+(p1_2.*0.5);%将第m个样本的隐层输出作为连接层的第m+1个样本
                %==== 
                chucun(:,mt)=fet;%储存第k次叠代隐层第m个样本的输出
                mt=mt+1;
                rewqt=e*fet;%隐层输出与隐层到输出层的加权结合
                ee=e;
                dmf=rewqt+g;                
                dm(mn,m)=dmf;
            end
            eror=0.5*mean((dm(mn,:)-t1).^2);
            [mp1,mp2]=max(p1);
            max_u_k=max(mp1);max_W_I3_ij=max(e);
            %----学习率和动量因子的确定
            alfa=1/n;%从隐层到输出层的学习率
            bieta1=4*(n*ny*abs( max_u_k)*abs(max_W_I3_ij))^(-1);
            bieta2=16*((1-0.5)^(2))*(1/(n*n*abs(max_W_I3_ij)*abs(max_W_I3_ij)));
            
 %----------------------------------------------           
            
            if eror<err_goal&epoch<=max_epoch
                %==预测所要预测的样本
                aatq2=f*p2;       
                aatq_22=f_2*p1_2; 
                fert2=aatq2+d+aatq_22;
                fet2=1./(1+exp(-fert2));
                rewqt2=e*fet2;
                dmf2=rewqt2+g;                
                dmn2=postmnmx(dmf2,mint,maxt);
                CCC(:,mn)=dmn2;
                break
            else
                %==隐层到输出层权值调整
                for cc=1:n
                    dieta_w3_1cc=alfa*(sum((t1-dm(mn,:)).*chucun(cc,:)))/it;
                    e(cc)=e(cc)+dieta_w3_1cc;
                end
                %==隐层到输出层阈限调整
                dieta_g=alfa*(mean((t1-dm(mn,:))));
                g=g+dieta_g;
                %==输入层到隐层权值调整
                for cc1=1:n
                    for cc2=1:ny
                        dieta_w2_cc1_cc2=bieta1*( mean( ((t1-dm(mn,:)).*chucun(cc1,:).*(1-chucun(cc1,:)).*p1(cc2,:))*ee(cc1) ) );
                        f(cc1,cc2)=f(cc1,cc2)+dieta_w2_cc1_cc2;
                    end
                end
                %==输入层到隐层阈限调整
                dieta_d=bieta1*( mean( ((t1-dm(mn,:)).*chucun(cc1,:).*(1-chucun(cc1,:)))*ee(cc1) ) );
                d=d+dieta_d;
                %==连接层到隐层权值调整
                for cc1=1:n
                    for cc2=1:n
                        dieta_w1_cc1_cc2=bieta2*( mean( ((t1-dm(mn,:)).*chucun(cc1,:).*(1-chucun(cc1,:)).*QO(cc2,:))*ee(cc1) ) );
                        f_2(cc1,cc2)=f_2(cc1,cc2)+dieta_w1_cc1_cc2;
                    end
                end
                %====调整完毕
            end
            epoch=epoch+1
            
            
            clear chucun;
            clear QO;
            clear aatq;
            clear aatq_2;
            clear fert;
            clear fet;
            clear rewqt ;
            clear ee dm;
            mn
        end
            if epoch>max_epoch
                %==预测所要预测的样本
                aatq2=f*p2;       
                aatq_22=f_2*p1_2; 
                fert2=aatq2+d+aatq_22;
                fet2=1./(1+exp(-fert2));
                rewqt2=e*fet2;
                dmf2=rewqt2+g;                
                dmn2=postmnmx(dmf2,mint,maxt);
                CCC(:,mn)=dmn2;
            end
        clear aatq2;
        clear aatq_22;
        clear fert2;
        clear fet2;
        clear rewqt2;
    end
    CCC2=CCC;
    average=mean(CCC2);
    W2WSD(TGB,:)=[Y2,average,abs(average-Y2)];
    
    
    it=it+1       
    
    clear maxfitting;
    clear bestpop;
    clear bestyou;
    clear worstpop;
    clear worstyou;
    clear averageobjvalue;
    clear pop;
    clear you;
    clear CCC;
    clear CCC2;
    clear dmn;
    close all     
end
[pow3,pow4]=size(W2WSD);
fprintf(fid2,'****************************************预测值***************************************************************\n');
fprintf(fid2,'**(原始值)     (预测值)       (误差)      (相对误差)   **\n');
for i=1:pow3
    for j=1:pow4           
        fprintf(fid2,'%14.5f',W2WSD(i,j));
    end
    fprintf(fid2,'\n');
end   
ASDWQ=mean(abs(W2WSD));
[pow5,pow6]=size(ASDWQ);
fprintf(fid2,'\n');
fprintf(fid2,'********************************************平均值*******************************************\n')
for i=1:pow5
    for j=1:pow6
        fprintf(fid2,'%14.5f',ASDWQ(i,j));
    end
    fprintf(fid2,'\n');
end

fclose(fid1);
fclose(fid2);

⌨️ 快捷键说明

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