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

📄 ga.txt

📁 GA
💻 TXT
字号:
%generic algorithm for function f(x1,x2) optimum
% function main(pc1,pm1)
clear all
close all
% parameters
size=30;
generation=500;
codel=12;
pc1=0.8;
pm1=0.01;
pc=pc1;
 pm=pm1;
umax=2.048;
umin=-2.048;

E=round(rand(size,2*codel));    %initial code

% main program

for k=1:generation
    time(k)=k;
    
    for s=1:size
        m=E(s,:);
        y1=0;
        y2=0;
        % uncoding
        m1=m(1:1:codel);
        for i=1:codel
            y1=y1+m1(i)*2^(i-1);
        end
        x1=(umax-umin)*y1/2^codel+umin;
        m2=m(codel+1:1:2*codel);
        for i=1:codel
            y2=y2+m2(i)*2^(i-1);
        end
        x2=(umax-umin)*y2/2^codel+umin;
 
       F(s)=100*(x1^2-x2)^2+(1-x1)^2;              %Calculate the fitness


        
    end
    
    %Ji=1./F
    BestJ(k)=min(F);
    tempf=min(F);
    if abs(tempf)<0.002
        break;
    end
    Ji=F+1.0e-10;
    fi=1./Ji;                    %由于要进行复制,适应度函数取反,最小值则复制个数最多
    [oderfi,indexfi]=sort(fi);    % arranging fi small to bigger
    Bestfi=oderfi(size);   %let bestfi=max(fi)
    BestS=E(indexfi(size),:);    % find the best individual
    
    bfi(k)=Bestfi;             % record the best fi to show in the figure 
    
    %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:size
        for j=1:fi_s(i)
            tempe(kk,:)=E(indexfi(i),:);
            kk=kk+1;             %kk is used to reproduce
        end
    end
    
    %crossover operation
    
 
    n=ceil(2*codel*rand);
    for i=1:2:(size-1)
        temp=rand;
        if pc>temp
            for j=n:1:2*codel
                tempe(i,j)=E(i+1,j);
                tempe(i+1,j)=E(i,j);
            end
        end
    end
    
    tempe(size,:)=BestS;   % the last to save the best one
    E=tempe;
    
    %mutation operation
 
    pm=0.2;
    for i=1:size
        for j=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
    
    %guarantee tempPop(30,:) is the code belong to the best individual (max(fi))
    tempe(size,:)=BestS;
    E=tempe;


%max_value=Bestfi;
BestS
%Bestfi
x1
x2
temp=100*(x1^2-x2)^2+(1-x1)^2
k
tempf

                   
    
        

⌨️ 快捷键说明

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