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

📄 gatest.m

📁 自适应滤波的遗传算法算法 自适应滤波的LMS算法
💻 M
字号:
%下面的程序是用遗传算法求函数:f(x1,x2)=100(x1*x1-x2*x2) (x1*x1-x2*x2)+(1-x1)(1-x1) 

%约束:-2.048<=x1,x2<=2.048的极大值

%Generic Algorithm for function f(x1,x2) optimum

clear all;

close all;

                         

%parameters

Size=80;

G=100;

Codel=10;

umax=2.048;

umin=-2.048;

E=round(rand(Size,2*Codel));  %initial code




%Main program

for k=1:1:G

    time(k)=k;

    for s=1:1:Size

        m=E(s,:);

        y1=0;

        y2=0;

        
        %uncoding

        m1=m(1:1:Codel);

        for i=1:1:Codel

            y1=y1+m1(i)*2^(i-1);

        end

        x1=(umax-umin)*y1/1023+umin;

        m2=m(Codel+1:1:2*Codel);

        for i=1:1:Codel

            y2=y2+m2(i)*2^(i-1);

        end

        x2=(umax-umin)*y2/1023+umin;

        F(s)=100*(x1^2-x2^2)^2+(1-x1)^2;

                          %F(s)=100*(x1^2-x2)^2+(1-x1)^2;

    end

    

    Ji=1./F;

    %***********Step 1:Evaluate BestJ**********

    BestJ(k)=min(Ji);

    fi=F;          %Fitness Function

    [Oderfi,Indexfi]=sort(fi);   %Arranging fi small to bigger

    Bestfi=Oderfi(Size);         %LetBestJfi=max(fi)                        %Bestfi 为种群种个体适应度的最大值

    BestS=E(Indexfi(Size),:);    %Let  BestS=E(m),m is the Indexfi belong to max(fi)                    %种群种适应度的最大的个体的编码

    bfi(k)=Bestfi;

     

    %**********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      %fi_S为每个个体繁殖到下一代种群种种的个数

     

    kk=1;

    for i=1:1:Size  %Selece and Reproduce

        for j=1:1:fi_S(i)               

            TempE(kk,:)=E(Indexfi(i),:);            %将编码按从小到大存入TempE

            kk=kk+1;     %kk is used to roproduce

        end;                    %循环后的kk为下一代种群种个体的个数
    
    end

    

    %**********Step 3:Crossover Operation***********

    pc=0.60;

    n=ceil(20*rand);

    for i=1:2:(Size-1)

        temp=rand;

        if pc>temp        %crossover condition

            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;                        %最大适应度的个体保留到下一代中并将TempE扩展成[size,20]的矩阵,除sie行外其他位置新加元素为00

    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:2*Codel              %这里每个编码为都可能变异

            temp=rand;

            if pm>temp      %Mutation condition

                if TempE(i,j)==0

                    TempE(i,j)=1;

                else

                    TempE(i,j)=0;

                end

            end

        end

    end

     

    %Guarantee TempPop(30,:)is the code belong to the best individual (maxz(fi))

    TempE(Size,:)=BestS;

    E=TempE;

end




Max_Value=Bestfi

BestS

x1

x2

figure(1);

plot(time,BestJ);

xlabel('Times');ylabel('bestJ');

figure(2);

plot(time,bfi);

xlabel('times');ylabel('Best F');

⌨️ 快捷键说明

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