象群游牧算法-Matlab

时间:2021-04-10 13:40:46   收藏:0   阅读:0

1. 适应度函数:

function z=chaffer(x)%chaffer函数x=(0...0) f(x)=0 x[-10,10]%%没测  
    n=10;
    s1=0;
    for i=1:n
        s1=s1+x(i)^2;
    end
    z=((sin(sqrt(s1)))^2-0.5)/(1+0.001*s1)+0.5;
end

2. EHO主函数

% ----------------------------------------------------------                                                                              
%   Title: Elephant Herbing Optimization Algorithm 
%   Institution: XI‘AN POLYTECHNIC UNIVERSITY
%   Author:  Liwenchao
%   Time: 2020-11-8
% ----------------------------------------------------------
clc 
clear
%   ----------------------------
%   Definition of Problems
%   ----------------------------

CostFunction = @(x) chaffer(x);  % cost function 
dim_Var = 10;  % variables of dimensions
VarMin = -32.768;  % lower boundary
VarMax = 32.768;  % upper boundary

%   -------------------------------------------------------------------------
%   Setting Parameters of Elephant Herbing Optimization Algorithm
%   -------------------------------------------------------------------------

alpha = 0.75;  % alpha is a scale factor that determines the influence of matriarch on elephant
beta = 0.01;  % beta is a scale factor that determines the influence of the center of clan on elephant
epoches = 1000;  % the maximum number of epoches
num_clan = 5;  % the number of clans
num_pop = 10;  % the number of elephants in each clans
num_male = 1;  % leave family group 

%  -----------------------------------
%   Initialization of Population
%  -----------------------------------

init_pop = VarMin + rand(num_clan*num_pop, dim_Var) .* (VarMax - VarMin);
pop_fitness = zeros(num_pop, 1);
fit_clan = zeros(num_pop, 1);
pop_best_fitness = zeros(epoches, 1);

for n=1: num_clan*num_pop
        pop_fitness(n) = CostFunction(init_pop(n, :));
end
[pop_fbest, pop_best_loc] = min(pop_fitness);
best_pop = init_pop(pop_best_loc, :);

%------------------------------------------
%    EHO generation starts   ......
%------------------------------------------
for iter=1:epoches    
    for j=1: num_clan 
        clan = init_pop((j-1)*num_pop +1: j * num_pop, :);
        for k=1: num_pop
            fit_clan(k,:) = CostFunction(clan(k, :));
        end
        %  best fitness value and its location
        [fbest, best_loc] = min(fit_clan);
        clan_best = clan(best_loc, :);
        %  worst fitness value and its location
        [fworst, worst_loc] = max(fit_clan);
        clan_worst = clan(worst_loc, :);
        for k=1:num_pop
            if any((k~=best_loc)&(k~=worst_loc))
                %  update elephant  position in clan except best and worst elephant
                clan(k, :) = clan(k, :) + alpha*(clan_best - clan(k, :))*rand;
            elseif k==best_loc
                %  update leader or matriarch 
                clan_center = sum(clan) / num_pop;
                clan(k, :) = beta*clan_center;
            elseif k==worst_loc
                %  update worst elephant or male
                clan(k, :) = VarMin + rand* (VarMax - VarMin + 1);
            end
            init_pop((j-1)*num_pop +1: j * num_pop, :) = clan;
        end
    end
    % ----------------------------------------------------------------
    %   evaluation population by the newly updated positions
    % ----------------------------------------------------------------
    for n=1: num_clan*num_pop
        pop_fitness(n) = CostFunction(init_pop(n, :));
    end
    [new_pop_fbest, new_pop_best_loc] = min(pop_fitness);
    if new_pop_fbest<pop_fbest
        pop_fbest = new_pop_fbest;
        pop_best_loc = new_pop_best_loc;
        init_pop(pop_best_loc, :) = init_pop(new_pop_best_loc, :);
    end
    pop_best_fitness(iter, :) = pop_fbest;
    disp([‘Iteration ‘ num2str(iter) ‘: Best Cost = ‘ num2str(pop_fbest)]);
    disp(init_pop(pop_best_loc, :));
end

3. 可视化

% -------------------------------------
%            visualization
% -------------------------------------
figure;
%plot(pop_best_fitness)
semilogy(pop_best_fitness);
xlabel(‘iteration‘);
ylabel(‘fitness‘);
legend(‘chaffer‘);
title(‘Elephant Herbing Optimization‘)

4. 结果显示

技术图片

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!