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


%用粒子群算法优化RBF网络权值

clear all
close all

G =250;   %迭代次数
n = 12;   %粒子维数
m = 20;   %种群规模
w = 0.1;  %算法参数
c1 = 2;   %算法参数
c2 = 2;   %算法参数

%取粒子的取值范围
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

%初始化种群pop
pop = rands(m,n);
for i = 1:m
    for j = 1:3
        if pop(i,j) < MinX(j)
            pop(i,j) = MinX(j);
        end
        if pop(i,j) > MaxX(j)
            pop(i,j) = MaxX(j);
        end
    end
    for j = 4:9
        if pop(i,j) < MinX(j)
            pop(i,j) = MinX(j);
        end
        if pop(i,j) > MaxX(j)
            pop(i,j) = MaxX(j);
        end
    end
    for j = 10:12
        if pop(i,j) < MinX(j)
            pop(i,j) = MinX(j);
        end
        if pop(i,j) > MaxX(j)
            pop(i,j) = MaxX(j);
        end
    end
end
        
%初始化粒子速度
V = 0.1*rands(m,n);
BsJ = 0;

%根据初始化的种群计算个体好坏,找出群体最优和个体最优
for s = 1:m
    indivi = pop(s,:);    %抽出个体
    [indivi,BsJ] = fitness(indivi,BsJ);   %求出每个粒子对应的误差
    Error(s) = BsJ;
end

[OderEr,IndexEr] = sort(Error);    %对误差进行排序
Error;
Errorleast = OderEr(1);    %求出最小误差
for i = 1:m
    if Errorleast == Error(i)
        gbest = pop(i,:);   %找出最小误差对应的个体极值gbest
        break;
    end
end
ibest = pop;   %把初始化的种群作为群体极值

%循环开始
for kg = 1:G
    kg
    for s = 1:m;
        %个体有4%的变异概率        
        for j = 1:n
            for i = 1:m
                if rand(1)<0.04
                    pop(i,j) = rands(1);  %对个体pop(i,j)进行变异
                end
            end
        end
        %r1,r2为粒子群算法参数        
        r1 = rand(1);
        r2 = rand(1);

        % 速度更新        
        V(s,:) = w*V(s,:) + c1*r1*(ibest(s,:)-pop(s,:)) + c2*r2*(gbest-pop(s,:));
        %个体更新
        pop(s,:) = pop(s,:) + 0.3*V(s,:);
        
        for j = 1:3
            if pop(s,j) < MinX(j)
                pop(s,j) = MinX(j);
            end
            if pop(s,j) > MaxX(j)
                pop(s,j) = MaxX(j);
            end
        end
        for j = 4:9
            if pop(s,j) < MinX(j)
                pop(s,j) = MinX(j);
            end
            if pop(s,j) > MaxX(j)
                pop(s,j) = MaxX(j);
            end
        end
        for j = 10:12
            if pop(s,j) < MinX(j)
                pop(s,j) = MinX(j);
            end
            if pop(s,j) > MaxX(j)
                pop(s,j) = MaxX(j);
            end
        end

        %求更新后的每个个体误差,可看成适应度值        
        [pop(s,:),BsJ] = fitness(pop(s,:),BsJ);
        error(s) = BsJ;
        %根据适应度值对个体最优和群体最优进行更新        
        if error(s)<Error(s)
            ibest(s,:) = pop(s,:);
            Error(s) = error(s);
        end
        if error(s)<Errorleast
            gbest = pop(s,:);
            Errorleast = error(s);
        end
    end
    
    Best(kg) = Errorleast;
end
plot(Best);
title('遗传算法优化RBF网络权值中最小误差进化过程')
xlabel('进化次数');
ylabel('最小误差');

save pfile1 gbest;