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

📄 ganlsysiden.m

📁 这是一个遗传算法的优化软件
💻 M
字号:
global yout time 
%系统辨识遗传主程序
a_1=subplot(3,1,1);
set(a_1,'XColor','white','YColor','white');
a_2=subplot(3,1,2);
set(a_2,'XColor','white','YColor','white');
pause(0.1);
if popsize<2
    popsize=2;
end
if maxgen<1
    maxgen=1;
end
codeL=78*lchrom;
%initial
bestTi=zeros(1,6);
code=round(rand(codeL,popsize));
bestobj=0;
MaxX=5;MinX=-5;
pamax=2^lchrom-1;
for i=1:1:maxgen
    timef(i)=i;
    %select save cross num to indexobj
    for t=1:1:popsize
       for(j=1:1:78) 
         m1=code((j-1)*lchrom+1:j*lchrom,t);
         y1=0;
         for k=1:1:lchrom
            y1=y1+m1(k)*2^(k-1);%转化为10进制
         end
         T(t,j)=(MaxX-MinX)*y1/pamax+MinX;
       end
       obj(t)=nlsysidenobj(T(t,:),popsize,lchrom,ts,rin,rout);
    end
    [orderobj,indexobj]=sort(obj);
    if(bestobj<obj(indexobj(popsize)))
        bestobj=obj(indexobj(popsize));
        bestTi=T(indexobj(popsize),:);
    end;
    beobj(i)=bestobj;
    obj_sum=sum(obj);
    for j=1:1:popsize
        select_rand=rand;
        select_p=0;
        k=0;
        while select_rand>select_p
            k=k+1;
            select_p=select_p+obj(k)/obj_sum;
        end
        cindex(j)=k;
    end
    %cross
    tempcode=code(:,cindex);
    for j=1:2:popsize-1
        cross_rand=rand;
        n=ceil(codeL*rand);
        if cross_rand<pcross
            tempcode(n:codeL,j)=code(n:codeL,cindex(j+1));
            tempcode(n:codeL,j+1)=code(n:codeL,cindex(j));
        end
    end
    %mutate
    for j=1:1:popsize
      for k=1:1:codeL
         mumate_rand=rand;
         if pmutate>mumate_rand               %Mutation Condition
            if tempcode(k,j)==0
               tempcode(k,j)=1;
            else
               tempcode(k,j)=0;
            end
        end
      end
   end
   code=tempcode;
end
nlsysidenobj(bestTi,popsize,lchrom,ts,rin,rout);

a_1=subplot(3,1,1);plot(timef,beobj);
set(a_1,'XColor','white','YColor','white');
xlabel('遗传代数');ylabel('最大适应度');

a_2=subplot(3,1,2);plot(time,rout(1:1:100),'r',time,yout(1:1:100),'b');
set(a_2,'XColor','white','YColor','white');
xlabel('时间(周期)');ylabel('实际输出(红),预测值(蓝)');


%t_s3 = uicontrol( 'Style','text','Position',[0.05 0.3 0.15 0.04],'String','时延输入权系数',...
%	'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
%t_s8 = uicontrol( 'Style','text','Position',[0.21 0.3 0.6 0.04],'String',mat2str(bestTi(1:6)),...
%	'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
%t_s3 = uicontrol( 'Style','text','Position',[0.05 0.3 0.15 0.04],'String','输入连接权系数',...
%	'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
%t_s8 = uicontrol( 'Style','text','Position',[0.21 0.3 0.6 0.04],'String',mat2str(bestTi(7:12)),...
%	'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
%t_s3 = uicontrol( 'Style','text','Position',[0.05 0.3 0.15 0.04],'String','输出连接权系数',...
%	'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
%t_s8 = uicontrol( 'Style','text','Position',[0.21 0.3 0.6 0.04],'String',mat2str(bestTi(13:18)),...
%	'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');

t_s3 = uicontrol( 'Style','text','Position',[0.4 0.3 0.2 0.05],'String','非线性系统辨识结果文件:''nlresult.txt''',...
	'BackgroundColor',[0.3 0.3 0.3],'fontsize',12, 'ForegroundColor','white');
fid=fopen('nlresult.txt','w');
fprintf(fid,'输入层连接权值: ');
fprintf(fid,'\n');
fprintf(fid,'%10.6f ',bestTi(1:30));
fprintf(fid,'\n');
fprintf(fid,'隐层连接权值: ');
fprintf(fid,'\n');
fprintf(fid,'%10.6f ',bestTi(31:72));
fprintf(fid,'\n');
fprintf(fid,'输出层连接权值: ');
fprintf(fid,'\n');
fprintf(fid,'%10.6f\n',bestTi(73:78));
fclose(fid);

⌨️ 快捷键说明

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