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

📄 gafuzzy.m

📁 利用遗传算法优化模糊
💻 M
字号:
clear all;
close all;
global rin yout timef

G=50;
Size=30;
CodeL=10;

MinX(1)=zeros(1);
MaxX(1)=10*ones(1);
MinX(2)=zeros(1);
MaxX(2)=30*ones(1);
MinX(3)=zeros(1);
MaxX(3)=100*ones(1);
MinX(4)=-0.5*ones(1);
MaxX(4)=0.5*ones(1);
MinX(5)=-0.5*ones(1);
MaxX(5)=0.5*ones(1);
MinX(6)=-0.5*ones(1);
MaxX(6)=0.5*ones(1);
MinX(7)=-0.5*ones(1);
MaxX(7)=0.5*ones(1);
MinX(8)=-0.5*ones(1);
MaxX(8)=0.5*ones(1);
MinX(9)=-0.5*ones(1);
MaxX(9)=0.5*ones(1);
MinX(10)=-0.5*ones(1);
MaxX(10)=0.5*ones(1);
MinX(11)=-0.5*ones(1);
MaxX(11)=0.5*ones(1);

E=round(rand(Size,11*CodeL));    %Initial Code!

BsJ=0;

for kg=1:1:G
    time(kg)=kg;

    for s=1:1:Size
        m=E(s,:);
        y1=0;y2=0;y3=0;y4=0;y5=0;y5=0;y6=0;y7=0;y8=0;y9=0;y10=0;y11=0;y12=0;

        m1=m(1:1:CodeL);
        for i=1:1:CodeL
            y1=y1+m1(i)*2^(i-1);
        end
        Kfuzzy(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
        Kfuzzy(s,2)=(MaxX(2)-MinX(2))*y2/1023+MinX(2);

        m3=m(2*CodeL+1:1:3*CodeL);
        for i=1:1:CodeL
            y3=y3+m3(i)*2^(i-1);
        end
        Kfuzzy(s,3)=(MaxX(3)-MinX(3))*y3/1023+MinX(3);

        m4=m(3*CodeL+1:1:4*CodeL);
        for i=1:1:CodeL
            y4=y4+m4(i)*2^(i-1);
        end
        Kfuzzy(s,4)=(MaxX(4)-MinX(4))*y4/1023+MinX(4);

        m5=m(4*CodeL+1:1:5*CodeL);
        for i=1:1:CodeL
            y5=y5+m5(i)*2^(i-1);
        end
        Kfuzzy(s,5)=(MaxX(5)-MinX(5))*y5/1023+MinX(5);

        m6=m(5*CodeL+1:1:6*CodeL);
        for i=1:1:CodeL
            y6=y6+m6(i)*2^(i-1);
        end
        Kfuzzy(s,6)=(MaxX(6)-MinX(6))*y6/1023+MinX(6);

        m7=m(6*CodeL+1:1:7*CodeL);
        for i=1:1:CodeL
            y7=y7+m7(i)*2^(i-1);
        end
        Kfuzzy(s,7)=(MaxX(7)-MinX(7))*y7/1023+MinX(7);

        m8=m(7*CodeL+1:1:8*CodeL);
        for i=1:1:CodeL
            y8=y8+m8(i)*2^(i-1);
        end
        Kfuzzy(s,8)=(MaxX(8)-MinX(8))*y8/1023+MinX(8);

        m9=m(8*CodeL+1:1:9*CodeL);
        for i=1:1:CodeL
            y9=y9+m9(i)*2^(i-1);
        end
        Kfuzzy(s,9)=(MaxX(9)-MinX(9))*y9/1023+MinX(9);

        m10=m(9*CodeL+1:1:10*CodeL);
        for i=1:1:CodeL
            y10=y10+m10(i)*2^(i-1);
        end
        Kfuzzy(s,10)=(MaxX(10)-MinX(10))*y10/1023+MinX(10);

        m11=m(10*CodeL+1:1:11*CodeL);
        for i=1:1:CodeL
            y11=y11+m11(i)*2^(i-1);
        end
        Kfuzzy(s,11)=(MaxX(11)-MinX(11))*y11/1023+MinX(11);
        
        %****** Step 1 : Evaluate BestJ ******
        Kfuzzyi=Kfuzzy(s,:);

        [Kfuzzyi,BsJ]=mychap5_2f(Kfuzzyi,BsJ);

        BsJi(s)=BsJ;
    end

    [OderJi,IndexJi]=sort(BsJi);
    BestJ(kg)=OderJi(1);
    BJ=BestJ(kg);
    Ji=BsJi+1e-10;

    fi=1./Ji;
    %  Cm=max(Ji);
    %  fi=Cm-Ji;        %Avoiding deviding zero

    [Oderfi,Indexfi]=sort(fi);          %Arranging fi small to bigger
    %   Bestfi=Oderfi(Size);               %Let Bestfi=max(fi)
    %   BestS=Kpid(Indexfi(Size),:);       %Let BestS=E(m), m is the Indexfi belong to max(fi)

    Bestfi=Oderfi(Size);           % Let Bestfi=max(fi)
    BestS=E(Indexfi(Size),:);      % Let BestS=E(m), m is the Indexfi belong to max(fi)

    kg
    BJ
    BestS;

    %****** Step 2 : Select and Reproduct Operation******
    fi_sum=sum(fi);
    fi_Size=(Oderfi/fi_sum)*Size;

    fi_S=floor(fi_Size);        %Selecting Bigger fi value

    kk=1;
    for i=1:1:Size
        for j=1:1:fi_S(i)        %Select and Reproduce
            TempE(kk,:)=E(Indexfi(i),:);
            kk=kk+1;              %kk is used to reproduce
        end
    end

    %************ Step 3 : Crossover Operation ************
    pc=0.60;
    n=ceil(100*rand);
    for i=1:2:(Size-1)
        temp=rand;
        if pc>temp                 %Crossover Condition
            for j=n:1:100
                TempE(i,j)=E(i+1,j);
                TempE(i+1,j)=E(i,j);
            end
        end
    end
    TempE(Size,:)=BestS;
    E=TempE;

    %************ Step 4: Mutation Operation **************
    %pm=0.001;
    pm=0.001-[1:1:Size]*(0.001)/Size; %Bigger fi, smaller pm
    %pm=0.0;    %No mutation
    %pm=0.1;    %Big mutation

    for i=1:1:Size
        for j=1:1:3*CodeL
            temp=rand;
            if pm(i)>temp               %Mutation Condition
                if TempE(i,j)==0
                    TempE(i,j)=1;
                else
                    TempE(i,j)=0;
                end
            end
        end
    end
    %Guarantee TempE(Size,:) belong to the best individual
    TempE(Size,:)=BestS;
    E=TempE;
    %*******************************************************
end

Bestfi
BestS
Kfuzzyi
Best_J=BestJ(G)
figure(1);
plot(time,BestJ);
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 + -