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

📄 galsysiden.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=pa_num*lchrom;
%initial
bestTi=zeros(1,6);
code=round(rand(codeL,popsize));
bestobj=0;
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:pa_num) 
         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(j)-MinX(j))*y1/pamax+MinX(j);
       end
       obj(t)=sysidenobj(T(t,:),pa_num,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
sysidenobj(bestTi,pa_num,popsize,lchrom,ts,rin,rout);

a_1=subplot(3,1,1);plot(timef,beobj);
set(a_1,'XColor','white','YColor','white');
xlabel('遗传代数');ylabel('最大适应度');
bestTi(pa_num)=round(bestTi(pa_num));
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','[m1,m2,m3,n1,n2,n3,zh]',...
	'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');
if pa_num==5
    set(t_s3,'string','[m1,m2,n1,n2,zh]');
end
t_s8 = uicontrol( 'Style','text','Position',[0.21 0.3 0.6 0.04],'String',mat2str(bestTi),...
	'BackgroundColor',[0.3 0.3 0.3], 'ForegroundColor','white');

⌨️ 快捷键说明

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