www.pudn.com > my_liziqun_RBF.rar > GA.m, change:2009-05-01,size:2687b


clear all
close all

%遗传算法优化来训练RBF网络权值
%G为进化代数,Size为种群规模,CodeL为参数的二进制编码长度
G = 250;
Size = 30;
CodeL = 10;

%确定每个参数的最大最小值
for i = 1:3
    MinX(i) = 0.1*ones(1);
    MaxX(i) = 3*ones(1);
end
for i = 4:1:9
    MinX(i) = -3*ones(1);
    MaxX(i) = 3*ones(1);
end
for i = 10:1:12
    MinX(i) = -ones(1);
    MaxX(i) = ones(1);
end

%初始化种群
E = round(rand(Size,12*CodeL));

BsJ = 0;

%进化开始
for kg = 1:1:G
    time(kg) = kg    
    for s = 1:1:Size
        m = E(s,:);    %取出其中个体
        
        %把二进制表示的参数转化为实数
        for j = 1:1:12
            y(j) = 0;           
            mj = m((j-1)*CodeL + 1:1:j*CodeL);
            for i = 1:1:CodeL
                y(j) = y(j) + mj(i)*2^(i - 1);
            end
            f(s,j) = (MaxX(j) - MinX(j))*y(j)/1023 + MinX(j);
        end    
        p = f(s,:);     
        [p,BsJ] = fitness(p,BsJ);        
        BsJi(s) = BsJ;             %记录每个个体的总误差
    end
    
    %对误差排序,求出最好误差
    [OderJi,IndexJi] = sort(BsJi);
    BestJ(kg) = OderJi(1);
    BJ = BestJ(kg);
    Ji = BsJi + 1e-10;
    
    %对误差取倒数,求出适应度值
    fi = 1./Ji;    %适应度值
    [Oderfi,Indexfi] = sort(fi);
    Bestfi = Oderfi(Size);      %最佳适应度值
    BestS = E(Indexfi(Size),:);     %最佳个体
    
    kg  %进化次数
    p    %最佳个体
    BJ   %最佳个体的误差

%**************Step 2:选择操作**********************%
    fi_sum = sum(fi);
    fi_Size = (Oderfi/fi_sum)*Size;
    
    fi_S = floor(fi_Size);
    
    kk = 1;
    for i = 1:1:Size
        for j = 1:1:fi_S(i)
            TempE(kk,:) = E(Indexfi(i),:);
            kk = kk + 1;
        end
    end
    
%***************Step 3:交叉操作***********************************%
    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;
    
%***************Step 4:变异操作**********************************%
    pm = 0.001 - [1:1:Size]*(0.001)/Size;
    for i = 1:1:Size
        for j = 1:1:12*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
fi 
Best_J = BestJ(G)
figure(1)
plot(time,BestJ);
title('遗传算法优化RBF网络权值中最小误差进化过程')
xlabel('进化次数');
ylabel('最小误差');
save pfile p;