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

📄 inherit_pid1.m

📁 mtlab仿真的基于遗传算法的pid,实数编码和二进制编码
💻 M
字号:
%利用遗传算法的PID-二进制编码
clear all;
close all;
clc;
global rin yout timef
%A=[0.1,-0.1;0.6,-0.6;0.5,-0.5;2.1,-2.1;2.6,-2.6]'
%round(A)%四舍五入取整
%floor(A)%向负无穷的方向取证
%ceil(A)%向正无穷的方向取证
%fix(A)%只是截取整数部分
Size=30;
G=100;
BsJ=0;
CodeL=10;
MinX(1)=zeros(1);
MinX(2)=zeros(1);
MinX(3)=zeros(1);
MaxX(1)=20*ones(1);
MaxX(2)=ones(1);
MaxX(3)=ones(1);

E=round(rand(Size,3*CodeL));

for kg=1:1:G
    kg
    time(kg)=kg;   
    for s=1:1:Size
        m=E(s,:);
        y1=0;y2=0;y3=0;
        %解码
        m1=m(1:1:CodeL);
        for i=1:1:CodeL
            y1=y1+m1(i)*2^(i-1);
        end
        Kpid(s,1)=(MaxX(1)-MinX(1))*y1/1023+MinX(1);
        m2=m(CodeL+1:1:2*CodeL);
        for i=1:1:CodeL
            y2=y2+m2(i)*2^(i-1);
        end
        Kpid(s,2)=(MaxX(2)-MinX(2))*y2/1023+MinX(2);
        
        m3=m(CodeL+1:1:2*CodeL);
        for i=1:1:CodeL
            y3=y3+m3(i)*2^(i-1);
        end
        Kpid(s,3)=(MaxX(3)-MinX(3))*y3/1023+MinX(3);
        %第一步:评价最佳J   
        Kpidi=Kpid(s,:);
        [Kpidi,BsJ]=Ga_f(Kpidi,BsJ);
        BsJi(s)=BsJ;
    end%for s=1:1:Size
    [OderJi,IndexJi]=sort(BsJi);
    BestJ(kg)=OderJi(1);  
    
    bestpid=Kpid(IndexJi(1),:);
    Ga_f(bestpid,BsJ);
   % figure(1);
%plot(time,BestJ,'b');
%xlabel('Times');ylabel('Best J');
%figure(kg);
%plot(timef,rin,'r',timef,yout,'b');
%xlabel('Time(s)');ylabel('rin,yout');
    
    
    BJ=BestJ(kg);
    Ji=BsJi+1e-10;
    fi=1./Ji;
    [Oderfi,Indexfi]=sort(fi);
    Bestfi=Oderfi(Size);
    BestS=E(Indexfi(Size),:);
    %第二步:筛选,繁殖
    fi_sum=sum(fi);
    fi_Size=(Oderfi/fi_sum)*Size;
    fi_S=floor(fi_Size);
    %r=Size-sum(fi_S);
    %Rest=fi_Size-fi_S;
    %[RestValue,Index]=sort(Rest);
    
    %for i=Size:-1:Size-r+1
        %fi_S(Index(i))=fi_S(Index(i))+1;
        %end    
    kk=1;
    for i=1:1:Size        
        for j=1:1:fi_S(i)
            TempE(kk,:)=E(Indexfi(i),:);
            kk=kk+1;
        end
    end
    %第三步:交配
     pc=0.60;
     n=ceil(20*rand);
     for i=1:2:(Size-1)
         temp=rand;
         if pc>temp
             for j=n:1:20
                 TempE(i,j)=E(i+1,j);
                 TempE(i+1,j)=E(i,j);
             end
         end
     end
     TempE(Size,:)=BestS;
     E=TempE;
     %第四步:变异
     pm=0.001-[1:1:Size]*0.001/Size;
     for i=1:1:Size
         for j=1:1:2*CodeL
             temp=rand;
             if pm>temp
                 if TempE(i,j)==0
                    TempE(i,j)=1;
                else
                    TempE(i,j)=0;
                end
            end
        end
    end
    TempE(Size,:)=BestS;
    E=TempE;
end
Bestfi
BestS
Kpidi
Best_J=BestJ(G)
figure(1);
plot(time,BestJ,'b');
xlabel('Times');ylabel('Best J');
figure(2);
plot(timef,rin,'r',timef,yout,'b');
xlabel('Time(s)');ylabel('rin,yout');

⌨️ 快捷键说明

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