www.pudn.com > GSO_IWO_IGS.rar > GSO.m, change:2015-09-07,size:4150b


%% Initialization 
clc 
clear all 
close all 
tic 
 
m = 2;      %维数 
n = 50;     %萤火虫个数 
a = 3;      %位置上限 
b = -3;     %位置下限 
jis = 0;    % 
L0 = 5;     %荧光素初值 
 
beta = 0.08;    %动态决策域的更新率 
nt = 5;         % 领域个数阈值,控制吸引邻居数量 
s = 0.05;       %步长 
%s=0.03;%步长 
gama = 0.6;     %荧光素更新率 
p = 0.4;        %荧光素消失率,荧光素挥发水平 
t = 2;          %次数初值   
iter_max = 200; %最大迭代次数 
R0 = 3;         %动态决策域Rd的初值,局部决策半径 
Rs = 5;         %感知域RS>=Rd 
L = zeros(n, iter_max);      %荧光素水平更新 
Rd = zeros(n, 1);              %局部决策半径 
P = zeros(n, 1);             %荧光素挥发水平 
 
Nei = cell(n, iter_max);       %吸引邻居集合 
 
Pos = cell(iter_max, 1);       %储存每个萤火虫的位置 
 
L(:, 1) = L0;       %随机分配个体荧光素及动态决策域 
Rd = Rd + R0; 
 
X = (a - b) * rand(n, m) + b;  %第i萤火虫在t时刻的位置初始化,X(n, m) 
n_ori = n;      %储存最初值 
Fitness = zeros(n, 1); 
 
%% Main Loop 
while t < iter_max 
    %荧光素的更新 
    %---------------------------------------------------------------% 
    L = zeros(n, iter_max);      %荧光素水平更新 
    Rd = zeros(n, 1);              %局部决策半径 
    P = zeros(n, 1);             %荧光素挥发水平 
 
    Nei = cell(n, iter_max);       %吸引邻居集合 
    X = (a - b) * rand(n, m) + b;  %第i萤火虫在t时刻的位置初始化,X(n, m) 
    %---------------------------------------------------------------%     
    for i = 1 : n 
        %--------------------------% 
        if t == 2 
            Fitness(i) = J1(X(i, :)); 
            Fitness_Max = max(Fitness); 
            Fitness_Min = min(Fitness); 
        end 
        %---------------------------% 
         
        L(i, t) = (1 - p) * L(i, (t - 1)) + gama * J1(X(i, :));     %荧光素水平更新,调用目标函数 
    end 
 
    %位置移动规则 
    for i = 1 : n 
        for j = 1 : n 
            if (norm(X(j, :) - X(i, :)) < Rd(i)) && (L(i,t) < L(j,t))       %两个判断条件 
               Nei{i, t} = [j, Nei{i, t}];      %获取邻域Nei 
            end 
        end 
    end 
 
    tempsum = zeros(n); 
    for i = 1 : n 
        for j = Nei{i, t} 
           tempsum(i) = L(j, t) - L(i, t) + tempsum(i);     %调用目标函数 
        end 
    end 
 
    %移动概率的计算 
    for i = 1 : n 
        for j = Nei{i, t} 
            P(i, j) = (L(j, t) - L(i, t)) / tempsum(i);     %调用目标函数 
        end 
    end 
     
  
    for i = 1 : n 
        if isempty(Nei{i, t})   %吸引不到邻居 
             
             %------------------------------------------------------------% 
            
             [X, Fitness_Max, Fitness_Min] = IWO(n_ori, m, X, iter_max, t, Fitness_Max, Fitness_Min); 
             n = length(X); 
             %------------------------------------------------------------% 
              
             Rd(i) = min(Rs, max(0, Rd(i) + beta * (nt - length(Nei{i, t}))));  
        else 
            for j = Nei{i, t} 
               if  P(i, j) == max(P(i, :)) && P(i, j) ~= 0 
                   SeJ = j;%选择最好的移动方向 
                   %位置更新 
                   X(i, :) = X(i, :) + s .* (X(SeJ, :) - X(i, :)) / norm(X(SeJ, :) - X(i, :));  
                   %动态决策域更新 
                   Rd(i) = min(Rs, max(0, Rd(i) + beta * (nt - length(Nei{i, t})))); 
               end 
            end 
%             P(i, :) = zeros(1, n_ori); 
        end 
    end 
 
    if t <= 150     %缩小步长 
        s = s - 0.0003; 
    end 
  
%    size(TempMax) 
    t = t + 1;      %迭代次数加一  
    Pos{t} = X; 
    hold on 
end  
 
%% Analysis 
%画图 
plot(Pos{3}(:, 1), Pos{3}(:, 2), 'or', 'LineWidth', 2);     %t的初始值是2,所以要从3开始 
hold on 
 
for i = 4 : iter_max - 1 
plot(Pos{i}(:, 1), Pos{i}(:, 2), 'o'); 
end 
 
plot(Pos{iter_max}(:, 1), Pos{iter_max}(:, 2), 'sk', 'LineWidth', 2); 
 
xlabel('Current Positions for para1', 'FontSize', 16); 
ylabel('Current Positions for para2', 'FontSize', 16); 
%title('GSO Algorithms Searching Process', 'FontSize', 16); 
title('GSO Algorithms Combined With IWO Algorithms Searching Process', 'FontSize', 16); 
legend('Beginning Position', 'Iterating Position'); 
 
grid on 
 
toc